Files

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

185 lines
5.3 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* OSDiscoveryTest.php
*
* Test all discovery for all OS
*
* 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 2016 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Tests;
use App\Models\Device;
2020-04-17 17:37:56 -05:00
use Illuminate\Support\Str;
2017-12-28 16:12:08 -06:00
use LibreNMS\Config;
use LibreNMS\Data\Source\NetSnmpQuery;
2020-10-05 07:26:37 -05:00
use LibreNMS\Modules\Core;
use LibreNMS\Tests\Mocks\SnmpQueryMock;
2021-04-29 22:42:18 -05:00
use LibreNMS\Util\Debug;
use LibreNMS\Util\OS;
class OSDiscoveryTest extends TestCase
{
private static $unchecked_files;
2020-05-19 16:31:50 +02:00
public static function setUpBeforeClass(): void
{
2017-12-28 16:12:08 -06:00
parent::setUpBeforeClass();
2017-12-28 16:12:08 -06:00
$glob = Config::get('install_dir') . '/tests/snmpsim/*.snmprec';
2022-01-30 16:28:18 -06:00
self::$unchecked_files = array_flip(array_filter(array_map(function ($file) {
return basename($file, '.snmprec');
2022-01-30 16:28:18 -06:00
}, glob($glob)), function ($file) {
return ! Str::contains($file, '@');
}));
2017-12-28 16:12:08 -06:00
}
2017-12-28 16:12:08 -06:00
/**
* Populate a list of files to check and make sure it isn't empty
*/
public function testHaveFilesToTest()
{
$this->assertNotEmpty(self::$unchecked_files);
}
/**
* Test each OS provided by osProvider
*
2017-12-28 16:12:08 -06:00
* @group os
* @dataProvider osProvider
2021-09-10 20:09:53 +02:00
*
2021-09-08 23:35:56 +02:00
* @param string $os_name
*/
2021-05-16 13:32:54 -05:00
public function testOSDetection($os_name)
{
if (! getenv('SNMPSIM')) {
$this->app->bind(NetSnmpQuery::class, SnmpQueryMock::class);
}
2017-12-28 16:12:08 -06:00
$glob = Config::get('install_dir') . "/tests/snmpsim/$os_name*.snmprec";
$files = array_map(function ($file) {
return basename($file, '.snmprec');
}, glob($glob));
$files = array_filter($files, function ($file) use ($os_name) {
2022-01-30 16:28:18 -06:00
if (Str::contains($file, '@')) {
return false;
}
2020-04-17 17:37:56 -05:00
return $file == $os_name || Str::startsWith($file, $os_name . '_');
});
if (empty($files)) {
2019-03-12 23:59:03 -05:00
$this->fail("No snmprec files found for $os_name!");
}
foreach ($files as $file) {
$this->checkOS($os_name, $file);
unset(self::$unchecked_files[$file]); // This file has been tested
}
}
/**
* Test that all files have been tested (removed from self::$unchecked_files
*
2021-05-16 13:32:54 -05:00
* @depends testOSDetection
*/
public function testAllFilesTested()
{
2017-12-28 16:12:08 -06:00
$this->assertEmpty(
self::$unchecked_files,
'Not all snmprec files were checked: ' . print_r(array_keys(self::$unchecked_files), true)
);
}
/**
2016-09-19 21:12:26 -05:00
* Set up and test an os
* If $filename is not set, it will use the snmprec file matching $expected_os
*
2021-09-08 23:35:56 +02:00
* @param string $expected_os The os we should get back from getHostOS()
* @param string $filename the name of the snmprec file to use
*/
2016-09-19 21:12:26 -05:00
private function checkOS($expected_os, $filename = null)
{
$start = microtime(true);
2016-09-19 21:12:26 -05:00
$community = $filename ?: $expected_os;
2021-04-29 22:42:18 -05:00
Debug::set();
Debug::setVerbose();
2016-09-19 21:12:26 -05:00
ob_start();
2020-10-05 07:26:37 -05:00
$os = Core::detectOS($this->genDevice($community));
2016-09-19 21:12:26 -05:00
$output = ob_get_contents();
ob_end_clean();
Debug::set(false);
Debug::setVerbose(false);
$this->assertLessThan(10, microtime(true) - $start, "OS $expected_os took longer than 10s to detect");
2016-09-19 21:12:26 -05:00
$this->assertEquals($expected_os, $os, "Test file: $community.snmprec\n$output");
}
/**
* Generate a fake $device array
*
2021-09-08 23:35:56 +02:00
* @param string $community The snmp community to set
* @return Device resulting device array
2016-09-19 21:12:26 -05:00
*/
private function genDevice($community): Device
2016-09-19 21:12:26 -05:00
{
return new Device([
2019-10-13 13:40:38 +00:00
'hostname' => $this->getSnmpsim()->getIP(),
2016-09-19 21:12:26 -05:00
'snmpver' => 'v2c',
2019-10-13 13:40:38 +00:00
'port' => $this->getSnmpsim()->getPort(),
2016-09-19 21:12:26 -05:00
'timeout' => 3,
'retries' => 0,
'snmp_max_repeaters' => 10,
'community' => $community,
2016-11-10 00:23:27 +00:00
'os' => 'generic',
]);
}
/**
* Provides a list of OS to generate tests.
*
* @return array
*/
public function osProvider()
2017-02-07 07:31:20 -06:00
{
2017-05-10 02:57:10 -05:00
// make sure all OS are loaded
2017-12-28 16:12:08 -06:00
$config_os = array_keys(Config::get('os'));
if (count($config_os) < count(glob(Config::get('install_dir') . '/includes/definitions/*.yaml'))) {
OS::loadAllDefinitions(false, true);
2018-05-09 08:05:17 -05:00
$config_os = array_keys(Config::get('os'));
2017-05-10 02:57:10 -05:00
}
2017-02-07 07:31:20 -06:00
$excluded_os = [
'default',
'generic',
'ping',
2017-02-07 07:31:20 -06:00
];
2017-12-28 16:12:08 -06:00
$filtered_os = array_diff($config_os, $excluded_os);
2017-02-07 07:31:20 -06:00
2017-12-28 16:12:08 -06:00
$all_os = [];
foreach ($filtered_os as $os) {
$all_os[$os] = [$os];
}
2016-09-18 19:11:10 +01:00
return $all_os;
}
}