Files
librenms-librenms/tests/OSModulesTest.php
T

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

183 lines
5.8 KiB
PHP
Raw 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;
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;
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
* @dataProvider dumpedDataProvider
*/
public function testDataIsValid($os, $variant, $modules)
{
// 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
* @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
*/
public function testOS($os, $variant, $modules)
{
$this->requireSnmpsim(); // require snmpsim for tests
2020-05-19 22:08:41 -05:00
// stub out Log::event and Fping->ping, we don't need to store them for these tests
$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->assertEquals(
2018-02-05 07:39:13 -06:00
$expected,
$actual,
"OS $os: Discovered $module data does not match that found in $filename\n"
2018-02-05 07:39:13 -06:00
. print_r(array_diff($expected, $actual), true)
2021-04-29 22:42:18 -05:00
. $helper->getDiscoveryOutput($phpunit_debug ? null : $module)
. "\nOS $os: Discovered $module data does not match that found in $filename"
);
2020-07-04 23:27:20 -05:00
if ($module === 'route') {
// no route poller module
continue;
}
if ($expected_data[$module]['poller'] == 'matches discovery') {
$expected = $expected_data[$module]['discovery'];
} else {
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->assertEquals(
2018-02-05 07:39:13 -06:00
$expected,
$actual,
"OS $os: Polled $module data does not match that found in $filename\n"
2018-02-05 07:39:13 -06:00
. print_r(array_diff($expected, $actual), true)
2021-04-29 22:42:18 -05:00
. $helper->getPollerOutput($phpunit_debug ? null : $module)
. "\nOS $os: Polled $module data does not match that found in $filename"
);
}
2019-11-14 21:56:06 +00:00
DeviceCache::flush(); // clear cached devices
}
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
{
$this->app->bind('log', function ($app) {
2021-11-17 19:23:55 -06:00
$mock = \Mockery::mock('\App\Facades\LogManager[event]', [$app]);
$mock->shouldReceive('event');
return $mock;
2020-05-19 22:08:41 -05:00
});
$this->app->bind(Fping::class, function ($app) {
$mock = \Mockery::mock('\LibreNMS\Data\Source\Fping');
$mock->shouldReceive('ping')->andReturn(FpingResponse::artificialUp());
2020-05-19 22:08:41 -05:00
return $mock;
});
}
}