2017-04-25 18:35:42 +01:00
|
|
|
source: Developing/os/Test-Units.md
|
2018-10-27 23:04:34 +01:00
|
|
|
path: blob/master/doc/
|
2017-04-25 18:35:42 +01:00
|
|
|
|
2017-12-20 08:36:49 -06:00
|
|
|
# Tests
|
2017-04-25 18:35:42 +01:00
|
|
|
|
2019-06-20 13:53:45 -05:00
|
|
|
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](http://snmpsim.sourceforge.net/) 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
|
2020-09-24 05:32:03 -05:00
|
|
|
> `./lnms dev:check`. You can install snmpsim with the
|
2019-06-20 13:53:45 -05:00
|
|
|
> command `pip3 install snmpsim`.
|
2017-12-20 08:36:49 -06:00
|
|
|
|
|
|
|
## Capturing test data
|
|
|
|
|
2020-09-01 13:00:44 +02:00
|
|
|
???+ warning "If test data already exists"
|
|
|
|
|
|
|
|
If test data already exists, but is for a different device/configuration with the same OS.
|
|
|
|
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.
|
|
|
|
|
|
|
|
### 1. Collect SNMP data
|
|
|
|
|
2019-06-20 13:53:45 -05:00
|
|
|
`./scripts/collect-snmp-data.php` is provided to make it easy to
|
|
|
|
collect data for tests. Running collect-snmp-data.php with the
|
2020-09-01 13:00:44 +02:00
|
|
|
`--hostname (-h)` allows you to capture all data used to discover and
|
2019-06-20 13:53:45 -05:00
|
|
|
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.
|
2017-12-20 08:36:49 -06:00
|
|
|
|
2020-09-01 13:00:44 +02:00
|
|
|
|
|
|
|
### 2. Save test data
|
|
|
|
|
2019-06-20 13:53:45 -05:00
|
|
|
After you have collected snmp data, run `./scripts/save-test-data.php`
|
2020-09-01 13:00:44 +02:00
|
|
|
with the `--os (-o)` option to dump the post discovery and post poll
|
2019-06-20 13:53:45 -05:00
|
|
|
database entries to json files. This step requires snmpsim, if you are
|
|
|
|
having issues, the maintainers may help you generate it from the
|
|
|
|
snmprec you created in the previous step.
|
2018-01-20 20:22:47 -06:00
|
|
|
|
2020-09-01 13:00:44 +02:00
|
|
|
Generally, you will only need to collect data once.
|
|
|
|
After you have the data you need in the snmprec file, you can just use save-test-data.php to update the database dump (json) after that.
|
2017-12-20 08:36:49 -06:00
|
|
|
|
|
|
|
|
|
|
|
## Running tests
|
|
|
|
|
2019-06-20 13:53:45 -05:00
|
|
|
**Note:** To run tests, ensure you have executed
|
|
|
|
`./scripts/composer_wrapper.php install` from your LibreNMS root
|
|
|
|
directory. This will read composer.json and install any dependencies required.
|
2018-05-21 19:00:08 +12:00
|
|
|
|
2019-06-20 13:53:45 -05:00
|
|
|
After you have saved your test data, you should run
|
2020-09-24 05:32:03 -05:00
|
|
|
`./lnms dev:check` verify they pass.
|
2017-12-20 08:36:49 -06:00
|
|
|
|
2019-06-20 13:53:45 -05:00
|
|
|
To run the full suite of tests enable database and snmpsim reliant
|
2020-09-24 05:32:03 -05:00
|
|
|
tests: `./lnms dev:check unit --db --snmpsim`
|
2017-12-28 16:12:08 -06:00
|
|
|
|
2019-10-21 02:47:40 +02:00
|
|
|
### Specific OS
|
2018-01-09 11:40:26 -06:00
|
|
|
|
2020-09-24 05:32:03 -05:00
|
|
|
`./lnms dev:check unit -o osname`
|
2018-01-09 11:40:26 -06:00
|
|
|
|
2019-10-21 02:47:40 +02:00
|
|
|
### Specific Module
|
2018-01-09 11:40:26 -06:00
|
|
|
|
2020-09-24 05:32:03 -05:00
|
|
|
`./lnms dev:check unit -m modulename`
|
2017-12-20 08:36:49 -06:00
|
|
|
|
|
|
|
## Using snmpsim for testing
|
|
|
|
|
2019-06-20 13:53:45 -05:00
|
|
|
You can run snmpsim to access test data by running
|
|
|
|
`./scripts/collect-snmp-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.
|
2017-12-20 08:36:49 -06:00
|
|
|
|
2019-10-21 02:47:40 +02:00
|
|
|
```bash
|
2017-12-20 08:36:49 -06:00
|
|
|
snmpget -v 2c -c ios_c3560e 127.1.6.1:1161 sysDescr.0
|
|
|
|
```
|
|
|
|
|
|
|
|
## Snmprec format
|
|
|
|
|
2019-06-20 13:53:45 -05:00
|
|
|
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.
|
2017-04-25 18:35:42 +01:00
|
|
|
|
2019-10-21 02:47:40 +02:00
|
|
|
```snmp
|
2017-04-25 18:35:42 +01:00
|
|
|
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
|
|
|
|
```
|
|
|
|
|
2019-06-20 13:53:45 -05:00
|
|
|
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.
|
2017-04-25 18:35:42 +01:00
|
|
|
|
|
|
|
To look up the numeric OID and type of an string OID with snmptranslate:
|
2019-06-20 13:53:45 -05:00
|
|
|
|
2017-04-25 18:35:42 +01:00
|
|
|
```bash
|
|
|
|
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.
|
|
|
|
|
2017-12-20 08:36:49 -06:00
|
|
|
## New discovery/poller modules
|
2017-04-25 18:35:42 +01:00
|
|
|
|
2017-12-20 08:36:49 -06:00
|
|
|
New discovery or poller modules should define database capture parameters in `/tests/module_tables.yaml`.
|
2018-01-02 15:36:31 -06:00
|
|
|
|
|
|
|
## Example workflow
|
2019-06-20 13:53:45 -05:00
|
|
|
|
|
|
|
If the base os (<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).
|
2018-01-02 15:36:31 -06:00
|
|
|
|
|
|
|
### Add initial detection
|
2019-06-20 13:53:45 -05:00
|
|
|
|
2018-01-02 15:36:31 -06:00
|
|
|
1. Add device to LibreNMS. It is generic and device_id = 42
|
2019-10-21 02:47:40 +02:00
|
|
|
1. Run `./scripts/collect-snmp-data.php -h 42`, initial snmprec will be created
|
|
|
|
1. [Add initial detection](Initial-Detection.md) for `example-os`
|
|
|
|
1. Run discovery to make sure it detects properly `./discovery.php -h 42`
|
|
|
|
1. Add any additional os items like version, hardware, features, or serial.
|
|
|
|
1. If there is additional snmp data required, run `./scripts/collect-snmp-data.php -h 42`
|
|
|
|
1. Run `./scripts/save-test-data.php -o example-os` to update the dumped database data.
|
|
|
|
1. Review data. If you modified the snmprec or code (don't modify json manually) run `./scripts/save-test-data.php -o example-os -m os`
|
2020-09-24 05:32:03 -05:00
|
|
|
1. Run `./lnms dev:check unit --db --snmpsim`
|
2019-10-21 02:47:40 +02:00
|
|
|
1. If the tests succeed submit a pull request
|
2018-01-02 15:36:31 -06:00
|
|
|
|
|
|
|
### Additional module support or test data
|
2019-10-21 02:47:40 +02:00
|
|
|
|
2018-01-02 15:36:31 -06:00
|
|
|
1. Add code to support module or support already exists.
|
2019-10-21 02:47:40 +02:00
|
|
|
1. `./scripts/collect-snmp-data.php -h 42 -m <module>`, this will add
|
2019-06-20 13:53:45 -05:00
|
|
|
more data to the snmprec file
|
2019-10-21 02:47:40 +02:00
|
|
|
1. Review data. If you modified the snmprec (don't modify json
|
2019-06-20 13:53:45 -05:00
|
|
|
manually) run `./scripts/save-test-data.php -o example-os -m <module>`
|
2020-09-24 05:32:03 -05:00
|
|
|
1. Run `./lnms dev:check unit --db --snmpsim`
|
2019-10-21 02:47:40 +02:00
|
|
|
1. If the tests succeed submit a pull request
|