176b903eeSLucas Batestdc - Linux Traffic Control (tc) unit testing suite
276b903eeSLucas Bates
376b903eeSLucas BatesAuthor: Lucas Bates - [email protected]
476b903eeSLucas Bates
576b903eeSLucas Batestdc is a Python script to load tc unit tests from a separate JSON file and
676b903eeSLucas Batesexecute them inside a network namespace dedicated to the task.
776b903eeSLucas Bates
876b903eeSLucas Bates
976b903eeSLucas BatesREQUIREMENTS
1076b903eeSLucas Bates------------
1176b903eeSLucas Bates
1276b903eeSLucas Bates*  Minimum Python version of 3.4. Earlier 3.X versions may work but are not
1376b903eeSLucas Bates   guaranteed.
1476b903eeSLucas Bates
1576b903eeSLucas Bates*  The kernel must have network namespace support
1676b903eeSLucas Bates
1776b903eeSLucas Bates*  The kernel must have veth support available, as a veth pair is created
1876b903eeSLucas Bates   prior to running the tests.
1976b903eeSLucas Bates
20*95ce14c3SBrenda J. Butler*  All tc-related features being tested must be built in or available as
21*95ce14c3SBrenda J. Butler   modules.  To check what is required in current setup run:
2276b903eeSLucas Bates   ./tdc.py -c
2376b903eeSLucas Bates
2476b903eeSLucas Bates   Note:
2576b903eeSLucas Bates   In the current release, tdc run will abort due to a failure in setup or
2676b903eeSLucas Bates   teardown commands - which includes not being able to run a test simply
2776b903eeSLucas Bates   because the kernel did not support a specific feature. (This will be
2876b903eeSLucas Bates   handled in a future version - the current workaround is to run the tests
2976b903eeSLucas Bates   on specific test categories that your kernel supports)
3076b903eeSLucas Bates
3176b903eeSLucas Bates
3276b903eeSLucas BatesBEFORE YOU RUN
3376b903eeSLucas Bates--------------
3476b903eeSLucas Bates
3576b903eeSLucas BatesThe path to the tc executable that will be most commonly tested can be defined
3676b903eeSLucas Batesin the tdc_config.py file. Find the 'TC' entry in the NAMES dictionary and
3776b903eeSLucas Batesdefine the path.
3876b903eeSLucas Bates
3976b903eeSLucas BatesIf you need to test a different tc executable on the fly, you can do so by
4076b903eeSLucas Batesusing the -p option when running tdc:
4176b903eeSLucas Bates	./tdc.py -p /path/to/tc
4276b903eeSLucas Bates
4376b903eeSLucas Bates
4476b903eeSLucas BatesRUNNING TDC
4576b903eeSLucas Bates-----------
4676b903eeSLucas Bates
47*95ce14c3SBrenda J. ButlerTo use tdc, root privileges are required.  This is because the
48*95ce14c3SBrenda J. Butlercommands being tested must be run as root.  The code that enforces
49*95ce14c3SBrenda J. Butlerexecution by root uid has been moved into a plugin (see PLUGIN
50*95ce14c3SBrenda J. ButlerARCHITECTURE, below).
5176b903eeSLucas Bates
52*95ce14c3SBrenda J. ButlerIf nsPlugin is linked, all tests are executed inside a network
53*95ce14c3SBrenda J. Butlernamespace to prevent conflicts within the host.
5476b903eeSLucas Bates
5576b903eeSLucas BatesRunning tdc without any arguments will run all tests. Refer to the section
5676b903eeSLucas Bateson command line arguments for more information, or run:
5776b903eeSLucas Bates	./tdc.py -h
5876b903eeSLucas Bates
5976b903eeSLucas Batestdc will list the test names as they are being run, and print a summary in
6076b903eeSLucas BatesTAP (Test Anything Protocol) format when they are done. If tests fail,
6176b903eeSLucas Batesoutput captured from the failing test will be printed immediately following
6276b903eeSLucas Batesthe failed test in the TAP output.
6376b903eeSLucas Bates
6476b903eeSLucas Bates
65*95ce14c3SBrenda J. ButlerOVERVIEW OF TDC EXECUTION
66*95ce14c3SBrenda J. Butler-------------------------
67*95ce14c3SBrenda J. Butler
68*95ce14c3SBrenda J. ButlerOne run of tests is considered a "test suite" (this will be refined in the
69*95ce14c3SBrenda J. Butlerfuture).  A test suite has one or more test cases in it.
70*95ce14c3SBrenda J. Butler
71*95ce14c3SBrenda J. ButlerA test case has four stages:
72*95ce14c3SBrenda J. Butler
73*95ce14c3SBrenda J. Butler  - setup
74*95ce14c3SBrenda J. Butler  - execute
75*95ce14c3SBrenda J. Butler  - verify
76*95ce14c3SBrenda J. Butler  - teardown
77*95ce14c3SBrenda J. Butler
78*95ce14c3SBrenda J. ButlerThe setup and teardown stages can run zero or more commands.  The setup
79*95ce14c3SBrenda J. Butlerstage does some setup if the test needs it.  The teardown stage undoes
80*95ce14c3SBrenda J. Butlerthe setup and returns the system to a "neutral" state so any other test
81*95ce14c3SBrenda J. Butlercan be run next.  These two stages require any commands run to return
82*95ce14c3SBrenda J. Butlersuccess, but do not otherwise verify the results.
83*95ce14c3SBrenda J. Butler
84*95ce14c3SBrenda J. ButlerThe execute and verify stages each run one command.  The execute stage
85*95ce14c3SBrenda J. Butlertests the return code against one or more acceptable values.  The
86*95ce14c3SBrenda J. Butlerverify stage checks the return code for success, and also compares
87*95ce14c3SBrenda J. Butlerthe stdout with a regular expression.
88*95ce14c3SBrenda J. Butler
89*95ce14c3SBrenda J. ButlerEach of the commands in any stage will run in a shell instance.
90*95ce14c3SBrenda J. Butler
91*95ce14c3SBrenda J. Butler
9276b903eeSLucas BatesUSER-DEFINED CONSTANTS
9376b903eeSLucas Bates----------------------
9476b903eeSLucas Bates
9576b903eeSLucas BatesThe tdc_config.py file contains multiple values that can be altered to suit
9676b903eeSLucas Batesyour needs. Any value in the NAMES dictionary can be altered without affecting
9776b903eeSLucas Batesthe tests to be run. These values are used in the tc commands that will be
9876b903eeSLucas Batesexecuted as part of the test. More will be added as test cases require.
9976b903eeSLucas Bates
10076b903eeSLucas BatesExample:
10176b903eeSLucas Bates	$TC qdisc add dev $DEV1 ingress
10276b903eeSLucas Bates
103*95ce14c3SBrenda J. ButlerThe NAMES values are used to substitute into the commands in the test cases.
104*95ce14c3SBrenda J. Butler
10576b903eeSLucas Bates
10676b903eeSLucas BatesCOMMAND LINE ARGUMENTS
10776b903eeSLucas Bates----------------------
10876b903eeSLucas Bates
10976b903eeSLucas BatesRun tdc.py -h to see the full list of available arguments.
11076b903eeSLucas Bates
111*95ce14c3SBrenda J. Butlerusage: tdc.py [-h] [-p PATH] [-D DIR [DIR ...]] [-f FILE [FILE ...]]
112*95ce14c3SBrenda J. Butler              [-c [CATG [CATG ...]]] [-e ID [ID ...]] [-l] [-s] [-i] [-v]
113*95ce14c3SBrenda J. Butler              [-d DEVICE] [-n NS] [-V]
114*95ce14c3SBrenda J. Butler
115*95ce14c3SBrenda J. ButlerLinux TC unit tests
116*95ce14c3SBrenda J. Butler
117*95ce14c3SBrenda J. Butleroptional arguments:
118*95ce14c3SBrenda J. Butler  -h, --help            show this help message and exit
119*95ce14c3SBrenda J. Butler  -p PATH, --path PATH  The full path to the tc executable to use
120*95ce14c3SBrenda J. Butler  -v, --verbose         Show the commands that are being run
121*95ce14c3SBrenda J. Butler  -d DEVICE, --device DEVICE
122*95ce14c3SBrenda J. Butler                        Execute the test case in flower category
123*95ce14c3SBrenda J. Butler
124*95ce14c3SBrenda J. Butlerselection:
125*95ce14c3SBrenda J. Butler  select which test cases: files plus directories; filtered by categories
126*95ce14c3SBrenda J. Butler  plus testids
127*95ce14c3SBrenda J. Butler
128*95ce14c3SBrenda J. Butler  -D DIR [DIR ...], --directory DIR [DIR ...]
129*95ce14c3SBrenda J. Butler                        Collect tests from the specified directory(ies)
130*95ce14c3SBrenda J. Butler                        (default [tc-tests])
131*95ce14c3SBrenda J. Butler  -f FILE [FILE ...], --file FILE [FILE ...]
132*95ce14c3SBrenda J. Butler                        Run tests from the specified file(s)
133*95ce14c3SBrenda J. Butler  -c [CATG [CATG ...]], --category [CATG [CATG ...]]
134*95ce14c3SBrenda J. Butler                        Run tests only from the specified category/ies, or if
135*95ce14c3SBrenda J. Butler                        no category/ies is/are specified, list known
136*95ce14c3SBrenda J. Butler                        categories.
137*95ce14c3SBrenda J. Butler  -e ID [ID ...], --execute ID [ID ...]
138*95ce14c3SBrenda J. Butler                        Execute the specified test cases with specified IDs
139*95ce14c3SBrenda J. Butler
140*95ce14c3SBrenda J. Butleraction:
141*95ce14c3SBrenda J. Butler  select action to perform on selected test cases
142*95ce14c3SBrenda J. Butler
143*95ce14c3SBrenda J. Butler  -l, --list            List all test cases, or those only within the
144*95ce14c3SBrenda J. Butler                        specified category
145*95ce14c3SBrenda J. Butler  -s, --show            Display the selected test cases
146*95ce14c3SBrenda J. Butler  -i, --id              Generate ID numbers for new test cases
147*95ce14c3SBrenda J. Butler
148*95ce14c3SBrenda J. Butlernetns:
149*95ce14c3SBrenda J. Butler  options for nsPlugin(run commands in net namespace)
150*95ce14c3SBrenda J. Butler
151*95ce14c3SBrenda J. Butler  -n NS, --namespace NS
152*95ce14c3SBrenda J. Butler                        Run commands in namespace NS
153*95ce14c3SBrenda J. Butler
154*95ce14c3SBrenda J. Butlervalgrind:
155*95ce14c3SBrenda J. Butler  options for valgrindPlugin (run command under test under Valgrind)
156*95ce14c3SBrenda J. Butler
157*95ce14c3SBrenda J. Butler  -V, --valgrind        Run commands under valgrind
158*95ce14c3SBrenda J. Butler
159*95ce14c3SBrenda J. Butler
160*95ce14c3SBrenda J. ButlerPLUGIN ARCHITECTURE
161*95ce14c3SBrenda J. Butler-------------------
162*95ce14c3SBrenda J. Butler
163*95ce14c3SBrenda J. ButlerThere is now a plugin architecture, and some of the functionality that
164*95ce14c3SBrenda J. Butlerwas in the tdc.py script has been moved into the plugins.
165*95ce14c3SBrenda J. Butler
166*95ce14c3SBrenda J. ButlerThe plugins are in the directory plugin-lib.  The are executed from
167*95ce14c3SBrenda J. Butlerdirectory plugins.  Put symbolic links from plugins to plugin-lib,
168*95ce14c3SBrenda J. Butlerand name them according to the order you want them to run.
169*95ce14c3SBrenda J. Butler
170*95ce14c3SBrenda J. ButlerExample:
171*95ce14c3SBrenda J. Butler
172*95ce14c3SBrenda J. Butlerbjb@bee:~/work/tc-testing$ ls -l plugins
173*95ce14c3SBrenda J. Butlertotal 4
174*95ce14c3SBrenda J. Butlerlrwxrwxrwx  1 bjb  bjb    27 Oct  4 16:12 10-rootPlugin.py -> ../plugin-lib/rootPlugin.py
175*95ce14c3SBrenda J. Butlerlrwxrwxrwx  1 bjb  bjb    25 Oct 12 17:55 20-nsPlugin.py -> ../plugin-lib/nsPlugin.py
176*95ce14c3SBrenda J. Butler-rwxr-xr-x  1 bjb  bjb     0 Sep 29 15:56 __init__.py
177*95ce14c3SBrenda J. Butler
178*95ce14c3SBrenda J. ButlerThe plugins are a subclass of TdcPlugin, defined in TdcPlugin.py and
179*95ce14c3SBrenda J. Butlermust be called "SubPlugin" so tdc can find them.  They are
180*95ce14c3SBrenda J. Butlerdistinguished from each other in the python program by their module
181*95ce14c3SBrenda J. Butlername.
182*95ce14c3SBrenda J. Butler
183*95ce14c3SBrenda J. ButlerThis base class supplies "hooks" to run extra functions.  These hooks are as follows:
184*95ce14c3SBrenda J. Butler
185*95ce14c3SBrenda J. Butlerpre- and post-suite
186*95ce14c3SBrenda J. Butlerpre- and post-case
187*95ce14c3SBrenda J. Butlerpre- and post-execute stage
188*95ce14c3SBrenda J. Butleradjust-command (runs in all stages and receives the stage name)
189*95ce14c3SBrenda J. Butler
190*95ce14c3SBrenda J. ButlerThe pre-suite hook receives the number of tests and an array of test ids.
191*95ce14c3SBrenda J. ButlerThis allows you to dump out the list of skipped tests in the event of a
192*95ce14c3SBrenda J. Butlerfailure during setup or teardown stage.
193*95ce14c3SBrenda J. Butler
194*95ce14c3SBrenda J. ButlerThe pre-case hook receives the ordinal number and test id of the current test.
195*95ce14c3SBrenda J. Butler
196*95ce14c3SBrenda J. ButlerThe adjust-command hook receives the stage id (see list below) and the
197*95ce14c3SBrenda J. Butlerfull command to be executed.  This allows for last-minute adjustment
198*95ce14c3SBrenda J. Butlerof the command.
199*95ce14c3SBrenda J. Butler
200*95ce14c3SBrenda J. ButlerThe stages are identified by the following strings:
201*95ce14c3SBrenda J. Butler
202*95ce14c3SBrenda J. Butler  - pre  (pre-suite)
203*95ce14c3SBrenda J. Butler  - setup
204*95ce14c3SBrenda J. Butler  - command
205*95ce14c3SBrenda J. Butler  - verify
206*95ce14c3SBrenda J. Butler  - teardown
207*95ce14c3SBrenda J. Butler  - post (post-suite)
208*95ce14c3SBrenda J. Butler
209*95ce14c3SBrenda J. Butler
210*95ce14c3SBrenda J. ButlerTo write a plugin, you need to inherit from TdcPlugin in
211*95ce14c3SBrenda J. ButlerTdcPlugin.py.  To use the plugin, you have to put the
212*95ce14c3SBrenda J. Butlerimplementation file in plugin-lib, and add a symbolic link to it from
213*95ce14c3SBrenda J. Butlerplugins.  It will be detected at run time and invoked at the
214*95ce14c3SBrenda J. Butlerappropriate times.  There are a few examples in the plugin-lib
215*95ce14c3SBrenda J. Butlerdirectory:
216*95ce14c3SBrenda J. Butler
217*95ce14c3SBrenda J. Butler  - rootPlugin.py:
218*95ce14c3SBrenda J. Butler      implements the enforcement of running as root
219*95ce14c3SBrenda J. Butler  - nsPlugin.py:
220*95ce14c3SBrenda J. Butler      sets up a network namespace and runs all commands in that namespace
221*95ce14c3SBrenda J. Butler  - valgrindPlugin.py
222*95ce14c3SBrenda J. Butler      runs each command in the execute stage under valgrind,
223*95ce14c3SBrenda J. Butler      and checks for leaks.
224*95ce14c3SBrenda J. Butler      This plugin will output an extra test for each test in the test file,
225*95ce14c3SBrenda J. Butler      one is the existing output as to whether the test passed or failed,
226*95ce14c3SBrenda J. Butler      and the other is a test whether the command leaked memory or not.
227*95ce14c3SBrenda J. Butler      (This one is a preliminary version, it may not work quite right yet,
228*95ce14c3SBrenda J. Butler      but the overall template is there and it should only need tweaks.)
22976b903eeSLucas Bates
23076b903eeSLucas Bates
23176b903eeSLucas BatesACKNOWLEDGEMENTS
23276b903eeSLucas Bates----------------
23376b903eeSLucas Bates
23476b903eeSLucas BatesThanks to:
23576b903eeSLucas Bates
23676b903eeSLucas BatesJamal Hadi Salim, for providing valuable test cases
23776b903eeSLucas BatesKeara Leibovitz, who wrote the CLI test driver that I used as a base for the
23876b903eeSLucas Bates   first version of the tc testing suite. This work was presented at
23976b903eeSLucas Bates   Netdev 1.2 Tokyo in October 2016.
24076b903eeSLucas BatesSamir Hussain, for providing help while I dove into Python for the first time
24176b903eeSLucas Bates    and being a second eye for this code.
242