mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
* core WIP * try to finish up * trim space too and a couple of cleanups * update test data * put escapes back * another net-snmp difference * correct copy paste error * WIP * Use new code YAY * a tiny bit more * Kind of working * Handle manual modules correctly * convert core to modern module * Only save metrics if modules is not overridden * correct module exists check * database error handling * debug handling * restore bad changes * Introduce Actions RunAlertRulesAction UpdateDeviceGroupsAction * tweaks to output * Fix some issues in outside code * Style fixes * fixes to module status checks * typehints! * Use logger only and DI * OS module not named correctly * Work on quiet output a bit more * generically don't change output when disabling debug if the driver is already stack * Fix missing $device variable for legacy os polling Fix missing dbFacile functions when no legacy modules polled in RunAlertRulesAction * restore legacy os module shim * use the new poller code for tests * PollingDevice event * Fix some issues and enable/disable error reporting around legacy modules * typehints * fully update baseline * Use Process for version commands so we don't leak debug output. * don't detect rrdtool version in ci every time * style fixes * Warning fixes * more fixes * re-update baseline * remove diff noise * fix up alerts * Catch exceptions in device ip lookup * Revert accidental snmp.inc.php poller target change (should have been ?: not ??)
183 lines
5.8 KiB
PHP
183 lines
5.8 KiB
PHP
<?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
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*
|
|
* @link https://www.librenms.org
|
|
*
|
|
* @copyright 2017 Tony Murray
|
|
* @author Tony Murray <murraytony@gmail.com>
|
|
*/
|
|
|
|
namespace LibreNMS\Tests;
|
|
|
|
use DeviceCache;
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
use LibreNMS\Config;
|
|
use LibreNMS\Data\Source\Fping;
|
|
use LibreNMS\Data\Source\FpingResponse;
|
|
use LibreNMS\Exceptions\FileNotFoundException;
|
|
use LibreNMS\Exceptions\InvalidModuleException;
|
|
use LibreNMS\Util\Debug;
|
|
use LibreNMS\Util\ModuleTestHelper;
|
|
|
|
class OSModulesTest extends DBTestCase
|
|
{
|
|
use DatabaseTransactions;
|
|
|
|
private $discoveryModules;
|
|
private $pollerModules;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
// backup modules
|
|
$this->discoveryModules = Config::get('discovery_modules');
|
|
$this->pollerModules = Config::get('poller_modules');
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
// restore modules
|
|
Config::set('discovery_modules', $this->discoveryModules);
|
|
Config::set('poller_modules', $this->pollerModules);
|
|
|
|
parent::tearDown();
|
|
}
|
|
|
|
/**
|
|
* 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");
|
|
}
|
|
|
|
/**
|
|
* Test all modules for a particular OS
|
|
*
|
|
* @group os
|
|
* @dataProvider dumpedDataProvider
|
|
*
|
|
* @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
|
|
// stub out Log::event and Fping->ping, we don't need to store them for these tests
|
|
$this->stubClasses();
|
|
|
|
try {
|
|
Debug::set(false); // avoid all undefined index errors in the legacy code
|
|
$helper = new ModuleTestHelper($modules, $os, $variant);
|
|
$helper->setQuiet();
|
|
|
|
$filename = $helper->getJsonFilepath(true);
|
|
$expected_data = $helper->getTestData();
|
|
$results = $helper->generateTestData($this->getSnmpsim(), true);
|
|
} catch (FileNotFoundException|InvalidModuleException $e) {
|
|
$this->fail($e->getMessage());
|
|
}
|
|
|
|
if (is_null($results)) {
|
|
$this->fail("$os: Failed to collect data.");
|
|
}
|
|
|
|
// output all discovery and poller output if debug mode is enabled for phpunit
|
|
$phpunit_debug = in_array('--debug', $_SERVER['argv'], true);
|
|
|
|
foreach ($modules as $module) {
|
|
$expected = $expected_data[$module]['discovery'] ?? [];
|
|
$actual = $results[$module]['discovery'] ?? [];
|
|
$this->assertEquals(
|
|
$expected,
|
|
$actual,
|
|
"OS $os: Discovered $module data does not match that found in $filename\n"
|
|
. print_r(array_diff($expected, $actual), true)
|
|
. $helper->getDiscoveryOutput($phpunit_debug ? null : $module)
|
|
. "\nOS $os: Discovered $module data does not match that found in $filename"
|
|
);
|
|
|
|
if ($module === 'route') {
|
|
// no route poller module
|
|
continue;
|
|
}
|
|
|
|
if ($expected_data[$module]['poller'] == 'matches discovery') {
|
|
$expected = $expected_data[$module]['discovery'];
|
|
} else {
|
|
$expected = $expected_data[$module]['poller'] ?? [];
|
|
}
|
|
$actual = $results[$module]['poller'] ?? [];
|
|
$this->assertEquals(
|
|
$expected,
|
|
$actual,
|
|
"OS $os: Polled $module data does not match that found in $filename\n"
|
|
. print_r(array_diff($expected, $actual), true)
|
|
. $helper->getPollerOutput($phpunit_debug ? null : $module)
|
|
. "\nOS $os: Polled $module data does not match that found in $filename"
|
|
);
|
|
}
|
|
|
|
DeviceCache::flush(); // clear cached devices
|
|
}
|
|
|
|
public function dumpedDataProvider()
|
|
{
|
|
$modules = [];
|
|
|
|
if (getenv('TEST_MODULES')) {
|
|
$modules = explode(',', getenv('TEST_MODULES'));
|
|
}
|
|
|
|
try {
|
|
return ModuleTestHelper::findOsWithData($modules);
|
|
} catch (InvalidModuleException $e) {
|
|
// special case for exception
|
|
return [[false, false, $e->getMessage()]];
|
|
}
|
|
}
|
|
|
|
private function stubClasses(): void
|
|
{
|
|
$this->app->bind('log', function ($app) {
|
|
$mock = \Mockery::mock('\App\Facades\LogManager[event]', [$app]);
|
|
$mock->shouldReceive('event');
|
|
|
|
return $mock;
|
|
});
|
|
|
|
$this->app->bind(Fping::class, function ($app) {
|
|
$mock = \Mockery::mock('\LibreNMS\Data\Source\Fping');
|
|
$mock->shouldReceive('ping')->andReturn(FpingResponse::artificialUp());
|
|
|
|
return $mock;
|
|
});
|
|
}
|
|
}
|