Package: nuts
Network Unit Testing SystemIntroductionThe Network Unit Testing System or "nuts" in short draws on the concept of unit tests, known from the domain of programming, and applies it to the domain of networking.One major difference between unit tests in programming and network tests is the definition of what a test actually is.
In programming, unit tests normally focus on testing edge cases, since the amount of non-edge cases is not definable.
In the network testing domain, tests are less about edge cases, but more about testing existing network states with
pre-defined test cases. Such a single test case might be "can host A reach neighbors X, Y, Z?" on many different devices.
This is what nuts tries to achieve:
Apply test cases based on your pre-defined network topology to your actual network and have the tests confirm the correct state.Installation InstructionsUsing pipRunpip install nutsUsing poetryNuts usespoetryas a dependency manager.Install poetry.Clone this repository.Run$ poetry installHow It Works: Test Bundles and Test DefinitionsThe project relies on thepytest frameworkto setup and execute the tests.
Nuts itself is written as a custom pytest plugin. In the background,nornirexecutes specific network tasks for the actual tests. This can be extended to use other context as well in the future.Nuts treats the test definition and the so-called test bundle as separate entities. Thetest definitionis modeled as a custompytest.Class, and a predefined set of test definitions can be found in the nuts modulebase_tests. New test definitions can be added easily by the user of the plugin.Thetest bundleis a file that is parsed by pytest. The file provides data on the desired network state and describes which test definitions should be collected and executed by pytest.
The structure of the test bundle should enable people without in-depth python knowledge to add new test bundles or update existing ones to reflect changes in the network.While the readme here is only a short overview, find thedocumentation of nuts on readthedocs.Test Bundle StructureCurrently only yaml files are supported as test bundles,
but other sources such as other file formats or database entries can be considered in later nuts versions.Each test bundle contains the following structure:----test_module:<module that contains the test class># optionaltest_class:<name of the test class>label:<label to uniquely identify the test># optionaltest_execution:<additional data used to execute the test># optionaltest_data:<data used to generate the test cases>...test_module: The full path of the Python module that contains the test class to be used.
This value is optional if the test class is registered inindex.pyof the pytest-nuts plugin.
Note that it can be relevant in which directorypytestis started if local test modules are used. Usingtest_modulesallows you to write your own test classes.Note: We currently do not support self-written test modules, since upcoming refactorings might introduce breaking changes.test_class: The name of the Python class which contains the tests that should be executed.
Note that currently every test in this class will be executed.label: Additional identifier that can be used to distinguish between multiple occurrences of the same
test class in a test bundle.test_execution: Data that is exposed as part of thenuts_parametersproperty.
By convention, this contains additional information that is passed directly to the nornir task in the background.
Therefore the key-value pairs must be consistent with the key-value pairs of the specific nornir task.
As an example, the test definitionnapalm_ping.pycalls a nornir task to execute napalm's ping-command.
This allows the additionalmax_dropparameter intest_execution, since it is in turn pre-defined by napalm.test_data: Data that is used to parametrize the tests in the test class which have thepytest.mark.nutsannotation. It is additionally part of thenuts_parametersproperty.Example: CDP NeighborsExample of a test bundle forTestNetmikoCdpNeighborswhich tests thatR1is a CDP Neighbor of bothR2andR3.
This example creates three different tests, one for each entry in thetest_datalist.----test_module:nuts.base_tests.netmiko_cdp_neighborstest_class:TestNetmikoCdpNeighborstest_data:-host:R1local_port:GigabitEthernet3destination_host:R2management_ip:172.16.12.2remote_port:GigabitEthernet2-host:R1local_port:GigabitEthernet4destination_host:R3management_ip:172.16.13.3remote_port:GigabitEthernet2-host:R2local_port:GigabitEthernet2destination_host:R1management_ip:172.16.12.1remote_port:GigabitEthernet3...How the Test Bundle Is Converted to a Pytest TestWhen nuts is executed, pytest converts the test bundles (the yaml files) into tests. During test collection, the custom pytest markernutsuses the data that has been defined in the test bundle mentioned above.
This annotation is a wrapper around thepytest.mark.parametrizeannotation and allows the plugin to use the data entries from the test bundle. For each entry in thetest_datasection of the test bundle, the custom marker generates a single test case. To achieve this, the plugin transforms the entries into n-tuples, sincepytest.mark.parametrizeexpects a list of n-tuples as input.The custom nuts marker takes two arguments: The first argument of the annotation determines the required fields.
For each entry intest_datathese fields are extracted and transformed to a tuple considering the correct order.
If any of these fields are not present in an entry oftest_data, the corresponding test case will be skipped.
A second argument determines optional fields that can also be used in a test case as well - non-present values are passed into the function asNone.The following test-run of CDP neighbors for example checks the local port:classTestNetmikoCdpNeighbors:@pytest.mark.nuts("remote_host,local_port")deftest_local_port(self,single_result,remote_host,local_port):assertsingle_result.result[remote_host]["local_port"]==local_portThe required fields arehost,remote_hostandlocal_port- they must be present in the custom marker,
but also be provided as argument to the test method itself.single_resultuses thehostfield and provides the result that has been processed via the specific context of a test.Test classes and their contextEach test module implements a context class to provide module-specific functionality to its tests. This context class is aNutsContextor a subclass of it.
This guarantees a consistent interface across all tests for test setup and execution.
Currently, the predefined test classes usenornirin order to communicate
with the network devices. Those test classes derive all from a more specificNornirNutsContext,
which provides a nornir instance and nornir-specific helpers. In the example above, it is a class calledCdpNeighborsContextthat derives fromNornirNutsContext.If you want to learn more how nuts works but do not have a running network in the background, there's a nuts showcase - an offline test class that displays the basic functionality of nuts. See thetutorialfor further information.Develop Your Own Test ClassesNuts is essentially designed as a pytest-plugin and it is possible to add your own, self-written test classes. Dev documentation on how to write your own test classes is planned for a future release.
Still, it is possible to write your own test classes nevertheless, even if we cannot guarantee that upcoming planned refactorings do not introduce breaking changes.Community-provided test classesCisco IOSXE Test Classbybriantsaundersnuts-experimentsbyubaumannThanksUrs Baumann, for originating the idea, supervising the development, and serving as the project ownerAndreas Stalder and David Meister, for developing the first version based on SaltStack in their term projectMike Schmid and Janik Schlatter, implemented the first version using Nornir in their term project.Matthias Gabriel, who laid the foundations of nuts as a pytest plugin.Marco MartinezandSeverin Grimm, added more test cases and interviewed companies in their term project.Méline Sieber,Lukas Murer, for maintenance during your working hours at the INSFlorian Bruhin (The Compiler)for invaluable feedback and advice.