Module tests update: per module output, only store modules data that are run (#8355)

* Test data: don't save data for modules that were not run

* Sometimes mibs are prefixed with +

* Save output for each module and only output the errored module.
Also makes it easier to identify modules that were ran.

* Clean up json files.
Message when no module data exists.

* verbose output when phpunit --debug is used
order by for ports
avoid graphite output in tests
only load module_tables.yaml once

* use explode and strpos instead of regex since it is failing...

* Fix some warnings

* Fix whitespace
This commit is contained in:
Tony Murray
2018-03-14 22:28:01 +00:00
committed by Neil Lathwood
parent 66a3f82269
commit 2bcc76625c
16 changed files with 122 additions and 377 deletions
+91 -17
View File
@@ -32,6 +32,8 @@ use Symfony\Component\Yaml\Yaml;
class ModuleTestHelper
{
private static $module_tables;
private $quiet = false;
private $modules;
private $variant;
@@ -41,7 +43,8 @@ class ModuleTestHelper
private $snmprec_dir;
private $json_dir;
private $file_name;
private $module_tables;
private $discovery_module_output = [];
private $poller_module_output = [];
private $discovery_output;
private $poller_output;
@@ -86,7 +89,10 @@ class ModuleTestHelper
$influxdb = false;
Config::set('nographite', true);
$this->module_tables = Yaml::parse($install_dir . '/tests/module_tables.yaml');
if (is_null(self::$module_tables)) {
// only load the yaml once, then keep it in memory
self::$module_tables = Yaml::parse($install_dir . '/tests/module_tables.yaml');
}
}
private static function compareOid($a, $b)
@@ -189,7 +195,7 @@ class ModuleTestHelper
'sysObjectID.0_get' => array('oid' => 'sysObjectID.0', 'mib' => 'SNMPv2-MIB', 'method' => 'get'),
);
foreach ($snmp_matches[0] as $index => $line) {
preg_match('/-m ([a-zA-Z0-9:\-]+)/', $line, $mib_matches);
preg_match('/-m \+?([a-zA-Z0-9:\-]+)/', $line, $mib_matches);
$mib = $mib_matches[1];
$method = $snmp_matches[1][$index];
$oids = explode(' ', trim($snmp_matches[2][$index]));
@@ -502,10 +508,10 @@ class ModuleTestHelper
$data = array(); // array to hold dumped data
// Run discovery
$save_debug = $debug;
$save_vedbug = $vdebug;
if ($this->quiet) {
ob_start();
$save_debug = $debug;
$save_vedbug = $vdebug;
$debug = true;
$vdebug = false;
}
@@ -521,8 +527,12 @@ class ModuleTestHelper
$this->qPrint(PHP_EOL);
// Parse discovered modules
$this->discovery_module_output = $this->extractModuleOutput($this->discovery_output, 'disco');
$discovered_modules = array_keys($this->discovery_module_output);
// Dump the discovered data
$data = array_merge_recursive($data, $this->dumpDb($device['device_id'], 'discovery'));
$data = array_merge_recursive($data, $this->dumpDb($device['device_id'], $discovered_modules, 'discovery'));
$device = device_by_id_cache($device_id, true); // refresh the device array
// Run the poller
@@ -541,8 +551,12 @@ class ModuleTestHelper
$vdebug = $save_vedbug;
}
// Parse polled modules
$this->poller_module_output = $this->extractModuleOutput($this->poller_output, 'poller');
$polled_modules = array_keys($this->poller_module_output);
// Dump polled data
$data = array_merge_recursive($data, $this->dumpDb($device['device_id'], 'poller'));
$data = array_merge_recursive($data, $this->dumpDb($device['device_id'], $polled_modules, 'poller'));
// Remove the test device, we don't need the debug from this
if ($device['hostname'] == $snmpsim->getIp()) {
@@ -577,28 +591,57 @@ class ModuleTestHelper
return $data;
}
/**
* @param string $output poller or discovery output
* @param string $type poller|disco identified by "#### Load disco module" string
* @return array
*/
private function extractModuleOutput($output, $type)
{
$module_output = [];
$module_start = "#### Load $type module ";
$module_end = "#### Unload $type module %s ####";
$parts = explode($module_start, $output);
array_shift($parts); // throw away first part of output
foreach ($parts as $part) {
// find the module name
$module = strtok($part, ' ');
// insert the name into the end string
$end = sprintf($module_end, $module);
// find the end
$end_pos = strrpos($part, $end) ?: -1;
// save output, re-add bits we used for parsing
$module_output[$module] = $module_start . substr($part, 0, $end_pos) . $end;
}
return $module_output;
}
/**
* Dump the current database data for the module to an array
* Mostly used for testing
*
* @param int $device_id The test device id
* @param array modules to capture data for (should be a list of modules that were actually run)
* @param string $key a key to store the data under the module key (usually discovery or poller)
* @return array The dumped data keyed by module -> table
*/
public function dumpDb($device_id, $key = null)
public function dumpDb($device_id, $modules, $key = null)
{
$data = array();
$module_dump_info = $this->getTableData();
// don't dump some modules by default unless they are manually listed
if (empty($this->modules)) {
foreach ($this->exclude_from_all as $module) {
unset($module_dump_info[$module]);
}
$modules = array_diff($modules, $this->exclude_from_all);
}
foreach ($module_dump_info as $module => $module_tables) {
foreach ($module_tables as $table => $info) {
// only dump data for the given modules
foreach ($modules as $module) {
foreach ($module_dump_info[$module] as $table => $info) {
// check for custom where
$where = isset($info['custom_where']) ? $info['custom_where'] : "WHERE `device_id`=?";
$params = array($device_id);
@@ -655,19 +698,50 @@ class ModuleTestHelper
*/
public function getTableData()
{
return array_intersect_key($this->module_tables, array_flip($this->getModules()));
return array_intersect_key(self::$module_tables, array_flip($this->getModules()));
}
public function getLastDiscoveryOutput()
/**
* Get the output from the last discovery that was run
* If module was specified, only return that module's output
*
* @param null $module
* @return mixed
*/
public function getDiscoveryOutput($module = null)
{
if ($module) {
if (isset($this->discovery_module_output[$module])) {
return $this->discovery_module_output[$module];
} else {
return "Module $module not run. Modules: " . implode(',', array_keys($this->poller_module_output));
}
}
return $this->discovery_output;
}
public function getLastPollerOutput()
/**
* Get output from the last poller that was run
* If module was specified, only return that module's output
*
* @param null $module
* @return mixed
*/
public function getPollerOutput($module = null)
{
if ($module) {
if (isset($this->poller_module_output[$module])) {
return $this->poller_module_output[$module];
} else {
return "Module $module not run. Modules: " . implode(',', array_keys($this->poller_module_output));
}
}
return $this->poller_output;
}
/**
* Get a list of all modules that support capturing data
*
@@ -675,7 +749,7 @@ class ModuleTestHelper
*/
public function getSupportedModules()
{
return array_keys($this->module_tables);
return array_keys(self::$module_tables);
}
/**
+1 -1
View File
@@ -15,7 +15,7 @@
function graphite_update($device, $measurement, $tags, $fields)
{
global $graphite, $config;
if ($graphite !== false) {
if ($graphite != false) {
$timestamp = time();
$graphite_prefix = $config['graphite']['prefix'];
// metrics will be built as prefix.hostname.measurement.field value timestamp
+15 -7
View File
@@ -25,7 +25,6 @@
namespace LibreNMS\Tests;
use LibreNMS\Config;
use LibreNMS\Exceptions\FileNotFoundException;
use LibreNMS\Exceptions\InvalidModuleException;
use LibreNMS\Util\ModuleTestHelper;
@@ -37,8 +36,8 @@ class OSModulesTest extends DBTestCase
*
* @group os
* @dataProvider dumpedDataProvider
* @param string $target_os name of the (and variant) to test
* @param string $filename file name of the json data
* @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)
@@ -49,20 +48,25 @@ class OSModulesTest extends DBTestCase
try {
$helper = new ModuleTestHelper($modules, $os, $variant);
$helper->setQuiet();
$filename = $helper->getJsonFilepath(true);
$filename = $helper->getJsonFilepath(true);
$expected_data = $helper->getTestData();
$results = $helper->generateTestData($snmpsim, true);
} catch (FileNotFoundException $e) {
$this->fail($e->getMessage());
return;
} catch (InvalidModuleException $e) {
$this->fail($e->getMessage());
return;
}
if (is_null($results)) {
$this->fail("$os: Failed to collect data.");
}
// output all discovery and poller output if debug mode is enabled for phpunit
$debug = in_array('--debug', $_SERVER['argv'], true);
foreach ($modules as $module) {
$expected = $expected_data[$module]['discovery'];
$actual = $results[$module]['discovery'];
@@ -71,18 +75,22 @@ class OSModulesTest extends DBTestCase
$actual,
"OS $os: Discovered $module data does not match that found in $filename\n"
. print_r(array_diff($expected, $actual), true)
. $helper->getLastDiscoveryOutput()
. $helper->getDiscoveryOutput($debug ? null : $module)
. "\nOS $os: Polled $module data does not match that found in $filename"
);
$expected = $expected_data[$module]['poller'] == 'matches discovery' ? $expected_data[$module]['discovery'] : $expected_data[$module]['poller'];
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->getLastPollerOutput()
. $helper->getPollerOutput($debug ? null : $module)
. "\nOS $os: Polled $module data does not match that found in $filename"
);
}
+2 -22
View File
@@ -1,11 +1,4 @@
{
"applications": {
"discovery": {
"applications": [],
"application_metrics": []
},
"poller": "matches discovery"
},
"arp-table": {
"discovery": {
"ipv4_mac": [
@@ -41,7 +34,7 @@
}
]
},
"poller": "matches discovery"
"poller": null
},
"bgp-peers": {
"discovery": {
@@ -5919,13 +5912,6 @@
},
"poller": "matches discovery"
},
"sensors": {
"discovery": {
"sensors": [],
"state_indexes": []
},
"poller": "matches discovery"
},
"storage": {
"discovery": {
"storage": [
@@ -5961,11 +5947,5 @@
}
]
}
},
"wireless": {
"discovery": {
"wireless_sensors": []
},
"poller": "matches discovery"
}
}
}
+2 -40
View File
@@ -1,11 +1,4 @@
{
"applications": {
"discovery": {
"applications": [],
"application_metrics": []
},
"poller": "matches discovery"
},
"arp-table": {
"discovery": {
"ipv4_mac": [
@@ -16,19 +9,7 @@
}
]
},
"poller": "matches discovery"
},
"bgp-peers": {
"discovery": {
"devices": [
{
"bgpLocalAs": null
}
],
"bgpPeers": [],
"bgpPeers_cbgp": []
},
"poller": "matches discovery"
"poller": null
},
"mempools": {
"discovery": {
@@ -10672,24 +10653,5 @@
"processors": []
},
"poller": "matches discovery"
},
"sensors": {
"discovery": {
"sensors": [],
"state_indexes": []
},
"poller": "matches discovery"
},
"storage": {
"discovery": {
"storage": []
},
"poller": "matches discovery"
},
"wireless": {
"discovery": {
"wireless_sensors": []
},
"poller": "matches discovery"
}
}
}
-32
View File
@@ -1,17 +1,4 @@
{
"applications": {
"discovery": {
"applications": [],
"application_metrics": []
},
"poller": "matches discovery"
},
"arp-table": {
"discovery": {
"ipv4_mac": []
},
"poller": "matches discovery"
},
"mempools": {
"discovery": {
"mempools": []
@@ -5159,24 +5146,5 @@
"processors": []
},
"poller": "matches discovery"
},
"sensors": {
"discovery": {
"sensors": [],
"state_indexes": []
},
"poller": "matches discovery"
},
"storage": {
"discovery": {
"storage": []
},
"poller": "matches discovery"
},
"wireless": {
"discovery": {
"wireless_sensors": []
},
"poller": "matches discovery"
}
}
+2 -40
View File
@@ -1,29 +1,4 @@
{
"applications": {
"discovery": {
"applications": [],
"application_metrics": []
},
"poller": "matches discovery"
},
"arp-table": {
"discovery": {
"ipv4_mac": []
},
"poller": "matches discovery"
},
"bgp-peers": {
"discovery": {
"devices": [
{
"bgpLocalAs": null
}
],
"bgpPeers": [],
"bgpPeers_cbgp": []
},
"poller": "matches discovery"
},
"mempools": {
"discovery": {
"mempools": [
@@ -2939,14 +2914,7 @@
"processors": []
},
"poller": "matches discovery"
},
"sensors": {
"discovery": {
"sensors": [],
"state_indexes": []
},
"poller": "matches discovery"
},
}
"storage": {
"discovery": {
"storage": [
@@ -3060,11 +3028,5 @@
}
]
}
},
"wireless": {
"discovery": {
"wireless_sensors": []
},
"poller": "matches discovery"
}
}
}
+1 -14
View File
@@ -1,11 +1,4 @@
{
"applications": {
"discovery": {
"applications": [],
"application_metrics": []
},
"poller": "matches discovery"
},
"arp-table": {
"discovery": {
"ipv4_mac": [
@@ -36,7 +29,7 @@
}
]
},
"poller": "matches discovery"
"poller": null
},
"bgp-peers": {
"discovery": {
@@ -12582,11 +12575,5 @@
}
]
}
},
"wireless": {
"discovery": {
"wireless_sensors": []
},
"poller": "matches discovery"
}
}
-56
View File
@@ -1,35 +1,4 @@
{
"applications": {
"discovery": {
"applications": [],
"application_metrics": []
},
"poller": "matches discovery"
},
"bgp-peers": {
"discovery": {
"devices": [
{
"bgpLocalAs": null
}
],
"bgpPeers": [],
"bgpPeers_cbgp": []
},
"poller": "matches discovery"
},
"mempools": {
"discovery": {
"mempools": []
},
"poller": "matches discovery"
},
"ports": {
"discovery": {
"ports": []
},
"poller": "matches discovery"
},
"os": {
"discovery": {
"devices": [
@@ -50,30 +19,5 @@
]
},
"poller": "matches discovery"
},
"processors": {
"discovery": {
"processors": []
},
"poller": "matches discovery"
},
"sensors": {
"discovery": {
"sensors": [],
"state_indexes": []
},
"poller": "matches discovery"
},
"storage": {
"discovery": {
"storage": []
},
"poller": "matches discovery"
},
"wireless": {
"discovery": {
"wireless_sensors": []
},
"poller": "matches discovery"
}
}
+2 -39
View File
@@ -1,11 +1,4 @@
{
"applications": {
"discovery": {
"applications": [],
"application_metrics": []
},
"poller": "matches discovery"
},
"arp-table": {
"discovery": {
"ipv4_mac": [
@@ -16,25 +9,7 @@
}
]
},
"poller": "matches discovery"
},
"bgp-peers": {
"discovery": {
"devices": [
{
"bgpLocalAs": null
}
],
"bgpPeers": [],
"bgpPeers_cbgp": []
},
"poller": "matches discovery"
},
"mempools": {
"discovery": {
"mempools": []
},
"poller": "matches discovery"
"poller": null
},
"ports": {
"discovery": {
@@ -749,17 +724,5 @@
}
]
}
},
"storage": {
"discovery": {
"storage": []
},
"poller": "matches discovery"
},
"wireless": {
"discovery": {
"wireless_sensors": []
},
"poller": "matches discovery"
}
}
}
+1 -5
View File
@@ -1,5 +1,4 @@
{
<<<<<<< HEAD
"bgp-peers": {
"discovery": {
"devices": [
@@ -117,9 +116,7 @@
],
"bgpPeers_cbgp": []
}
}
}
=======
},
"processors": {
"discovery": {
"processors": [
@@ -150,4 +147,3 @@
"poller": "matches discovery"
}
}
>>>>>>> Extract DiscoveryItem and move some things to better places.
+1 -45
View File
@@ -1,11 +1,4 @@
{
"applications": {
"discovery": {
"applications": [],
"application_metrics": []
},
"poller": "matches discovery"
},
"arp-table": {
"discovery": {
"ipv4_mac": [
@@ -16,25 +9,7 @@
}
]
},
"poller": "matches discovery"
},
"bgp-peers": {
"discovery": {
"devices": [
{
"bgpLocalAs": null
}
],
"bgpPeers": [],
"bgpPeers_cbgp": []
},
"poller": "matches discovery"
},
"mempools": {
"discovery": {
"mempools": []
},
"poller": "matches discovery"
"poller": null
},
"ports": {
"discovery": {
@@ -14729,24 +14704,5 @@
]
},
"poller": "matches discovery"
},
"sensors": {
"discovery": {
"sensors": [],
"state_indexes": []
},
"poller": "matches discovery"
},
"storage": {
"discovery": {
"storage": []
},
"poller": "matches discovery"
},
"wireless": {
"discovery": {
"wireless_sensors": []
},
"poller": "matches discovery"
}
}
+2 -21
View File
@@ -1,11 +1,4 @@
{
"applications": {
"discovery": {
"applications": [],
"application_metrics": []
},
"poller": "matches discovery"
},
"arp-table": {
"discovery": {
"ipv4_mac": [
@@ -86,19 +79,7 @@
}
]
},
"poller": "matches discovery"
},
"bgp-peers": {
"discovery": {
"devices": [
{
"bgpLocalAs": null
}
],
"bgpPeers": [],
"bgpPeers_cbgp": []
},
"poller": "matches discovery"
"poller": null
},
"mempools": {
"discovery": {
@@ -4294,4 +4275,4 @@
]
}
}
}
}
+1 -1
View File
@@ -4333,6 +4333,6 @@
}
]
},
"poller": "matches discovery"
"poller": null
}
}
-37
View File
@@ -37,31 +37,6 @@
]
}
},
"applications": {
"discovery": {
"applications": [],
"application_metrics": []
},
"poller": "matches discovery"
},
"arp-table": {
"discovery": {
"ipv4_mac": []
},
"poller": "matches discovery"
},
"bgp-peers": {
"discovery": {
"devices": [
{
"bgpLocalAs": null
}
],
"bgpPeers": [],
"bgpPeers_cbgp": []
},
"poller": "matches discovery"
},
"mempools": {
"discovery": {
"mempools": [
@@ -4159,17 +4134,5 @@
]
},
"poller": "matches discovery"
},
"storage": {
"discovery": {
"storage": []
},
"poller": "matches discovery"
},
"wireless": {
"discovery": {
"wireless_sensors": []
},
"poller": "matches discovery"
}
}
+1
View File
@@ -25,6 +25,7 @@ ports:
excluded_fields: [device_id, port_id, poll_time, poll_period]
joins:
- { left: ports.port_id, right: ports_statistics.port_id }
order_by: ports.ifIndex, ports.ifDescr, ports.ifName
os:
devices:
included_fields: [sysName, sysObjectID, sysDescr, sysContact, version, hardware, features, location, os, type, serial, icon]