|
2 | 2 |
|
3 | 3 | LTP shell API |
4 | 4 | ============= |
| 5 | + |
| 6 | +Shell API overview |
| 7 | +------------------ |
| 8 | + |
| 9 | +First lines of the shell test should be a shebang, a license, and copyrights. |
| 10 | + |
| 11 | +.. code-block:: shell |
| 12 | +
|
| 13 | + #!/bin/sh |
| 14 | + # SPDX-License-Identifier: GPL-2.0-or-later |
| 15 | + # Copyright 2099 Foo Bar <[email protected]> |
| 16 | +
|
| 17 | +A documentation comment block formatted in ReStructuredText should follow right |
| 18 | +after these lines. This comment is parsed and exported into the LTP |
| 19 | +documentation at https://linux-test-project.readthedocs.io/en/latest/users/test_catalog.html |
| 20 | + |
| 21 | +.. code-block:: shell |
| 22 | +
|
| 23 | + # --- |
| 24 | + # doc |
| 25 | + # Test for a foo bar. |
| 26 | + # |
| 27 | + # This test is testing foo by checking that bar is doing xyz. |
| 28 | + # --- |
| 29 | +
|
| 30 | +The shell loader test library uses the :doc:`../developers/api_c_tests` |
| 31 | +internally by parsing a special JSON formatted comment and |
| 32 | +initializing it accordingly. The JSON format is nearly 1:1 serialization of the |
| 33 | +:ref:`struct tst_test` into a JSON. The environment must be always preset even |
| 34 | +when it's empty. |
| 35 | + |
| 36 | +.. code-block:: shell |
| 37 | +
|
| 38 | + # --- |
| 39 | + # env |
| 40 | + # { |
| 41 | + # "needs_root": true, |
| 42 | + # "needs_tmpdir": true, |
| 43 | + # "needs_kconfigs": ["CONFIG_NUMA=y"], |
| 44 | + # "tags": { |
| 45 | + # ["linux-git", "432fd03240fa"] |
| 46 | + # } |
| 47 | + # } |
| 48 | +
|
| 49 | +After the documentation and environment has been laid out we finally import the |
| 50 | +:shell_lib:`tst_loader.sh`. This will, among other things, start the |
| 51 | +:shell_lib:`tst_run_shell.c` binary, that will parse the shell test environment |
| 52 | +comment and initialize the C test library accordingly. |
| 53 | + |
| 54 | +.. code-block:: shell |
| 55 | +
|
| 56 | + . tst_loader.sh |
| 57 | +
|
| 58 | +At this point everything has been set up and we can finally write the test |
| 59 | +function. The test results are reported by the usual functions :ref:`tst_res` and |
| 60 | +:ref:`tst_brk`. As in the C API these functions store results into a piece of shared |
| 61 | +memory as soon as they return so there is no need to propagate results event |
| 62 | +from child processes. |
| 63 | + |
| 64 | +.. code-block:: shell |
| 65 | +
|
| 66 | + tst_test() |
| 67 | + { |
| 68 | + tst_res TPASS "Bar enabled Foo" |
| 69 | + } |
| 70 | +
|
| 71 | +In order for the test to be actually executed the very last line of the script |
| 72 | +must source the :shell_lib:`tst_run.sh` script. |
| 73 | + |
| 74 | +.. code-block:: shell |
| 75 | +
|
| 76 | + . tst_run.sh |
| 77 | +
|
| 78 | +In order to run a test from a LTP tree a few directories has to be added to the |
| 79 | +`$PATH`. Note that the number of `../` may depend on the depth of the current |
| 80 | +directory relative to the LTP root. |
| 81 | + |
| 82 | +.. code-block:: shell |
| 83 | +
|
| 84 | + $ PATH=$PATH:$PWD:$PWD/../../lib/ ./foo01.sh |
| 85 | +
|
| 86 | +Test setup and cleanup |
| 87 | +---------------------- |
| 88 | + |
| 89 | +The test setup and cleanup functions are optional and passed via variables. |
| 90 | +Similarly to the C API the setup is executed exactly once at the start of the |
| 91 | +test and the test cleanup is executed at the test end or when test was |
| 92 | +interrupted by :ref:`tst_brk`. |
| 93 | + |
| 94 | + .. literalinclude:: ../../testcases/lib/tests/shell_loader_setup_cleanup.sh |
| 95 | + :language: shell |
0 commit comments