README 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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.8.
  8. * The kernel must have network namespace support if using nsPlugin
  9. * The kernel must have veth support available, as a veth pair is created
  10. prior to running the tests when using nsPlugin.
  11. * The kernel must have the appropriate infrastructure enabled to run all tdc
  12. unit tests. See the config file in this directory for minimum required
  13. features. As new tests will be added, config options list will be updated.
  14. * All tc-related features being tested must be built in or available as
  15. modules. To check what is required in current setup run:
  16. ./tdc.py -c
  17. Note:
  18. In the current release, tdc run will abort due to a failure in setup or
  19. teardown commands - which includes not being able to run a test simply
  20. because the kernel did not support a specific feature. (This will be
  21. handled in a future version - the current workaround is to run the tests
  22. on specific test categories that your kernel supports)
  23. BEFORE YOU RUN
  24. --------------
  25. The path to the tc executable that will be most commonly tested can be defined
  26. in the tdc_config.py file. Find the 'TC' entry in the NAMES dictionary and
  27. define the path.
  28. If you need to test a different tc executable on the fly, you can do so by
  29. using the -p option when running tdc:
  30. ./tdc.py -p /path/to/tc
  31. RUNNING TDC
  32. -----------
  33. To use tdc, root privileges are required. This is because the
  34. commands being tested must be run as root. The code that enforces
  35. execution by root uid has been moved into a plugin (see PLUGIN
  36. ARCHITECTURE, below).
  37. Tests that use a network device should have nsPlugin.py listed as a
  38. requirement for that test. nsPlugin executes all commands within a
  39. network namespace and creates a veth pair which may be used in those test
  40. cases. To disable execution within the namespace, pass the -N option
  41. to tdc when starting a test run; the veth pair will still be created
  42. by the plugin.
  43. Running tdc without any arguments will run all tests. Refer to the section
  44. on command line arguments for more information, or run:
  45. ./tdc.py -h
  46. tdc will list the test names as they are being run, and print a summary in
  47. TAP (Test Anything Protocol) format when they are done. If tests fail,
  48. output captured from the failing test will be printed immediately following
  49. the failed test in the TAP output.
  50. OVERVIEW OF TDC EXECUTION
  51. -------------------------
  52. One run of tests is considered a "test suite" (this will be refined in the
  53. future). A test suite has one or more test cases in it.
  54. A test case has four stages:
  55. - setup
  56. - execute
  57. - verify
  58. - teardown
  59. The setup and teardown stages can run zero or more commands. The setup
  60. stage does some setup if the test needs it. The teardown stage undoes
  61. the setup and returns the system to a "neutral" state so any other test
  62. can be run next. These two stages require any commands run to return
  63. success, but do not otherwise verify the results.
  64. The execute and verify stages each run one command. The execute stage
  65. tests the return code against one or more acceptable values. The
  66. verify stage checks the return code for success, and also compares
  67. the stdout with a regular expression.
  68. Each of the commands in any stage will run in a shell instance.
  69. Each test is an atomic unit. A test that for whatever reason spans multiple test
  70. definitions is a bug.
  71. A test that runs inside a namespace (requires "nsPlugin") will run in parallel
  72. with other tests.
  73. Tests that use netdevsim or don't run inside a namespace run serially with regards
  74. to each other.
  75. USER-DEFINED CONSTANTS
  76. ----------------------
  77. The tdc_config.py file contains multiple values that can be altered to suit
  78. your needs. Any value in the NAMES dictionary can be altered without affecting
  79. the tests to be run. These values are used in the tc commands that will be
  80. executed as part of the test. More will be added as test cases require.
  81. Example:
  82. $TC qdisc add dev $DEV1 ingress
  83. The NAMES values are used to substitute into the commands in the test cases.
  84. COMMAND LINE ARGUMENTS
  85. ----------------------
  86. Run tdc.py -h to see the full list of available arguments.
  87. PLUGIN ARCHITECTURE
  88. -------------------
  89. There is now a plugin architecture, and some of the functionality that
  90. was in the tdc.py script has been moved into the plugins.
  91. The plugins are in the directory plugin-lib. The are executed from
  92. directory plugins. Put symbolic links from plugins to plugin-lib,
  93. and name them according to the order you want them to run. This is not
  94. necessary if a test case being run requires a specific plugin to work.
  95. Example:
  96. bjb@bee:~/work/tc-testing$ ls -l plugins
  97. total 4
  98. lrwxrwxrwx 1 bjb bjb 27 Oct 4 16:12 10-rootPlugin.py -> ../plugin-lib/rootPlugin.py
  99. lrwxrwxrwx 1 bjb bjb 25 Oct 12 17:55 20-nsPlugin.py -> ../plugin-lib/nsPlugin.py
  100. -rwxr-xr-x 1 bjb bjb 0 Sep 29 15:56 __init__.py
  101. The plugins are a subclass of TdcPlugin, defined in TdcPlugin.py and
  102. must be called "SubPlugin" so tdc can find them. They are
  103. distinguished from each other in the python program by their module
  104. name.
  105. This base class supplies "hooks" to run extra functions. These hooks are as follows:
  106. pre- and post-suite
  107. pre- and post-case
  108. pre- and post-execute stage
  109. adjust-command (runs in all stages and receives the stage name)
  110. The pre-suite hook receives the number of tests and an array of test ids.
  111. This allows you to dump out the list of skipped tests in the event of a
  112. failure during setup or teardown stage.
  113. The pre-case hook receives the ordinal number and test id of the current test.
  114. The adjust-command hook receives the stage id (see list below) and the
  115. full command to be executed. This allows for last-minute adjustment
  116. of the command.
  117. The stages are identified by the following strings:
  118. - pre (pre-suite)
  119. - setup
  120. - command
  121. - verify
  122. - teardown
  123. - post (post-suite)
  124. To write a plugin, you need to inherit from TdcPlugin in
  125. TdcPlugin.py. To use the plugin, you have to put the
  126. implementation file in plugin-lib, and add a symbolic link to it from
  127. plugins. It will be detected at run time and invoked at the
  128. appropriate times. There are a few examples in the plugin-lib
  129. directory:
  130. - rootPlugin.py:
  131. implements the enforcement of running as root
  132. - nsPlugin.py:
  133. sets up a network namespace and runs all commands in that namespace,
  134. while also setting up dummy devices to be used in testing.
  135. - valgrindPlugin.py
  136. runs each command in the execute stage under valgrind,
  137. and checks for leaks.
  138. This plugin will output an extra test for each test in the test file,
  139. one is the existing output as to whether the test passed or failed,
  140. and the other is a test whether the command leaked memory or not.
  141. (This one is a preliminary version, it may not work quite right yet,
  142. but the overall template is there and it should only need tweaks.)
  143. ACKNOWLEDGEMENTS
  144. ----------------
  145. Thanks to:
  146. Jamal Hadi Salim, for providing valuable test cases
  147. Keara Leibovitz, who wrote the CLI test driver that I used as a base for the
  148. first version of the tc testing suite. This work was presented at
  149. Netdev 1.2 Tokyo in October 2016.
  150. Samir Hussain, for providing help while I dove into Python for the first time
  151. and being a second eye for this code.