Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

202 lines
6.6 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* OSModulesTest.php
*
* Test discovery and poller modules
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2021-02-09 00:29:04 +01:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
2021-02-09 00:29:04 +01:00
* @link https://www.librenms.org
2021-09-10 20:09:53 +02:00
*
* @copyright 2017 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Tests;
2019-11-14 21:56:06 +00:00
use DeviceCache;
2020-05-24 13:49:01 -05:00
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Arr;
2019-10-13 13:40:38 +00:00
use LibreNMS\Config;
use LibreNMS\Data\Source\Fping;
use LibreNMS\Data\Source\FpingResponse;
2018-02-05 07:39:13 -06:00
use LibreNMS\Exceptions\FileNotFoundException;
use LibreNMS\Exceptions\InvalidModuleException;
2021-04-29 22:42:18 -05:00
use LibreNMS\Util\Debug;
use LibreNMS\Util\ModuleTestHelper;
2023-05-19 09:57:39 -05:00
use LibreNMS\Util\Number;
use PHPUnit\Util\Color;
class OSModulesTest extends DBTestCase
{
2020-05-24 13:49:01 -05:00
use DatabaseTransactions;
2019-10-13 13:40:38 +00:00
private $discoveryModules;
private $pollerModules;
2020-05-19 16:31:50 +02:00
protected function setUp(): void
2019-10-13 13:40:38 +00:00
{
parent::setUp();
// backup modules
$this->discoveryModules = Config::get('discovery_modules');
$this->pollerModules = Config::get('poller_modules');
}
2020-05-19 16:31:50 +02:00
protected function tearDown(): void
2019-10-13 13:40:38 +00:00
{
// restore modules
Config::set('discovery_modules', $this->discoveryModules);
Config::set('poller_modules', $this->pollerModules);
parent::tearDown();
}
2018-08-28 14:18:59 -05:00
/**
* Test all modules for a particular OS
*
* @group os
2023-03-13 22:32:22 +01:00
*
2018-08-28 14:18:59 -05:00
* @dataProvider dumpedDataProvider
*/
2023-05-24 22:21:54 +02:00
public function testDataIsValid($os, $variant, $modules): void
2018-08-28 14:18:59 -05:00
{
// special case if data provider throws exception
if ($os === false) {
$this->fail($modules);
}
$this->assertNotEmpty($modules, "No modules to test for $os $variant");
}
/**
2017-12-28 16:12:08 -06:00
* Test all modules for a particular OS
*
* @group os
2023-03-13 22:32:22 +01:00
*
* @dataProvider dumpedDataProvider
2021-09-10 20:09:53 +02:00
*
2021-09-08 23:35:56 +02:00
* @param string $os base os
* @param string $variant optional variant
* @param array $modules modules to test for this os
*/
2023-05-24 22:21:54 +02:00
public function testOS($os, $variant, $modules): void
{
// Lock testing time
$this->travelTo(new \DateTime('2022-01-01 00:00:00'));
$this->requireSnmpsim(); // require snmpsim for tests
2022-11-09 09:47:19 +01:00
// stub out Eventlog::log and Fping->ping, we don't need to store them for these tests
2020-05-19 22:08:41 -05:00
$this->stubClasses();
2020-04-11 19:53:25 -05:00
2018-02-05 07:39:13 -06:00
try {
2021-04-29 22:42:18 -05:00
Debug::set(false); // avoid all undefined index errors in the legacy code
2018-02-05 07:39:13 -06:00
$helper = new ModuleTestHelper($modules, $os, $variant);
$helper->setQuiet();
$filename = $helper->getJsonFilepath(true);
2018-02-05 07:39:13 -06:00
$expected_data = $helper->getTestData();
2019-10-13 13:40:38 +00:00
$results = $helper->generateTestData($this->getSnmpsim(), true);
2021-11-17 19:23:55 -06:00
} catch (FileNotFoundException|InvalidModuleException $e) {
$this->fail($e->getMessage());
2018-02-05 07:39:13 -06:00
}
2018-01-12 15:24:12 -06:00
if (is_null($results)) {
$this->fail("$os: Failed to collect data.");
2018-01-12 15:24:12 -06:00
}
// output all discovery and poller output if debug mode is enabled for phpunit
2021-04-29 22:42:18 -05:00
$phpunit_debug = in_array('--debug', $_SERVER['argv'], true);
foreach ($modules as $module) {
2021-03-12 18:10:14 -06:00
$expected = $expected_data[$module]['discovery'] ?? [];
$actual = $results[$module]['discovery'] ?? [];
$this->checkTestData($expected, $actual, 'Discovered', $os, $module, $filename, $helper, $phpunit_debug);
2023-08-13 11:43:52 -05:00
// modules without polling
if (in_array($module, ['route', 'vlans'])) {
2020-07-04 23:27:20 -05:00
continue;
}
if ($expected_data[$module]['poller'] !== 'matches discovery') {
2021-03-12 18:10:14 -06:00
$expected = $expected_data[$module]['poller'] ?? [];
}
2021-03-12 18:10:14 -06:00
$actual = $results[$module]['poller'] ?? [];
$this->checkTestData($expected, $actual, 'Polled', $os, $module, $filename, $helper, $phpunit_debug);
}
2019-11-14 21:56:06 +00:00
$this->assertTrue(true, "Tested $os successfully"); // avoid no asserts error
2019-11-14 21:56:06 +00:00
DeviceCache::flush(); // clear cached devices
$this->travelBack();
}
public function dumpedDataProvider()
{
$modules = [];
if (getenv('TEST_MODULES')) {
$modules = explode(',', getenv('TEST_MODULES'));
}
2018-08-28 14:18:59 -05:00
try {
return ModuleTestHelper::findOsWithData($modules);
} catch (InvalidModuleException $e) {
// special case for exception
return [[false, false, $e->getMessage()]];
}
}
2020-05-19 22:08:41 -05:00
private function stubClasses(): void
{
2022-11-09 09:47:19 +01:00
$this->app->bind(\App\Models\Eventlog::class, function ($app) {
$mock = \Mockery::mock(\App\Models\Eventlog::class);
$mock->shouldReceive('_log');
2021-11-17 19:23:55 -06:00
return $mock;
2020-05-19 22:08:41 -05:00
});
$this->app->bind(Fping::class, function ($app) {
2022-11-09 09:47:19 +01:00
$mock = \Mockery::mock(\LibreNMS\Data\Source\Fping::class);
$mock->shouldReceive('ping')->andReturn(FpingResponse::artificialUp());
2020-05-19 22:08:41 -05:00
return $mock;
});
}
private function checkTestData(array $expected, array $actual, string $type, string $os, mixed $module, string $filename, ModuleTestHelper $helper, bool $phpunit_debug): void
{
// try simple and fast comparison first, if that fails, do a costly/well formatted comparison
if ($expected != $actual) {
$message = Color::colorize('bg-red', "OS $os: $type $module data does not match that found in $filename");
$message .= PHP_EOL;
$message .= ($type == 'Discovered'
? $helper->getDiscoveryOutput($phpunit_debug ? null : $module)
: $helper->getPollerOutput($phpunit_debug ? null : $module));
2023-05-19 09:57:39 -05:00
// convert to dot notation so the array is flat and easier to compare visually
$expected = Arr::dot($expected);
$actual = Arr::dot($actual);
// json will store 43.0 as 43, Number::cast will change those to integers too
foreach ($actual as $index => $value) {
if (is_float($value)) {
$actual[$index] = Number::cast($value);
}
}
$this->assertSame($expected, $actual, $message);
}
}
}