Debian Codegraph @

doc/source/contributing/scripts_testing.rst

raw
1 Write Integration Tests for the Scripts
2 ---------------------------------------
3
4 {kiwi} comes with a set of helper functions that can be used in :file:`config.sh` (see
5 also: :ref:`working-with-kiwi-user-defined-scripts`). These functions utilize containers
6 to run the individual tasks and verify the final result.
7
8 Ensure that you have either :command:`podman` or :command:`docker` installed and
9 configured on your system. Podman is used by default. You can select
10 :command:`docker` instead by setting the environment variable
11 ``CONTAINER_RUNTIME`` to ``docker``. Then, you can run the integration tests via
12 the `test_scripts` Makefile target:
13
14 .. code:: shell-session
15
16 $ make test_scripts
17
18
19
20 The tests are written using the `pytest-container
21 <https://github.com/dcermak/pytest_container>`__ plugin. If applicable,
22 use the utility functions and fixtures of the plugin. For example, the
23 `auto_container` and `auto_container_per_test` fixtures in conjunction with
24 `testinfra <https://testinfra.readthedocs.io/>`__.
25
26
27 Test setup
28 ~~~~~~~~~~
29
30 The script tests can be run inside different containers specified in
31 :file:`test/scripts/conftest.py`. This file contains the `CONTAINERS` list
32 with all currently present images. These images are pulled and built as needed,
33 and the :file:`functions.sh` is copied into `/bin/`, so it is
34 available in `PATH`.
35
36 To use any of these containers, you can either define the global variable
37 `CONTAINER_IMAGES` in a test module and use the `auto_container` fixture, or
38 `parametrize <https://docs.pytest.org/en/stable/parametrize.html>`__ the
39 `container` fixture indirectly:
40
41 .. code:: python
42
43 @pytest.mark.parametrize("container_per_test", (TUMBLEWEED, LEAP_15_5), indirect=True)
44 def test_RmWorks(container_per_test):
45 # create the file /root/foobar
46 container_per_test.connection.run_expect([0], "touch /root/foobar")
47 assert container_per_test.connection.file("/root/foobar").exists
48
49 # source the functions and execute our function under test
50 container_per_test.connection.run_expect([0], ". /bin/functions.sh && Rm /root/foobar")
51
52 # verify the result
53 assert not container_per_test.connection.file("/root/foobar").exists
54
55
56 The example above uses the `_per_test` variant of the `container` fixture.
57 It ensures that the container is used only in a single test function. Use this
58 variant for tests that mutate the system under test because otherwise, it may
59 lead to race conditions that are difficult to debug. For tests that only perform
60 reads, you can omit the `_per_test` suffix so that the container environment can be
61 shared with other tests. This improves execution speed but comes at the
62 expense of safety in case of mutation.
63
64 For further information, refer to `pytest-container
65 <https://github.com/dcermak/pytest_container>`__.