Used a mock ooklatester in tests

Speed up test suite
This commit is contained in:
Henry Whitaker
2021-04-11 08:46:01 +01:00
parent 91b7ec7fab
commit f809f816c1
12 changed files with 154 additions and 14 deletions

View File

@@ -1,6 +1,6 @@
# Speedtest Tracker
[![Docker pulls](https://img.shields.io/docker/pulls/henrywhitaker3/speedtest-tracker?style=flat-square)](https://hub.docker.com/r/henrywhitaker3/speedtest-tracker) [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/henrywhitaker3/Speedtest-Tracker/Stable?label=master&logo=github&style=flat-square)](https://github.com/henrywhitaker3/Speedtest-Tracker/actions) [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/henrywhitaker3/Speedtest-Tracker/Dev?label=dev&logo=github&style=flat-square)](https://github.com/henrywhitaker3/Speedtest-Tracker/actions) [![last_commit](https://img.shields.io/github/last-commit/henrywhitaker3/Speedtest-Tracker?style=flat-square)](https://github.com/henrywhitaker3/Speedtest-Tracker/commits) [![issues](https://img.shields.io/github/issues/henrywhitaker3/Speedtest-Tracker?style=flat-square)](https://github.com/henrywhitaker3/Speedtest-Tracker/issues) [![commit_freq](https://img.shields.io/github/commit-activity/m/henrywhitaker3/Speedtest-Tracker?style=flat-square)](https://github.com/henrywhitaker3/Speedtest-Tracker/commits) ![version](https://img.shields.io/badge/version-v1.11.1-success?style=flat-square) [![license](https://img.shields.io/github/license/henrywhitaker3/Speedtest-Tracker?style=flat-square)](https://github.com/henrywhitaker3/Speedtest-Tracker/blob/master/LICENSE)
[![Docker pulls](https://img.shields.io/docker/pulls/henrywhitaker3/speedtest-tracker?style=flat-square)](https://hub.docker.com/r/henrywhitaker3/speedtest-tracker) [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/henrywhitaker3/Speedtest-Tracker/Stable?label=master&logo=github&style=flat-square)](https://github.com/henrywhitaker3/Speedtest-Tracker/actions) [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/henrywhitaker3/Speedtest-Tracker/Dev?label=dev&logo=github&style=flat-square)](https://github.com/henrywhitaker3/Speedtest-Tracker/actions) [![last_commit](https://img.shields.io/github/last-commit/henrywhitaker3/Speedtest-Tracker?style=flat-square)](https://github.com/henrywhitaker3/Speedtest-Tracker/commits) [![issues](https://img.shields.io/github/issues/henrywhitaker3/Speedtest-Tracker?style=flat-square)](https://github.com/henrywhitaker3/Speedtest-Tracker/issues) [![commit_freq](https://img.shields.io/github/commit-activity/m/henrywhitaker3/Speedtest-Tracker?style=flat-square)](https://github.com/henrywhitaker3/Speedtest-Tracker/commits) ![version](https://img.shields.io/badge/version-v1.12.0-success?style=flat-square) [![license](https://img.shields.io/github/license/henrywhitaker3/Speedtest-Tracker?style=flat-square)](https://github.com/henrywhitaker3/Speedtest-Tracker/blob/master/LICENSE)
This program runs a speedtest check every hour and graphs the results. The back-end is written in [Laravel](https://laravel.com/) and the front-end uses [React](https://reactjs.org/). It uses the [Ookla's speedtest cli](https://www.speedtest.net/apps/cli) package to get the data and uses [Chart.js](https://www.chartjs.org/) to plot the results.
@@ -18,6 +18,7 @@ Disclaimer: You will need to accept Ookla's EULA and privacy agreements in order
- Slack/Discord/Telegram notifications
- [healthchecks.io](https://healthchecks.io) integration
- Organizr integration
- InfluxDB integration (currently v1 only, v2 is a WIP)
## Installation & Setup
@@ -84,6 +85,8 @@ Container images are configured using parameters passed at runtime (such as thos
| `-e PUID` | Optional. Supply a local user ID for volume permissions |
| `-e PGID` | Optional. Supply a local group ID for volume permissions |
| `-e AUTH` | Optional. Set to 'true' to enable authentication for the app |
| `-e INFLUXDB_RETENTION`| Optional. Sets the InfluxDB retention period, defaults to `30d` |
| `-e INFLUXDB_HOST_TAG | Optional. Sets the InfluxDB host tag value, defaults to `speedtest` |
### Authentication

View File

@@ -2,6 +2,7 @@
namespace App\Helpers;
use App\Interfaces\SpeedtestProvider;
use App\Models\Speedtest;
use App\Utils\OoklaTester;
use Carbon\Carbon;
@@ -25,7 +26,7 @@ class SpeedtestHelper
*/
public static function runSpeedtest()
{
$tester = new OoklaTester();
$tester = app()->make(SpeedtestProvider::class);
return $tester->run();
}

View File

@@ -0,0 +1,37 @@
<?php
namespace App\Utils\InfluxDB;
use App\Interfaces\InfluxDBWrapperInterface;
use App\Models\Speedtest;
use InfluxDB2\Client;
class InfluxDBVersion2Wrapper implements InfluxDBWrapperInterface
{
private Client $client;
public function __construct(Client $client)
{
$this->client = $client;
}
public function testConnection(): bool
{
return $this->client->health()->getStatus() !== 'pass';
}
public function doesDatabaseExist(string $database): bool
{
return true;
}
public function createDatabase(string $database): bool
{
return true;
}
public function store(Speedtest $speedtest): bool
{
return true;
}
}

View File

@@ -1,4 +1,10 @@
{
"1.12.0": [
{
"description": "Added InfluxDB intergation.",
"link": ""
}
],
"1.11.1": [
{
"description": "Add option to show/hide columns in the all tests table.",

View File

@@ -7,7 +7,7 @@ return [
|--------------------------------------------------------------------------
*/
'version' => '1.11.1',
'version' => '1.12.0',
/*
|--------------------------------------------------------------------------

View File

@@ -26,6 +26,7 @@ class SpeedtestFactory extends Factory
'upload' => rand(15, 900),
'ping' => rand(1, 25),
'scheduled' => (bool) rand(0, 1),
'failed' => false,
];
}
}

Binary file not shown.

View File

@@ -3,14 +3,28 @@
namespace Tests\Feature;
use App\Helpers\SpeedtestHelper;
use App\Interfaces\SpeedtestProvider;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\Mocks\OoklaTesterMocker;
use Tests\TestCase;
class SpeedtestTest extends TestCase
{
use RefreshDatabase;
public function setUp(): void
{
parent::setUp();
$this->app->singleton(
SpeedtestProvider::class,
function () {
return new OoklaTesterMocker();
}
);
}
/**
* Runs a speedtest
*

View File

@@ -0,0 +1,26 @@
<?php
namespace Tests\Feature\Utils\InfluxDB;
use App\Exceptions\InfluxDBNotEnabledException;
use App\Utils\InfluxDB\InfluxDB;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class InfluxDBWrapperWrapperTest extends TestCase
{
use RefreshDatabase;
/**
* Test it throws the right exception.
*
* @return void
*/
public function test_it_throws_excetpion_when_it_is_disabled()
{
$this->expectException(InfluxDBNotEnabledException::class);
InfluxDB::connect();
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace Tests\Mocks;
use App\Exceptions\SpeedtestFailureException;
use App\Interfaces\SpeedtestProvider;
use App\Models\Speedtest;
use Exception;
class OoklaTesterMocker implements SpeedtestProvider
{
private bool $passes;
public function __construct(bool $passes = true)
{
$this->passes = $passes;
}
public function run($output = null): Speedtest
{
$output = $output ?? $this->output();
try {
$output = json_decode($output, true, 512, JSON_THROW_ON_ERROR);
} catch (Exception $e) {
throw new SpeedtestFailureException();
}
return $this->passes
? Speedtest::factory()->create()
: Speedtest::factory()->create([
'download' => 0,
'upload' => 0,
'ping' => 0,
'failed' => true,
]);
}
public function output()
{
return !$this->passes
? null
: json_encode([
'type' => 'result',
'download' => ['bandwidth' => '50'],
'upload' => ['bandwidth' => '50'],
'ping' => ['latency' => '50'],
'server' => [
'id' => '1',
'name' => 'PHPUnit',
'host' => 'phpunit',
'port' => '443',
],
'result' => [
'url' => 'some-url',
]
]);
}
}

View File

@@ -4,6 +4,7 @@ namespace Tests\Unit\Helpers\SpeedtestHelper;
use App\Utils\OoklaTester;
use PHPUnit\Framework\TestCase;
use Tests\Mocks\OoklaTesterMocker;
class CheckOutputTest extends TestCase
{
@@ -12,6 +13,7 @@ class CheckOutputTest extends TestCase
public function setUp(): void
{
$this->speedtestProvider = new OoklaTester();
$this->mocker = new OoklaTesterMocker();
}
/**

View File

@@ -6,6 +6,7 @@ use App\Exceptions\SpeedtestFailureException;
use App\Helpers\SpeedtestHelper;
use App\Utils\OoklaTester;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\Mocks\OoklaTesterMocker;
use Tests\TestCase;
class SpeedtestTest extends TestCase
@@ -22,17 +23,7 @@ class SpeedtestTest extends TestCase
$this->speedtestProvider = new OoklaTester();
$this->output = $this->speedtestProvider->output();
}
/**
* A basic unit test example.
*
* @return void
*/
public function testOutputFunction()
{
$this->assertJson($this->output);
$this->output = (new OoklaTesterMocker())->output();
}
/**