2017-12-20 08:36:49 -06:00
|
|
|
<?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/>.
|
2017-12-20 08:36:49 -06:00
|
|
|
*
|
2021-02-09 00:29:04 +01:00
|
|
|
* @link https://www.librenms.org
|
2021-09-10 20:09:53 +02:00
|
|
|
*
|
2017-12-20 08:36:49 -06: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;
|
2023-04-27 14:49:52 -05:00
|
|
|
use Illuminate\Support\Arr;
|
2019-10-13 13:40:38 +00:00
|
|
|
use LibreNMS\Config;
|
2021-10-03 22:45:10 -05:00
|
|
|
use LibreNMS\Data\Source\Fping;
|
|
|
|
use LibreNMS\Data\Source\FpingResponse;
|
2018-02-05 07:39:13 -06:00
|
|
|
use LibreNMS\Exceptions\FileNotFoundException;
|
2018-02-25 20:03:18 -06:00
|
|
|
use LibreNMS\Exceptions\InvalidModuleException;
|
2021-04-29 22:42:18 -05:00
|
|
|
use LibreNMS\Util\Debug;
|
2017-12-20 08:36:49 -06:00
|
|
|
use LibreNMS\Util\ModuleTestHelper;
|
2023-05-19 09:57:39 -05:00
|
|
|
use LibreNMS\Util\Number;
|
2023-04-27 14:49:52 -05:00
|
|
|
use PHPUnit\Util\Color;
|
2017-12-20 08:36:49 -06:00
|
|
|
|
|
|
|
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-20 08:36:49 -06:00
|
|
|
/**
|
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
|
|
|
*
|
2017-12-20 08:36:49 -06: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
|
2017-12-20 08:36:49 -06:00
|
|
|
*/
|
2023-05-24 22:21:54 +02:00
|
|
|
public function testOS($os, $variant, $modules): void
|
2017-12-20 08:36:49 -06:00
|
|
|
{
|
2023-03-10 14:50:56 +01:00
|
|
|
// Lock testing time
|
|
|
|
$this->travelTo(new \DateTime('2022-01-01 00:00:00'));
|
Refactored and update Location Geocoding (#9359)
- Fix location so it is a regular database relation (this allows multiple devices to be accurately linked to one location and saves api calls)
- Parse coordinates from the location more consistently
- Add settings to webui
- ~~Used [PHP Geocoder](http://geocoder-php.org/), which has lots of backends and is well tested. (also includes reverse and geoip)~~
- Google Maps, Bing, Mapquest, and OpenStreetMap supported initially.
- Default to OpenStreetMap, which doesn't require a key. They will liberally hand out bans if you exceed 1 query per second though.
- All other Geocoding APIs require an API key. (Google requires a credit card on file, but seems to be the most accurate)
- Update all (I think) sql queries to handle the new structure
- Remove final vestiges of override_sysLocation as a device attribute
- Update existing device groups and rules in DB
- Tested all APIs with good/bad location, no/bad/good key, and no connection.
- Cannot fix advanced queries that use location
This blocks #8868
DO NOT DELETE THIS TEXT
#### Please note
> Please read this information carefully. You can run `./scripts/pre-commit.php` to check your code before submitting.
- [x] Have you followed our [code guidelines?](http://docs.librenms.org/Developing/Code-Guidelines/)
#### Testers
If you would like to test this pull request then please run: `./scripts/github-apply <pr_id>`, i.e `./scripts/github-apply 5926`
After you are done testing, you can remove the changes with `./scripts/github-remove`. If there are schema changes, you can ask on discord how to revert.
2018-11-28 16:49:18 -06: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();
|
2017-12-20 08:36:49 -06:00
|
|
|
|
2018-03-14 17:28:01 -05:00
|
|
|
$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
|
|
|
}
|
2017-12-20 08:36:49 -06:00
|
|
|
|
2018-01-12 15:24:12 -06:00
|
|
|
if (is_null($results)) {
|
2018-01-20 20:22:47 -06:00
|
|
|
$this->fail("$os: Failed to collect data.");
|
2018-01-12 15:24:12 -06:00
|
|
|
}
|
|
|
|
|
2018-03-14 17:28:01 -05: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);
|
2018-03-14 17:28:01 -05:00
|
|
|
|
2018-01-09 11:40:26 -06:00
|
|
|
foreach ($modules as $module) {
|
2021-03-12 18:10:14 -06:00
|
|
|
$expected = $expected_data[$module]['discovery'] ?? [];
|
|
|
|
$actual = $results[$module]['discovery'] ?? [];
|
2023-04-27 14:49:52 -05:00
|
|
|
$this->checkTestData($expected, $actual, 'Discovered', $os, $module, $filename, $helper, $phpunit_debug);
|
2017-12-20 08:36:49 -06:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-04-27 14:49:52 -05:00
|
|
|
if ($expected_data[$module]['poller'] !== 'matches discovery') {
|
2021-03-12 18:10:14 -06:00
|
|
|
$expected = $expected_data[$module]['poller'] ?? [];
|
2018-03-14 17:28:01 -05:00
|
|
|
}
|
2021-03-12 18:10:14 -06:00
|
|
|
$actual = $results[$module]['poller'] ?? [];
|
2023-04-27 14:49:52 -05:00
|
|
|
$this->checkTestData($expected, $actual, 'Polled', $os, $module, $filename, $helper, $phpunit_debug);
|
2017-12-20 08:36:49 -06:00
|
|
|
}
|
2019-11-14 21:56:06 +00:00
|
|
|
|
2023-04-27 14:49:52 -05:00
|
|
|
$this->assertTrue(true, "Tested $os successfully"); // avoid no asserts error
|
|
|
|
|
2019-11-14 21:56:06 +00:00
|
|
|
DeviceCache::flush(); // clear cached devices
|
2023-03-10 14:50:56 +01:00
|
|
|
$this->travelBack();
|
2017-12-20 08:36:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
public function dumpedDataProvider()
|
|
|
|
{
|
2018-01-20 20:22:47 -06:00
|
|
|
$modules = [];
|
2018-01-09 11:40:26 -06:00
|
|
|
|
2018-01-20 20:22:47 -06:00
|
|
|
if (getenv('TEST_MODULES')) {
|
|
|
|
$modules = explode(',', getenv('TEST_MODULES'));
|
2017-12-20 08:36:49 -06:00
|
|
|
}
|
|
|
|
|
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()]];
|
|
|
|
}
|
2017-12-20 08:36:49 -06:00
|
|
|
}
|
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);
|
2021-10-03 22:45:10 -05:00
|
|
|
$mock->shouldReceive('ping')->andReturn(FpingResponse::artificialUp());
|
2020-05-19 22:08:41 -05:00
|
|
|
|
|
|
|
return $mock;
|
|
|
|
});
|
|
|
|
}
|
2023-04-27 14:49:52 -05:00
|
|
|
|
|
|
|
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);
|
2023-04-27 14:49:52 -05:00
|
|
|
}
|
|
|
|
}
|
2017-12-20 08:36:49 -06:00
|
|
|
}
|