Skip to content

Commit

Permalink
testcaes/lib: Add shell loader
Browse files Browse the repository at this point in the history
This commit implements a shell loader so that we don't have to write a C
loader for each LTP shell test. The idea is simple, the loader parses
the shell test and prepares the tst_test structure accordingly, then
runs the actual shell test.

The format for the metadata in the shell test was choosen to be JSON
because:

- I didn't want to invent an adhoc format and JSON is perfect for
  serializing data structures
- The metadata parser for shell test will be trivial, it will just pick
  the JSON from the comment, no parsing will be required

Signed-off-by: Cyril Hrubis <[email protected]>
Reviewed-by: Richard Palethorpe <[email protected]>
Reviewed-by: Li Wang <[email protected]>
  • Loading branch information
metan-ucw committed Sep 16, 2024
1 parent c50a0e0 commit 5528da0
Show file tree
Hide file tree
Showing 18 changed files with 738 additions and 2 deletions.
2 changes: 1 addition & 1 deletion include/tst_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ struct tst_fs {
const char *const *mkfs_opts;
const char *mkfs_size_opt;

const unsigned int mnt_flags;
unsigned int mnt_flags;
const void *mnt_data;
};

Expand Down
1 change: 1 addition & 0 deletions testcases/lib/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@
/tst_supported_fs
/tst_timeout_kill
/tst_res_
/tst_run_shell
6 changes: 5 additions & 1 deletion testcases/lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

top_srcdir ?= ../..

LTPLIBS = ujson
tst_run_shell: LTPLDLIBS = -lujson

include $(top_srcdir)/include/mk/testcases.mk

INSTALL_TARGETS := *.sh
Expand All @@ -13,6 +16,7 @@ MAKE_TARGETS := tst_sleep tst_random tst_checkpoint tst_rod tst_kvcmp\
tst_getconf tst_supported_fs tst_check_drivers tst_get_unused_port\
tst_get_median tst_hexdump tst_get_free_pids tst_timeout_kill\
tst_check_kconfigs tst_cgctl tst_fsfreeze tst_ns_create tst_ns_exec\
tst_ns_ifmove tst_lockdown_enabled tst_secureboot_enabled tst_res_
tst_ns_ifmove tst_lockdown_enabled tst_secureboot_enabled tst_res_\
tst_run_shell

include $(top_srcdir)/include/mk/generic_trunk_target.mk
21 changes: 21 additions & 0 deletions testcases/lib/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,24 @@ for i in `seq -w 01 06`; do
echo
./tests/shell_test$i
done

for i in shell_loader.sh shell_loader_all_filesystems.sh shell_loader_no_metadata.sh \
shell_loader_wrong_metadata.sh shell_loader_invalid_metadata.sh\
shell_loader_supported_archs.sh shell_loader_filesystems.sh\
shell_loader_tcnt.sh shell_loader_kconfigs.sh shell_loader_tags.sh \
shell_loader_invalid_block.sh; do
echo
echo "*** Running $i ***"
echo
$i
done

echo
echo "*** Testing LTP test -h option ***"
echo
shell_loader.sh -h

echo
echo "*** Testing LTP test -i option ***"
echo
shell_loader.sh -i 2
26 changes: 26 additions & 0 deletions testcases/lib/tests/shell_loader.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/sh
#
# ---
# doc
#
# [Description]
#
# This is a simple shell test loader example.
# ---
#
# ---
# env
# {
# "needs_tmpdir": true
# }
# ---

. tst_loader.sh

