| 1 |
= Writing and running tests for OpenSCAP |
| 2 |
|
| 3 |
This document should help you with writing and running tests for your pull |
| 4 |
requests. It is recommended to add a new test if a new functionality is added |
| 5 |
or if a code that is not covered by any test is updated. Another recommendation |
| 6 |
is to add your test in a separate commit. |
| 7 |
|
| 8 |
All the tests reside in the link:../../tests[tests] directory. It has multiple |
| 9 |
subdirectories which should represent various parts of the OpenSCAP library and |
| 10 |
its utilities. When you contribute to some part of the OpenSCAP project you |
| 11 |
should put a test for your contribution into the corresponding subdirectory |
| 12 |
in the link:../../tests[tests] directory. Use your best judgement when deciding |
| 13 |
where to put the test for your pull request and if you are not sure don't be |
| 14 |
afraid to ask in the pull request, someone will definitely help you with that. |
| 15 |
|
| 16 |
NOTE: OpenSCAP project uses the **CMake** buildsystem which has built-in |
| 17 |
testing support through the |
| 18 |
link:https://gitlab.kitware.com/cmake/community/wikis/doc/ctest/Testing-With-CTest[CTest] |
| 19 |
testing tool. |
| 20 |
|
| 21 |
|
| 22 |
== Preparing test environment and running tests |
| 23 |
To run a specific test or all tests you first need to compile the OpenSCAP |
| 24 |
library and then install additional packages required for testing. See the |
| 25 |
*Building OpenSCAP on Linux* section in the link:../developer/developer.adoc[OpenSCAP Developer Manual] |
| 26 |
for more details. |
| 27 |
|
| 28 |
|
| 29 |
== Writing a new test |
| 30 |
In this guide we will use an example to describe the process of writing a test |
| 31 |
for the OpenSCAP project. Let's suppose you want to write a new test for |
| 32 |
the Script Check Engine (SCE) to test its basic functionality. SCE allows you |
| 33 |
to define your own scripts (usually written in Bash or Python) to extend XCCDF |
| 34 |
rule checking capabilities. Custom check scripts can be referenced from |
| 35 |
an XCCDF rule using `<check-content-ref>` element, for example: |
| 36 |
---- |
| 37 |
<check system="http://open-scap.org/page/SCE"> |
| 38 |
<check-content-ref href="YOUR_BASH_SCRIPT.sh"/> |
| 39 |
</check> |
| 40 |
---- |
| 41 |
|
| 42 |
|
| 43 |
=== Deciding where to put a new test |
| 44 |
In our example, we are testing the SCE module, therefore we will look for |
| 45 |
its subdirectory in the link:../../tests[tests] directory and we will find it |
| 46 |
at the following link: link:../../tests/sce[tests/sce]. We will add our new test |
| 47 |
into this subdirectory. |
| 48 |
|
| 49 |
|
| 50 |
==== Scenario A: There is a suitable directory for my new test |
| 51 |
This will happen most of the times. As stated above in our example we will place |
| 52 |
our test into the link:../../tests/sce[tests/sce] directory. |
| 53 |
|
| 54 |
|
| 55 |
==== Scenario B: There is no suitable directory for my new test |
| 56 |
This might happen if your test covers a part of the OpenSCAP which has no tests |
| 57 |
at all. In this case you would need to add a new directory with suitable name |
| 58 |
into the link:../../tests[tests] directory structure and create/update |
| 59 |
`CMakeLists.txt` files. |
| 60 |
|
| 61 |
To have an example also for this scenario, let's suppose we want to add the |
| 62 |
`foo` subdirectory into the link:../../tests[tests] directory. We need to: |
| 63 |
|
| 64 |
. Create the `foo` subdirectory in the link:../../tests[tests] directory. |
| 65 |
. Use the link:https://cmake.org/cmake/help/latest/command/add_subdirectory.html[add_subdirectory] |
| 66 |
command from the link:../../tests/CMakeLists.txt[tests/CMakeLists.txt] |
| 67 |
to add the `foo` subdirectory to the CMake buildsystem. |
| 68 |
. Create `CMakeLists.txt` inside the `tests/foo` directory and use the |
| 69 |
`add_oscap_test` function (defined in the |
| 70 |
link:../../tests/CMakeLists.txt[tests/CMakeLists.txt]) to add all your test |
| 71 |
scripts from the `foo` directory. |
| 72 |
|
| 73 |
Please see the |
| 74 |
link:https://gitlab.kitware.com/cmake/community/wikis/doc/ctest/Testing-With-CTest[ |
| 75 |
CTest documentation] for more details. |
| 76 |
|
| 77 |
|
| 78 |
=== Common test library |
| 79 |
When writing tests for OpenSCAP you should use the common test library which is |
| 80 |
located at link:../../tests/test_common.sh.in[tests/test_common.sh.in]. |
| 81 |
|
| 82 |
NOTE: The `cmake` command will generate the `tests/test_common.sh` file from |
| 83 |
the link:../../tests/test_common.sh.in[tests/test_common.sh.in] adding |
| 84 |
configuration specific settings. |
| 85 |
|
| 86 |
You will need to source the `tests/test_common.sh` in your test scripts to use |
| 87 |
the functions which it provides: |
| 88 |
[source,bash] |
| 89 |
---- |
| 90 |
#!/usr/bin/env bash |
| 91 |
|
| 92 |
set -o pipefail |
| 93 |
|
| 94 |
. $builddir/tests/test_common.sh |
| 95 |
|
| 96 |
test1 |
| 97 |
test2 |
| 98 |
... |
| 99 |
---- |
| 100 |
|
| 101 |
NOTE: The `$builddir` variable contains the path to the top level of the build |
| 102 |
tree. It is defined in the link:../../tests/CMakeLists.txt[tests/CMakeLists.txt] |
| 103 |
as `builddir=${CMAKE_BINARY_DIR}`. |
| 104 |
|
| 105 |
|
| 106 |
==== Global variables exported by test library |
| 107 |
Always use `$OSCAP` variable instead of the plain `oscap` in your tests when |
| 108 |
calling the `oscap` command line tool. This is because tests might be run with |
| 109 |
`CUSTOM_OSCAP` variable. |
| 110 |
|
| 111 |
You can use `$XMLDIFF` in your tests which will call the |
| 112 |
link:../../tests/xmldiff.pl[tests/xmldiff.pl] script. |
| 113 |
|
| 114 |
It is also possible to do XPath queries using `$XPATH` variable, the usage is: |
| 115 |
[source,bash] |
| 116 |
---- |
| 117 |
$XPATH FILE 'QUERY' |
| 118 |
---- |
| 119 |
|
| 120 |
|
| 121 |
==== Best practices when writing bash tests |
| 122 |
It is always good to set `pipefail` option for all your test scripts so you |
| 123 |
accidentally don't lose non-zero exit statuses of commands in a pipeline: |
| 124 |
[source,bash] |
| 125 |
---- |
| 126 |
set -o pipefail |
| 127 |
---- |
| 128 |
|
| 129 |
The next option you can consider is `errexit` which exits your script |
| 130 |
immediately if a command exits with a non-zero status. You might want to use |
| 131 |
this option if tests are somehow dependent and it doesn't make sense to continue |
| 132 |
testing after one test fails. |
| 133 |
[source,bash] |
| 134 |
---- |
| 135 |
set -e |
| 136 |
---- |
| 137 |
|
| 138 |
Also consider splitting the tests up into several separate bash scripts if |
| 139 |
you test many things at once; long running "all.sh" test should be avoided |
| 140 |
if possible, as when these fail it is harder to debug than having more, |
| 141 |
smaller tests. However, make sure these independent tests don't have hidden |
| 142 |
dependencies: often we'll run the test suite in parallel, meaning in-order |
| 143 |
execution of different test scripts cannot be ensured. (However, order |
| 144 |
order inside the test script is guaranteed per bash's rules). |
| 145 |
|
| 146 |
Lastly, make sure your tests are architecture, distribution, and operating |
| 147 |
system independent. Try to make your tests dependent on local file contents, |
| 148 |
not system file contents, and make them independent of listening ports and |
| 149 |
installed software as much as possible. |
| 150 |
|
| 151 |
==== test_init function |
| 152 |
This function does nothing. Logging is done by the CTest and the log from |
| 153 |
testing can be found at `build/Testing/Temporary/LastTest.log`. |
| 154 |
|
| 155 |
|
| 156 |
==== test_run function |
| 157 |
The function is responsible for executing a test script file or a function and |
| 158 |
logging its result into the log file. |
| 159 |
[source,bash] |
| 160 |
---- |
| 161 |
test_run "DESCRIPTION" TEST_FUNCTION|$srcdir/TEST_SCRIPT_FILE ARG [ARG...] |
| 162 |
---- |
| 163 |
|
| 164 |
NOTE: The `$srcdir` variable contains the path to the directory with the test |
| 165 |
script. It is defined in the link:../../tests/CMakeLists.txt[tests/CMakeLists.txt] |
| 166 |
as `srcdir=${CMAKE_CURRENT_SOURCE_DIR}`. The reason is backward compatibility |
| 167 |
as originally tests were executed by the GNU automake. |
| 168 |
|
| 169 |
The `test_run` function reports the following results into the log file: |
| 170 |
|
| 171 |
* *PASS* when script/function returns *0*, |
| 172 |
* *FAIL* when script/function returns *1*, |
| 173 |
* *SKIP* when script/function returns *255*, |
| 174 |
* *WARN* when script/function returns none of the above exit statuses. |
| 175 |
|
| 176 |
The result of every test executed by the `test_run` function will be reported |
| 177 |
in the log file in a following way: |
| 178 |
[source,bash] |
| 179 |
---- |
| 180 |
TEST: DESCRIPTION |
| 181 |
<test stdout + stderr output> |
| 182 |
RESULT: PASS/FAIL/SKIP/WARN |
| 183 |
---- |
| 184 |
|
| 185 |
|
| 186 |
==== test_exit function |
| 187 |
The function is responsible for cleaning-up the testing environment. You can |
| 188 |
call it without arguments or with one argument -- a script/function which will |
| 189 |
do additional clean-up tasks. |
| 190 |
[source,bash] |
| 191 |
---- |
| 192 |
test_exit [CLEAN_SCRIPT|CLEAN_FUNCTION] |
| 193 |
---- |
| 194 |
|
| 195 |
|
| 196 |
==== require function |
| 197 |
Checks if requirements are in the `$PATH`, use it as follows: |
| 198 |
[source,bash] |
| 199 |
---- |
| 200 |
require 'program' || return 255 |
| 201 |
---- |
| 202 |
|
| 203 |
|
| 204 |
==== probecheck function |
| 205 |
Checks if probe exists, use it as follows: |
| 206 |
[source,bash] |
| 207 |
---- |
| 208 |
probecheck 'probe' || return 255 |
| 209 |
---- |
| 210 |
|
| 211 |
|
| 212 |
==== verify_results function |
| 213 |
Verifies that there is the `COUNT` number of results of selected OVAL `TYPE` in |
| 214 |
a `RESULTS_FILE`: |
| 215 |
[source,bash] |
| 216 |
---- |
| 217 |
verify_results TYPE CONTENT_FILE RESULTS_FILE COUNT |
| 218 |
|
| 219 |
verify_results "def" test_probe_foo.xml results.xml 13 |
| 220 |
---- |
| 221 |
|
| 222 |
The function extracts the actual test/definition result, and compares it with the respective test/definition comment. |
| 223 |
For example, if the test contains `comment="true"`, the test passes only if result of the respective test is `true`, |
| 224 |
if `comment="false"`, a `false` result is expected. |
| 225 |
If the comment is missing or it has other value, it is assumed that the result should be neither `true` nor `false`. |
| 226 |
|
| 227 |
NOTE: This function expects that the OVAL `TYPE` is numbered from `1` to `COUNT` |
| 228 |
in the `RESULTS_FILE`. |
| 229 |
`TYPE` is typically `def` or `tst` for definitions and tests respectively. |
| 230 |
|
| 231 |
|
| 232 |
==== assert_exists function |
| 233 |
Does an XPath query to a file specified in the `$result` variable and checks if |
| 234 |
number of results matches with an expected number specified as an argument: |
| 235 |
[source,bash] |
| 236 |
---- |
| 237 |
result="relative_path_to_file" |
| 238 |
assert_exists EXPECTED_NUMBER_OF_RESULTS XPATH_QUERY_STRING |
| 239 |
---- |
| 240 |
|
| 241 |
For example, let's say you want to check that in the `results.xml` file the |
| 242 |
result of the rule `xccdf_com.example.www_rule_test` is fail: |
| 243 |
[source,bash] |
| 244 |
---- |
| 245 |
result="./results.xml" |
| 246 |
my_rule_="xccdf_com.example.www_rule_test" |
| 247 |
assert_exists 1 "//rule-result[@idref=\"$my_rule\"]/result[text()=\"fail\"]" |
| 248 |
---- |
| 249 |
|
| 250 |
|
| 251 |
=== Adding test files |
| 252 |
Now, as we know where a new test should go and what functions and capabilities |
| 253 |
are provided by the common test library, we can add test files which will |
| 254 |
contain test scripts and content required for testing. |
| 255 |
|
| 256 |
To sum up, we are adding a tests to check the basic functionality of the Script |
| 257 |
Check Engine (SCE) and we have decided that the test will go into the |
| 258 |
link:../../tests/sce[tests/sce] directory. |
| 259 |
|
| 260 |
We will add the link:../../tests/sce/test_sce.sh[tests/sce/test_sce.sh] |
| 261 |
script which will contain our test and |
| 262 |
link:../../tests/sce/sce_xccdf.xml[tests/sce/sce_xccdf.xml], an XML file with |
| 263 |
XCCDF rules which are referencing various check scripts (grep the |
| 264 |
`check-content-ref` element to see the referenced files). All the referenced |
| 265 |
check script files are set to always pass and the |
| 266 |
link:../../tests/sce/test_sce.sh[tests/sce/test_sce.sh] script will perform |
| 267 |
evaluation of the link:../../tests/sce/sce_xccdf.xml[tests/sce/sce_xccdf.xml] |
| 268 |
XCCDF document file and it will check that all rule results are `pass`. |
| 269 |
|
| 270 |
|
| 271 |
=== Plugging your new test into the test library |
| 272 |
You need to plug your test into the test library so it will be run automatically |
| 273 |
everytime `make test` is run. To do this, you need to add your test script |
| 274 |
into the `CMakeLists.txt`. The `CMakeLists.txt` which you need to modify is |
| 275 |
located in the same directory as your test script. |
| 276 |
|
| 277 |
We will demonstrate this on our example with the SCE test. We have prepared our |
| 278 |
test script, the XML document file with custom rules and various check scripts |
| 279 |
for testing. We placed all our test files into the |
| 280 |
link:../../tests/sce[tests/sce] directory. Now we will modify the |
| 281 |
link:../../tests/sce/CMakeLists.txt[tests/sce/CMakeLists.txt] and we will add |
| 282 |
our test script file using the `add_oscap_test` function which will make sure |
| 283 |
that our test will be executed by the `make test`: |
| 284 |
---- |
| 285 |
if(ENABLE_SCE) |
| 286 |
... |
| 287 |
*add_oscap_test("test_sce.sh")* |
| 288 |
... |
| 289 |
endif() |
| 290 |
---- |
| 291 |
|
| 292 |
|
| 293 |
=== Running your new test |
| 294 |
To run your new test you first need to compile the OpenSCAP library. See the |
| 295 |
*Building OpenSCAP on Linux* section in the link:../developer/developer.adoc[OpenSCAP Developer Manual] |
| 296 |
for more details. |
| 297 |
Also you don't need to run all the tests using `make test`, you can run only |
| 298 |
the specific test(s). To do so, you need to be in the build directory and |
| 299 |
run `ctest -R` from there, for example: |
| 300 |
[source,bash] |
| 301 |
---- |
| 302 |
$ cd build/ |
| 303 |
$ ctest -R sce/test_sce.sh |
| 304 |
$ less Testing/Temporary/LastTest.log |
| 305 |
---- |
| 306 |
|
| 307 |
Results from testing will be printed on the stdout and detailed log file with |
| 308 |
your test results can be found in the `Testing/Temporary/LastTest.log` file. |
| 309 |
|
| 310 |
== Running the MITRE tests |
| 311 |
|
| 312 |
The MITRE tests (in `tests/mitre`) are functionality tests for several |
| 313 |
key probes. |
| 314 |
|
| 315 |
There are several reasons for putting these tests behind an `ENABLE_MITRE` |
| 316 |
CMake flag: they cannot be run in parallel as they race to create and |
| 317 |
delete temporary support files; they require port 25 to be open and |
| 318 |
listening (`mitre/test_mitre_linux_probes.sh`); there are outdated assumptions |
| 319 |
which need to be gated around operating system version checks; and a number of |
| 320 |
probes had to be disabled because they're not supported (e.g., SQL and LDAP |
| 321 |
checks, etc.). |
| 322 |
|
| 323 |
To run only the MITRE tests, please make sure you've installed and started a |
| 324 |
SMTP server that is listening on `127.0.0.1:25`, and that you're running on a |
| 325 |
RPM-based distribution. Then: |
| 326 |
|
| 327 |
---- |
| 328 |
$ cd build/ |
| 329 |
$ cmake -DENABLE_MITRE=TRUE .. |
| 330 |
$ make |
| 331 |
$ ctest --output-on-failure -R mitre |
| 332 |
---- |
| 333 |
|
| 334 |
To run the containerized MITRE tests (which installs a SMTP server and tests |
| 335 |
it): |
| 336 |
|
| 337 |
---- |
| 338 |
$ cd openscap/ |
| 339 |
$ docker build --tag openscap_mitre_tests:latest -f Dockerfiles/mitre_tests . |
| 340 |
$ docker run openscap_mitre_tests:latest |
| 341 |
---- |