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 30f08c3bdfSopenharmony_cido_test() 31f08c3bdfSopenharmony_ci{ 32f08c3bdfSopenharmony_ci true 33f08c3bdfSopenharmony_ci ret=$? 34f08c3bdfSopenharmony_ci 35f08c3bdfSopenharmony_ci if [ $ret -eq 0 ]; then 36f08c3bdfSopenharmony_ci tst_res TPASS "true returned 0" 37f08c3bdfSopenharmony_ci else 38f08c3bdfSopenharmony_ci tst_res TFAIL "true returned $ret" 39f08c3bdfSopenharmony_ci fi 40f08c3bdfSopenharmony_ci} 41f08c3bdfSopenharmony_ci 42f08c3bdfSopenharmony_ci. tst_test.sh 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 82f08c3bdfSopenharmony_citest1() 83f08c3bdfSopenharmony_ci{ 84f08c3bdfSopenharmony_ci tst_res TPASS "Test $1 passed" 85f08c3bdfSopenharmony_ci} 86f08c3bdfSopenharmony_ci 87f08c3bdfSopenharmony_citest2() 88f08c3bdfSopenharmony_ci{ 89f08c3bdfSopenharmony_ci tst_res TPASS "Test $1 passed" 90f08c3bdfSopenharmony_ci} 91f08c3bdfSopenharmony_ci 92f08c3bdfSopenharmony_ci. tst_test.sh 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 112f08c3bdfSopenharmony_cido_test() 113f08c3bdfSopenharmony_ci{ 114f08c3bdfSopenharmony_ci case $1 in 115f08c3bdfSopenharmony_ci 1) tst_res TPASS "Test $1 passed";; 116f08c3bdfSopenharmony_ci 2) tst_res TPASS "Test $1 passed";; 117f08c3bdfSopenharmony_ci esac 118f08c3bdfSopenharmony_ci} 119f08c3bdfSopenharmony_ci 120f08c3bdfSopenharmony_ci. tst_test.sh 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 142f08c3bdfSopenharmony_cido_test() 143f08c3bdfSopenharmony_ci{ 144f08c3bdfSopenharmony_ci tst_res TPASS "Test $1 passed with data '$2'" 145f08c3bdfSopenharmony_ci} 146f08c3bdfSopenharmony_ci 147f08c3bdfSopenharmony_ci. tst_test.sh 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 168f08c3bdfSopenharmony_cido_test() 169f08c3bdfSopenharmony_ci{ 170f08c3bdfSopenharmony_ci case $1 in 171f08c3bdfSopenharmony_ci 1) tst_res TPASS "Test $1 passed with data '$2'";; 172f08c3bdfSopenharmony_ci 2) tst_res TPASS "Test $1 passed with data '$2'";; 173f08c3bdfSopenharmony_ci esac 174f08c3bdfSopenharmony_ci} 175f08c3bdfSopenharmony_ci 176f08c3bdfSopenharmony_ci. tst_test.sh 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_FOO'. 193f08c3bdfSopenharmony_ci 194f08c3bdfSopenharmony_ci[options="header"] 195f08c3bdfSopenharmony_ci|============================================================================= 196f08c3bdfSopenharmony_ci| Variable name | Action done 197f08c3bdfSopenharmony_ci| 'TST_ALL_FILESYSTEMS' | Testing on all available filesystems 198f08c3bdfSopenharmony_ci ('tst_test.all_filesystems' equivalent). 199f08c3bdfSopenharmony_ci When 'TST_SKIP_FILESYSTEMS' any listed filesystem is not 200f08c3bdfSopenharmony_ci included in the resulting list of supported filesystems. 201f08c3bdfSopenharmony_ci| 'TST_DEV_EXTRA_OPTS' | Pass extra 'mkfs' options _after_ device name, 202f08c3bdfSopenharmony_ci to 'tst_mkfs', use with 'TST_FORMAT_DEVICE=1'. 203f08c3bdfSopenharmony_ci| 'TST_DEV_FS_OPTS' | Pass 'mkfs' options _before_ the device name, 204f08c3bdfSopenharmony_ci to 'tst_mkfs', use with 'TST_FORMAT_DEVICE=1'. 205f08c3bdfSopenharmony_ci| 'TST_FORMAT_DEVICE' | Format a block device with a filesystem, see 206f08c3bdfSopenharmony_ci https://github.com/linux-test-project/ltp/wiki/Shell-Test-API#formatting-device-with-a-filesystem[Formatting device with a filesystem]. 207f08c3bdfSopenharmony_ci See also 'TST_DEV_EXTRA_OPTS', 'TST_DEV_FS_OPTS', 'TST_FS_TYPE'. 208f08c3bdfSopenharmony_ci Implies 'TST_NEEDS_DEVICE=1' (no need to set it). 209f08c3bdfSopenharmony_ci| 'TST_DEVICE' | Block device name for 'tst_mount' and 'tst_mkfs', see 210f08c3bdfSopenharmony_ci https://github.com/linux-test-project/ltp/wiki/Shell-Test-API#formatting-device-with-a-filesystem[Formatting device with a filesystem]. 211f08c3bdfSopenharmony_ci| 'TST_FS_TYPE' | Override the default filesystem to be used. Also 212f08c3bdfSopenharmony_ci contains currently used filesystem during looping 213f08c3bdfSopenharmony_ci filesystems in 'TST_ALL_FILESYSTEMS=1' 214f08c3bdfSopenharmony_ci ('tst_device->fs_type' equivalent). 215f08c3bdfSopenharmony_ci| 'TST_MNTPOINT' | Holds path to mountpoint used in 'tst_mount', see 216f08c3bdfSopenharmony_ci https://github.com/linux-test-project/ltp/wiki/Shell-Test-API#formatting-device-with-a-filesystem[Formatting device with a filesystem]. 217f08c3bdfSopenharmony_ci| 'TST_MNT_PARAMS' | Extra mount params for 'tst_mount', see 218f08c3bdfSopenharmony_ci https://github.com/linux-test-project/ltp/wiki/Shell-Test-API#formatting-device-with-a-filesystem[Formatting device with a filesystem]. 219f08c3bdfSopenharmony_ci| 'TST_MOUNT_DEVICE' | Mount device, see 220f08c3bdfSopenharmony_ci https://github.com/linux-test-project/ltp/wiki/Shell-Test-API#mounting-and-unmounting-filesystems[Mounting and unmounting filesystems]. 221f08c3bdfSopenharmony_ci| 'TST_NEEDS_ROOT' | Exit the test with 'TCONF' unless executed under root. 222f08c3bdfSopenharmony_ci Alternatively the 'tst_require_root' command can be used. 223f08c3bdfSopenharmony_ci| 'TST_NEEDS_TMPDIR' | Create test temporary directory and cd into it. 224f08c3bdfSopenharmony_ci| 'TST_NEEDS_DEVICE' | Prepare test temporary device, the path to testing 225f08c3bdfSopenharmony_ci device is stored in '$TST_DEVICE' variable. 226f08c3bdfSopenharmony_ci The option implies 'TST_NEEDS_TMPDIR'. 227f08c3bdfSopenharmony_ci| 'TST_NEEDS_CMDS' | String with command names that has to be present for 228f08c3bdfSopenharmony_ci the test (see below). 229f08c3bdfSopenharmony_ci| 'TST_NEEDS_MODULE' | Test module name needed for the test (see below). 230f08c3bdfSopenharmony_ci| 'TST_NEEDS_DRIVERS' | Checks kernel drivers support for the test. 231f08c3bdfSopenharmony_ci| 'TST_NEEDS_KCONFIGS' | Checks kernel kconfigs support for the test (see below). 232f08c3bdfSopenharmony_ci| 'TST_NEEDS_KCONFIGS_IFS' | Used for splitting '$TST_NEEDS_KCONFIGS' variable, 233f08c3bdfSopenharmony_ci default value is comma, it only supports single character. 234f08c3bdfSopenharmony_ci| 'TST_SKIP_FILESYSTEMS' | Comma separated list of filesystems on which test will be skipped 235f08c3bdfSopenharmony_ci (tst_test.skip_filesystems equivalent). 236f08c3bdfSopenharmony_ci| 'TST_TIMEOUT' | Maximum timeout set for the test in sec. Must be int >= 1, 237f08c3bdfSopenharmony_ci or -1 (special value to disable timeout), default is 300. 238f08c3bdfSopenharmony_ci Variable is meant be set in tests, not by user. 239f08c3bdfSopenharmony_ci It's an equivalent of `tst_test.timeout` in C, can be set 240f08c3bdfSopenharmony_ci via 'tst_set_timeout(timeout)' after test has started. 241f08c3bdfSopenharmony_ci|============================================================================= 242f08c3bdfSopenharmony_ci 243f08c3bdfSopenharmony_ci[options="header"] 244f08c3bdfSopenharmony_ci|============================================================================= 245f08c3bdfSopenharmony_ci| Function name | Action done 246f08c3bdfSopenharmony_ci| 'tst_set_timeout(timeout)' | Maximum timeout set for the test in sec. 247f08c3bdfSopenharmony_ci See 'TST_TIMEOUT' variable. 248f08c3bdfSopenharmony_ci|============================================================================= 249f08c3bdfSopenharmony_ci 250f08c3bdfSopenharmony_ciNOTE: Network tests (see testcases/network/README.md) use additional variables 251f08c3bdfSopenharmony_ciand functions in 'tst_net.sh'. 252f08c3bdfSopenharmony_ci 253f08c3bdfSopenharmony_ciChecking for presence of commands 254f08c3bdfSopenharmony_ci+++++++++++++++++++++++++++++++++ 255f08c3bdfSopenharmony_ci 256f08c3bdfSopenharmony_ci[source,sh] 257f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 258f08c3bdfSopenharmony_ci#!/bin/sh 259f08c3bdfSopenharmony_ci 260f08c3bdfSopenharmony_ci... 261f08c3bdfSopenharmony_ci 262f08c3bdfSopenharmony_ciTST_NEEDS_CMDS="modinfo modprobe" 263f08c3bdfSopenharmony_ci. tst_test.sh 264f08c3bdfSopenharmony_ci 265f08c3bdfSopenharmony_ci... 266f08c3bdfSopenharmony_ci 267f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 268f08c3bdfSopenharmony_ci 269f08c3bdfSopenharmony_ciSetting '$TST_NEEDS_CMDS' to a string listing required commands will check for 270f08c3bdfSopenharmony_ciexistence each of them and exits the test with 'TCONF' on first missing. 271f08c3bdfSopenharmony_ci 272f08c3bdfSopenharmony_ciAlternatively the 'tst_require_cmds()' function can be used to do the same on 273f08c3bdfSopenharmony_ciruntime, since sometimes we need to the check at runtime too. 274f08c3bdfSopenharmony_ci 275f08c3bdfSopenharmony_ci'tst_check_cmds()' can be used for requirements just for a particular test 276f08c3bdfSopenharmony_cias it doesn't exit (it issues 'tst_res TCONF'). Expected usage is: 277f08c3bdfSopenharmony_ci 278f08c3bdfSopenharmony_ci[source,sh] 279f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 280f08c3bdfSopenharmony_ci#!/bin/sh 281f08c3bdfSopenharmony_ci 282f08c3bdfSopenharmony_ciTST_TESTFUNC=do_test 283f08c3bdfSopenharmony_ci 284f08c3bdfSopenharmony_cido_test() 285f08c3bdfSopenharmony_ci{ 286f08c3bdfSopenharmony_ci tst_check_cmds cmd || return 287f08c3bdfSopenharmony_ci cmd --foo 288f08c3bdfSopenharmony_ci ... 289f08c3bdfSopenharmony_ci} 290f08c3bdfSopenharmony_ci 291f08c3bdfSopenharmony_ci. tst_test.sh 292f08c3bdfSopenharmony_citst_run 293f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 294f08c3bdfSopenharmony_ci 295f08c3bdfSopenharmony_ciLocating kernel modules 296f08c3bdfSopenharmony_ci+++++++++++++++++++++++ 297f08c3bdfSopenharmony_ci 298f08c3bdfSopenharmony_ciThe LTP build system can build kernel modules as well, setting 299f08c3bdfSopenharmony_ci'$TST_NEEDS_MODULE' to module name will cause the library to look for the 300f08c3bdfSopenharmony_cimodule in a few possible paths. 301f08c3bdfSopenharmony_ci 302f08c3bdfSopenharmony_ciIf module was found the path to it will be stored into '$TST_MODPATH' 303f08c3bdfSopenharmony_civariable, if module wasn't found the test will exit with 'TCONF'. 304f08c3bdfSopenharmony_ci 305f08c3bdfSopenharmony_ciAlternatively the 'tst_require_module()' function can be used to do the same 306f08c3bdfSopenharmony_ciat runtime. 307f08c3bdfSopenharmony_ci 308f08c3bdfSopenharmony_ci1.3 Optional command line parameters 309f08c3bdfSopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 310f08c3bdfSopenharmony_ci 311f08c3bdfSopenharmony_ci[source,sh] 312f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 313f08c3bdfSopenharmony_ci#!/bin/sh 314f08c3bdfSopenharmony_ci# SPDX-License-Identifier: GPL-2.0-or-later 315f08c3bdfSopenharmony_ci# Optional test command line parameters 316f08c3bdfSopenharmony_ci 317f08c3bdfSopenharmony_ciTST_OPTS="af:" 318f08c3bdfSopenharmony_ciTST_USAGE=usage 319f08c3bdfSopenharmony_ciTST_PARSE_ARGS=parse_args 320f08c3bdfSopenharmony_ciTST_TESTFUNC=do_test 321f08c3bdfSopenharmony_ci 322f08c3bdfSopenharmony_ciALTERNATIVE=0 323f08c3bdfSopenharmony_ciMODE="foo" 324f08c3bdfSopenharmony_ci 325f08c3bdfSopenharmony_ciusage() 326f08c3bdfSopenharmony_ci{ 327f08c3bdfSopenharmony_ci cat << EOF 328f08c3bdfSopenharmony_ciusage: $0 [-a] [-f <foo|bar>] 329f08c3bdfSopenharmony_ci 330f08c3bdfSopenharmony_ciOPTIONS 331f08c3bdfSopenharmony_ci-a Enable support for alternative foo 332f08c3bdfSopenharmony_ci-f Specify foo or bar mode 333f08c3bdfSopenharmony_ciEOF 334f08c3bdfSopenharmony_ci} 335f08c3bdfSopenharmony_ci 336f08c3bdfSopenharmony_ciparse_args() 337f08c3bdfSopenharmony_ci{ 338f08c3bdfSopenharmony_ci case $1 in 339f08c3bdfSopenharmony_ci a) ALTERNATIVE=1;; 340f08c3bdfSopenharmony_ci f) MODE="$2";; 341f08c3bdfSopenharmony_ci esac 342f08c3bdfSopenharmony_ci} 343f08c3bdfSopenharmony_ci 344f08c3bdfSopenharmony_cido_test() 345f08c3bdfSopenharmony_ci{ 346f08c3bdfSopenharmony_ci ... 347f08c3bdfSopenharmony_ci} 348f08c3bdfSopenharmony_ci 349f08c3bdfSopenharmony_ci. tst_test.sh 350f08c3bdfSopenharmony_citst_run 351f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 352f08c3bdfSopenharmony_ci 353f08c3bdfSopenharmony_ciThe 'getopts' string for optional parameters is passed in the '$TST_OPTS' 354f08c3bdfSopenharmony_civariable. There are a few default parameters that cannot be used by a test, 355f08c3bdfSopenharmony_cithese can be listed with passing help '-h' option to any test. 356f08c3bdfSopenharmony_ci 357f08c3bdfSopenharmony_ciThe function that prints the usage is passed in '$TST_USAGE', the help for 358f08c3bdfSopenharmony_cithe options implemented in the library is appended when usage is printed. 359f08c3bdfSopenharmony_ci 360f08c3bdfSopenharmony_ciLastly the function '$PARSE_ARGS' is called with the option name in the '$1' 361f08c3bdfSopenharmony_ciand, if option has argument, its value in the '$2'. 362f08c3bdfSopenharmony_ci 363f08c3bdfSopenharmony_ci[source,sh] 364f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 365f08c3bdfSopenharmony_ci#!/bin/sh 366f08c3bdfSopenharmony_ci# SPDX-License-Identifier: GPL-2.0-or-later 367f08c3bdfSopenharmony_ci# Optional test positional parameters 368f08c3bdfSopenharmony_ci 369f08c3bdfSopenharmony_ciTST_POS_ARGS=3 370f08c3bdfSopenharmony_ciTST_USAGE=usage 371f08c3bdfSopenharmony_ciTST_TESTFUNC=do_test 372f08c3bdfSopenharmony_ci 373f08c3bdfSopenharmony_ciusage() 374f08c3bdfSopenharmony_ci{ 375f08c3bdfSopenharmony_ci cat << EOF 376f08c3bdfSopenharmony_ciusage: $0 [min] [max] [size] 377f08c3bdfSopenharmony_ci 378f08c3bdfSopenharmony_ciEOF 379f08c3bdfSopenharmony_ci} 380f08c3bdfSopenharmony_ci 381f08c3bdfSopenharmony_cimin="$1" 382f08c3bdfSopenharmony_cimax="$2" 383f08c3bdfSopenharmony_cisize="$3" 384f08c3bdfSopenharmony_ci 385f08c3bdfSopenharmony_cido_test() 386f08c3bdfSopenharmony_ci{ 387f08c3bdfSopenharmony_ci ... 388f08c3bdfSopenharmony_ci} 389f08c3bdfSopenharmony_ci 390f08c3bdfSopenharmony_ci. tst_test.sh 391f08c3bdfSopenharmony_citst_run 392f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 393f08c3bdfSopenharmony_ci 394f08c3bdfSopenharmony_ciYou can also request a number of positional parameters by setting the 395f08c3bdfSopenharmony_ci'$TST_POS_ARGS' variable. If you do, these will be available as they were 396f08c3bdfSopenharmony_cipassed directly to the script in '$1', '$2', ..., '$n'. 397f08c3bdfSopenharmony_ci 398f08c3bdfSopenharmony_ci1.4 Useful library functions 399f08c3bdfSopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 400f08c3bdfSopenharmony_ci 401f08c3bdfSopenharmony_ciRetrieving configuration variables 402f08c3bdfSopenharmony_ci++++++++++++++++++++++++++++++++++ 403f08c3bdfSopenharmony_ci 404f08c3bdfSopenharmony_ciYou may need to retrieve configuration values such as PAGESIZE, there is 405f08c3bdfSopenharmony_ci'getconf' but as some system may not have it, you are advised to use 406f08c3bdfSopenharmony_ci'tst_getconf' instead. Note that it implements subset of 'getconf' 407f08c3bdfSopenharmony_cisystem variables used by the testcases only. 408f08c3bdfSopenharmony_ci 409f08c3bdfSopenharmony_ci[source,sh] 410f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 411f08c3bdfSopenharmony_ci# retrieve PAGESIZE 412f08c3bdfSopenharmony_cipagesize=`tst_getconf PAGESIZE` 413f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 414f08c3bdfSopenharmony_ci 415f08c3bdfSopenharmony_ciSleeping for subsecond intervals 416f08c3bdfSopenharmony_ci++++++++++++++++++++++++++++++++ 417f08c3bdfSopenharmony_ci 418f08c3bdfSopenharmony_ciAlbeit there is a sleep command available basically everywhere not all 419f08c3bdfSopenharmony_ciimplementations can support sleeping for less than one second. And most of the 420f08c3bdfSopenharmony_citime sleeping for a second is too much. Therefore LTP includes 'tst_sleep' 421f08c3bdfSopenharmony_cithat can sleep for defined amount of seconds, milliseconds or microseconds. 422f08c3bdfSopenharmony_ci 423f08c3bdfSopenharmony_ci[source,sh] 424f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 425f08c3bdfSopenharmony_ci# sleep for 100 milliseconds 426f08c3bdfSopenharmony_citst_sleep 100ms 427f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 428f08c3bdfSopenharmony_ci 429f08c3bdfSopenharmony_ciRetry a function call multiple times 430f08c3bdfSopenharmony_ci++++++++++++++++++++++++++++++++++++ 431f08c3bdfSopenharmony_ci 432f08c3bdfSopenharmony_ciSometimes an LTP test needs to retry a function call multiple times because 433f08c3bdfSopenharmony_cithe system is not ready to process it successfully on the first try. The LTP 434f08c3bdfSopenharmony_cilibrary has useful tools to handle the call retry automatically. 435f08c3bdfSopenharmony_ci'TST_RETRY_FUNC()' will keep retrying for up to 1 second. If you want a custom 436f08c3bdfSopenharmony_citime limit use 'TST_RETRY_FN_EXP_BACKOFF()'. Both methods return the value 437f08c3bdfSopenharmony_cireturned by the last 'FUNC' call. 438f08c3bdfSopenharmony_ci 439f08c3bdfSopenharmony_ciThe delay between retries starts at 1 microsecond and doubles after each call. 440f08c3bdfSopenharmony_ciThe retry loop ends when the function call succeeds or when the next delay 441f08c3bdfSopenharmony_ciexceeds the specified time (1 second for 'TST_RETRY_FUNC()'). The maximum 442f08c3bdfSopenharmony_cidelay is multiplied by TST_TIMEOUT_MUL. The total cumulative delay may be up 443f08c3bdfSopenharmony_cito twice as long as the adjusted maximum delay. 444f08c3bdfSopenharmony_ci 445f08c3bdfSopenharmony_ciThe C version of 'TST_RETRY_FUNC()' is a macro which takes two arguments: 446f08c3bdfSopenharmony_ci 447f08c3bdfSopenharmony_ci* 'FUNC' is the complete function call with arguments which should be retried 448f08c3bdfSopenharmony_ci multiple times. 449f08c3bdfSopenharmony_ci* 'SUCCESS_CHECK' is a macro or function which will validate 'FUNC' return 450f08c3bdfSopenharmony_ci value. 'FUNC' call was successful if 'SUCCESS_CHECK(ret)' evaluates to 451f08c3bdfSopenharmony_ci non-zero. 452f08c3bdfSopenharmony_ci 453f08c3bdfSopenharmony_ciBoth retry methods clear 'errno' before every 'FUNC' call so your 454f08c3bdfSopenharmony_ci'SUCCESS_CHECK' can look for specific error codes as well. The LTP library 455f08c3bdfSopenharmony_cialso includes predefined 'SUCCESS_CHECK' macros for the most common call 456f08c3bdfSopenharmony_ciconventions: 457f08c3bdfSopenharmony_ci 458f08c3bdfSopenharmony_ci* 'TST_RETVAL_EQ0()' - The call was successful if 'FUNC' returned 0 or NULL 459f08c3bdfSopenharmony_ci* 'TST_RETVAL_NOTNULL()' - The call was successful if 'FUNC' returned any 460f08c3bdfSopenharmony_ci value other than 0 or NULL. 461f08c3bdfSopenharmony_ci* 'TST_RETVAL_GE0()' - The call was successful if 'FUNC' returned value >= 0. 462f08c3bdfSopenharmony_ci 463f08c3bdfSopenharmony_ci[source,c] 464f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 465f08c3bdfSopenharmony_ci/* Keep trying for 1 second */ 466f08c3bdfSopenharmony_ciTST_RETRY_FUNC(FUNC, SUCCESS_CHECK) 467f08c3bdfSopenharmony_ci 468f08c3bdfSopenharmony_ci/* Keep trying for up to 2*N seconds */ 469f08c3bdfSopenharmony_ciTST_RETRY_FN_EXP_BACKOFF(FUNC, SUCCESS_CHECK, N) 470f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 471f08c3bdfSopenharmony_ci 472f08c3bdfSopenharmony_ciThe shell version of 'TST_RETRY_FUNC()' is simpler and takes slightly 473f08c3bdfSopenharmony_cidifferent arguments: 474f08c3bdfSopenharmony_ci 475f08c3bdfSopenharmony_ci* 'FUNC' is a string containing the complete function or program call with 476f08c3bdfSopenharmony_ci arguments. 477f08c3bdfSopenharmony_ci* 'EXPECTED_RET' is a single expected return value. 'FUNC' call was successful 478f08c3bdfSopenharmony_ci if the return value is equal to EXPECTED_RET. 479f08c3bdfSopenharmony_ci 480f08c3bdfSopenharmony_ci[source,sh] 481f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 482f08c3bdfSopenharmony_ci# Keep trying for 1 second 483f08c3bdfSopenharmony_ciTST_RETRY_FUNC "FUNC arg1 arg2 ..." "EXPECTED_RET" 484f08c3bdfSopenharmony_ci 485f08c3bdfSopenharmony_ci# Keep trying for up to 2*N seconds 486f08c3bdfSopenharmony_ciTST_RETRY_FN_EXP_BACKOFF "FUNC arg1 arg2 ..." "EXPECTED_RET" "N" 487f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 488f08c3bdfSopenharmony_ci 489f08c3bdfSopenharmony_ciChecking for integers 490f08c3bdfSopenharmony_ci+++++++++++++++++++++ 491f08c3bdfSopenharmony_ci 492f08c3bdfSopenharmony_ci[source,sh] 493f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 494f08c3bdfSopenharmony_ci# returns zero if passed an integer parameter, non-zero otherwise 495f08c3bdfSopenharmony_citst_is_int "$FOO" 496f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 497f08c3bdfSopenharmony_ci 498f08c3bdfSopenharmony_ciChecking for integers and floating point numbers 499f08c3bdfSopenharmony_ci++++++++++++++++++++++++++++++++++++++++++++++++ 500f08c3bdfSopenharmony_ci 501f08c3bdfSopenharmony_ci[source,sh] 502f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 503f08c3bdfSopenharmony_ci# returns zero if passed an integer or floating point number parameter, 504f08c3bdfSopenharmony_ci# non-zero otherwise 505f08c3bdfSopenharmony_citst_is_num "$FOO" 506f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 507f08c3bdfSopenharmony_ci 508f08c3bdfSopenharmony_ciObtaining random numbers 509f08c3bdfSopenharmony_ci++++++++++++++++++++++++ 510f08c3bdfSopenharmony_ci 511f08c3bdfSopenharmony_ciThere is no '$RANDOM' in portable shell, use 'tst_random' instead. 512f08c3bdfSopenharmony_ci 513f08c3bdfSopenharmony_ci[source,sh] 514f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 515f08c3bdfSopenharmony_ci# get random integer between 0 and 1000 (including 0 and 1000) 516f08c3bdfSopenharmony_citst_random 0 1000 517f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 518f08c3bdfSopenharmony_ci 519f08c3bdfSopenharmony_ciFormatting device with a filesystem 520f08c3bdfSopenharmony_ci+++++++++++++++++++++++++++++++++++ 521f08c3bdfSopenharmony_ci 522f08c3bdfSopenharmony_ci'TST_FORMAT_DEVICE=1' can be used to format device before running the test. 523f08c3bdfSopenharmony_ciUses '$TST_FS_TYPE' (by default ext2), '$TST_DEVICE' a block device to be 524f08c3bdfSopenharmony_ciformatted, usually prepared by the library (TST_NEEDS_DEVICE=1 must be set). 525f08c3bdfSopenharmony_ci'$TST_DEV_FS_OPTS' a 'mkfs' options _before_ the device path and 526f08c3bdfSopenharmony_ci'$TST_DEV_EXTRA_OPTS' extra 'mkfs'' options _after_ the device path. 527f08c3bdfSopenharmony_ci 528f08c3bdfSopenharmony_ci[source,sh] 529f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 530f08c3bdfSopenharmony_ciTST_FORMAT_DEVICE=1 531f08c3bdfSopenharmony_ciTST_DEV_FS_OPTS="-b 1024 -O quota" 532f08c3bdfSopenharmony_ciTST_DEV_EXTRA_OPTS="5m" 533f08c3bdfSopenharmony_ciTST_TESTFUNC=test 534f08c3bdfSopenharmony_ci 535f08c3bdfSopenharmony_citest() 536f08c3bdfSopenharmony_ci{ 537f08c3bdfSopenharmony_ci tst_res TPASS "device formatted" 538f08c3bdfSopenharmony_ci} 539f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 540f08c3bdfSopenharmony_ci 541f08c3bdfSopenharmony_ci[source,sh] 542f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 543f08c3bdfSopenharmony_ci# format test device with ext2 544f08c3bdfSopenharmony_citst_mkfs ext2 $TST_DEVICE 545f08c3bdfSopenharmony_ci# default params are $TST_FS_TYPE $TST_DEVICE 546f08c3bdfSopenharmony_citst_mkfs 547f08c3bdfSopenharmony_ci# optional parameters 548f08c3bdfSopenharmony_citst_mkfs ext4 /dev/device -T largefile 549f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 550f08c3bdfSopenharmony_ci 551f08c3bdfSopenharmony_ciMounting and unmounting filesystems 552f08c3bdfSopenharmony_ci+++++++++++++++++++++++++++++++++++ 553f08c3bdfSopenharmony_ci 554f08c3bdfSopenharmony_ciThe 'tst_mount' and 'tst_umount' helpers are a safe way to mount/umount 555f08c3bdfSopenharmony_cia filesystem. 556f08c3bdfSopenharmony_ci 557f08c3bdfSopenharmony_ciThe 'tst_mount' mounts '$TST_DEVICE' of '$TST_FS_TYPE' (optional) to 558f08c3bdfSopenharmony_ci'$TST_MNTPOINT' (defaults to mntpoint), optionally using the 559f08c3bdfSopenharmony_ci'$TST_MNT_PARAMS'. The '$TST_MNTPOINT' directory is created if it didn't 560f08c3bdfSopenharmony_ciexist prior to the function call. 561f08c3bdfSopenharmony_ci 562f08c3bdfSopenharmony_ciIf the path passed (optional, must be absolute path, defaults to '$TST_MNTPOINT') 563f08c3bdfSopenharmony_cito the 'tst_umount' is not mounted (present in '/proc/mounts') it's noop. 564f08c3bdfSopenharmony_ciOtherwise it retries to umount the filesystem a few times on failure. 565f08c3bdfSopenharmony_ciThis is a workaround since there are daemons dumb enough to probe all newly 566f08c3bdfSopenharmony_cimounted filesystems, and prevents them from being umounted shortly after they 567f08c3bdfSopenharmony_ciwere mounted. 568f08c3bdfSopenharmony_ci 569f08c3bdfSopenharmony_ciROD and ROD_SILENT 570f08c3bdfSopenharmony_ci++++++++++++++++++ 571f08c3bdfSopenharmony_ci 572f08c3bdfSopenharmony_ciThese functions supply the 'SAFE_MACROS' used in C although they work and are 573f08c3bdfSopenharmony_cinamed differently. 574f08c3bdfSopenharmony_ci 575f08c3bdfSopenharmony_ci[source,sh] 576f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 577f08c3bdfSopenharmony_ciROD_SILENT command arg1 arg2 ... 578f08c3bdfSopenharmony_ci 579f08c3bdfSopenharmony_ci# is shorthand for: 580f08c3bdfSopenharmony_ci 581f08c3bdfSopenharmony_cicommand arg1 arg2 ... > /dev/null 2>&1 582f08c3bdfSopenharmony_ciif [ $? -ne 0 ]; then 583f08c3bdfSopenharmony_ci tst_brk TBROK "..." 584f08c3bdfSopenharmony_cifi 585f08c3bdfSopenharmony_ci 586f08c3bdfSopenharmony_ci 587f08c3bdfSopenharmony_ciROD command arg1 arg2 ... 588f08c3bdfSopenharmony_ci 589f08c3bdfSopenharmony_ci# is shorthand for: 590f08c3bdfSopenharmony_ci 591f08c3bdfSopenharmony_ciROD arg1 arg2 ... 592f08c3bdfSopenharmony_ciif [ $? -ne 0 ]; then 593f08c3bdfSopenharmony_ci tst_brk TBROK "..." 594f08c3bdfSopenharmony_cifi 595f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 596f08c3bdfSopenharmony_ci 597f08c3bdfSopenharmony_ciWARNING: Keep in mind that output redirection (to a file) happens in the 598f08c3bdfSopenharmony_ci caller rather than in the ROD function and cannot be checked for 599f08c3bdfSopenharmony_ci write errors by the ROD function. 600f08c3bdfSopenharmony_ci 601f08c3bdfSopenharmony_ciAs a matter of a fact doing +ROD echo a > /proc/cpuinfo+ would work just fine 602f08c3bdfSopenharmony_cisince the 'ROD' function will only get the +echo a+ part that will run just 603f08c3bdfSopenharmony_cifine. 604f08c3bdfSopenharmony_ci 605f08c3bdfSopenharmony_ci[source,sh] 606f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 607f08c3bdfSopenharmony_ci# Redirect output to a file with ROD 608f08c3bdfSopenharmony_ciROD echo foo \> bar 609f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 610f08c3bdfSopenharmony_ci 611f08c3bdfSopenharmony_ciNote the '>' is escaped with '\', this causes that the '>' and filename are 612f08c3bdfSopenharmony_cipassed to the 'ROD' function as parameters and the 'ROD' function contains 613f08c3bdfSopenharmony_cicode to split '$@' on '>' and redirects the output to the file. 614f08c3bdfSopenharmony_ci 615f08c3bdfSopenharmony_ciEXPECT_PASS{,_BRK} and EXPECT_FAIL{,_BRK} 616f08c3bdfSopenharmony_ci+++++++++++++++++++++++++++++++++++++++++ 617f08c3bdfSopenharmony_ci 618f08c3bdfSopenharmony_ci[source,sh] 619f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 620f08c3bdfSopenharmony_ciEXPECT_PASS command arg1 arg2 ... [ \> file ] 621f08c3bdfSopenharmony_ciEXPECT_FAIL command arg1 arg2 ... [ \> file ] 622f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 623f08c3bdfSopenharmony_ci 624f08c3bdfSopenharmony_ci'EXPECT_PASS' calls 'tst_res TPASS' if the command exited with 0 exit code, 625f08c3bdfSopenharmony_ciand 'tst_res TFAIL' otherwise. 'EXPECT_FAIL' does vice versa. 626f08c3bdfSopenharmony_ci 627f08c3bdfSopenharmony_ciOutput redirection rules are the same as for the 'ROD' function. In addition 628f08c3bdfSopenharmony_cito that, 'EXPECT_FAIL' always redirects the command's stderr to '/dev/null'. 629f08c3bdfSopenharmony_ci 630f08c3bdfSopenharmony_ciThere are also 'EXPECT_PASS_BRK' and 'EXPECT_FAIL_BRK', which works the same way 631f08c3bdfSopenharmony_ciexcept breaking a test when unexpected action happen. 632f08c3bdfSopenharmony_ci 633f08c3bdfSopenharmony_ciIt's possible to detect whether expected value happened: 634f08c3bdfSopenharmony_ci[source,sh] 635f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 636f08c3bdfSopenharmony_ciif ! EXPECT_PASS command arg1 2\> /dev/null; then 637f08c3bdfSopenharmony_ci continue 638f08c3bdfSopenharmony_cifi 639f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 640f08c3bdfSopenharmony_ci 641f08c3bdfSopenharmony_citst_kvcmp 642f08c3bdfSopenharmony_ci+++++++++ 643f08c3bdfSopenharmony_ci 644f08c3bdfSopenharmony_ciThis command compares the currently running kernel version given conditions 645f08c3bdfSopenharmony_ciwith syntax similar to the shell test command. 646f08c3bdfSopenharmony_ci 647f08c3bdfSopenharmony_ci[source,sh] 648f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 649f08c3bdfSopenharmony_ci# Exit the test if kernel version is older or equal to 4.0.0 650f08c3bdfSopenharmony_ciif tst_kvcmp -le 4.0.0; then 651f08c3bdfSopenharmony_ci tst_brk TCONF "Kernel newer than 4.0.0 is needed" 652f08c3bdfSopenharmony_cifi 653f08c3bdfSopenharmony_ci 654f08c3bdfSopenharmony_ci# Exit the test if kernel is newer than 3.16 and older than 4.0.1 655f08c3bdfSopenharmony_ciif tst_kvcmp -gt 3.16 -a -lt 4.0.1; then 656f08c3bdfSopenharmony_ci tst_brk TCONF "Kernel must be older than 3.16 or newer than 4.0.1" 657f08c3bdfSopenharmony_cifi 658f08c3bdfSopenharmony_ci 659f08c3bdfSopenharmony_ciif tst_kvcmp -lt "6.1 RHEL9:5.14.0-191"; then 660f08c3bdfSopenharmony_ci # code for kernel < 6.1 or RHEL9 kernel < 5.14.0-191 661f08c3bdfSopenharmony_cifi 662f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 663f08c3bdfSopenharmony_ci 664f08c3bdfSopenharmony_ci[options="header"] 665f08c3bdfSopenharmony_ci|======================================================================= 666f08c3bdfSopenharmony_ci| expression | description 667f08c3bdfSopenharmony_ci| -eq kver | Returns true if kernel version is equal 668f08c3bdfSopenharmony_ci| -ne kver | Returns true if kernel version is not equal 669f08c3bdfSopenharmony_ci| -gt kver | Returns true if kernel version is greater 670f08c3bdfSopenharmony_ci| -ge kver | Returns true if kernel version is greater or equal 671f08c3bdfSopenharmony_ci| -lt kver | Returns true if kernel version is lesser 672f08c3bdfSopenharmony_ci| -le kver | Returns true if kernel version is lesser or equal 673f08c3bdfSopenharmony_ci| -a | Does logical and between two expressions 674f08c3bdfSopenharmony_ci| -o | Does logical or between two expressions 675f08c3bdfSopenharmony_ci|======================================================================= 676f08c3bdfSopenharmony_ci 677f08c3bdfSopenharmony_ciThe format for kernel version has to either be with one dot e.g. '2.6' or with 678f08c3bdfSopenharmony_citwo dots e.g. '4.8.1'. 679f08c3bdfSopenharmony_ci 680f08c3bdfSopenharmony_ciKernel version can also be followed by a space separated list of extra versions 681f08c3bdfSopenharmony_ciprefixed by distribution which when matched take precedence, e.g. '6.1 RHEL9:5.14.0-191'. 682f08c3bdfSopenharmony_ci 683f08c3bdfSopenharmony_ciFor more info see 'tst_kvercmp()' and 'tst_kvercmp2()' in 684f08c3bdfSopenharmony_cihttps://github.com/linux-test-project/ltp/wiki/C-Test-API#16-runtime-kernel-version-detection[C Test API]. 685f08c3bdfSopenharmony_ci 686f08c3bdfSopenharmony_ciNOTE: See also LTP 687f08c3bdfSopenharmony_ci https://github.com/linux-test-project/ltp/wiki/Supported-kernel,-libc,-toolchain-versions#13-minimal-supported-kernel-version[minimal supported kernel version]. 688f08c3bdfSopenharmony_ci 689f08c3bdfSopenharmony_citst_fs_has_free 690f08c3bdfSopenharmony_ci+++++++++++++++ 691f08c3bdfSopenharmony_ci 692f08c3bdfSopenharmony_ci[source,sh] 693f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 694f08c3bdfSopenharmony_ci#!/bin/sh 695f08c3bdfSopenharmony_ci 696f08c3bdfSopenharmony_ci... 697f08c3bdfSopenharmony_ci 698f08c3bdfSopenharmony_ci# whether current directory has 100MB free space at least. 699f08c3bdfSopenharmony_ciif ! tst_fs_has_free . 100MB; then 700f08c3bdfSopenharmony_ci tst_brkm TCONF "Not enough free space" 701f08c3bdfSopenharmony_cifi 702f08c3bdfSopenharmony_ci 703f08c3bdfSopenharmony_ci... 704f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 705f08c3bdfSopenharmony_ci 706f08c3bdfSopenharmony_ciThe 'tst_fs_has_free' shell interface returns 0 if the specified free space is 707f08c3bdfSopenharmony_cisatisfied, 1 if not, and 2 on error. 708f08c3bdfSopenharmony_ci 709f08c3bdfSopenharmony_ciThe second argument supports suffixes kB, MB and GB, the default unit is Byte. 710f08c3bdfSopenharmony_ci 711f08c3bdfSopenharmony_citst_retry 712f08c3bdfSopenharmony_ci+++++++++ 713f08c3bdfSopenharmony_ci 714f08c3bdfSopenharmony_ci[source,sh] 715f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 716f08c3bdfSopenharmony_ci#!/bin/sh 717f08c3bdfSopenharmony_ci 718f08c3bdfSopenharmony_ci... 719f08c3bdfSopenharmony_ci 720f08c3bdfSopenharmony_ci# Retry ping command three times 721f08c3bdfSopenharmony_citst_retry "ping -c 1 127.0.0.1" 722f08c3bdfSopenharmony_ci 723f08c3bdfSopenharmony_ciif [ $? -ne 0 ]; then 724f08c3bdfSopenharmony_ci tst_resm TFAIL "Failed to ping 127.0.0.1" 725f08c3bdfSopenharmony_cielse 726f08c3bdfSopenharmony_ci tst_resm TPASS "Successfully pinged 127.0.0.1" 727f08c3bdfSopenharmony_cifi 728f08c3bdfSopenharmony_ci 729f08c3bdfSopenharmony_ci... 730f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 731f08c3bdfSopenharmony_ci 732f08c3bdfSopenharmony_ciThe 'tst_retry' function allows you to retry a command after waiting small 733f08c3bdfSopenharmony_ciamount of time until it succeeds or until given amount of retries has been 734f08c3bdfSopenharmony_cireached (default is three attempts). 735f08c3bdfSopenharmony_ci 736f08c3bdfSopenharmony_ci1.5 Restarting daemons 737f08c3bdfSopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~ 738f08c3bdfSopenharmony_ci 739f08c3bdfSopenharmony_ciRestarting system daemons is a complicated task for two reasons. 740f08c3bdfSopenharmony_ci 741f08c3bdfSopenharmony_ci* There are different init systems 742f08c3bdfSopenharmony_ci (SysV init, systemd, etc...) 743f08c3bdfSopenharmony_ci 744f08c3bdfSopenharmony_ci* Daemon names are not unified between distributions 745f08c3bdfSopenharmony_ci (apache vs httpd, cron vs crond, various syslog variations) 746f08c3bdfSopenharmony_ci 747f08c3bdfSopenharmony_ciTo solve these problems LTP has 'testcases/lib/daemonlib.sh' library that 748f08c3bdfSopenharmony_ciprovides functions to start/stop/query daemons as well as variables that store 749f08c3bdfSopenharmony_cicorrect daemon name. 750f08c3bdfSopenharmony_ci 751f08c3bdfSopenharmony_ci.Supported operations 752f08c3bdfSopenharmony_ci|============================================================================== 753f08c3bdfSopenharmony_ci| start_daemon() | Starts daemon, name is passed as first parameter. 754f08c3bdfSopenharmony_ci| stop_daemon() | Stops daemon, name is passed as first parameter. 755f08c3bdfSopenharmony_ci| restart_daemon() | Restarts daemon, name is passed as first parameter. 756f08c3bdfSopenharmony_ci| status_daemon() | Detect daemon status (exit code: 0: running, 1: not running). 757f08c3bdfSopenharmony_ci|============================================================================== 758f08c3bdfSopenharmony_ci 759f08c3bdfSopenharmony_ci.Variables with detected names 760f08c3bdfSopenharmony_ci|============================================================================== 761f08c3bdfSopenharmony_ci| CROND_DAEMON | Cron daemon name (cron, crond). 762f08c3bdfSopenharmony_ci| SYSLOG_DAEMON | Syslog daemon name (syslog, syslog-ng, rsyslog). 763f08c3bdfSopenharmony_ci|============================================================================== 764f08c3bdfSopenharmony_ci 765f08c3bdfSopenharmony_ciCron daemon restart example 766f08c3bdfSopenharmony_ci+++++++++++++++++++++++++++ 767f08c3bdfSopenharmony_ci 768f08c3bdfSopenharmony_ci[source,sh] 769f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 770f08c3bdfSopenharmony_ci#!/bin/sh 771f08c3bdfSopenharmony_ci# SPDX-License-Identifier: GPL-2.0-or-later 772f08c3bdfSopenharmony_ci# Cron daemon restart example 773f08c3bdfSopenharmony_ci 774f08c3bdfSopenharmony_ciTCID=cron01 775f08c3bdfSopenharmony_ciTST_COUNT=1 776f08c3bdfSopenharmony_ci. test.sh 777f08c3bdfSopenharmony_ci. daemonlib.sh 778f08c3bdfSopenharmony_ci 779f08c3bdfSopenharmony_ci... 780f08c3bdfSopenharmony_ci 781f08c3bdfSopenharmony_cirestart_daemon $CROND_DAEMON 782f08c3bdfSopenharmony_ci 783f08c3bdfSopenharmony_ci... 784f08c3bdfSopenharmony_ci 785f08c3bdfSopenharmony_citst_exit 786f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 787f08c3bdfSopenharmony_ci 788f08c3bdfSopenharmony_ci1.6 Access to the checkpoint interface 789f08c3bdfSopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 790f08c3bdfSopenharmony_ci 791f08c3bdfSopenharmony_ciThe shell library provides an implementation of the checkpoint interface 792f08c3bdfSopenharmony_cicompatible with the C version. All 'TST_CHECKPOINT_*' functions are available. 793f08c3bdfSopenharmony_ci 794f08c3bdfSopenharmony_ciIn order to initialize checkpoints '$TST_NEEDS_CHECKPOINTS' must be set to '1' 795f08c3bdfSopenharmony_cibefore the inclusion of 'tst_test.sh': 796f08c3bdfSopenharmony_ci 797f08c3bdfSopenharmony_ci[source,sh] 798f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 799f08c3bdfSopenharmony_ci#!/bin/sh 800f08c3bdfSopenharmony_ci 801f08c3bdfSopenharmony_ciTST_NEEDS_CHECKPOINTS=1 802f08c3bdfSopenharmony_ci. tst_test.sh 803f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 804f08c3bdfSopenharmony_ci 805f08c3bdfSopenharmony_ciSince both the implementations are compatible, it's also possible to start 806f08c3bdfSopenharmony_cia child binary process from a shell test and synchronize with it. This process 807f08c3bdfSopenharmony_cimust have checkpoints initialized by calling 'tst_reinit()'. 808f08c3bdfSopenharmony_ci 809f08c3bdfSopenharmony_ci1.7 Parsing kernel .config 810f08c3bdfSopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~ 811f08c3bdfSopenharmony_ciThe shell library provides an implementation of the kconfig parsing interface 812f08c3bdfSopenharmony_cicompatible with the C version. 813f08c3bdfSopenharmony_ci 814f08c3bdfSopenharmony_ciIt's possible to pass kernel kconfig list for tst_require_kconfigs API with 815f08c3bdfSopenharmony_ci'$TST_NEEDS_KCONFIGS'. 816f08c3bdfSopenharmony_ciOptional '$TST_NEEDS_KCONFIGS_IFS' is used for splitting, default value is comma. 817f08c3bdfSopenharmony_ci 818f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 819f08c3bdfSopenharmony_ci#!/bin/sh 820f08c3bdfSopenharmony_ciTST_NEEDS_KCONFIGS="CONFIG_EXT4_FS, CONFIG_QUOTACTL=y" 821f08c3bdfSopenharmony_ci 822f08c3bdfSopenharmony_ci. tst_test.sh 823f08c3bdfSopenharmony_ci------------------------------------------------------------------------------- 824f08c3bdfSopenharmony_ci 825f08c3bdfSopenharmony_ci1.8 Skipping test based on system state 826f08c3bdfSopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 827f08c3bdfSopenharmony_ciTest can be skipped on various conditions: on enabled SecureBoot 828f08c3bdfSopenharmony_ci('TST_SKIP_IN_SECUREBOOT=1'), lockdown ('TST_SKIP_IN_LOCKDOWN=1'). 829