1f08c3bdfSopenharmony_ciLTP Shell Test API
2f08c3bdfSopenharmony_ci==================
3f08c3bdfSopenharmony_ci
4f08c3bdfSopenharmony_ciNOTE: See also
5f08c3bdfSopenharmony_ci      https://github.com/linux-test-project/ltp/wiki/Test-Writing-Guidelines[Test Writing Guidelines],
6f08c3bdfSopenharmony_ci      https://github.com/linux-test-project/ltp/wiki/C-Test-API[C Test API].
7f08c3bdfSopenharmony_ci
8f08c3bdfSopenharmony_ci1 Writing a testcase in shell
9f08c3bdfSopenharmony_ci-----------------------------
10f08c3bdfSopenharmony_ci
11f08c3bdfSopenharmony_ciLTP supports testcases to be written in a portable shell too.
12f08c3bdfSopenharmony_ci
13f08c3bdfSopenharmony_ciThere is a shell library modeled closely to the C interface at
14f08c3bdfSopenharmony_ci'testcases/lib/tst_test.sh'.
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_ciWARNING: All identifiers starting with 'TST_' or 'tst_' are reserved for the
17f08c3bdfSopenharmony_ci         test library.
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_ci1.1 Basic test interface
20f08c3bdfSopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_ci[source,sh]
23f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
24f08c3bdfSopenharmony_ci#!/bin/sh
25f08c3bdfSopenharmony_ci# SPDX-License-Identifier: GPL-2.0-or-later
26f08c3bdfSopenharmony_ci# This is a basic test for true shell builtin
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_ciTST_TESTFUNC=do_test
29f08c3bdfSopenharmony_ci. tst_test.sh
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_cido_test()
32f08c3bdfSopenharmony_ci{
33f08c3bdfSopenharmony_ci	true
34f08c3bdfSopenharmony_ci	ret=$?
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci	if [ $ret -eq 0 ]; then
37f08c3bdfSopenharmony_ci		tst_res TPASS "true returned 0"
38f08c3bdfSopenharmony_ci	else
39f08c3bdfSopenharmony_ci		tst_res TFAIL "true returned $ret"
40f08c3bdfSopenharmony_ci	fi
41f08c3bdfSopenharmony_ci}
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_citst_run
44f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_ciTIP: To execute this test the 'tst_test.sh' library must be in '$PATH'. If you
47f08c3bdfSopenharmony_ci     are executing the test from a git checkout you can run it as
48f08c3bdfSopenharmony_ci     'PATH="$PATH:../../lib" ./foo01.sh'
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_ciThe shell library expects test setup, cleanup and the test function executing
51f08c3bdfSopenharmony_cithe test in the '$TST_SETUP', '$TST_CLEANUP' and '$TST_TESTFUNC' variables.
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ciBoth '$TST_SETUP' and '$TST_CLEANUP' are optional.
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ciThe '$TST_TESTFUNC' may be called several times if more than one test
56f08c3bdfSopenharmony_ciiteration was requested by passing right command line options to the test.
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ciThe '$TST_CLEANUP' may be called even in the middle of the setup and must be
59f08c3bdfSopenharmony_ciable to clean up correctly even in this situation. The easiest solution for
60f08c3bdfSopenharmony_cithis is to keep track of what was initialized and act accordingly in the
61f08c3bdfSopenharmony_cicleanup.
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_ciWARNING: Similar to the C library, calling 'tst_brk' in the $TST_CLEANUP does
64f08c3bdfSopenharmony_ci         not exit the test and 'TBROK' is converted to 'TWARN'.
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ciNotice also the 'tst_run' shell API function called at the end of the test that
67f08c3bdfSopenharmony_ciactually starts the test.
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_ciWARNING: cleanup function is called only after 'tst_run' has been started.
70f08c3bdfSopenharmony_ciCalling 'tst_brk' in shell libraries, e.g. 'tst_test.sh' or 'tst_net.sh' does
71f08c3bdfSopenharmony_cinot trigger calling it.
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_ci[source,sh]
74f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
75f08c3bdfSopenharmony_ci#!/bin/sh
76f08c3bdfSopenharmony_ci# SPDX-License-Identifier: GPL-2.0-or-later
77f08c3bdfSopenharmony_ci# Example test with tests in separate functions
78f08c3bdfSopenharmony_ci
79f08c3bdfSopenharmony_ciTST_TESTFUNC=test
80f08c3bdfSopenharmony_ciTST_CNT=2
81f08c3bdfSopenharmony_ci. tst_test.sh
82f08c3bdfSopenharmony_ci
83f08c3bdfSopenharmony_citest1()
84f08c3bdfSopenharmony_ci{
85f08c3bdfSopenharmony_ci	tst_res TPASS "Test $1 passed"
86f08c3bdfSopenharmony_ci}
87f08c3bdfSopenharmony_ci
88f08c3bdfSopenharmony_citest2()
89f08c3bdfSopenharmony_ci{
90f08c3bdfSopenharmony_ci	tst_res TPASS "Test $1 passed"
91f08c3bdfSopenharmony_ci}
92f08c3bdfSopenharmony_ci
93f08c3bdfSopenharmony_citst_run
94f08c3bdfSopenharmony_ci# output:
95f08c3bdfSopenharmony_ci# foo 1 TPASS: Test 1 passed
96f08c3bdfSopenharmony_ci# foo 2 TPASS: Test 2 passed
97f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
98f08c3bdfSopenharmony_ci
99f08c3bdfSopenharmony_ciIf '$TST_CNT' is set, the test library looks if there are functions named
100f08c3bdfSopenharmony_ci'$\{TST_TESTFUNC\}1', ..., '$\{TST_TESTFUNC\}$\{TST_CNT\}' and if these are
101f08c3bdfSopenharmony_cifound they are executed one by one. The test number is passed to it in the '$1'.
102f08c3bdfSopenharmony_ci
103f08c3bdfSopenharmony_ci[source,sh]
104f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
105f08c3bdfSopenharmony_ci#!/bin/sh
106f08c3bdfSopenharmony_ci# SPDX-License-Identifier: GPL-2.0-or-later
107f08c3bdfSopenharmony_ci# Example test with tests in a single function
108f08c3bdfSopenharmony_ci
109f08c3bdfSopenharmony_ciTST_TESTFUNC=do_test
110f08c3bdfSopenharmony_ciTST_CNT=2
111f08c3bdfSopenharmony_ci. tst_test.sh
112f08c3bdfSopenharmony_ci
113f08c3bdfSopenharmony_cido_test()
114f08c3bdfSopenharmony_ci{
115f08c3bdfSopenharmony_ci	case $1 in
116f08c3bdfSopenharmony_ci	1) tst_res TPASS "Test $1 passed";;
117f08c3bdfSopenharmony_ci	2) tst_res TPASS "Test $1 passed";;
118f08c3bdfSopenharmony_ci	esac
119f08c3bdfSopenharmony_ci}
120f08c3bdfSopenharmony_ci
121f08c3bdfSopenharmony_citst_run
122f08c3bdfSopenharmony_ci# output:
123f08c3bdfSopenharmony_ci# foo 1 TPASS: Test 1 passed
124f08c3bdfSopenharmony_ci# foo 2 TPASS: Test 2 passed
125f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
126f08c3bdfSopenharmony_ci
127f08c3bdfSopenharmony_ciOtherwise, if '$TST_CNT' is set but there is no '$\{TST_TESTFUNC\}1', etc.,
128f08c3bdfSopenharmony_cithe '$TST_TESTFUNC' is executed '$TST_CNT' times and the test number is passed
129f08c3bdfSopenharmony_cito it in the '$1'.
130f08c3bdfSopenharmony_ci
131f08c3bdfSopenharmony_ci[source,sh]
132f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
133f08c3bdfSopenharmony_ci#!/bin/sh
134f08c3bdfSopenharmony_ci# SPDX-License-Identifier: GPL-2.0-or-later
135f08c3bdfSopenharmony_ci# Example test with tests in a single function, using $TST_TEST_DATA and
136f08c3bdfSopenharmony_ci# $TST_TEST_DATA_IFS
137f08c3bdfSopenharmony_ci
138f08c3bdfSopenharmony_ciTST_TESTFUNC=do_test
139f08c3bdfSopenharmony_ciTST_TEST_DATA="foo:bar:d dd"
140f08c3bdfSopenharmony_ciTST_TEST_DATA_IFS=":"
141f08c3bdfSopenharmony_ci. tst_test.sh
142f08c3bdfSopenharmony_ci
143f08c3bdfSopenharmony_cido_test()
144f08c3bdfSopenharmony_ci{
145f08c3bdfSopenharmony_ci	tst_res TPASS "Test $1 passed with data '$2'"
146f08c3bdfSopenharmony_ci}
147f08c3bdfSopenharmony_ci
148f08c3bdfSopenharmony_citst_run
149f08c3bdfSopenharmony_ci# output:
150f08c3bdfSopenharmony_ci# foo 1 TPASS: Test 1 passed with data 'foo'
151f08c3bdfSopenharmony_ci# foo 2 TPASS: Test 1 passed with data 'bar'
152f08c3bdfSopenharmony_ci# foo 3 TPASS: Test 1 passed with data 'd dd'
153f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
154f08c3bdfSopenharmony_ci
155f08c3bdfSopenharmony_ciIt's possible to pass data for function with '$TST_TEST_DATA'. Optional
156f08c3bdfSopenharmony_ci'$TST_TEST_DATA_IFS' is used for splitting, default value is space.
157f08c3bdfSopenharmony_ci
158f08c3bdfSopenharmony_ci[source,sh]
159f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
160f08c3bdfSopenharmony_ci#!/bin/sh
161f08c3bdfSopenharmony_ci# SPDX-License-Identifier: GPL-2.0-or-later
162f08c3bdfSopenharmony_ci# Example test with tests in a single function, using $TST_TEST_DATA and $TST_CNT
163f08c3bdfSopenharmony_ci
164f08c3bdfSopenharmony_ciTST_TESTFUNC=do_test
165f08c3bdfSopenharmony_ciTST_CNT=2
166f08c3bdfSopenharmony_ciTST_TEST_DATA="foo bar"
167f08c3bdfSopenharmony_ci. tst_test.sh
168f08c3bdfSopenharmony_ci
169f08c3bdfSopenharmony_cido_test()
170f08c3bdfSopenharmony_ci{
171f08c3bdfSopenharmony_ci	case $1 in
172f08c3bdfSopenharmony_ci	1) tst_res TPASS "Test $1 passed with data '$2'";;
173f08c3bdfSopenharmony_ci	2) tst_res TPASS "Test $1 passed with data '$2'";;
174f08c3bdfSopenharmony_ci	esac
175f08c3bdfSopenharmony_ci}
176f08c3bdfSopenharmony_ci
177f08c3bdfSopenharmony_citst_run
178f08c3bdfSopenharmony_ci# output:
179f08c3bdfSopenharmony_ci# foo 1 TPASS: Test 1 passed with data 'foo'
180f08c3bdfSopenharmony_ci# foo 2 TPASS: Test 2 passed with data 'foo'
181f08c3bdfSopenharmony_ci# foo 3 TPASS: Test 1 passed with data 'bar'
182f08c3bdfSopenharmony_ci# foo 4 TPASS: Test 2 passed with data 'bar'
183f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
184f08c3bdfSopenharmony_ci
185f08c3bdfSopenharmony_ci'$TST_TEST_DATA' can be used with '$TST_CNT'. If '$TST_TEST_DATA_IFS' not specified,
186f08c3bdfSopenharmony_cispace as default value is used. Of course, it's possible to use separate functions.
187f08c3bdfSopenharmony_ci
188f08c3bdfSopenharmony_ci1.2 Library environment variables and functions for shell
189f08c3bdfSopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
190f08c3bdfSopenharmony_ci
191f08c3bdfSopenharmony_ciSimilarily to the C library various checks and preparations can be requested
192f08c3bdfSopenharmony_cisimply by setting right '$TST_NEEDS_FOO'.
193f08c3bdfSopenharmony_ci
194f08c3bdfSopenharmony_ci[options="header"]
195f08c3bdfSopenharmony_ci|=============================================================================
196f08c3bdfSopenharmony_ci| Variable name            | Action done
197f08c3bdfSopenharmony_ci| 'TST_NEEDS_ROOT'         | Exit the test with 'TCONF' unless executed under root.
198f08c3bdfSopenharmony_ci|                          | Alternatively the 'tst_require_root' command can be used.
199f08c3bdfSopenharmony_ci| 'TST_NEEDS_TMPDIR'       | Create test temporary directory and cd into it.
200f08c3bdfSopenharmony_ci| 'TST_NEEDS_DEVICE'       | Prepare test temporary device, the path to testing
201f08c3bdfSopenharmony_ci                             device is stored in '$TST_DEVICE' variable.
202f08c3bdfSopenharmony_ci                             The option implies 'TST_NEEDS_TMPDIR'.
203f08c3bdfSopenharmony_ci| 'TST_NEEDS_CMDS'         | String with command names that has to be present for
204f08c3bdfSopenharmony_ci                             the test (see below).
205f08c3bdfSopenharmony_ci| 'TST_NEEDS_MODULE'       | Test module name needed for the test (see below).
206f08c3bdfSopenharmony_ci| 'TST_NEEDS_DRIVERS'      | Checks kernel drivers support for the test.
207f08c3bdfSopenharmony_ci| 'TST_NEEDS_KCONFIGS'     | Checks kernel kconfigs support for the test (see below).
208f08c3bdfSopenharmony_ci| 'TST_NEEDS_KCONFIGS_IFS' | Used for splitting '$TST_NEEDS_KCONFIGS' variable,
209f08c3bdfSopenharmony_ci                             default value is comma, it only supports single character.
210f08c3bdfSopenharmony_ci| 'TST_TIMEOUT'            | Maximum timeout set for the test in sec. Must be int >= 1,
211f08c3bdfSopenharmony_ci                             or -1 (special value to disable timeout), default is 300.
212f08c3bdfSopenharmony_ci                             Variable is meant be set in tests, not by user.
213f08c3bdfSopenharmony_ci                             It's an equivalent of `tst_test.timeout` in C, can be set
214f08c3bdfSopenharmony_ci                             via 'tst_set_timeout(timeout)' after test has started.
215f08c3bdfSopenharmony_ci|=============================================================================
216f08c3bdfSopenharmony_ci
217f08c3bdfSopenharmony_ci[options="header"]
218f08c3bdfSopenharmony_ci|=============================================================================
219f08c3bdfSopenharmony_ci| Function name              | Action done
220f08c3bdfSopenharmony_ci| 'tst_set_timeout(timeout)' | Maximum timeout set for the test in sec.
221f08c3bdfSopenharmony_ci                               See 'TST_TIMEOUT' variable.
222f08c3bdfSopenharmony_ci|=============================================================================
223f08c3bdfSopenharmony_ci
224f08c3bdfSopenharmony_ciNOTE: Network tests (see testcases/network/README.md) use additional variables
225f08c3bdfSopenharmony_ciand functions in 'tst_net.sh'.
226f08c3bdfSopenharmony_ci
227f08c3bdfSopenharmony_ciChecking for presence of commands
228f08c3bdfSopenharmony_ci+++++++++++++++++++++++++++++++++
229f08c3bdfSopenharmony_ci
230f08c3bdfSopenharmony_ci[source,sh]
231f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
232f08c3bdfSopenharmony_ci#!/bin/sh
233f08c3bdfSopenharmony_ci
234f08c3bdfSopenharmony_ci...
235f08c3bdfSopenharmony_ci
236f08c3bdfSopenharmony_ciTST_NEEDS_CMDS="modinfo modprobe"
237f08c3bdfSopenharmony_ci. tst_test.sh
238f08c3bdfSopenharmony_ci
239f08c3bdfSopenharmony_ci...
240f08c3bdfSopenharmony_ci
241f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
242f08c3bdfSopenharmony_ci
243f08c3bdfSopenharmony_ciSetting '$TST_NEEDS_CMDS' to a string listing required commands will check for
244f08c3bdfSopenharmony_ciexistence each of them and exits the test with 'TCONF' on first missing.
245f08c3bdfSopenharmony_ci
246f08c3bdfSopenharmony_ciAlternatively the 'tst_require_cmds()' function can be used to do the same on
247f08c3bdfSopenharmony_ciruntime, since sometimes we need to the check at runtime too.
248f08c3bdfSopenharmony_ci
249f08c3bdfSopenharmony_ci'tst_check_cmds()' can be used for requirements just for a particular test
250f08c3bdfSopenharmony_cias it doesn't exit (it issues 'tst_res TCONF'). Expected usage is:
251f08c3bdfSopenharmony_ci
252f08c3bdfSopenharmony_ci[source,sh]
253f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
254f08c3bdfSopenharmony_ci#!/bin/sh
255f08c3bdfSopenharmony_ci
256f08c3bdfSopenharmony_ciTST_TESTFUNC=do_test
257f08c3bdfSopenharmony_ci. tst_test.sh
258f08c3bdfSopenharmony_ci
259f08c3bdfSopenharmony_cido_test()
260f08c3bdfSopenharmony_ci{
261f08c3bdfSopenharmony_ci	tst_check_cmds cmd || return
262f08c3bdfSopenharmony_ci	cmd --foo
263f08c3bdfSopenharmony_ci	...
264f08c3bdfSopenharmony_ci}
265f08c3bdfSopenharmony_ci
266f08c3bdfSopenharmony_citst_run
267f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
268f08c3bdfSopenharmony_ci
269f08c3bdfSopenharmony_ciLocating kernel modules
270f08c3bdfSopenharmony_ci+++++++++++++++++++++++
271f08c3bdfSopenharmony_ci
272f08c3bdfSopenharmony_ciThe LTP build system can build kernel modules as well, setting
273f08c3bdfSopenharmony_ci'$TST_NEEDS_MODULE' to module name will cause the library to look for the
274f08c3bdfSopenharmony_cimodule in a few possible paths.
275f08c3bdfSopenharmony_ci
276f08c3bdfSopenharmony_ciIf module was found the path to it will be stored into '$TST_MODPATH'
277f08c3bdfSopenharmony_civariable, if module wasn't found the test will exit with 'TCONF'.
278f08c3bdfSopenharmony_ci
279f08c3bdfSopenharmony_ciAlternatively the 'tst_require_module()' function can be used to do the same
280f08c3bdfSopenharmony_ciat runtime.
281f08c3bdfSopenharmony_ci
282f08c3bdfSopenharmony_ci1.3 Optional command line parameters
283f08c3bdfSopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
284f08c3bdfSopenharmony_ci
285f08c3bdfSopenharmony_ci[source,sh]
286f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
287f08c3bdfSopenharmony_ci#!/bin/sh
288f08c3bdfSopenharmony_ci# SPDX-License-Identifier: GPL-2.0-or-later
289f08c3bdfSopenharmony_ci# Optional test command line parameters
290f08c3bdfSopenharmony_ci
291f08c3bdfSopenharmony_ciTST_OPTS="af:"
292f08c3bdfSopenharmony_ciTST_USAGE=usage
293f08c3bdfSopenharmony_ciTST_PARSE_ARGS=parse_args
294f08c3bdfSopenharmony_ciTST_TESTFUNC=do_test
295f08c3bdfSopenharmony_ci
296f08c3bdfSopenharmony_ci. tst_test.sh
297f08c3bdfSopenharmony_ci
298f08c3bdfSopenharmony_ciALTERNATIVE=0
299f08c3bdfSopenharmony_ciMODE="foo"
300f08c3bdfSopenharmony_ci
301f08c3bdfSopenharmony_ciusage()
302f08c3bdfSopenharmony_ci{
303f08c3bdfSopenharmony_ci	cat << EOF
304f08c3bdfSopenharmony_ciusage: $0 [-a] [-f <foo|bar>]
305f08c3bdfSopenharmony_ci
306f08c3bdfSopenharmony_ciOPTIONS
307f08c3bdfSopenharmony_ci-a     Enable support for alternative foo
308f08c3bdfSopenharmony_ci-f     Specify foo or bar mode
309f08c3bdfSopenharmony_ciEOF
310f08c3bdfSopenharmony_ci}
311f08c3bdfSopenharmony_ci
312f08c3bdfSopenharmony_ciparse_args()
313f08c3bdfSopenharmony_ci{
314f08c3bdfSopenharmony_ci	case $1 in
315f08c3bdfSopenharmony_ci	a) ALTERNATIVE=1;;
316f08c3bdfSopenharmony_ci	f) MODE="$2";;
317f08c3bdfSopenharmony_ci	esac
318f08c3bdfSopenharmony_ci}
319f08c3bdfSopenharmony_ci
320f08c3bdfSopenharmony_cido_test()
321f08c3bdfSopenharmony_ci{
322f08c3bdfSopenharmony_ci	...
323f08c3bdfSopenharmony_ci}
324f08c3bdfSopenharmony_ci
325f08c3bdfSopenharmony_citst_run
326f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
327f08c3bdfSopenharmony_ci
328f08c3bdfSopenharmony_ciThe 'getopts' string for optional parameters is passed in the '$TST_OPTS'
329f08c3bdfSopenharmony_civariable. There are a few default parameters that cannot be used by a test,
330f08c3bdfSopenharmony_cithese can be listed with passing help '-h' option to any test.
331f08c3bdfSopenharmony_ci
332f08c3bdfSopenharmony_ciThe function that prints the usage is passed in '$TST_USAGE', the help for
333f08c3bdfSopenharmony_cithe options implemented in the library is appended when usage is printed.
334f08c3bdfSopenharmony_ci
335f08c3bdfSopenharmony_ciLastly the function '$PARSE_ARGS' is called with the option name in the '$1'
336f08c3bdfSopenharmony_ciand, if option has argument, its value in the '$2'.
337f08c3bdfSopenharmony_ci
338f08c3bdfSopenharmony_ci[source,sh]
339f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
340f08c3bdfSopenharmony_ci#!/bin/sh
341f08c3bdfSopenharmony_ci# SPDX-License-Identifier: GPL-2.0-or-later
342f08c3bdfSopenharmony_ci# Optional test positional parameters
343f08c3bdfSopenharmony_ci
344f08c3bdfSopenharmony_ciTST_POS_ARGS=3
345f08c3bdfSopenharmony_ciTST_USAGE=usage
346f08c3bdfSopenharmony_ciTST_TESTFUNC=do_test
347f08c3bdfSopenharmony_ci
348f08c3bdfSopenharmony_ci. tst_test.sh
349f08c3bdfSopenharmony_ci
350f08c3bdfSopenharmony_ciusage()
351f08c3bdfSopenharmony_ci{
352f08c3bdfSopenharmony_ci	cat << EOF
353f08c3bdfSopenharmony_ciusage: $0 [min] [max] [size]
354f08c3bdfSopenharmony_ci
355f08c3bdfSopenharmony_ciEOF
356f08c3bdfSopenharmony_ci}
357f08c3bdfSopenharmony_ci
358f08c3bdfSopenharmony_cimin="$1"
359f08c3bdfSopenharmony_cimax="$2"
360f08c3bdfSopenharmony_cisize="$3"
361f08c3bdfSopenharmony_ci
362f08c3bdfSopenharmony_cido_test()
363f08c3bdfSopenharmony_ci{
364f08c3bdfSopenharmony_ci	...
365f08c3bdfSopenharmony_ci}
366f08c3bdfSopenharmony_ci
367f08c3bdfSopenharmony_citst_run
368f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
369f08c3bdfSopenharmony_ci
370f08c3bdfSopenharmony_ciYou can also request a number of positional parameters by setting the
371f08c3bdfSopenharmony_ci'$TST_POS_ARGS' variable. If you do, these will be available as they were
372f08c3bdfSopenharmony_cipassed directly to the script in '$1', '$2', ..., '$n'.
373f08c3bdfSopenharmony_ci
374f08c3bdfSopenharmony_ci1.4 Useful library functions
375f08c3bdfSopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
376f08c3bdfSopenharmony_ci
377f08c3bdfSopenharmony_ciRetrieving configuration variables
378f08c3bdfSopenharmony_ci++++++++++++++++++++++++++++++++++
379f08c3bdfSopenharmony_ci
380f08c3bdfSopenharmony_ciYou may need to retrieve configuration values such as PAGESIZE, there is
381f08c3bdfSopenharmony_ci'getconf' but as some system may not have it, you are advised to use
382f08c3bdfSopenharmony_ci'tst_getconf' instead. Note that it implements subset of 'getconf'
383f08c3bdfSopenharmony_cisystem variables used by the testcases only.
384f08c3bdfSopenharmony_ci
385f08c3bdfSopenharmony_ci[source,sh]
386f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
387f08c3bdfSopenharmony_ci# retrieve PAGESIZE
388f08c3bdfSopenharmony_cipagesize=`tst_getconf PAGESIZE`
389f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
390f08c3bdfSopenharmony_ci
391f08c3bdfSopenharmony_ciSleeping for subsecond intervals
392f08c3bdfSopenharmony_ci++++++++++++++++++++++++++++++++
393f08c3bdfSopenharmony_ci
394f08c3bdfSopenharmony_ciAlbeit there is a sleep command available basically everywhere not all
395f08c3bdfSopenharmony_ciimplementations can support sleeping for less than one second. And most of the
396f08c3bdfSopenharmony_citime sleeping for a second is too much. Therefore LTP includes 'tst_sleep'
397f08c3bdfSopenharmony_cithat can sleep for defined amount of seconds, milliseconds or microseconds.
398f08c3bdfSopenharmony_ci
399f08c3bdfSopenharmony_ci[source,sh]
400f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
401f08c3bdfSopenharmony_ci# sleep for 100 milliseconds
402f08c3bdfSopenharmony_citst_sleep 100ms
403f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
404f08c3bdfSopenharmony_ci
405f08c3bdfSopenharmony_ciRetry a function call multiple times
406f08c3bdfSopenharmony_ci++++++++++++++++++++++++++++++++++++
407f08c3bdfSopenharmony_ci
408f08c3bdfSopenharmony_ciSometimes an LTP test needs to retry a function call multiple times because
409f08c3bdfSopenharmony_cithe system is not ready to process it successfully on the first try. The LTP
410f08c3bdfSopenharmony_cilibrary has useful tools to handle the call retry automatically.
411f08c3bdfSopenharmony_ci'TST_RETRY_FUNC()' will keep retrying for up to 1 second. If you want a custom
412f08c3bdfSopenharmony_citime limit use 'TST_RETRY_FN_EXP_BACKOFF()'. Both methods return the value
413f08c3bdfSopenharmony_cireturned by the last 'FUNC' call.
414f08c3bdfSopenharmony_ci
415f08c3bdfSopenharmony_ciThe delay between retries starts at 1 microsecond and doubles after each call.
416f08c3bdfSopenharmony_ciThe retry loop ends when the function call succeeds or when the next delay
417f08c3bdfSopenharmony_ciexceeds the specified time (1 second for 'TST_RETRY_FUNC()'). The maximum
418f08c3bdfSopenharmony_cidelay is multiplied by TST_TIMEOUT_MUL. The total cumulative delay may be up
419f08c3bdfSopenharmony_cito twice as long as the adjusted maximum delay.
420f08c3bdfSopenharmony_ci
421f08c3bdfSopenharmony_ciThe C version of 'TST_RETRY_FUNC()' is a macro which takes two arguments:
422f08c3bdfSopenharmony_ci
423f08c3bdfSopenharmony_ci* 'FUNC' is the complete function call with arguments which should be retried
424f08c3bdfSopenharmony_ci  multiple times.
425f08c3bdfSopenharmony_ci* 'SUCCESS_CHECK' is a macro or function which will validate 'FUNC' return
426f08c3bdfSopenharmony_ci  value. 'FUNC' call was successful if 'SUCCESS_CHECK(ret)' evaluates to
427f08c3bdfSopenharmony_ci  non-zero.
428f08c3bdfSopenharmony_ci
429f08c3bdfSopenharmony_ciBoth retry methods clear 'errno' before every 'FUNC' call so your
430f08c3bdfSopenharmony_ci'SUCCESS_CHECK' can look for specific error codes as well. The LTP library
431f08c3bdfSopenharmony_cialso includes predefined 'SUCCESS_CHECK' macros for the most common call
432f08c3bdfSopenharmony_ciconventions:
433f08c3bdfSopenharmony_ci
434f08c3bdfSopenharmony_ci* 'TST_RETVAL_EQ0()' - The call was successful if 'FUNC' returned 0 or NULL
435f08c3bdfSopenharmony_ci* 'TST_RETVAL_NOTNULL()' - The call was successful if 'FUNC' returned any
436f08c3bdfSopenharmony_ci  value other than 0 or NULL.
437f08c3bdfSopenharmony_ci* 'TST_RETVAL_GE0()' - The call was successful if 'FUNC' returned value >= 0.
438f08c3bdfSopenharmony_ci
439f08c3bdfSopenharmony_ci[source,c]
440f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
441f08c3bdfSopenharmony_ci/* Keep trying for 1 second */
442f08c3bdfSopenharmony_ciTST_RETRY_FUNC(FUNC, SUCCESS_CHECK)
443f08c3bdfSopenharmony_ci
444f08c3bdfSopenharmony_ci/* Keep trying for up to 2*N seconds */
445f08c3bdfSopenharmony_ciTST_RETRY_FN_EXP_BACKOFF(FUNC, SUCCESS_CHECK, N)
446f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
447f08c3bdfSopenharmony_ci
448f08c3bdfSopenharmony_ciThe shell version of 'TST_RETRY_FUNC()' is simpler and takes slightly
449f08c3bdfSopenharmony_cidifferent arguments:
450f08c3bdfSopenharmony_ci
451f08c3bdfSopenharmony_ci* 'FUNC' is a string containing the complete function or program call with
452f08c3bdfSopenharmony_ci  arguments.
453f08c3bdfSopenharmony_ci* 'EXPECTED_RET' is a single expected return value. 'FUNC' call was successful
454f08c3bdfSopenharmony_ci  if the return value is equal to EXPECTED_RET.
455f08c3bdfSopenharmony_ci
456f08c3bdfSopenharmony_ci[source,sh]
457f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
458f08c3bdfSopenharmony_ci# Keep trying for 1 second
459f08c3bdfSopenharmony_ciTST_RETRY_FUNC "FUNC arg1 arg2 ..." "EXPECTED_RET"
460f08c3bdfSopenharmony_ci
461f08c3bdfSopenharmony_ci# Keep trying for up to 2*N seconds
462f08c3bdfSopenharmony_ciTST_RETRY_FN_EXP_BACKOFF "FUNC arg1 arg2 ..." "EXPECTED_RET" "N"
463f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
464f08c3bdfSopenharmony_ci
465f08c3bdfSopenharmony_ciChecking for integers
466f08c3bdfSopenharmony_ci+++++++++++++++++++++
467f08c3bdfSopenharmony_ci
468f08c3bdfSopenharmony_ci[source,sh]
469f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
470f08c3bdfSopenharmony_ci# returns zero if passed an integer parameter, non-zero otherwise
471f08c3bdfSopenharmony_citst_is_int "$FOO"
472f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
473f08c3bdfSopenharmony_ci
474f08c3bdfSopenharmony_ciChecking for integers and floating point numbers
475f08c3bdfSopenharmony_ci++++++++++++++++++++++++++++++++++++++++++++++++
476f08c3bdfSopenharmony_ci
477f08c3bdfSopenharmony_ci[source,sh]
478f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
479f08c3bdfSopenharmony_ci# returns zero if passed an integer or floating point number parameter,
480f08c3bdfSopenharmony_ci# non-zero otherwise
481f08c3bdfSopenharmony_citst_is_num "$FOO"
482f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
483f08c3bdfSopenharmony_ci
484f08c3bdfSopenharmony_ciObtaining random numbers
485f08c3bdfSopenharmony_ci++++++++++++++++++++++++
486f08c3bdfSopenharmony_ci
487f08c3bdfSopenharmony_ciThere is no '$RANDOM' in portable shell, use 'tst_random' instead.
488f08c3bdfSopenharmony_ci
489f08c3bdfSopenharmony_ci[source,sh]
490f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
491f08c3bdfSopenharmony_ci# get random integer between 0 and 1000 (including 0 and 1000)
492f08c3bdfSopenharmony_citst_random 0 1000
493f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
494f08c3bdfSopenharmony_ci
495f08c3bdfSopenharmony_ciFormatting device with a filesystem
496f08c3bdfSopenharmony_ci+++++++++++++++++++++++++++++++++++
497f08c3bdfSopenharmony_ci
498f08c3bdfSopenharmony_ciThe 'tst_mkfs' helper will format device with the filesystem.
499f08c3bdfSopenharmony_ci
500f08c3bdfSopenharmony_ci[source,sh]
501f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
502f08c3bdfSopenharmony_ci# format test device with ext2
503f08c3bdfSopenharmony_citst_mkfs ext2 $TST_DEVICE
504f08c3bdfSopenharmony_ci# default params are $TST_FS_TYPE $TST_DEVICE
505f08c3bdfSopenharmony_citst_mkfs
506f08c3bdfSopenharmony_ci# optional parameters
507f08c3bdfSopenharmony_citst_mkfs ext4 /dev/device -T largefile
508f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
509f08c3bdfSopenharmony_ci
510f08c3bdfSopenharmony_ciMounting and unmounting filesystems
511f08c3bdfSopenharmony_ci+++++++++++++++++++++++++++++++++++
512f08c3bdfSopenharmony_ci
513f08c3bdfSopenharmony_ciThe 'tst_mount' and 'tst_umount' helpers are a safe way to mount/umount
514f08c3bdfSopenharmony_cia filesystem.
515f08c3bdfSopenharmony_ci
516f08c3bdfSopenharmony_ciThe 'tst_mount' mounts '$TST_DEVICE' of '$TST_FS_TYPE' (optional) to
517f08c3bdfSopenharmony_ci'$TST_MNTPOINT' (defaults to mntpoint), optionally using the
518f08c3bdfSopenharmony_ci'$TST_MNT_PARAMS'. The '$TST_MNTPOINT' directory is created if it didn't
519f08c3bdfSopenharmony_ciexist prior to the function call.
520f08c3bdfSopenharmony_ci
521f08c3bdfSopenharmony_ciIf the path passed (optional, must be absolute path, defaults to '$TST_MNTPOINT')
522f08c3bdfSopenharmony_cito the 'tst_umount' is not mounted (present in '/proc/mounts') it's noop.
523f08c3bdfSopenharmony_ciOtherwise it retries to umount the filesystem a few times on failure.
524f08c3bdfSopenharmony_ciThis is a workaround since there are daemons dumb enough to probe all newly
525f08c3bdfSopenharmony_cimounted filesystems, and prevents them from being umounted shortly after they
526f08c3bdfSopenharmony_ciwere mounted.
527f08c3bdfSopenharmony_ci
528f08c3bdfSopenharmony_ciROD and ROD_SILENT
529f08c3bdfSopenharmony_ci++++++++++++++++++
530f08c3bdfSopenharmony_ci
531f08c3bdfSopenharmony_ciThese functions supply the 'SAFE_MACROS' used in C although they work and are
532f08c3bdfSopenharmony_cinamed differently.
533f08c3bdfSopenharmony_ci
534f08c3bdfSopenharmony_ci[source,sh]
535f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
536f08c3bdfSopenharmony_ciROD_SILENT command arg1 arg2 ...
537f08c3bdfSopenharmony_ci
538f08c3bdfSopenharmony_ci# is shorthand for:
539f08c3bdfSopenharmony_ci
540f08c3bdfSopenharmony_cicommand arg1 arg2 ... > /dev/null 2>&1
541f08c3bdfSopenharmony_ciif [ $? -ne 0 ]; then
542f08c3bdfSopenharmony_ci        tst_brk TBROK "..."
543f08c3bdfSopenharmony_cifi
544f08c3bdfSopenharmony_ci
545f08c3bdfSopenharmony_ci
546f08c3bdfSopenharmony_ciROD command arg1 arg2 ...
547f08c3bdfSopenharmony_ci
548f08c3bdfSopenharmony_ci# is shorthand for:
549f08c3bdfSopenharmony_ci
550f08c3bdfSopenharmony_ciROD arg1 arg2 ...
551f08c3bdfSopenharmony_ciif [ $? -ne 0 ]; then
552f08c3bdfSopenharmony_ci        tst_brk TBROK "..."
553f08c3bdfSopenharmony_cifi
554f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
555f08c3bdfSopenharmony_ci
556f08c3bdfSopenharmony_ciWARNING: Keep in mind that output redirection (to a file) happens in the
557f08c3bdfSopenharmony_ci         caller rather than in the ROD function and cannot be checked for
558f08c3bdfSopenharmony_ci         write errors by the ROD function.
559f08c3bdfSopenharmony_ci
560f08c3bdfSopenharmony_ciAs a matter of a fact doing +ROD echo a > /proc/cpuinfo+ would work just fine
561f08c3bdfSopenharmony_cisince the 'ROD' function will only get the +echo a+ part that will run just
562f08c3bdfSopenharmony_cifine.
563f08c3bdfSopenharmony_ci
564f08c3bdfSopenharmony_ci[source,sh]
565f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
566f08c3bdfSopenharmony_ci# Redirect output to a file with ROD
567f08c3bdfSopenharmony_ciROD echo foo \> bar
568f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
569f08c3bdfSopenharmony_ci
570f08c3bdfSopenharmony_ciNote the '>' is escaped with '\', this causes that the '>' and filename are
571f08c3bdfSopenharmony_cipassed to the 'ROD' function as parameters and the 'ROD' function contains
572f08c3bdfSopenharmony_cicode to split '$@' on '>' and redirects the output to the file.
573f08c3bdfSopenharmony_ci
574f08c3bdfSopenharmony_ciEXPECT_PASS{,_BRK} and EXPECT_FAIL{,_BRK}
575f08c3bdfSopenharmony_ci+++++++++++++++++++++++++++++++++++++++++
576f08c3bdfSopenharmony_ci
577f08c3bdfSopenharmony_ci[source,sh]
578f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
579f08c3bdfSopenharmony_ciEXPECT_PASS command arg1 arg2 ... [ \> file ]
580f08c3bdfSopenharmony_ciEXPECT_FAIL command arg1 arg2 ... [ \> file ]
581f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
582f08c3bdfSopenharmony_ci
583f08c3bdfSopenharmony_ci'EXPECT_PASS' calls 'tst_res TPASS' if the command exited with 0 exit code,
584f08c3bdfSopenharmony_ciand 'tst_res TFAIL' otherwise. 'EXPECT_FAIL' does vice versa.
585f08c3bdfSopenharmony_ci
586f08c3bdfSopenharmony_ciOutput redirection rules are the same as for the 'ROD' function. In addition
587f08c3bdfSopenharmony_cito that, 'EXPECT_FAIL' always redirects the command's stderr to '/dev/null'.
588f08c3bdfSopenharmony_ci
589f08c3bdfSopenharmony_ciThere are also 'EXPECT_PASS_BRK' and 'EXPECT_FAIL_BRK', which works the same way
590f08c3bdfSopenharmony_ciexcept breaking a test when unexpected action happen.
591f08c3bdfSopenharmony_ci
592f08c3bdfSopenharmony_ciIt's possible to detect whether expected value happened:
593f08c3bdfSopenharmony_ci[source,sh]
594f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
595f08c3bdfSopenharmony_ciif ! EXPECT_PASS command arg1 2\> /dev/null; then
596f08c3bdfSopenharmony_ci	continue
597f08c3bdfSopenharmony_cifi
598f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
599f08c3bdfSopenharmony_ci
600f08c3bdfSopenharmony_citst_kvcmp
601f08c3bdfSopenharmony_ci+++++++++
602f08c3bdfSopenharmony_ci
603f08c3bdfSopenharmony_ciThis command compares the currently running kernel version given conditions
604f08c3bdfSopenharmony_ciwith syntax similar to the shell test command.
605f08c3bdfSopenharmony_ci
606f08c3bdfSopenharmony_ci[source,sh]
607f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
608f08c3bdfSopenharmony_ci# Exit the test if kernel version is older or equal to 2.6.8
609f08c3bdfSopenharmony_ciif tst_kvcmp -le 2.6.8; then
610f08c3bdfSopenharmony_ci	tst_brk TCONF "Kernel newer than 2.6.8 is needed"
611f08c3bdfSopenharmony_cifi
612f08c3bdfSopenharmony_ci
613f08c3bdfSopenharmony_ci# Exit the test if kernel is newer than 3.8 and older than 4.0.1
614f08c3bdfSopenharmony_ciif tst_kvcmp -gt 3.8 -a -lt 4.0.1; then
615f08c3bdfSopenharmony_ci	tst_brk TCONF "Kernel must be older than 3.8 or newer than 4.0.1"
616f08c3bdfSopenharmony_cifi
617f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
618f08c3bdfSopenharmony_ci
619f08c3bdfSopenharmony_ci[options="header"]
620f08c3bdfSopenharmony_ci|=======================================================================
621f08c3bdfSopenharmony_ci| expression | description
622f08c3bdfSopenharmony_ci| -eq kver   | Returns true if kernel version is equal
623f08c3bdfSopenharmony_ci| -ne kver   | Returns true if kernel version is not equal
624f08c3bdfSopenharmony_ci| -gt kver   | Returns true if kernel version is greater
625f08c3bdfSopenharmony_ci| -ge kver   | Returns true if kernel version is greater or equal
626f08c3bdfSopenharmony_ci| -lt kver   | Returns true if kernel version is lesser
627f08c3bdfSopenharmony_ci| -le kver   | Returns true if kernel version is lesser or equal
628f08c3bdfSopenharmony_ci| -a         | Does logical and between two expressions
629f08c3bdfSopenharmony_ci| -o         | Does logical or between two expressions
630f08c3bdfSopenharmony_ci|=======================================================================
631f08c3bdfSopenharmony_ci
632f08c3bdfSopenharmony_ciThe format for kernel version has to either be with one dot e.g. '2.6' or with
633f08c3bdfSopenharmony_citwo dots e.g. '4.8.1'.
634f08c3bdfSopenharmony_ci
635f08c3bdfSopenharmony_ci.tst_fs_has_free
636f08c3bdfSopenharmony_ci[source,sh]
637f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
638f08c3bdfSopenharmony_ci#!/bin/sh
639f08c3bdfSopenharmony_ci
640f08c3bdfSopenharmony_ci...
641f08c3bdfSopenharmony_ci
642f08c3bdfSopenharmony_ci# whether current directory has 100MB free space at least.
643f08c3bdfSopenharmony_ciif ! tst_fs_has_free . 100MB; then
644f08c3bdfSopenharmony_ci	tst_brkm TCONF "Not enough free space"
645f08c3bdfSopenharmony_cifi
646f08c3bdfSopenharmony_ci
647f08c3bdfSopenharmony_ci...
648f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
649f08c3bdfSopenharmony_ci
650f08c3bdfSopenharmony_ciThe 'tst_fs_has_free' shell interface returns 0 if the specified free space is
651f08c3bdfSopenharmony_cisatisfied, 1 if not, and 2 on error.
652f08c3bdfSopenharmony_ci
653f08c3bdfSopenharmony_ciThe second argument supports suffixes kB, MB and GB, the default unit is Byte.
654f08c3bdfSopenharmony_ci
655f08c3bdfSopenharmony_ci.tst_retry
656f08c3bdfSopenharmony_ci[source,sh]
657f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
658f08c3bdfSopenharmony_ci#!/bin/sh
659f08c3bdfSopenharmony_ci
660f08c3bdfSopenharmony_ci...
661f08c3bdfSopenharmony_ci
662f08c3bdfSopenharmony_ci# Retry ping command three times
663f08c3bdfSopenharmony_citst_retry "ping -c 1 127.0.0.1"
664f08c3bdfSopenharmony_ci
665f08c3bdfSopenharmony_ciif [ $? -ne 0 ]; then
666f08c3bdfSopenharmony_ci	tst_resm TFAIL "Failed to ping 127.0.0.1"
667f08c3bdfSopenharmony_cielse
668f08c3bdfSopenharmony_ci	tst_resm TPASS "Successfully pinged 127.0.0.1"
669f08c3bdfSopenharmony_cifi
670f08c3bdfSopenharmony_ci
671f08c3bdfSopenharmony_ci...
672f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
673f08c3bdfSopenharmony_ci
674f08c3bdfSopenharmony_ciThe 'tst_retry' function allows you to retry a command after waiting small
675f08c3bdfSopenharmony_ciamount of time until it succeeds or until given amount of retries has been
676f08c3bdfSopenharmony_cireached (default is three attempts).
677f08c3bdfSopenharmony_ci
678f08c3bdfSopenharmony_ci1.5 Restarting daemons
679f08c3bdfSopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~
680f08c3bdfSopenharmony_ci
681f08c3bdfSopenharmony_ciRestarting system daemons is a complicated task for two reasons.
682f08c3bdfSopenharmony_ci
683f08c3bdfSopenharmony_ci* There are different init systems
684f08c3bdfSopenharmony_ci  (SysV init, systemd, etc...)
685f08c3bdfSopenharmony_ci
686f08c3bdfSopenharmony_ci* Daemon names are not unified between distributions
687f08c3bdfSopenharmony_ci  (apache vs httpd, cron vs crond, various syslog variations)
688f08c3bdfSopenharmony_ci
689f08c3bdfSopenharmony_ciTo solve these problems LTP has 'testcases/lib/daemonlib.sh' library that
690f08c3bdfSopenharmony_ciprovides functions to start/stop/query daemons as well as variables that store
691f08c3bdfSopenharmony_cicorrect daemon name.
692f08c3bdfSopenharmony_ci
693f08c3bdfSopenharmony_ci.Supported operations
694f08c3bdfSopenharmony_ci|==============================================================================
695f08c3bdfSopenharmony_ci| start_daemon()   | Starts daemon, name is passed as first parameter.
696f08c3bdfSopenharmony_ci| stop_daemon()    | Stops daemon, name is passed as first parameter.
697f08c3bdfSopenharmony_ci| restart_daemon() | Restarts daemon, name is passed as first parameter.
698f08c3bdfSopenharmony_ci| status_daemon()  | Detect daemon status (exit code: 0: running, 1: not running).
699f08c3bdfSopenharmony_ci|==============================================================================
700f08c3bdfSopenharmony_ci
701f08c3bdfSopenharmony_ci.Variables with detected names
702f08c3bdfSopenharmony_ci|==============================================================================
703f08c3bdfSopenharmony_ci| CROND_DAEMON | Cron daemon name (cron, crond).
704f08c3bdfSopenharmony_ci| SYSLOG_DAEMON | Syslog daemon name (syslog, syslog-ng, rsyslog).
705f08c3bdfSopenharmony_ci|==============================================================================
706f08c3bdfSopenharmony_ci
707f08c3bdfSopenharmony_ci.Cron daemon restart example
708f08c3bdfSopenharmony_ci[source,sh]
709f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
710f08c3bdfSopenharmony_ci#!/bin/sh
711f08c3bdfSopenharmony_ci# SPDX-License-Identifier: GPL-2.0-or-later
712f08c3bdfSopenharmony_ci# Cron daemon restart example
713f08c3bdfSopenharmony_ci
714f08c3bdfSopenharmony_ciTCID=cron01
715f08c3bdfSopenharmony_ciTST_COUNT=1
716f08c3bdfSopenharmony_ci. test.sh
717f08c3bdfSopenharmony_ci. daemonlib.sh
718f08c3bdfSopenharmony_ci
719f08c3bdfSopenharmony_ci...
720f08c3bdfSopenharmony_ci
721f08c3bdfSopenharmony_cirestart_daemon $CROND_DAEMON
722f08c3bdfSopenharmony_ci
723f08c3bdfSopenharmony_ci...
724f08c3bdfSopenharmony_ci
725f08c3bdfSopenharmony_citst_exit
726f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
727f08c3bdfSopenharmony_ci
728f08c3bdfSopenharmony_ci1.6 Access to the checkpoint interface
729f08c3bdfSopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
730f08c3bdfSopenharmony_ci
731f08c3bdfSopenharmony_ciThe shell library provides an implementation of the checkpoint interface
732f08c3bdfSopenharmony_cicompatible with the C version. All 'TST_CHECKPOINT_*' functions are available.
733f08c3bdfSopenharmony_ci
734f08c3bdfSopenharmony_ciIn order to initialize checkpoints '$TST_NEEDS_CHECKPOINTS' must be set to '1'
735f08c3bdfSopenharmony_cibefore the inclusion of 'tst_test.sh':
736f08c3bdfSopenharmony_ci
737f08c3bdfSopenharmony_ci[source,sh]
738f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
739f08c3bdfSopenharmony_ci#!/bin/sh
740f08c3bdfSopenharmony_ci
741f08c3bdfSopenharmony_ciTST_NEEDS_CHECKPOINTS=1
742f08c3bdfSopenharmony_ci. tst_test.sh
743f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
744f08c3bdfSopenharmony_ci
745f08c3bdfSopenharmony_ciSince both the implementations are compatible, it's also possible to start
746f08c3bdfSopenharmony_cia child binary process from a shell test and synchronize with it. This process
747f08c3bdfSopenharmony_cimust have checkpoints initialized by calling 'tst_reinit()'.
748f08c3bdfSopenharmony_ci
749f08c3bdfSopenharmony_ci1.7 Parsing kernel .config
750f08c3bdfSopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
751f08c3bdfSopenharmony_ciThe shell library provides an implementation of the kconfig parsing interface
752f08c3bdfSopenharmony_cicompatible with the C version.
753f08c3bdfSopenharmony_ci
754f08c3bdfSopenharmony_ciIt's possible to pass kernel kconfig list for tst_require_kconfigs API with
755f08c3bdfSopenharmony_ci'$TST_NEEDS_KCONFIGS'.
756f08c3bdfSopenharmony_ciOptional '$TST_NEEDS_KCONFIGS_IFS' is used for splitting, default value is comma.
757f08c3bdfSopenharmony_ci
758f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
759f08c3bdfSopenharmony_ci#!/bin/sh
760f08c3bdfSopenharmony_ciTST_NEEDS_KCONFIGS="CONFIG_EXT4_FS, CONFIG_QUOTACTL=y"
761f08c3bdfSopenharmony_ci
762f08c3bdfSopenharmony_ci. tst_test.sh
763f08c3bdfSopenharmony_ci-------------------------------------------------------------------------------
764