README 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. tdc - Linux Traffic Control (tc) unit testing suite
  2. Author: Lucas Bates - lucasb@mojatatu.com
  3. tdc is a Python script to load tc unit tests from a separate JSON file and
  4. execute them inside a network namespace dedicated to the task.
  5. REQUIREMENTS
  6. ------------
  7. * Minimum Python version of 3.4. Earlier 3.X versions may work but are not
  8. guaranteed.
  9. * The kernel must have network namespace support
  10. * The kernel must have veth support available, as a veth pair is created
  11. prior to running the tests.
  12. * The kernel must have the appropriate infrastructure enabled to run all tdc
  13. unit tests. See the config file in this directory for minimum required
  14. features. As new tests will be added, config options list will be updated.
  15. * All tc-related features being tested must be built in or available as
  16. modules. To check what is required in current setup run:
  17. ./tdc.py -c
  18. Note:
  19. In the current release, tdc run will abort due to a failure in setup or
  20. teardown commands - which includes not being able to run a test simply
  21. because the kernel did not support a specific feature. (This will be
  22. handled in a future version - the current workaround is to run the tests
  23. on specific test categories that your kernel supports)
  24. BEFORE YOU RUN
  25. --------------
  26. The path to the tc executable that will be most commonly tested can be defined
  27. in the tdc_config.py file. Find the 'TC' entry in the NAMES dictionary and
  28. define the path.
  29. If you need to test a different tc executable on the fly, you can do so by
  30. using the -p option when running tdc:
  31. ./tdc.py -p /path/to/tc
  32. RUNNING TDC
  33. -----------
  34. To use tdc, root privileges are required. This is because the
  35. commands being tested must be run as root. The code that enforces
  36. execution by root uid has been moved into a plugin (see PLUGIN
  37. ARCHITECTURE, below).
  38. If nsPlugin is linked, all tests are executed inside a network
  39. namespace to prevent conflicts within the host.
  40. Running tdc without any arguments will run all tests. Refer to the section
  41. on command line arguments for more information, or run:
  42. ./tdc.py -h
  43. tdc will list the test names as they are being run, and print a summary in
  44. TAP (Test Anything Protocol) format when they are done. If tests fail,
  45. output captured from the failing test will be printed immediately following
  46. the failed test in the TAP output.
  47. OVERVIEW OF TDC EXECUTION
  48. -------------------------
  49. One run of tests is considered a "test suite" (this will be refined in the
  50. future). A test suite has one or more test cases in it.
  51. A test case has four stages:
  52. - setup
  53. - execute
  54. - verify
  55. - teardown
  56. The setup and teardown stages can run zero or more commands. The setup
  57. stage does some setup if the test needs it. The teardown stage undoes
  58. the setup and returns the system to a "neutral" state so any other test
  59. can be run next. These two stages require any commands run to return
  60. success, but do not otherwise verify the results.
  61. The execute and verify stages each run one command. The execute stage
  62. tests the return code against one or more acceptable values. The
  63. verify stage checks the return code for success, and also compares
  64. the stdout with a regular expression.
  65. Each of the commands in any stage will run in a shell instance.
  66. USER-DEFINED CONSTANTS
  67. ----------------------
  68. The tdc_config.py file contains multiple values that can be altered to suit
  69. your needs. Any value in the NAMES dictionary can be altered without affecting
  70. the tests to be run. These values are used in the tc commands that will be
  71. executed as part of the test. More will be added as test cases require.
  72. Example:
  73. $TC qdisc add dev $DEV1 ingress
  74. The NAMES values are used to substitute into the commands in the test cases.
  75. COMMAND LINE ARGUMENTS
  76. ----------------------
  77. Run tdc.py -h to see the full list of available arguments.
  78. usage: tdc.py [-h] [-p PATH] [-D DIR [DIR ...]] [-f FILE [FILE ...]]
  79. [-c [CATG [CATG ...]]] [-e ID [ID ...]] [-l] [-s] [-i] [-v] [-N]
  80. [-d DEVICE] [-P] [-n] [-V]
  81. Linux TC unit tests
  82. optional arguments:
  83. -h, --help show this help message and exit
  84. -p PATH, --path PATH The full path to the tc executable to use
  85. -v, --verbose Show the commands that are being run
  86. -N, --notap Suppress tap results for command under test
  87. -d DEVICE, --device DEVICE
  88. Execute the test case in flower category
  89. -P, --pause Pause execution just before post-suite stage
  90. selection:
  91. select which test cases: files plus directories; filtered by categories
  92. plus testids
  93. -D DIR [DIR ...], --directory DIR [DIR ...]
  94. Collect tests from the specified directory(ies)
  95. (default [tc-tests])
  96. -f FILE [FILE ...], --file FILE [FILE ...]
  97. Run tests from the specified file(s)
  98. -c [CATG [CATG ...]], --category [CATG [CATG ...]]
  99. Run tests only from the specified category/ies, or if
  100. no category/ies is/are specified, list known
  101. categories.
  102. -e ID [ID ...], --execute ID [ID ...]
  103. Execute the specified test cases with specified IDs
  104. action:
  105. select action to perform on selected test cases
  106. -l, --list List all test cases, or those only within the
  107. specified category
  108. -s, --show Display the selected test cases
  109. -i, --id Generate ID numbers for new test cases
  110. netns:
  111. options for nsPlugin (run commands in net namespace)
  112. -n, --namespace
  113. Run commands in namespace as specified in tdc_config.py
  114. valgrind:
  115. options for valgrindPlugin (run command under test under Valgrind)
  116. -V, --valgrind Run commands under valgrind
  117. PLUGIN ARCHITECTURE
  118. -------------------
  119. There is now a plugin architecture, and some of the functionality that
  120. was in the tdc.py script has been moved into the plugins.
  121. The plugins are in the directory plugin-lib. The are executed from
  122. directory plugins. Put symbolic links from plugins to plugin-lib,
  123. and name them according to the order you want them to run.
  124. Example:
  125. bjb@bee:~/work/tc-testing$ ls -l plugins
  126. total 4
  127. lrwxrwxrwx 1 bjb bjb 27 Oct 4 16:12 10-rootPlugin.py -> ../plugin-lib/rootPlugin.py
  128. lrwxrwxrwx 1 bjb bjb 25 Oct 12 17:55 20-nsPlugin.py -> ../plugin-lib/nsPlugin.py
  129. -rwxr-xr-x 1 bjb bjb 0 Sep 29 15:56 __init__.py
  130. The plugins are a subclass of TdcPlugin, defined in TdcPlugin.py and
  131. must be called "SubPlugin" so tdc can find them. They are
  132. distinguished from each other in the python program by their module
  133. name.
  134. This base class supplies "hooks" to run extra functions. These hooks are as follows:
  135. pre- and post-suite
  136. pre- and post-case
  137. pre- and post-execute stage
  138. adjust-command (runs in all stages and receives the stage name)
  139. The pre-suite hook receives the number of tests and an array of test ids.
  140. This allows you to dump out the list of skipped tests in the event of a
  141. failure during setup or teardown stage.
  142. The pre-case hook receives the ordinal number and test id of the current test.
  143. The adjust-command hook receives the stage id (see list below) and the
  144. full command to be executed. This allows for last-minute adjustment
  145. of the command.
  146. The stages are identified by the following strings:
  147. - pre (pre-suite)
  148. - setup
  149. - command
  150. - verify
  151. - teardown
  152. - post (post-suite)
  153. To write a plugin, you need to inherit from TdcPlugin in
  154. TdcPlugin.py. To use the plugin, you have to put the
  155. implementation file in plugin-lib, and add a symbolic link to it from
  156. plugins. It will be detected at run time and invoked at the
  157. appropriate times. There are a few examples in the plugin-lib
  158. directory:
  159. - rootPlugin.py:
  160. implements the enforcement of running as root
  161. - nsPlugin.py:
  162. sets up a network namespace and runs all commands in that namespace
  163. - valgrindPlugin.py
  164. runs each command in the execute stage under valgrind,
  165. and checks for leaks.
  166. This plugin will output an extra test for each test in the test file,
  167. one is the existing output as to whether the test passed or failed,
  168. and the other is a test whether the command leaked memory or not.
  169. (This one is a preliminary version, it may not work quite right yet,
  170. but the overall template is there and it should only need tweaks.)
  171. - buildebpfPlugin.py:
  172. builds all programs in $EBPFDIR.
  173. ACKNOWLEDGEMENTS
  174. ----------------
  175. Thanks to:
  176. Jamal Hadi Salim, for providing valuable test cases
  177. Keara Leibovitz, who wrote the CLI test driver that I used as a base for the
  178. first version of the tc testing suite. This work was presented at
  179. Netdev 1.2 Tokyo in October 2016.
  180. Samir Hussain, for providing help while I dove into Python for the first time
  181. and being a second eye for this code.