xref: /linux/tools/testing/selftests/tc-testing/creating-testcases/AddingTestCases.txt (revision 307797159ac25fe5a2048bf5c6a5718298edca57)
1tdc - Adding test cases for tdc
2
3Author: Lucas Bates - lucasb@mojatatu.com
4
5ADDING TEST CASES
6-----------------
7
8User-defined tests should be added by defining a separate JSON file.  This
9will help prevent conflicts when updating the repository. Refer to
10template.json for the required JSON format for test cases.
11
12Include the 'id' field, but do not assign a value. Running tdc with the -i
13option will generate a unique ID for that test case.
14
15tdc will recursively search the 'tc-tests' subdirectory (or the
16directories named with the -D option) for .json files.  Any test case
17files you create in these directories will automatically be included.
18If you wish to store your custom test cases elsewhere, be sure to run
19tdc with the -f argument and the path to your file, or the -D argument
20and the path to your directory(ies).
21
22Be aware of required escape characters in the JSON data - particularly
23when defining the match pattern. Refer to the supplied json test files
24for examples when in doubt.  The match pattern is written in json, and
25will be used by python.  So the match pattern will be a python regular
26expression, but should be written using json syntax.
27
28
29TEST CASE STRUCTURE
30-------------------
31
32Each test case has required data:
33
34id:           A unique alphanumeric value to identify a particular test case
35name:         Descriptive name that explains the command under test
36category:     A list of single-word descriptions covering what the command
37              under test is testing. Example: filter, actions, u32, gact, etc.
38setup:        The list of commands required to ensure the command under test
39              succeeds. For example: if testing a filter, the command to create
40              the qdisc would appear here.
41	      This list can be empty.
42	      Each command can be a string to be executed, or a list consisting
43	      of a string which is a command to be executed, followed by 1 or
44	      more acceptable exit codes for this command.
45	      If only a string is given for the command, then an exit code of 0
46	      will be expected.
47cmdUnderTest: The tc command being tested itself.
48expExitCode:  The code returned by the command under test upon its termination.
49              tdc will compare this value against the actual returned value.
50verifyCmd:    The tc command to be run to verify successful execution.
51              For example: if the command under test creates a gact action,
52              verifyCmd should be "$TC actions show action gact"
53matchPattern: A regular expression to be applied against the output of the
54              verifyCmd to prove the command under test succeeded. This pattern
55              should be as specific as possible so that a false positive is not
56              matched.
57matchCount:   How many times the regex in matchPattern should match. A value
58              of 0 is acceptable.
59teardown:     The list of commands to clean up after the test is completed.
60              The environment should be returned to the same state as when
61              this test was started: qdiscs deleted, actions flushed, etc.
62	      This list can be empty.
63	      Each command can be a string to be executed, or a list consisting
64	      of a string which is a command to be executed, followed by 1 or
65	      more acceptable exit codes for this command.
66	      If only a string is given for the command, then an exit code of 0
67	      will be expected.
68
69
70SETUP/TEARDOWN ERRORS
71---------------------
72
73If an error is detected during the setup/teardown process, execution of the
74tests will immediately stop with an error message and the namespace in which
75the tests are run will be destroyed. This is to prevent inaccurate results
76in the test cases.  tdc will output a series of TAP results for the skipped
77tests.
78
79Repeated failures of the setup/teardown may indicate a problem with the test
80case, or possibly even a bug in one of the commands that are not being tested.
81
82It's possible to include acceptable exit codes with the setup/teardown command
83so that it doesn't halt the script for an error that doesn't matter. Turn the
84individual command into a list, with the command being first, followed by all
85acceptable exit codes for the command.
86
87Example:
88
89A pair of setup commands.  The first can have exit code 0, 1 or 255, the
90second must have exit code 0.
91
92        "setup": [
93            [
94                "$TC actions flush action gact",
95                0,
96                1,
97                255
98            ],
99            "$TC actions add action reclassify index 65536"
100        ],
101