Nornir Service Test Task¤
task api name:
test
The Nornir Service test
task designed to facilitate the execution of network tests. This task provides network operations engineers and network automation developers with tools to validate network configurations, ensure compliance, and monitor network performance. By leveraging the capabilities of the Nornir service, users can automate testing process, identify issues proactively, and maintain a robust network infrastructure.
Nornir service test
task uses Nornir TestsProcessor to run the tests and support test suites definition in YAML format, where test suite YAML files can be stored on and sourced from broker.
Nornir Test Sample Usage¤
Nornir service test
task uses suites in YAML format to define tests, sample tests suite:
- name: Check ceos version
task: "show version"
test: contains
pattern: "4.30.0F"
- name: Check NTP status
test: ncontains
pattern: "unsynchronised"
task: "show ntp status"
- name: Check Mgmt Interface Status
test: contains
pattern: "is up, line protocol is up"
task: "show interface management0"
File suite_3.txt
stored on broker and downloaded by Nornir service prior to running tests, below is an example of how to run the tests suite.
Example
C:\nf>nfcli
Welcome to NorFab Interactive Shell.
nf#
nf#nornir
nf[nornir-test]#
nf[nornir-test]#suite nf://nornir_test_suites/suite_3.txt FC spine,leaf
--------------------------------------------- Job Events -----------------------------------------------
07-Jan-2025 18:44:35 0c3309c54ee44397b055257a0d442e62 job started
07-Jan-2025 18:44:35.207 nornir nornir-worker-1 ceos-spine-1, ceos-spine-2 task started - 'netmiko_send_commands'
07-Jan-2025 18:44:35.211 nornir nornir-worker-2 ceos-leaf-1, ceos-leaf-2, ceos-leaf-3 task started - 'netmiko_send_commands'
<omitted for brevity>
07-Jan-2025 18:44:36 0c3309c54ee44397b055257a0d442e62 job completed in 1.391 seconds
--------------------------------------------- Job Results --------------------------------------------
+----+--------------+-----------------------------+----------+-------------------+
| | host | name | result | exception |
+====+==============+=============================+==========+===================+
| 0 | ceos-leaf-1 | Check ceos version | PASS | |
+----+--------------+-----------------------------+----------+-------------------+
| 1 | ceos-leaf-1 | Check NTP status | FAIL | Pattern in output |
+----+--------------+-----------------------------+----------+-------------------+
| 2 | ceos-leaf-1 | Check Mgmt Interface Status | PASS | |
+----+--------------+-----------------------------+----------+-------------------+
| 3 | ceos-leaf-2 | Check ceos version | PASS | |
+----+--------------+-----------------------------+----------+-------------------+
| 4 | ceos-leaf-2 | Check NTP status | FAIL | Pattern in output |
+----+--------------+-----------------------------+----------+-------------------+
| 5 | ceos-leaf-2 | Check Mgmt Interface Status | PASS | |
+----+--------------+-----------------------------+----------+-------------------+
| 6 | ceos-leaf-3 | Check ceos version | PASS | |
+----+--------------+-----------------------------+----------+-------------------+
| 7 | ceos-leaf-3 | Check NTP status | FAIL | Pattern in output |
+----+--------------+-----------------------------+----------+-------------------+
| 8 | ceos-leaf-3 | Check Mgmt Interface Status | PASS | |
+----+--------------+-----------------------------+----------+-------------------+
| 9 | ceos-spine-1 | Check ceos version | PASS | |
+----+--------------+-----------------------------+----------+-------------------+
| 10 | ceos-spine-1 | Check NTP status | FAIL | Pattern in output |
+----+--------------+-----------------------------+----------+-------------------+
| 12 | ceos-spine-2 | Check ceos version | PASS | |
+----+--------------+-----------------------------+----------+-------------------+
| 12 | ceos-spine-2 | Check ceos version | PASS | |
+----+--------------+-----------------------------+----------+-------------------+
| 12 | ceos-spine-2 | Check ceos version | PASS | |
+----+--------------+-----------------------------+----------+-------------------+
| 12 | ceos-spine-2 | Check ceos version | PASS | |
+----+--------------+-----------------------------+----------+-------------------+
| 12 | ceos-spine-2 | Check ceos version | PASS | |
+----+--------------+-----------------------------+----------+-------------------+
| 13 | ceos-spine-2 | Check NTP status | FAIL | Pattern in output |
+----+--------------+-----------------------------+----------+-------------------+
| 12 | ceos-spine-2 | Check ceos version | PASS | |
+----+--------------+-----------------------------+----------+-------------------+
| 12 | ceos-spine-2 | Check ceos version | PASS | |
+----+--------------+-----------------------------+----------+-------------------+
| 13 | ceos-spine-2 | Check NTP status | FAIL | Pattern in output |
+----+--------------+-----------------------------+----------+-------------------+
| 14 | ceos-spine-2 | Check Mgmt Interface Status | PASS | |
+----+--------------+-----------------------------+----------+-------------------+
nf[nornir-test]#
nf[nornir-test]#top
nf#
Demo
In this example:
nfcli
command starts the NorFab Interactive Shell.nornir
command switches to the Nornir sub-shell.test
command switches to thetest
task sub-shell.suite
argument refers to a path forsuite_3.txt
file with a set of tests to run.- Devices filtered using
FC
- "Filter Contains" Nornir hosts targeting filter to only run tests on devices that containspine
orleaf
in their hostname.
inventory.yaml
should be located in same folder where we start nfcli, unless nfcli -i path_to_inventory.yaml
flag used. Refer to Getting Started section on how to construct inventory.yaml
file
This code is complete and can run as is
import pprint
from norfab.core.nfapi import NorFab
if __name__ == '__main__':
nf = NorFab(inventory="inventory.yaml")
nf.start()
client = nf.make_client()
res = client.run_job(
service="nornir",
task="test",
kwargs={
"suite": "nf://nornir_test_suites/suite_3.txt",
"FC": "spine,leaf"
}
)
pprint.pprint(res)
nf.destroy()
Refer to Getting Started section on how to construct inventory.yaml
file.
Formatting Tests Output¤
NorFab interactive shell allows you to format the results of network tests into text tables. This is particularly useful for presenting test results in a clear and organized manner, making it easier to analyze and interpret the data. The NorFab interactive shell supports the table
command, which relies on the tabulate module to generate text tables. By outputting test results in table format, you can quickly identify issues and take appropriate action.
Using Jinja2 Templates to Generate Tests¤
Using Jinja2 Templates enables you to create dynamic test suites based on variables defined in your inventory or passed as job data. This approach allows you to tailor tests to specific devices or scenarios, ensuring that the tests are relevant and accurate. Jinja2 templates provide a powerful way to automate the creation of complex test cases, incorporating conditional logic, loops, and other advanced features to meet your testing requirements.
Templating Tests with Inline Job Data¤
Inline Job Data allows you to define test parameters directly within the job_data
argument, making it easy to customize tests on the fly. This feature is particularly useful for scenarios where test parameters need to be adjusted frequently or based on specific conditions. By templating tests with inline job data, you can ensure that your tests are always up-to-date and aligned with the current network state.
Using Dry Run¤
The Using Dry Run feature allows you to generate the content of network test suites without actually performing any actions on the devices. This is useful for validation purposes, as it enables you to verify the correctness of your tests before running them. By using dry run, you can identify potential issues and make necessary adjustments, ensuring that your tests will execute successfully when run for real.
Running a Subset of Tests¤
Running a Subset of Tests allows you to execute only a specific set of tests, rather than running the entire test suite. This is useful for targeted testing, such as validating changes in a particular part of the network configuration or focusing on specific devices features. By running a subset of tests, you can save time and resources, while still ensuring that critical aspects of the network are thoroughly tested.
Returning Only Failed Tests¤
Returning only failed tests enables you to filter the test results to show only the tests that have failed. This is particularly useful for quickly identifying and addressing issues, as it allows you to focus on the areas that require attention. By returning only failed tests, you can streamline the troubleshooting process and ensure that network problems are resolved efficiently.
NORFAB Nornir Test Shell Reference¤
The NORFAB Nornir Test Shell Reference provides a comprehensive set of command options for the Nornir test
task. These commands allow you to control various aspects of the test execution, such as setting job timeouts, filtering devices, adding task details to results, and configuring retry mechanisms. By leveraging these command options, you can tailor the behavior of the tests to meet your specific network management needs, ensuring that your network remains reliable and performant.
NorFab shell supports these command options for Nornir test
task:
nf#man tree nornir.test
root
└── nornir: Nornir service
└── test: Run network tests
├── timeout: Job timeout
├── workers: Filter worker to target, default 'all'
├── add-details: Add task details to results, default 'False'
├── num-workers: RetryRunner number of threads for tasks execution
├── num-connectors: RetryRunner number of threads for device connections
├── connect-retry: RetryRunner number of connection attempts
├── task-retry: RetryRunner number of attempts to run task
├── reconnect-on-fail: RetryRunner perform reconnect to host on task failure
├── connect-check: RetryRunner test TCP connection before opening actual connection
├── connect-timeout: RetryRunner timeout in seconds to wait for test TCP connection to establish
├── creds-retry: RetryRunner list of connection credentials and parameters to retry
├── tf: File group name to save task results to on worker file system
├── tf-skip-failed: Save results to file for failed tasks
├── diff: File group name to run the diff for
├── diff-last: File version number to diff, default is 1 (last)
├── table: Table format (brief, terse, extend) or parameters or True, default 'brief'
├── headers: Table headers
├── headers-exclude: Table headers to exclude
├── sortby: Table header column to sort by
├── reverse: Table reverse the sort by order
├── FO: Filter hosts using Filter Object
├── FB: Filter hosts by name using Glob Patterns
├── FH: Filter hosts by hostname
├── FC: Filter hosts containment of pattern in name
├── FR: Filter hosts by name using Regular Expressions
├── FG: Filter hosts by group
├── FP: Filter hosts by hostname using IP Prefix
├── FL: Filter hosts by names list
├── FM: Filter hosts by platform
├── FX: Filter hosts excluding them by name
├── FN: Negate the match
├── hosts: Filter hosts to target
├── *suite: Nornir suite nf://path/to/file.py
├── dry-run: Return produced per-host tests suite content without running tests
├── subset: Filter tests by name
├── failed-only: Return test results for failed tests only
├── remove-tasks: Include/Exclude tested task results
└── job-data: Path to YAML file with job data
nf#
*
- mandatory/required command argument
Python API Reference¤
Function to test networks using a suite of tests.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
suite
|
Union[list, str]
|
URL Path to YAML file with tests or a list of test definitions or template URL that resolves to a file path. |
required |
subset
|
str
|
List or string with comma-separated non-case-sensitive glob patterns to filter tests by name. Ignored if dry_run is True. |
None
|
dry_run
|
bool
|
If True, returns produced per-host tests suite content only. |
False
|
remove_tasks
|
bool
|
If False, results will include other tasks output. |
True
|
failed_only
|
bool
|
If True, returns test results for failed tests only. |
False
|
return_tests_suite
|
bool
|
If True, returns rendered per-host tests suite
content in addition to test results using a dictionary with |
False
|
job_data
|
str
|
URL to YAML file with data or dictionary/list of data to pass on to Jinja2 rendering context. |
None
|
**kwargs
|
Any additional arguments to pass on to the Nornir service task. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
dict |
dict
|
A dictionary containing the test results. If |
Note
Result failed
attribute is set to True if any of the tests failed for any of the hosts.
Raises:
Type | Description |
---|---|
RuntimeError
|
If there is an error in rendering the Jinja2 templates or loading the YAML. |
Source code in norfab\workers\nornir_worker.py
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 |
|