librenms/tests/FunctionsTest.php
Martijn Schmidt a64bd45dbc
Update IronWare sensors & bgp-peers discovery, allow skip_values to target a specific index appended to the OID (#10941)
* Migrate ironware sensor discovery from php to yaml.

* More sensors, add grouping.

* dynamic_discovery_get_value() becomes getValueFromData()

* Target a specific index with skip_values.

* Improve Brocade BGP session discovery/polling

This commit allows for the correct discovery of BGP sessions
with 32-bit ASNs, IPv6 neighbors using the BGP4V2-MIB which is
based on draft-ietf-idr-bgp4-mibv2-11 and also polls for IPv4
unicast received routes through the FOUNDRY-SN-BGP4-GROUP-MIB.

Copied most of the code from PR#8877 by @Mikeburke14, cleaned
up the code a little bit to match the normal LibreNMS style,
and fixed bgpPeers_cbgp discovery as well as polling for the
ipv4.unicast neighbors.

Note that older Brocade IronWare firmware versions are known
to have multiple defects relating to the BGP4V2-MIB which
might result in certain missing non-established neighbors.

Related vendor defect numbers:

- DEFECT000633962
-- Symptom: The OID bgp4V2PeerAdminStatus does not return the
            correct value
-- Reported: NI 05.7.00
-- Resolved: NI 05.8.00g

- DEFECT000583319
-- Symptom: SNMP polling on bgp4V2PeerTable (OID brcdIp.3.5.1.1.2)
            does not display all the BGP entries
-- Reported: NI 05.6.00
-- Resolved: NI 05.8.00e

- DEFECT000550309
-- Symptom: SNMP polling on bgp4V2PeerTable (OID brcdIp.3.5.1.1.2)
            does not display the full information
-- Reported: NI 05.7.00
-- Resolved: NI 05.8.00c

* Add ironware CER & ICX platform test data.

* Re-add ironware.json compatible with current master branch.
2020-02-01 23:28:03 +01:00

138 lines
5.6 KiB
PHP

<?php
/**
* FunctionsTest.php
*
* tests functions in includes/functions.php
*
* 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 <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2017 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Tests;
use LibreNMS\Device\YamlDiscovery;
class FunctionsTest extends TestCase
{
public function testMacCleanToReadable()
{
$this->assertEquals('de:ad:be:ef:a0:c3', mac_clean_to_readable('deadbeefa0c3'));
}
public function testHex2Str()
{
$this->assertEquals('Big 10 UP', hex2str('426967203130205550'));
}
public function testSnmpHexstring()
{
$input = '4c 61 72 70 69 6e 67 20 34 20 55 00 0a';
$this->assertEquals("Larping 4 U\n", snmp_hexstring($input));
}
public function testIsHexString()
{
$this->assertTrue(isHexString('af 28 02'));
$this->assertTrue(isHexString('aF 28 02 CE'));
$this->assertFalse(isHexString('a5 fj 53'));
$this->assertFalse(isHexString('a5fe53'));
}
public function testLinkify()
{
$input = 'foo@demo.net bar.ba@test.co.uk
www.demo.com http://foo.co.uk/
sdfsd ftp://192.168.1.1/help/me/now.php
http://regexr.com/foo.html?q=bar
https://mediatemple.net.';
$expected = 'foo@demo.net bar.ba@test.co.uk
www.demo.com <a href="http://foo.co.uk/">http://foo.co.uk/</a>
sdfsd <a href="ftp://192.168.1.1/help/me/now.php">ftp://192.168.1.1/help/me/now.php</a>
<a href="http://regexr.com/foo.html?q=bar">http://regexr.com/foo.html?q=bar</a>
<a href="https://mediatemple.net">https://mediatemple.net</a>.';
$this->assertSame($expected, linkify($input));
}
public function testDynamicDiscoveryGetValue()
{
$pre_cache = array(
'firstdata' => array(
0 => array('temp' => 1),
1 => array('temp' => 2),
),
'high' => array(
0 => array('high' => 3),
1 => array('high' => 4),
),
'table' => array(
0 => array('first' => 5, 'second' => 6),
1 => array('first' => 7, 'second' => 8),
),
'single' => array('something' => 9),
'oneoff' => 10,
'singletable' => array(
11 => array('singletable' => 'Pickle')
),
'doubletable' => array(
12 => array('doubletable' => 'Mustard'),
13 => array('doubletable' => 'BBQ')
),
);
$data = array('value' => 'temp', 'oid' => 'firstdata');
$this->assertNull(YamlDiscovery::getValueFromData('missing', 0, $data, $pre_cache));
$this->assertSame('yar', YamlDiscovery::getValueFromData('default', 0, $data, $pre_cache, 'yar'));
$this->assertSame(2, YamlDiscovery::getValueFromData('value', 1, $data, $pre_cache));
$data = array('oid' => 'high');
$this->assertSame(3, YamlDiscovery::getValueFromData('high', 0, $data, $pre_cache));
$data = array('oid' => 'table');
$this->assertSame(5, YamlDiscovery::getValueFromData('first', 0, $data, $pre_cache));
$this->assertSame(7, YamlDiscovery::getValueFromData('first', 1, $data, $pre_cache));
$this->assertSame(6, YamlDiscovery::getValueFromData('second', 0, $data, $pre_cache));
$this->assertSame(8, YamlDiscovery::getValueFromData('second', 1, $data, $pre_cache));
$this->assertSame(9, YamlDiscovery::getValueFromData('single', 0, $data, $pre_cache));
$this->assertSame(10, YamlDiscovery::getValueFromData('oneoff', 3, $data, $pre_cache));
$this->assertSame('Pickle', YamlDiscovery::getValueFromData('singletable', 11, $data, $pre_cache));
$this->assertSame('BBQ', YamlDiscovery::getValueFromData('doubletable', 13, $data, $pre_cache));
}
public function testParseAtTime()
{
$this->assertEquals(time(), parse_at_time('now'), 'now did not match');
$this->assertEquals(time()+180, parse_at_time('+3m'), '+3m did not match');
$this->assertEquals(time()+7200, parse_at_time('+2h'), '+2h did not match');
$this->assertEquals(time()+172800, parse_at_time('+2d'), '+2d did not match');
$this->assertEquals(time()+63115200, parse_at_time('+2y'), '+2y did not match');
$this->assertEquals(time()-180, parse_at_time('-3m'), '-3m did not match');
$this->assertEquals(time()-7200, parse_at_time('-2h'), '-2h did not match');
$this->assertEquals(time()-172800, parse_at_time('-2d'), '-2d did not match');
$this->assertEquals(time()-63115200, parse_at_time('-2y'), '-2y did not match');
$this->assertEquals(429929439, parse_at_time('429929439'));
$this->assertEquals(212334234, parse_at_time(212334234));
$this->assertEquals(time()-43, parse_at_time('-43'), '-43 did not match');
$this->assertEquals(0, parse_at_time('invalid'));
$this->assertEquals(606614400, parse_at_time('March 23 1989 UTC'));
$this->assertEquals(time()+86400, parse_at_time('+1 day'));
}
}