blob: 8cbe961f88719c07b11f55cfe39d6e4406bffea3 [file] [log] [blame]
Klement Sekera778c2762016-11-08 02:00:28 +01001.. _unittest: https://docs.python.org/2/library/unittest.html
2.. _TestCase: https://docs.python.org/2/library/unittest.html#unittest.TestCase
3.. _AssertionError: https://docs.python.org/2/library/exceptions.html#exceptions.AssertionError
4.. _SkipTest: https://docs.python.org/2/library/unittest.html#unittest.SkipTest
5.. _virtualenv: http://docs.python-guide.org/en/latest/dev/virtualenvs/
6.. _scapy: http://www.secdev.org/projects/scapy/
Klement Sekeraf62ae122016-10-11 11:47:09 +02007
Klement Sekera778c2762016-11-08 02:00:28 +01008.. |vtf| replace:: VPP Test Framework
Klement Sekeraf62ae122016-10-11 11:47:09 +02009
Klement Sekera778c2762016-11-08 02:00:28 +010010|vtf|
11=====
Klement Sekeraf62ae122016-10-11 11:47:09 +020012
Klement Sekera778c2762016-11-08 02:00:28 +010013Overview
14########
15
16The goal of the |vtf| is to ease writing, running and debugging
17unit tests for the VPP. For this, python was chosen as a high level language
18allowing rapid development with scapy_ providing the necessary tool for creating
19and dissecting packets.
20
21Anatomy of a test case
22######################
23
24Python's unittest_ is used as the base framework upon which the VPP test
Matej Klotton86d87c42016-11-11 11:38:55 +010025framework is built. A test suite in the |vtf| consists of multiple classes
Klement Sekera778c2762016-11-08 02:00:28 +010026derived from `VppTestCase`, which is itself derived from TestCase_.
27The test class defines one or more test functions, which act as test cases.
28
29Function flow when running a test case is:
30
311. `setUpClass <VppTestCase.setUpClass>`:
32 This function is called once for each test class, allowing a one-time test
33 setup to be executed. If this functions throws an exception,
34 none of the test functions are executed.
352. `setUp <VppTestCase.setUp>`:
36 The setUp function runs before each of the test functions. If this function
37 throws an exception other than AssertionError_ or SkipTest_, then this is
38 considered an error, not a test failure.
393. *test_<name>*:
40 This is the guts of the test case. It should execute the test scenario
41 and use the various assert functions from the unittest framework to check
42 necessary.
434. `tearDown <VppTestCase.tearDown>`:
44 The tearDown function is called after each test function with the purpose
45 of doing partial cleanup.
465. `tearDownClass <VppTestCase.tearDownClass>`:
Matej Klotton86d87c42016-11-11 11:38:55 +010047 Method called once after running all of the test functions to perform
Klement Sekera778c2762016-11-08 02:00:28 +010048 the final cleanup.
49
50Test temporary directory and VPP life cycle
Matej Klotton86d87c42016-11-11 11:38:55 +010051###########################################
Klement Sekera778c2762016-11-08 02:00:28 +010052
53Test separation is achieved by separating the test files and vpp instances.
54Each test creates a temporary directory and it's name is used to create
55a shared memory prefix which is used to run a VPP instance.
56This way, there is no conflict between any other VPP instances running
57on the box and the test VPP. Any temporary files created by the test case
58are stored in this temporary test directory.
59
60Virtual environment
61###################
62
Matej Klotton86d87c42016-11-11 11:38:55 +010063Virtualenv_ is a python module which provides a means to create an environment
Klement Sekera778c2762016-11-08 02:00:28 +010064containing the dependencies required by the |vtf|, allowing a separation
65from any existing system-wide packages. |vtf|'s Makefile automatically
66creates a virtualenv_ inside build-root and installs the required packages
67in that environment. The environment is entered whenever executing a test
68via one of the make test targets.
69
70Naming conventions
71##################
72
73Most unit tests do some kind of packet manipulation - sending and receiving
74packets between VPP and virtual hosts connected to the VPP. Referring
Matej Klotton86d87c42016-11-11 11:38:55 +010075to the sides, addresses, etc. is always done as if looking from the VPP side,
Klement Sekera778c2762016-11-08 02:00:28 +010076thus:
77
78* *local_* prefix is used for the VPP side.
79 So e.g. `local_ip4 <VppInterface.local_ip4>` address is the IPv4 address
80 assigned to the VPP interface.
81* *remote_* prefix is used for the virtual host side.
82 So e.g. `remote_mac <VppInterface.remote_mac>` address is the MAC address
83 assigned to the virtual host connected to the VPP.
84
85Packet flow in the |vtf|
86########################
87
88Test framework -> VPP
89~~~~~~~~~~~~~~~~~~~~~
90
91|vtf| doesn't send any packets to VPP directly. Traffic is instead injected
92using packet-generator interfaces, represented by the `VppPGInterface` class.
93Packets are written into a temporary .pcap file, which is then read by the VPP
94and the packets are injected into the VPP world.
95
96VPP -> test framework
97~~~~~~~~~~~~~~~~~~~~~
98
99Similarly, VPP doesn't send any packets to |vtf| directly. Instead, packet
100capture feature is used to capture and write traffic to a temporary .pcap file,
101which is then read and analyzed by the |vtf|.
102
103Test framework objects
104######################
105
106The following objects provide VPP abstraction and provide a means to do
107common tasks easily in the test cases.
108
109* `VppInterface`: abstract class representing generic VPP interface
110 and contains some common functionality, which is then used by derived classes
111* `VppPGInterface`: class representing VPP packet-generator interface.
112 The interface is created/destroyed when the object is created/destroyed.
113* `VppSubInterface`: VPP sub-interface abstract class, containing common
114 functionality for e.g. `VppDot1QSubint` and `VppDot1ADSubint` classes
115
116How VPP API/CLI is called
117#########################
118
119Vpp provides python bindings in a python module called vpp-papi, which the test
120framework installs in the virtual environment. A shim layer represented by
121the `VppPapiProvider` class is built on top of the vpp-papi, serving these
122purposes:
123
1241. Automatic return value checks:
125 After each API is called, the return value is checked against the expected
126 return value (by default 0, but can be overridden) and an exception
127 is raised if the check fails.
1282. Automatic call of hooks:
129
130 a. `before_cli <Hook.before_cli>` and `before_api <Hook.before_api>` hooks
131 are used for debug logging and stepping through the test
132 b. `after_cli <Hook.after_cli>` and `after_api <Hook.after_api>` hooks
133 are used for monitoring the vpp process for crashes
1343. Simplification of API calls:
135 Many of the VPP APIs take a lot of parameters and by providing sane defaults
136 for these, the API is much easier to use in the common case and the code is
137 more readable. E.g. ip_add_del_route API takes ~25 parameters, of which
138 in the common case, only 3 are needed.
139
140Example: how to add a new test
141##############################
142
143In this example, we will describe how to add a new test case which tests VPP...
144
1451. Add a new file called...
1462. Add a setUpClass function containing...
1473. Add the test code in test...
1484. Run the test...
149
150|vtf| module documentation
151##########################
152
Klement Sekeraf62ae122016-10-11 11:47:09 +0200153.. toctree::
154 :maxdepth: 2
Klement Sekera778c2762016-11-08 02:00:28 +0100155 :glob:
Klement Sekeraf62ae122016-10-11 11:47:09 +0200156
157 modules.rst
158
159Indices and tables
160==================
161
162* :ref:`genindex`
163* :ref:`modindex`
164* :ref:`search`
165