* Testing updates module testing and collect only * cache clear no-longer needed.
5.0 KiB
source: Developing/os/Test-Units.md
Tests
Tests ensure LibreNMS works as expected, now and in the future. New OS should provide as much test data as needed and added test data for existing OS is welcome.
Saved snmp data can be found in tests/snmpsim/*.snmprec
and saved database data can be found in tests/data/*.json
.
Please review this for any sensitive data before submitting. When replacing data, make sure it is modified in a
consistent manner.
We utilise snmpsim to do unit testing. For OS discovery, we can mock snmpsim, but for other tests you will need it installed and functioning. We run snmpsim during our integration tests, but not by default when running
./scripts/pre-commit.php
.
Capturing test data
./scripts/save-tests-data.php
is provided to make it easy to collect data for tests. Running save-tests-data.php with
the --hostname (-h) allows you to capture all data used to discover and poll a device already added to LibreNMS. Make sure to
re-run the script if you add additional support. Check the command-line help for more options.
Generally, you will only need to capture data (-h) once. After you have the data you need in the snmprec file, you can update the json using -o (and -v if using a variant) after that.
OS Variants
If test data already exists, but is for a different device/configuration then you should use the --variant (-v) option to specify a different variant of the os, this will be tested completely separate from other variants. If there is only one variant, please do not specify one.
Running tests
After you have saved your test data, you should run ./scripts/pre-commit.php -p -u
verify they pass.
To run the full suite of tests enable database and snmpsim reliant tests: ./scripts/pre-commit.php --db --snmpsim -p -u
Specific OS
./scripts/pre-commit.php -p -o osname
Specific Module
./scripts/pre-commit.php -p -m modulename
Using snmpsim for testing
You can run snmpsim to access test data by running ./scripts/save-tests-data.php --snmpsim
You may then run snmp queries against it using the os (and variant) as the community and 127.1.6.1:1161 as the host.
snmpget -v 2c -c ios_c3560e 127.1.6.1:1161 sysDescr.0
Snmprec format
Snmprec files are simple files that store the snmp data. The data format is simple with three columns: numeric oid, type code, and data. Here is an example snippet.
1.3.6.1.2.1.1.1.0|4|Pulse Secure,LLC,MAG-2600,8.0R14 (build 41869)
1.3.6.1.2.1.1.2.0|6|1.3.6.1.4.1.12532.254.1.1
During testing LibreNMS will use any info in the snmprec file for snmp calls. This one provides
sysDescr (.1.3.6.1.2.1.1.1.0
, 4 = Octet String) and sysObjectID (.1.3.6.1.2.1.1.2.0
, 6 = Object Identifier),
which is the minimum that should be provided for new snmprec files.
To look up the numeric OID and type of an string OID with snmptranslate:
snmptranslate -On -Td SNMPv2-MIB::sysDescr.0
List of SNMP data types:
Type | Value |
---|---|
OCTET STRING | 4 |
HEX STRING | 4x |
Integer32 | 2 |
NULL | 5 |
OBJECT IDENTIFIER | 6 |
IpAddress | 64 |
Counter32 | 65 |
Gauge32 | 66 |
TimeTicks | 67 |
Opaque | 68 |
Counter64 | 70 |
Hex encoded strings (4x) should be used for any strings that contain line returns.
New discovery/poller modules
New discovery or poller modules should define database capture parameters in /tests/module_tables.yaml
.
Example workflow
If the base os (.snmprec) already contains test data for the module you are testing or that data conflicts with your new data, you must use a variant to store your test data (-v).
Add initial detection
- Add device to LibreNMS. It is generic and device_id = 42
- Run
./scripts/save-test-data.php -h 42 -m os
, initial snmprec will be created - Add initial detection for
example-os
- Run discovery to make sure it detects properly
./discovery.php -h 42
- Add any additional os items like version, hardware, features, or serial.
- If there is additional snmp data required, run
./scripts/save-test-data.php -h 42 -m os
otherwise, run./scripts/save-test-data.php -o example-os -m os
- Review data. If you modified the snmprec (don't modify json manually) run
./scripts/save-test-data.php -o example-os -m os
- Run
./scripts/pre-commit.php --db --snmpsim
- If the tests succeed submit a pull request
Additional module support or test data
- Add code to support module or support already exists.
./scripts/save-test-data.php -h 42 -m <module>
, this will add more data to the snmprec file- Review data. If you modified the snmprec (don't modify json manually) run
./scripts/save-test-data.php -o example-os -m <module>
- Run
./scripts/pre-commit.php --db --snmpsim
- If the tests succeed submit a pull request