tst_res TPASS "Shell loader works fine!"
case "$PWD" in
/tmp/*)
tst_res TPASS "We are running in temp directory in $PWD";;
*)
tst_res TFAIL "We are not running in temp directory but $PWD";;
esac
27 changes: 27 additions & 0 deletions testcases/lib/tests/shell_loader_all_filesystems.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/sh
#
# ---
# env
# {
# "needs_root": true,
# "mount_device": true,
# "all_filesystems": true,
# "mntpoint": "ltp_mntpoint"
# }
# ---

. tst_loader.sh

tst_res TINFO "In shell"

mntpath=$(realpath ltp_mntpoint)
mounted=$(grep $mntpath /proc/mounts)

if [ -n "$mounted" ]; then
device=$(echo $mounted |cut -d' ' -f 1)
path=$(echo $mounted |cut -d' ' -f 2)

tst_res TPASS "$device mounted at $path"
else
tst_res TFAIL "Device not mounted!"
fi
33 changes: 33 additions & 0 deletions testcases/lib/tests/shell_loader_filesystems.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/sh
#
# ---
# env
# {
# "mount_device": true,
# "mntpoint": "ltp_mntpoint",
# "filesystems": [
# {
# "type": "btrfs"
# },
# {
# "type": "xfs",
# "mkfs_opts": ["-m", "reflink=1"]
# }
# ]
# }
# ---

. tst_loader.sh

tst_res TINFO "In shell"

mntpoint=$(realpath ltp_mntpoint)
mounted=$(grep $mntpoint /proc/mounts)

if [ -n "$mounted" ]; then
fs=$(echo $mounted |cut -d' ' -f 3)

tst_res TPASS "Mounted device formatted with $fs"
else
tst_res TFAIL "Device not mounted!"
fi
26 changes: 26 additions & 0 deletions testcases/lib/tests/shell_loader_invalid_block.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/sh
#
# ---
# doc
#
# [Description]
#
# This is a simple shell test loader example.
# ---
#
# ---
# env
# {
# "needs_tmpdir": true
# }
# ---
#
# ---
# inv
#
# This is an invalid block that breaks the test.
# ---

. tst_loader.sh

tst_res TPASS "This should pass!"
15 changes: 15 additions & 0 deletions testcases/lib/tests/shell_loader_invalid_metadata.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh
#
# This test has wrong metadata and should not be run
#
# ---
# env
# {
# {"needs_tmpdir": 42,
# }
# ---
#

. tst_loader.sh

tst_res TFAIL "Shell loader should TBROK the test"
12 changes: 12 additions & 0 deletions testcases/lib/tests/shell_loader_kconfigs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh
#
# ---
# env
# {
# "needs_kconfigs": ["CONFIG_NUMA=y"]
# }
# ---

. tst_loader.sh

tst_res TPASS "Shell loader works fine!"
8 changes: 8 additions & 0 deletions testcases/lib/tests/shell_loader_no_metadata.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh
#
# This test has no metadata and should not be executed
#

. tst_loader.sh

tst_res TFAIL "Shell loader should TBROK the test"
12 changes: 12 additions & 0 deletions testcases/lib/tests/shell_loader_supported_archs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh
#
# ---
# env
# {
# "supported_archs": ["x86", "ppc64", "x86_64"]
# }
# ---

. tst_loader.sh

tst_res TPASS "We are running on supported architecture"
15 changes: 15 additions & 0 deletions testcases/lib/tests/shell_loader_tags.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh
#
# ---
# env
# {
# "tags": [
# ["linux-git", "832478cd342ab"],
# ["CVE", "2099-999"]
# ]
# }
# ---

. tst_loader.sh

tst_res TFAIL "Fails the test so that tags are shown."
15 changes: 15 additions & 0 deletions testcases/lib/tests/shell_loader_tcnt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh
#
# The script should be executed tcnt times and the iteration number should be in $1
#
# ---
# env
# {
# "tcnt": 2
# }
# ---
#

. tst_loader.sh

tst_res TPASS "Iteration $1"
15 changes: 15 additions & 0 deletions testcases/lib/tests/shell_loader_wrong_metadata.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh
#
# This test has wrong metadata and should not be run
#
# ---
# env
# {
# "needs_tmpdir": 42,
# }
# ---
#

. tst_loader.sh

tst_res TFAIL "Shell loader should TBROK the test"
4 changes: 4 additions & 0 deletions testcases/lib/tst_env.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#!/bin/sh
#
# This is a minimal test environment for a shell scripts executed from C by
# tst_run_shell() function. Shell tests must use the tst_loader.sh instead!
#

tst_script_name=$(basename $0)

Expand Down
11 changes: 11 additions & 0 deletions testcases/lib/tst_loader.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
#
# This is a loader for shell tests that use the C test library.
#

if [ -z "$LTP_IPC_PATH" ]; then
tst_run_shell $(basename "$0") "$@"
exit $?
else
. tst_env.sh
fi
Loading

0 comments on commit 5528da0

Please sign in to comment.