Add fdb-table module to tests (#8723)

* Add fdb-table module to tests
Also add the capability to select fields on joins, makes it easier to have consistent data without extra storage.

* add ticks for safety

* support multiple fields, but not aliasing

* more ticks

* Fix up selects

* Add Vrf Support too

* Fix application select
This commit is contained in:
Tony Murray
2018-05-17 09:42:00 -05:00
committed by GitHub
parent edab606428
commit 909aacd27a
2 changed files with 57 additions and 33 deletions

View File

@@ -50,10 +50,12 @@ class ModuleTestHelper
// Definitions // Definitions
// ignore these when dumping all modules // ignore these when dumping all modules
private $exclude_from_all = ['arp-table']; private $exclude_from_all = ['arp-table', 'fdb-table'];
private $module_deps = [ private $module_deps = [
'arp-table' => ['ports', 'arp-table'], 'arp-table' => ['ports', 'arp-table'],
'fdb-table' => ['ports', 'vlans', 'fdb-table'],
'vlans' => ['ports', 'vlans'], 'vlans' => ['ports', 'vlans'],
'vrf' => ['ports', 'vrf'],
]; ];
@@ -138,7 +140,7 @@ class ModuleTestHelper
$device = device_by_id_cache($device_id, true); $device = device_by_id_cache($device_id, true);
$snmprec_data = array(); $snmprec_data = [];
foreach ($snmp_oids as $oid_data) { foreach ($snmp_oids as $oid_data) {
$this->qPrint(" " . $oid_data['oid']); $this->qPrint(" " . $oid_data['oid']);
@@ -191,21 +193,21 @@ class ModuleTestHelper
preg_match_all($snmp_query_regex, $collection_output, $snmp_matches); preg_match_all($snmp_query_regex, $collection_output, $snmp_matches);
// extract mibs and group with oids // extract mibs and group with oids
$snmp_oids = array( $snmp_oids = [
'sysDescr.0_get' => array('oid' => 'sysDescr.0', 'mib' => 'SNMPv2-MIB', 'method' => 'get'), 'sysDescr.0_get' => ['oid' => 'sysDescr.0', 'mib' => 'SNMPv2-MIB', 'method' => 'get'],
'sysObjectID.0_get' => array('oid' => 'sysObjectID.0', 'mib' => 'SNMPv2-MIB', 'method' => 'get'), 'sysObjectID.0_get' => ['oid' => 'sysObjectID.0', 'mib' => 'SNMPv2-MIB', 'method' => 'get'],
); ];
foreach ($snmp_matches[0] as $index => $line) { 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]; $mib = $mib_matches[1];
$method = $snmp_matches[1][$index]; $method = $snmp_matches[1][$index];
$oids = explode(' ', trim($snmp_matches[2][$index])); $oids = explode(' ', trim($snmp_matches[2][$index]));
foreach ($oids as $oid) { foreach ($oids as $oid) {
$snmp_oids["{$oid}_$method"] = array( $snmp_oids["{$oid}_$method"] = [
'oid' => $oid, 'oid' => $oid,
'mib' => $mib, 'mib' => $mib,
'method' => $method, 'method' => $method,
); ];
} }
} }
@@ -225,9 +227,9 @@ class ModuleTestHelper
* @param array $modules * @param array $modules
* @return array * @return array
*/ */
public static function findOsWithData($modules = array()) public static function findOsWithData($modules = [])
{ {
$os_list = array(); $os_list = [];
foreach (glob(Config::get('install_dir') . "/tests/data/*.json") as $file) { foreach (glob(Config::get('install_dir') . "/tests/data/*.json") as $file) {
$base_name = basename($file, '.json'); $base_name = basename($file, '.json');
@@ -246,11 +248,11 @@ class ModuleTestHelper
continue; // no test data for selected modules continue; // no test data for selected modules
} }
$os_list[$base_name] = array( $os_list[$base_name] = [
$os, $os,
$variant, $variant,
$valid_modules, $valid_modules,
); ];
} }
return $os_list; return $os_list;
@@ -287,7 +289,7 @@ class ModuleTestHelper
private function resolveModuleDependencies($modules) private function resolveModuleDependencies($modules)
{ {
// generate a full list of modules // generate a full list of modules
$full_list = array(); $full_list = [];
foreach ($modules as $module) { foreach ($modules as $module) {
// only allow valid modules // only allow valid modules
if (!(Config::has("poller_modules.$module") || Config::has("discovery_modules.$module"))) { if (!(Config::has("poller_modules.$module") || Config::has("discovery_modules.$module"))) {
@@ -307,10 +309,10 @@ class ModuleTestHelper
private function getArgs() private function getArgs()
{ {
if (empty($this->modules)) { if (empty($this->modules)) {
return array(); return [];
} }
return array('m' => implode(',', $this->modules)); return ['m' => implode(',', $this->modules)];
} }
private function qPrint($var) private function qPrint($var)
@@ -328,7 +330,7 @@ class ModuleTestHelper
private function convertSnmpToSnmprec($snmp_data) private function convertSnmpToSnmprec($snmp_data)
{ {
$result = array(); $result = [];
foreach (explode(PHP_EOL, $snmp_data) as $line) { foreach (explode(PHP_EOL, $snmp_data) as $line) {
if (empty($line)) { if (empty($line)) {
continue; continue;
@@ -382,7 +384,7 @@ class ModuleTestHelper
private function getSnmprecType($text) private function getSnmprecType($text)
{ {
$snmpTypes = array( $snmpTypes = [
'STRING' => '4', 'STRING' => '4',
'OID' => '6', 'OID' => '6',
'Hex-STRING' => '4x', 'Hex-STRING' => '4x',
@@ -399,7 +401,7 @@ class ModuleTestHelper
'Opaque' => '68', 'Opaque' => '68',
'Counter64' => '70', 'Counter64' => '70',
'Network Address' => '4' 'Network Address' => '4'
); ];
return $snmpTypes[$text]; return $snmpTypes[$text];
} }
@@ -409,10 +411,10 @@ class ModuleTestHelper
if (is_file($this->snmprec_file)) { if (is_file($this->snmprec_file)) {
$existing_data = $this->indexSnmprec(explode(PHP_EOL, file_get_contents($this->snmprec_file))); $existing_data = $this->indexSnmprec(explode(PHP_EOL, file_get_contents($this->snmprec_file)));
} else { } else {
$existing_data = array(); $existing_data = [];
} }
$new_data = array(); $new_data = [];
foreach ($data as $part) { foreach ($data as $part) {
$new_data = array_merge($new_data, $this->indexSnmprec($part)); $new_data = array_merge($new_data, $this->indexSnmprec($part));
} }
@@ -427,7 +429,7 @@ class ModuleTestHelper
} }
// put data in the proper order for snmpsim // put data in the proper order for snmpsim
uksort($results, array($this, 'compareOid')); uksort($results, [$this, 'compareOid']);
$output = implode(PHP_EOL, $results) . PHP_EOL; $output = implode(PHP_EOL, $results) . PHP_EOL;
@@ -442,7 +444,7 @@ class ModuleTestHelper
private function indexSnmprec(array $snmprec_data) private function indexSnmprec(array $snmprec_data)
{ {
$result = array(); $result = [];
foreach ($snmprec_data as $line) { foreach ($snmprec_data as $line) {
if (!empty($line)) { if (!empty($line)) {
@@ -456,11 +458,11 @@ class ModuleTestHelper
private function cleanSnmprecData(&$data) private function cleanSnmprecData(&$data)
{ {
$private_oid = array( $private_oid = [
'1.3.6.1.2.1.1.6.0', '1.3.6.1.2.1.1.6.0',
'1.3.6.1.2.1.1.4.0', '1.3.6.1.2.1.1.4.0',
'1.3.6.1.2.1.1.5.0', '1.3.6.1.2.1.1.5.0',
); ];
foreach ($private_oid as $oid) { foreach ($private_oid as $oid) {
if (isset($data[$oid])) { if (isset($data[$oid])) {
@@ -495,7 +497,7 @@ class ModuleTestHelper
// Add the test device // Add the test device
try { try {
Config::set('snmp.community', array($this->file_name)); Config::set('snmp.community', [$this->file_name]);
$device_id = addHost($snmpsim->getIp(), 'v2c', $snmpsim->getPort()); $device_id = addHost($snmpsim->getIp(), 'v2c', $snmpsim->getPort());
$this->qPrint("Added device: $device_id\n"); $this->qPrint("Added device: $device_id\n");
} catch (\Exception $e) { } catch (\Exception $e) {
@@ -506,7 +508,7 @@ class ModuleTestHelper
// Populate the device variable // Populate the device variable
$device = device_by_id_cache($device_id, true); $device = device_by_id_cache($device_id, true);
$data = array(); // array to hold dumped data $data = []; // array to hold dumped data
// Run discovery // Run discovery
$save_debug = $debug; $save_debug = $debug;
@@ -584,10 +586,10 @@ class ModuleTestHelper
continue; continue;
} }
if ($module_data['discovery'] == $module_data['poller']) { if ($module_data['discovery'] == $module_data['poller']) {
$existing_data[$module] = array( $existing_data[$module] = [
'discovery' => $module_data['discovery'], 'discovery' => $module_data['discovery'],
'poller' => 'matches discovery', 'poller' => 'matches discovery',
); ];
} else { } else {
$existing_data[$module] = $module_data; $existing_data[$module] = $module_data;
} }
@@ -640,7 +642,7 @@ class ModuleTestHelper
*/ */
public function dumpDb($device_id, $modules, $key = null) public function dumpDb($device_id, $modules, $key = null)
{ {
$data = array(); $data = [];
$module_dump_info = $this->getTableData(); $module_dump_info = $this->getTableData();
// don't dump some modules by default unless they are manually listed // don't dump some modules by default unless they are manually listed
@@ -652,19 +654,27 @@ class ModuleTestHelper
foreach ($modules as $module) { foreach ($modules as $module) {
foreach ($module_dump_info[$module] as $table => $info) { foreach ($module_dump_info[$module] as $table => $info) {
// check for custom where // check for custom where
$where = isset($info['custom_where']) ? $info['custom_where'] : "WHERE `device_id`=?"; $where = isset($info['custom_where']) ? $info['custom_where'] : "WHERE `$table`.`device_id`=?";
$params = array($device_id); $params = [$device_id];
// build joins // build joins
$join = ''; $join = '';
$select = ["`$table`.*"];
foreach ($info['joins'] as $join_info) { foreach ($info['joins'] as $join_info) {
if (isset($join_info['custom'])) { if (isset($join_info['custom'])) {
$join .= ' ' . $join_info['custom']; $join .= ' ' . $join_info['custom'];
$default_select = [];
} else { } else {
list($left, $lkey) = explode('.', $join_info['left']); list($left, $lkey) = explode('.', $join_info['left']);
list($right, $rkey) = explode('.', $join_info['right']); list($right, $rkey) = explode('.', $join_info['right']);
$join .= " LEFT JOIN `$right` ON (`$left`.`$lkey` = `$right`.`$rkey`)"; $join .= " LEFT JOIN `$right` ON (`$left`.`$lkey` = `$right`.`$rkey`)";
$default_select = ["`$right`.*"];
} }
// build selects
$select = array_merge($select, isset($join_info['select']) ? $join_info['select'] : $default_select);
} }
if (isset($info['order_by'])) { if (isset($info['order_by'])) {
@@ -673,7 +683,8 @@ class ModuleTestHelper
$order_by = ''; $order_by = '';
} }
$rows = dbFetchRows("SELECT * FROM `$table` $join $where $order_by", $params); $fields = implode(', ', $select);
$rows = dbFetchRows("SELECT $fields FROM `$table` $join $where $order_by", $params);
// remove unwanted fields // remove unwanted fields
if (isset($info['included_fields'])) { if (isset($info['included_fields'])) {

View File

@@ -4,7 +4,7 @@ applications:
application_metrics: application_metrics:
excluded_fields: [app_id] excluded_fields: [app_id]
joins: joins:
- { custom: 'INNER JOIN (SELECT app_id, app_type FROM applications WHERE `device_id`=?) I USING (app_id)' } - { custom: 'INNER JOIN (SELECT app_id, app_type FROM applications WHERE `device_id`=?) I USING (app_id)', select: ['app_type'] }
custom_where: '' custom_where: ''
arp-table: arp-table:
ipv4_mac: ipv4_mac:
@@ -17,6 +17,13 @@ bgp-peers:
excluded_fields: [device_id, bgpPeer_id] excluded_fields: [device_id, bgpPeer_id]
bgpPeers_cbgp: bgpPeers_cbgp:
excluded_fields: [device_id] excluded_fields: [device_id]
fdb-table:
ports_fdb:
excluded_fields: [device_id, ports_fdb_id, port_id, vlan_id]
joins:
- { left: ports_fdb.port_id, right: ports.port_id, select: ifIndex }
- { left: ports_fdb.vlan_id, right: vlans.vlan_id, select: vlan_vlan }
order_by: ports.ifIndex, vlans.vlan_vlan, ports_fdb.mac_address
mempools: mempools:
mempools: mempools:
excluded_fields: [device_id, mempool_id] excluded_fields: [device_id, mempool_id]
@@ -60,6 +67,12 @@ vlans:
ports_vlans: ports_vlans:
excluded_fields: [port_vlan_id, device_id, port_id] excluded_fields: [port_vlan_id, device_id, port_id]
order_by: vlan, baseport order_by: vlan, baseport
vrf:
vrfs:
excluded_fields: [vrf_id, device_id]
ports:
included_fields: [ifIndex, ifVrf]
custom_where: device_id=? AND ifVrf!=0
wireless: wireless:
wireless_sensors: wireless_sensors:
excluded_fields: [device_id, sensor_id, access_point_id, lastupdate] excluded_fields: [device_id, sensor_id, access_point_id, lastupdate]