Shell test library v3
cover Shell test library v3
Changes againts RFC:
- The metadata in tests are encoded in JSON now, there is a proper parser used to decode them and construct the tst_test accordingly. Most of the C library features are now available in shell tests as well.
- The shell tests use a script to bootstrap themselves so they can be just executed normally see tst_loader.sh
- Many small fixes that were pointed out for the RFC
Cyril Hrubis (3):
Add support for mixing C and shell code
libs: Vendor ujson library
testcaes/lib: Add shell loader
include/tst_test.h | 40 +- include/ujson.h | 13 + include/ujson_common.h | 69 ++ include/ujson_reader.h | 539 ++++++++ include/ujson_utf.h | 168 +++ include/ujson_writer.h | 224 ++++ lib/tst_test.c | 51 + libs/ujson/Makefile | 12 + libs/ujson/ujson_common.c | 38 + libs/ujson/ujson_reader.c | 1081 +++++++++++++++++ libs/ujson/ujson_utf.c | 105 ++ libs/ujson/ujson_writer.c | 491 ++++++++ testcases/lib/.gitignore | 2 + testcases/lib/Makefile | 8 +- testcases/lib/run_tests.sh | 20 + testcases/lib/tests/.gitignore | 6 + testcases/lib/tests/Makefile | 11 + testcases/lib/tests/shell_loader.sh | 15 + .../lib/tests/shell_loader_all_filesystems.sh | 24 + .../lib/tests/shell_loader_filesystems.sh | 30 + .../tests/shell_loader_invalid_metadata.sh | 12 + .../lib/tests/shell_loader_no_metadata.sh | 8 + .../lib/tests/shell_loader_supported_archs.sh | 9 + .../lib/tests/shell_loader_wrong_metadata.sh | 12 + testcases/lib/tests/shell_test01.c | 17 + testcases/lib/tests/shell_test02.c | 18 + testcases/lib/tests/shell_test03.c | 25 + testcases/lib/tests/shell_test04.c | 18 + testcases/lib/tests/shell_test05.c | 27 + testcases/lib/tests/shell_test06.c | 16 + testcases/lib/tests/shell_test_brk.sh | 6 + testcases/lib/tests/shell_test_check_argv.sh | 23 + testcases/lib/tests/shell_test_checkpoint.sh | 7 + testcases/lib/tests/shell_test_pass.sh | 6 + testcases/lib/tst_env.sh | 25 + testcases/lib/tst_loader.sh | 11 + testcases/lib/tst_res_.c | 58 + testcases/lib/tst_run_shell.c | 378 ++++++ 38 files changed, 3620 insertions(+), 3 deletions(-) create mode 100644 include/ujson.h create mode 100644 include/ujson_common.h create mode 100644 include/ujson_reader.h create mode 100644 include/ujson_utf.h create mode 100644 include/ujson_writer.h create mode 100644 libs/ujson/Makefile create mode 100644 libs/ujson/ujson_common.c create mode 100644 libs/ujson/ujson_reader.c create mode 100644 libs/ujson/ujson_utf.c create mode 100644 libs/ujson/ujson_writer.c create mode 100755 testcases/lib/run_tests.sh create mode 100644 testcases/lib/tests/.gitignore create mode 100644 testcases/lib/tests/Makefile create mode 100755 testcases/lib/tests/shell_loader.sh create mode 100755 testcases/lib/tests/shell_loader_all_filesystems.sh create mode 100755 testcases/lib/tests/shell_loader_filesystems.sh create mode 100755 testcases/lib/tests/shell_loader_invalid_metadata.sh create mode 100755 testcases/lib/tests/shell_loader_no_metadata.sh create mode 100755 testcases/lib/tests/shell_loader_supported_archs.sh create mode 100755 testcases/lib/tests/shell_loader_wrong_metadata.sh create mode 100644 testcases/lib/tests/shell_test01.c create mode 100644 testcases/lib/tests/shell_test02.c create mode 100644 testcases/lib/tests/shell_test03.c create mode 100644 testcases/lib/tests/shell_test04.c create mode 100644 testcases/lib/tests/shell_test05.c create mode 100644 testcases/lib/tests/shell_test06.c create mode 100755 testcases/lib/tests/shell_test_brk.sh create mode 100755 testcases/lib/tests/shell_test_check_argv.sh create mode 100755 testcases/lib/tests/shell_test_checkpoint.sh create mode 100755 testcases/lib/tests/shell_test_pass.sh create mode 100644 testcases/lib/tst_env.sh create mode 100644 testcases/lib/tst_loader.sh create mode 100644 testcases/lib/tst_res_.c create mode 100644 testcases/lib/tst_run_shell.c
Signature · 5 lines
2.44.2 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 1/3 Add support for mixing C and shell code
This is a proof of a concept of a seamless C and shell integration. The idea is that with this you can mix shell and C code as much as as you wish to get the best of the two worlds.
--- include/tst_test.h | 38 +++++++++++++ lib/tst_test.c | 51 +++++++++++++++++ testcases/lib/.gitignore | 1 + testcases/lib/Makefile | 4 +- testcases/lib/run_tests.sh | 11 ++++ testcases/lib/tests/.gitignore | 6 ++ testcases/lib/tests/Makefile | 11 ++++ testcases/lib/tests/shell_test01.c | 17 ++++++ testcases/lib/tests/shell_test02.c | 18 ++++++ testcases/lib/tests/shell_test03.c | 25 +++++++++ testcases/lib/tests/shell_test04.c | 18 ++++++ testcases/lib/tests/shell_test05.c | 27 +++++++++ testcases/lib/tests/shell_test06.c | 16 ++++++ testcases/lib/tests/shell_test_brk.sh | 6 ++ testcases/lib/tests/shell_test_check_argv.sh | 23 ++++++++ testcases/lib/tests/shell_test_checkpoint.sh | 7 +++ testcases/lib/tests/shell_test_pass.sh | 6 ++ testcases/lib/tst_env.sh | 21 +++++++ testcases/lib/tst_res_.c | 58 ++++++++++++++++++++ 19 files changed, 362 insertions(+), 2 deletions(-) create mode 100755 testcases/lib/run_tests.sh create mode 100644 testcases/lib/tests/.gitignore create mode 100644 testcases/lib/tests/Makefile create mode 100644 testcases/lib/tests/shell_test01.c create mode 100644 testcases/lib/tests/shell_test02.c create mode 100644 testcases/lib/tests/shell_test03.c create mode 100644 testcases/lib/tests/shell_test04.c create mode 100644 testcases/lib/tests/shell_test05.c create mode 100644 testcases/lib/tests/shell_test06.c create mode 100755 testcases/lib/tests/shell_test_brk.sh create mode 100755 testcases/lib/tests/shell_test_check_argv.sh create mode 100755 testcases/lib/tests/shell_test_checkpoint.sh create mode 100755 testcases/lib/tests/shell_test_pass.sh create mode 100644 testcases/lib/tst_env.sh create mode 100644 testcases/lib/tst_res_.c
include/tst_test.h +38 -0
@@ -331,6 +331,8 @@ struct tst_fs {* @child_needs_reinit: Has to be set if the test needs to call tst_reinit() * from a process started by exec(). * +* @runs_script: Implies child_needs_reinit and forks_child at the moment. +* * @needs_devfs: If set the devfs is mounted at tst_test.mntpoint. This is * needed for tests that need to create device files since tmpfs * at /tmp is usually mounted with 'nodev' option.
@@ -518,6 +520,7 @@ struct tst_fs { unsigned int mount_device:1; unsigned int needs_rofs:1; unsigned int child_needs_reinit:1; + unsigned int runs_script:1; unsigned int needs_devfs:1; unsigned int restore_wallclock:1;
@@ -526,6 +529,8 @@ struct tst_fs { unsigned int skip_in_lockdown:1; unsigned int skip_in_secureboot:1; unsigned int skip_in_compat:1; + +int needs_abi_bits; unsigned int needs_hugetlbfs:1;
@@ -611,6 +616,39 @@ void tst_run_tcases(int argc, char *argv[], struct tst_test *self)*/ void tst_reinit (void ); +/** +* tst_run_shell() - Prepare the environment and execute a shell script. +* +* @script_name: A filename of the script. +* @params: A NULL terminated array of shell script parameters, pass NULL if +* none are needed. This what is passed starting from argv[1]. +* +* The shell script is executed with LTP_IPC_PATH in environment so that the +* binary helpers such as tst_res_ or tst_checkpoint work properly when executed +* from the script. This also means that the tst_test.runs_script flag needs to +* be set. +* +* The shell script itself has to source the tst_env.sh shell script at the +* start and after that it's free to use tst_res in the same way C code would +* use. +* +* Example shell script that reports success:: +* +* #!/bin/sh +* . tst_env.sh +* +* tst_res TPASS "Example test works" +* +* The call returns a pid in a case that you want to examine the return value +* of the script yourself. If you do not need to check the return value +* yourself you can use tst_reap_children() to wait for the completion. Or let +* the test library collect the child automatically, just be wary that the +* script and the test both runs concurently at the same time in this case. +* +* Return: A pid of the shell process. +*/ +int tst_run_shell (char *script_name,char *const params[]); +unsigned int tst_multiply_timeout (unsigned int timeout);/*
lib/tst_test.c +51 -0
69 lines not on this page · raw message
testcases/lib/.gitignore +1 -0
@@ -23,3 +23,4 @@ /tst_sleep /tst_supported_fs /tst_timeout_kill +/tst_res_
testcases/lib/Makefile +2 -2
@@ -13,6 +13,6 @@ 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_ns_ifmove tst_lockdown_enabled tst_secureboot_enabled tst_res_ -include $(top_srcdir)/include/mk/generic_leaf_target.mk +include $(top_srcdir)/include/mk/generic_trunk_target.mk
testcases/lib/run_tests.sh new file +11 -0
@@ -0,0 +1,11 @@ +#!/bin/sh + +testdir=$(realpath $(dirname $0)) +export PATH=$PATH:$testdir:$testdir/tests/ + +for i in `seq -w 01 06`; do + echo + echo "*** Running shell_test$i ***" + echo + ./tests/shell_test$i +done
testcases/lib/tests/.gitignore new file +6 -0
@@ -0,0 +1,6 @@ +shell_test01 +shell_test02 +shell_test03 +shell_test04 +shell_test05 +shell_test06
testcases/lib/tests/Makefile new file +11 -0
@@ -0,0 +1,11 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# Copyright (C) 2009, Cisco Systems Inc. +# Ngie Cooper, August 2009 + +top_srcdir ?= ../../.. + +include $(top_srcdir)/include/mk/testcases.mk + +INSTALL_TARGETS= + +include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/lib/tests/shell_test01.c new file +17 -0
@@ -0,0 +1,17 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Shell test example. +*/ + +#include "tst_test.h" + +static void run_test (void ) +{ +tst_run_shell ("shell_test_pass.sh" ,NULL ); +tst_res (TINFO ,"C test exits now" ); +} + +static struct tst_test test = { + .runs_script =1 , + .test_all = run_test, +};
testcases/lib/tests/shell_test02.c new file +18 -0
@@ -0,0 +1,18 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Shell test example. +*/ + +#include "tst_test.h" + +static void run_test (void ) +{ +tst_run_shell ("shell_test_pass.sh" ,NULL ); +tst_reap_children (); +tst_res (TINFO ,"Shell test has finished at this point!" ); +} + +static struct tst_test test = { + .runs_script =1 , + .test_all = run_test, +};
testcases/lib/tests/shell_test03.c new file +25 -0
@@ -0,0 +1,25 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Shell test example. +*/ + +#include <sys/wait.h> +#include "tst_test.h" + +static void run_test (void ) +{ +int pid, status; + + pid =tst_run_shell ("shell_test_pass.sh" ,NULL ); + +tst_res (TINFO ,"Waiting for the pid %i" , pid); + +waitpid (pid, &status,0 ); + +tst_res (TINFO ,"Shell test has %s" ,tst_strstatus (status)); +} + +static struct tst_test test = { + .runs_script =1 , + .test_all = run_test, +};
testcases/lib/tests/shell_test04.c new file +18 -0
@@ -0,0 +1,18 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Shell test example. +*/ + +#include "tst_test.h" + +static void run_test (void ) +{ +char *const params[] = {"param1" ,"param2" ,NULL }; + +tst_run_shell ("shell_test_check_argv.sh" , params); +} + +static struct tst_test test = { + .runs_script =1 , + .test_all = run_test, +};
testcases/lib/tests/shell_test05.c new file +27 -0
@@ -0,0 +1,27 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Shell test example. +*/ + +#include "tst_test.h" + +static void run_test (void ) +{ +int pid; + + pid =tst_run_shell ("shell_test_checkpoint.sh" ,NULL ); + +tst_res (TINFO ,"Waiting for shell to sleep on checkpoint!" ); + +TST_PROCESS_STATE_WAIT (pid,'S' ,10000 ); + +tst_res (TINFO ,"Waking shell child!" ); + +TST_CHECKPOINT_WAKE (0 ); +} + +static struct tst_test test = { + .runs_script =1 , + .needs_checkpoints =1 , + .test_all = run_test, +};
testcases/lib/tests/shell_test06.c new file +16 -0
@@ -0,0 +1,16 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Shell test example. +*/ + +#include "tst_test.h" + +static void run_test (void ) +{ +tst_run_shell ("shell_test_brk.sh" ,NULL ); +} + +static struct tst_test test = { + .runs_script =1 , + .test_all = run_test, +};
testcases/lib/tests/shell_test_brk.sh new file +6 -0
@@ -0,0 +1,6 @@ +#!/bin/sh + +. tst_env.sh + +tst_brk TCONF "This exits test and the next message should not be reached" +tst_res TFAIL "If you see this the test failed"
testcases/lib/tests/shell_test_check_argv.sh new file +23 -0
@@ -0,0 +1,23 @@ +#!/bin/sh + +. tst_env.sh + +tst_res TINFO "argv = $@" + +if [ $# -ne 2 ]; then + tst_res TFAIL "Wrong number of parameters got $# expected 2" +else + tst_res TPASS "Got 2 parameters" +fi + +if [ "$1" != "param1" ]; then + tst_res TFAIL "First parameter is $1 expected param1" +else + tst_res TPASS "First parameter is $1" +fi + +if [ "$2" != "param2" ]; then + tst_res TFAIL "Second parameter is $2 expected param2" +else + tst_res TPASS "Second parameter is $2" +fi
testcases/lib/tests/shell_test_checkpoint.sh new file +7 -0
@@ -0,0 +1,7 @@ +#!/bin/sh + +. tst_env.sh + +tst_res TINFO "Waiting for a checkpoint 0" +tst_checkpoint wait 10000 0 +tst_res TPASS "Continuing after checkpoint"
testcases/lib/tests/shell_test_pass.sh new file +6 -0
@@ -0,0 +1,6 @@ +#!/bin/sh + +. tst_env.sh + +tst_res TPASS "This is called from the shell script!" +tst_sleep 100ms
testcases/lib/tst_env.sh new file +21 -0
@@ -0,0 +1,21 @@ +#!/bin/sh + +tst_script_name=$(basename $0) + +if [ -z "$LTP_IPC_PATH" ]; then + echo "This script has to be executed from a LTP loader!" + exit 1 +fi + +tst_brk_() +{ + tst_res_ "$@" + + case "$3" in + "TBROK") exit 2;; + *) exit 0;; + esac +} + +alias tst_res="tst_res_ $tst_script_name \$LINENO" +alias tst_brk="tst_brk_ $tst_script_name \$LINENO"
testcases/lib/tst_res_.c new file +58 -0
58 lines not on this page · raw message
Signature · 5 lines
2.44.2 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 1/3] Add support for mixing C and shell code
Cyril Hrubis <[email protected]> writes:
Hello Cyril,
In general I like the idea except that it will encourage more shell usage, but I'm guessing that is a battle that has already been lost. Also if a suitable embedded scripting language were found this is a starting point for that.
Quoted 56 lines
This is a proof of a concept of a seamless C and shell integration. The idea is that with this you can mix shell and C code as much as as you wish to get the best of the two worlds.
Signed-off-by Cyril Hrubis <[email protected]>--- include/tst_test.h | 38 +++++++++++++ lib/tst_test.c | 51 +++++++++++++++++ testcases/lib/.gitignore | 1 + testcases/lib/Makefile | 4 +- testcases/lib/run_tests.sh | 11 ++++ testcases/lib/tests/.gitignore | 6 ++ testcases/lib/tests/Makefile | 11 ++++ testcases/lib/tests/shell_test01.c | 17 ++++++ testcases/lib/tests/shell_test02.c | 18 ++++++ testcases/lib/tests/shell_test03.c | 25 +++++++++ testcases/lib/tests/shell_test04.c | 18 ++++++ testcases/lib/tests/shell_test05.c | 27 +++++++++ testcases/lib/tests/shell_test06.c | 16 ++++++ testcases/lib/tests/shell_test_brk.sh | 6 ++ testcases/lib/tests/shell_test_check_argv.sh | 23 ++++++++ testcases/lib/tests/shell_test_checkpoint.sh | 7 +++ testcases/lib/tests/shell_test_pass.sh | 6 ++ testcases/lib/tst_env.sh | 21 +++++++ testcases/lib/tst_res_.c | 58 ++++++++++++++++++++ 19 files changed, 362 insertions(+), 2 deletions(-) create mode 100755 testcases/lib/run_tests.sh create mode 100644 testcases/lib/tests/.gitignore create mode 100644 testcases/lib/tests/Makefile create mode 100644 testcases/lib/tests/shell_test01.c create mode 100644 testcases/lib/tests/shell_test02.c create mode 100644 testcases/lib/tests/shell_test03.c create mode 100644 testcases/lib/tests/shell_test04.c create mode 100644 testcases/lib/tests/shell_test05.c create mode 100644 testcases/lib/tests/shell_test06.c create mode 100755 testcases/lib/tests/shell_test_brk.sh create mode 100755 testcases/lib/tests/shell_test_check_argv.sh create mode 100755 testcases/lib/tests/shell_test_checkpoint.sh create mode 100755 testcases/lib/tests/shell_test_pass.sh create mode 100644 testcases/lib/tst_env.sh create mode 100644 testcases/lib/tst_res_.cinclude/tst_test.h +2
-0@@ -331,6 +331,8 @@ struct tst_fs {* @child_needs_reinit: Has to be set if the test needs to call tst_reinit() * from a process started by exec(). * +* @runs_script: Implies child_needs_reinit and forks_child at the moment. +* * @needs_devfs: If set the devfs is mounted at tst_test.mntpoint. This is * needed for tests that need to create device files since tmpfs * at /tmp is usually mounted with 'nodev' option.
include/tst_test.h struct tst_fs { · line 523
Quoted 4 lines
include/tst_test.h +1
-0@@ -518,6 +520,7 @@ struct tst_fs { unsigned int mount_device:1; unsigned int needs_rofs:1; unsigned int child_needs_reinit:1; + unsigned int runs_script:1;
This could be a string constant instead of a flag if you want to future proof against multiple scripting languages or you could change it to runs_shell.
Quoted 75 lines
include/tst_test.h +35
-0unsigned int needs_devfs:1; unsigned int restore_wallclock:1;@@ -526,6 +529,8 @@ struct tst_fs { unsigned int skip_in_lockdown:1; unsigned int skip_in_secureboot:1; unsigned int skip_in_compat:1; + +int needs_abi_bits; unsigned int needs_hugetlbfs:1;@@ -611,6 +616,39 @@ void tst_run_tcases(int argc, char *argv[], struct tst_test *self)*/ void tst_reinit (void ); +/** +* tst_run_shell() - Prepare the environment and execute a shell script. +* +* @script_name: A filename of the script. +* @params: A NULL terminated array of shell script parameters, pass NULL if +* none are needed. This what is passed starting from argv[1]. +* +* The shell script is executed with LTP_IPC_PATH in environment so that the +* binary helpers such as tst_res_ or tst_checkpoint work properly when executed +* from the script. This also means that the tst_test.runs_script flag needs to +* be set. +* +* The shell script itself has to source the tst_env.sh shell script at the +* start and after that it's free to use tst_res in the same way C code would +* use. +* +* Example shell script that reports success:: +* +* #!/bin/sh +* . tst_env.sh +* +* tst_res TPASS "Example test works" +* +* The call returns a pid in a case that you want to examine the return value +* of the script yourself. If you do not need to check the return value +* yourself you can use tst_reap_children() to wait for the completion. Or let +* the test library collect the child automatically, just be wary that the +* script and the test both runs concurently at the same time in this case. +* +* Return: A pid of the shell process. +*/ +int tst_run_shell (char *script_name,char *const params[]); +unsigned int tst_multiply_timeout (unsigned int timeout);/* lib/tst_test.c +8
-0@@ -4,6 +4,8 @@* Copyright (c) Linux Test Project, 2016-2024 */ +#define _GNU_SOURCE +#include <limits.h> #include <stdio.h> #include <stdarg.h> @@ -173,6 +175,50 @@ void tst_reinit(void)SAFE_CLOSE (fd); } +extern char **environ; + +static unsigned int params_array_len (char *const array[]) +{ +unsigned int ret =0 ; +
lib/tst_test.c void tst_reinit(void) · line 193
Quoted 10 lines
lib/tst_test.c +10
-0+if (!array) +return 0 ; + +while (*(array++)) + ret++; + +return ret; +} + +int tst_run_shell (char *script_name,char *const params[])
script_name should really be const as this is an API
lib/tst_test.c void tst_reinit(void) · line 202
Quoted 9 lines
lib/tst_test.c +9
-0+{ +int pid; +unsigned int i, params_len =params_array_len (params); +char *argv[params_len +2 ]; + +if (!tst_test->runs_script) +tst_brk (TBROK ,"runs_script flag must be set!" ); + + argv[0 ] = script_name;
maybe you'd have to cast it here or something, but IMO worth it
Signature · 6 lines
Thank you, Richard, richiejp.com. -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 1/3] Add support for mixing C and shell code
Hi!
Quoted 3 lines
In general I like the idea except that it will encourage more shell usage, but I'm guessing that is a battle that has already been lost.
Indeed. However messy shell is we are not going to get rid of it.
Quoted 2 lines
Also if a suitable embedded scripting language were found this is a starting point for that.
My hopes are not that high, LTP targets embedded, so we are quite limited in what we can do here and having python or perl as a dependency is a no-go. Perhaps there is a minimalistic scripting language that integrated into UNIX well out there, but I'm not aware of any. So as far as I can tell we are stuck with shell for the foreseeable future.
Quoted 55 lines
Quoted 55 lines
This is a proof of a concept of a seamless C and shell integration. The idea is that with this you can mix shell and C code as much as as you wish to get the best of the two worlds.
Signed-off-by Cyril Hrubis <[email protected]>--- include/tst_test.h | 38 +++++++++++++ lib/tst_test.c | 51 +++++++++++++++++ testcases/lib/.gitignore | 1 + testcases/lib/Makefile | 4 +- testcases/lib/run_tests.sh | 11 ++++ testcases/lib/tests/.gitignore | 6 ++ testcases/lib/tests/Makefile | 11 ++++ testcases/lib/tests/shell_test01.c | 17 ++++++ testcases/lib/tests/shell_test02.c | 18 ++++++ testcases/lib/tests/shell_test03.c | 25 +++++++++ testcases/lib/tests/shell_test04.c | 18 ++++++ testcases/lib/tests/shell_test05.c | 27 +++++++++ testcases/lib/tests/shell_test06.c | 16 ++++++ testcases/lib/tests/shell_test_brk.sh | 6 ++ testcases/lib/tests/shell_test_check_argv.sh | 23 ++++++++ testcases/lib/tests/shell_test_checkpoint.sh | 7 +++ testcases/lib/tests/shell_test_pass.sh | 6 ++ testcases/lib/tst_env.sh | 21 +++++++ testcases/lib/tst_res_.c | 58 ++++++++++++++++++++ 19 files changed, 362 insertions(+), 2 deletions(-) create mode 100755 testcases/lib/run_tests.sh create mode 100644 testcases/lib/tests/.gitignore create mode 100644 testcases/lib/tests/Makefile create mode 100644 testcases/lib/tests/shell_test01.c create mode 100644 testcases/lib/tests/shell_test02.c create mode 100644 testcases/lib/tests/shell_test03.c create mode 100644 testcases/lib/tests/shell_test04.c create mode 100644 testcases/lib/tests/shell_test05.c create mode 100644 testcases/lib/tests/shell_test06.c create mode 100755 testcases/lib/tests/shell_test_brk.sh create mode 100755 testcases/lib/tests/shell_test_check_argv.sh create mode 100755 testcases/lib/tests/shell_test_checkpoint.sh create mode 100755 testcases/lib/tests/shell_test_pass.sh create mode 100644 testcases/lib/tst_env.sh create mode 100644 testcases/lib/tst_res_.cinclude/tst_test.h +2
-0@@ -331,6 +331,8 @@ struct tst_fs {* @child_needs_reinit: Has to be set if the test needs to call tst_reinit() * from a process started by exec(). * +* @runs_script: Implies child_needs_reinit and forks_child at the moment. +* * @needs_devfs: If set the devfs is mounted at tst_test.mntpoint. This is * needed for tests that need to create device files since tmpfs * at /tmp is usually mounted with 'nodev' option.
Quoted 8 lines
include/tst_test.h struct tst_fs { · line 523
Quoted 5 lines
include/tst_test.h +1
-0@@ -518,6 +520,7 @@ struct tst_fs { unsigned int mount_device:1; unsigned int needs_rofs:1; unsigned int child_needs_reinit:1; + unsigned int runs_script:1;This could be a string constant instead of a flag if you want to future proof against multiple scripting languages or you could change it to
runs_shell.
Heh, Martin Doucha requtested runs_shell to be changed to runs_script, and that was a rightful request, since there is actually nothing shell specific in the test library, it just sets up env variables and passes command line option to something that is executed by execvpe(). All that is missing for any other scripting language is a bit of glue code that would interface the shared memory passed down in LTP_IPC_ENV.
So quite contrary the tst_run_shell() function should be renamed to tst_run_script() as well, which I will do in v3.
Quoted 9 lines
include/tst_test.h struct tst_fs {
Quoted 8 lines
include/tst_test.h +8
-0+int pid; +unsigned int i, params_len =params_array_len (params); +char *argv[params_len +2 ]; + +if (!tst_test->runs_script) +tst_brk (TBROK ,"runs_script flag must be set!" ); + + argv[0 ] = script_name;maybe you'd have to cast it here or something, but IMO worth it
Hmm, I was expecting that we would have to copy the data, but casting seems to work, will do.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 2/3 libs: Vendor ujson library
In reply to Cyril HrubisSee: https://github.com/metan-ucw/ujson
--- include/ujson.h | 13 + include/ujson_common.h | 69 +++ include/ujson_reader.h | 539 ++++++++++++++++++ include/ujson_utf.h | 168 ++++++ include/ujson_writer.h | 224 ++++++++ libs/ujson/Makefile | 12 + libs/ujson/ujson_common.c | 38 ++ libs/ujson/ujson_reader.c | 1081 +++++++++++++++++++++++++++++++++++++ libs/ujson/ujson_utf.c | 105 ++++ libs/ujson/ujson_writer.c | 491 +++++++++++++++++ 10 files changed, 2740 insertions(+) create mode 100644 include/ujson.h create mode 100644 include/ujson_common.h create mode 100644 include/ujson_reader.h create mode 100644 include/ujson_utf.h create mode 100644 include/ujson_writer.h create mode 100644 libs/ujson/Makefile create mode 100644 libs/ujson/ujson_common.c create mode 100644 libs/ujson/ujson_reader.c create mode 100644 libs/ujson/ujson_utf.c create mode 100644 libs/ujson/ujson_writer.c
include/ujson.h new file +13 -0
@@ -0,0 +1,13 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later +/* +* Copyright (C) 2021-2024 Cyril Hrubis <[email protected]> +*/ + +#ifndef UJSON_H +#define UJSON_H + +#include <ujson_common.h> +#include <ujson_reader.h> +#include <ujson_writer.h> + +#endif /* UJSON_H */
include/ujson_common.h new file +69 -0
69 lines not on this page · raw message
include/ujson_reader.h new file +539 -0
539 lines not on this page · raw message
include/ujson_utf.h new file +168 -0
168 lines not on this page · raw message
include/ujson_writer.h new file +224 -0
224 lines not on this page · raw message
libs/ujson/Makefile new file +12 -0
@@ -0,0 +1,12 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# +# Copyright (C) Cyril Hrubis <[email protected]> + +top_srcdir ?= ../.. + +include $(top_srcdir)/include/mk/env_pre.mk + +INTERNAL_LIB := libujson.a + +include $(top_srcdir)/include/mk/lib.mk +include $(top_srcdir)/include/mk/generic_leaf_target.mk
libs/ujson/ujson_common.c new file +38 -0
@@ -0,0 +1,38 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later +/* +* Copyright (C) 2021-2024 Cyril Hrubis <[email protected]> +*/ + +#include <stdio.h> +#include "ujson_common.h" + +void ujson_err_handler (void *err_print_priv,const char *line) +{ +fputs (line, err_print_priv); +putc ('\n' , err_print_priv); +} + +const char *ujson_type_name (enum ujson_type type) +{ +switch (type) { +case UJSON_VOID : +return "void" ; +case UJSON_INT : +return "integer" ; +case UJSON_FLOAT : +return "float" ; +case UJSON_BOOL : +return "boolean" ; +case UJSON_NULL : +return "null" ; +case UJSON_STR : +return "string" ; +case UJSON_OBJ : +return "object" ; +case UJSON_ARR : +return "array" ; +default : +return "invalid" ; + } +} +
libs/ujson/ujson_reader.c new file +1081 -0
1,081 lines not on this page · raw message
libs/ujson/ujson_utf.c new file +105 -0
105 lines not on this page · raw message
libs/ujson/ujson_writer.c new file +491 -0
491 lines not on this page · raw message
Signature · 5 lines
2.44.2 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 2/3] libs: Vendor ujson library
I spent quite some time in the past looking at this lib and don't think I found any issues that I haven't already mentioned to you, so:
Signature · 6 lines
Thank you, Richard, richiejp.com. -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 3/3 testcaes/lib: Add shell loader
In reply to Cyril HrubisThis 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
--- include/tst_test.h | 2 +- testcases/lib/.gitignore | 1 + testcases/lib/Makefile | 6 +- testcases/lib/run_tests.sh | 9 + testcases/lib/tests/shell_loader.sh | 15 + .../lib/tests/shell_loader_all_filesystems.sh | 24 ++ .../lib/tests/shell_loader_filesystems.sh | 30 ++ .../tests/shell_loader_invalid_metadata.sh | 12 + .../lib/tests/shell_loader_no_metadata.sh | 8 + .../lib/tests/shell_loader_supported_archs.sh | 9 + .../lib/tests/shell_loader_wrong_metadata.sh | 12 + testcases/lib/tst_env.sh | 4 + testcases/lib/tst_loader.sh | 11 + testcases/lib/tst_run_shell.c | 378 ++++++++++++++++++ 14 files changed, 519 insertions(+), 2 deletions(-) create mode 100755 testcases/lib/tests/shell_loader.sh create mode 100755 testcases/lib/tests/shell_loader_all_filesystems.sh create mode 100755 testcases/lib/tests/shell_loader_filesystems.sh create mode 100755 testcases/lib/tests/shell_loader_invalid_metadata.sh create mode 100755 testcases/lib/tests/shell_loader_no_metadata.sh create mode 100755 testcases/lib/tests/shell_loader_supported_archs.sh create mode 100755 testcases/lib/tests/shell_loader_wrong_metadata.sh create mode 100644 testcases/lib/tst_loader.sh create mode 100644 testcases/lib/tst_run_shell.c
include/tst_test.h +1 -1
@@ -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; };
testcases/lib/.gitignore +1 -0
@@ -24,3 +24,4 @@ /tst_supported_fs /tst_timeout_kill /tst_res_ +/tst_run_shell
testcases/lib/Makefile +5 -1
@@ -4,6 +4,9 @@ top_srcdir ?= ../.. +LTPLIBS = ujson +tst_run_shell: LTPLDLIBS = -lujson + include $(top_srcdir)/include/mk/testcases.mk INSTALL_TARGETS := *.sh
@@ -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
testcases/lib/run_tests.sh +9 -0
@@ -9,3 +9,12 @@ 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; do + echo + echo "*** Running $i ***" + echo + $i +done
testcases/lib/tests/shell_loader.sh new file +15 -0
@@ -0,0 +1,15 @@ +#!/bin/sh +# +# TEST = { +# "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
testcases/lib/tests/shell_loader_all_filesystems.sh new file +24 -0
@@ -0,0 +1,24 @@ +#!/bin/sh +# +# TEST = { +# "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
testcases/lib/tests/shell_loader_filesystems.sh new file +30 -0
@@ -0,0 +1,30 @@ +#!/bin/sh +# +# TEST = { +# "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
testcases/lib/tests/shell_loader_invalid_metadata.sh new file +12 -0
@@ -0,0 +1,12 @@ +#!/bin/sh +# +# This test has wrong metadata and should not be run +# +# TEST = { +# {"needs_tmpdir": 42, +# } +# + +. tst_loader.sh + +tst_res TFAIL "Shell loader should TBROK the test"
testcases/lib/tests/shell_loader_no_metadata.sh new file +8 -0
@@ -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"
testcases/lib/tests/shell_loader_supported_archs.sh new file +9 -0
@@ -0,0 +1,9 @@ +#!/bin/sh +# +# TEST = { +# "supported_archs": ["x86", "ppc64", "x86_64"] +# } + +. tst_loader.sh + +tst_res TPASS "We are running on supported architecture"
testcases/lib/tests/shell_loader_wrong_metadata.sh new file +12 -0
@@ -0,0 +1,12 @@ +#!/bin/sh +# +# This test has wrong metadata and should not be run +# +# TEST = { +# "needs_tmpdir": 42, +# } +# + +. tst_loader.sh + +tst_res TFAIL "Shell loader should TBROK the test"
testcases/lib/tst_env.sh +4 -0
@@ -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)
testcases/lib/tst_loader.sh new file +11 -0
@@ -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
testcases/lib/tst_run_shell.c new file +378 -0
378 lines not on this page · raw message
Signature · 5 lines
2.44.2 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 3/3] testcaes/lib: Add shell loader
Quoted 318 lines · Hello Cyril Hrubis
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_teststructure 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 requiredSigned-off-by Cyril Hrubis <[email protected]>--- include/tst_test.h | 2 +- testcases/lib/.gitignore | 1 + testcases/lib/Makefile | 6 +- testcases/lib/run_tests.sh | 9 + testcases/lib/tests/shell_loader.sh | 15 + .../lib/tests/shell_loader_all_filesystems.sh | 24 ++ .../lib/tests/shell_loader_filesystems.sh | 30 ++ .../tests/shell_loader_invalid_metadata.sh | 12 + .../lib/tests/shell_loader_no_metadata.sh | 8 + .../lib/tests/shell_loader_supported_archs.sh | 9 + .../lib/tests/shell_loader_wrong_metadata.sh | 12 + testcases/lib/tst_env.sh | 4 + testcases/lib/tst_loader.sh | 11 + testcases/lib/tst_run_shell.c | 378 ++++++++++++++++++ 14 files changed, 519 insertions(+), 2 deletions(-) create mode 100755 testcases/lib/tests/shell_loader.sh create mode 100755 testcases/lib/tests/shell_loader_all_filesystems.sh create mode 100755 testcases/lib/tests/shell_loader_filesystems.sh create mode 100755 testcases/lib/tests/shell_loader_invalid_metadata.sh create mode 100755 testcases/lib/tests/shell_loader_no_metadata.sh create mode 100755 testcases/lib/tests/shell_loader_supported_archs.sh create mode 100755 testcases/lib/tests/shell_loader_wrong_metadata.sh create mode 100644 testcases/lib/tst_loader.sh create mode 100644 testcases/lib/tst_run_shell.cinclude/tst_test.h +1
-1@@ -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; };testcases/lib/.gitignore +1
-0@@ -24,3 +24,4 @@ /tst_supported_fs /tst_timeout_kill /tst_res_ +/tst_run_shelltestcases/lib/Makefile +5
-1@@ -4,6 +4,9 @@ top_srcdir ?= ../.. +LTPLIBS = ujson +tst_run_shell: LTPLDLIBS = -lujson + include $(top_srcdir)/include/mk/testcases.mk INSTALL_TARGETS := *.sh@@ -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.mktestcases/lib/run_tests.sh +9
-0@@ -9,3 +9,12 @@ 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; do + echo + echo "*** Running $i ***" + echo + $i +donetestcases/lib/tests/shell_loader.sh new file +15
-0@@ -0,0 +1,15 @@ +#!/bin/sh +# +# TEST = { +# "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";; +esactestcases/lib/tests/shell_loader_all_filesystems.sh new file +24
-0@@ -0,0 +1,24 @@ +#!/bin/sh +# +# TEST = { +# "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!" +fitestcases/lib/tests/shell_loader_filesystems.sh new file +30
-0@@ -0,0 +1,30 @@ +#!/bin/sh +# +# TEST = { +# "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!" +fitestcases/lib/tests/shell_loader_invalid_metadata.sh new file +12
-0@@ -0,0 +1,12 @@ +#!/bin/sh +# +# This test has wrong metadata and should not be run +# +# TEST = { +# {"needs_tmpdir": 42, +# } +# + +. tst_loader.sh + +tst_res TFAIL "Shell loader should TBROK the test"testcases/lib/tests/shell_loader_no_metadata.sh new file +8
-0@@ -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"testcases/lib/tests/shell_loader_supported_archs.sh new file +9
-0@@ -0,0 +1,9 @@ +#!/bin/sh +# +# TEST = { +# "supported_archs": ["x86", "ppc64", "x86_64"] +# } + +. tst_loader.sh + +tst_res TPASS "We are running on supported architecture"testcases/lib/tests/shell_loader_wrong_metadata.sh new file +12
-0@@ -0,0 +1,12 @@ +#!/bin/sh +# +# This test has wrong metadata and should not be run +# +# TEST = { +# "needs_tmpdir": 42, +# } +# + +. tst_loader.sh + +tst_res TFAIL "Shell loader should TBROK the test"testcases/lib/tst_env.sh +4
-0@@ -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)testcases/lib/tst_loader.sh new file +11
-0@@ -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 +fitestcases/lib/tst_run_shell.c new file +28
-0@@ -0,0 +1,378 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2024 Cyril Hrubis <[email protected]> +*/ +#include <sys/mount.h> + +#define TST_NO_DEFAULT_MAIN +#include "tst_test.h" +#include "tst_safe_stdio.h" +#include "ujson.h" + +static char *shell_filename; + +static void run_shell (void ) +{ +tst_run_shell (shell_filename,NULL ); +} + +struct tst_test test = { + .test_all = run_shell, + .runs_script =1 , +}; + +static void print_help (void ) +{ +printf ("Usage: tst_shell_loader ltp_shell_test.sh ..." ); +} +
testcases/lib/tst_run_shell.c line 38
Quoted 10 lines
testcases/lib/tst_run_shell.c new file +10
-0+static char *metadata; +static size_t metadata_size; +static size_t metadata_used; + +static void metadata_append (const char *line) +{ +size_t linelen =strlen (line); + +if (metadata_size - metadata_used < linelen +1 ) { + metadata_size +=128 ;
This seems like a very small amount to bother allocating.
Quoted 29 lines
testcases/lib/tst_run_shell.c new file +29
-0+ metadata =SAFE_REALLOC (metadata, metadata_size); + } + +strcpy (metadata + metadata_used, line); + metadata_used += linelen; +} + +static ujson_obj_attr test_attrs[] = { +UJSON_OBJ_ATTR ("all_filesystems" ,UJSON_BOOL ), +UJSON_OBJ_ATTR ("dev_min_size" ,UJSON_INT ), +UJSON_OBJ_ATTR ("filesystems" ,UJSON_ARR ), +UJSON_OBJ_ATTR ("format_device" ,UJSON_BOOL ), +UJSON_OBJ_ATTR ("min_cpus" ,UJSON_INT ), +UJSON_OBJ_ATTR ("min_mem_avail" ,UJSON_INT ), +UJSON_OBJ_ATTR ("min_kver" ,UJSON_STR ), +UJSON_OBJ_ATTR ("min_swap_avail" ,UJSON_INT ), +UJSON_OBJ_ATTR ("mntpoint" ,UJSON_STR ), +UJSON_OBJ_ATTR ("mount_device" ,UJSON_BOOL ), +UJSON_OBJ_ATTR ("needs_abi_bits" ,UJSON_INT ), +UJSON_OBJ_ATTR ("needs_devfs" ,UJSON_BOOL ), +UJSON_OBJ_ATTR ("needs_device" ,UJSON_BOOL ), +UJSON_OBJ_ATTR ("needs_hugetlbfs" ,UJSON_BOOL ), +UJSON_OBJ_ATTR ("needs_rofs" ,UJSON_BOOL ), +UJSON_OBJ_ATTR ("needs_root" ,UJSON_BOOL ), +UJSON_OBJ_ATTR ("needs_tmpdir" ,UJSON_BOOL ), +UJSON_OBJ_ATTR ("restore_wallclock" ,UJSON_BOOL ), +UJSON_OBJ_ATTR ("skip_filesystems" ,UJSON_ARR ), +UJSON_OBJ_ATTR ("skip_in_compat" ,UJSON_BOOL ), +UJSON_OBJ_ATTR ("skip_in_lockdown" ,UJSON_BOOL ),
testcases/lib/tst_run_shell.c line 77
Quoted 10 lines
testcases/lib/tst_run_shell.c new file +10
-0+UJSON_OBJ_ATTR ("skip_in_secureboot" ,UJSON_BOOL ), + UJSON_OBJ_ATTR("supported_archs", UJSON_ARR), +}; + +static ujson_obj test_obj = { + .attrs = test_attrs, + .attr_cnt =UJSON_ARRAY_SIZE (test_attrs), +}; + +/* Must match the order of test_attrs. */
You could use the index syntax like [ALL_FILESYSTEMS] = UJASON_OBJ_ATTR... IIRC.
Then the order can't be messed up
Quoted 262 lines
testcases/lib/tst_run_shell.c new file +262
-0+enum test_attr_ids { +ALL_FILESYSTEMS , +DEV_MIN_SIZE , +FILESYSTEMS , +FORMAT_DEVICE , +MIN_CPUS , +MIN_MEM_AVAIL , +MIN_KVER , +MIN_SWAP_AVAIL , +MNTPOINT , +MOUNT_DEVICE , +NEEDS_ABI_BITS , +NEEDS_DEVFS , +NEEDS_DEVICE , +NEEDS_HUGETLBFS , +NEEDS_ROFS , +NEEDS_ROOT , +NEEDS_TMPDIR , +RESTORE_WALLCLOCK , +SKIP_FILESYSTEMS , +SKIP_IN_COMPAT , +SKIP_IN_LOCKDOWN , +SKIP_IN_SECUREBOOT , +SUPPORTED_ARCHS , +}; + +static const char *const *parse_strarr (ujson_reader *reader,ujson_val *val) +{ +unsigned int cnt =0 , i =0 ; +char **ret; + +ujson_reader_state state =ujson_reader_state_save (reader); + + UJSON_ARR_FOREACH(reader, val) { +if (val->type !=UJSON_STR ) { +ujson_err (reader,"Expected string!" ); +return NULL ; + } + + cnt++; + } + +ujson_reader_state_load (reader, state); + + ret =SAFE_MALLOC (sizeof (char *) * (cnt +1 )); + + UJSON_ARR_FOREACH(reader, val) { + ret[i++] =strdup (val->val_str); + } + + ret[i] =NULL ; + +return (const char *const *)ret; +} + +static ujson_obj_attr fs_attrs[] = { +UJSON_OBJ_ATTR ("mkfs_opts" ,UJSON_ARR ), +UJSON_OBJ_ATTR ("mkfs_size_opt" ,UJSON_STR ), +UJSON_OBJ_ATTR ("mnt_flags" ,UJSON_ARR ), +UJSON_OBJ_ATTR ("type" ,UJSON_STR ), +}; + +static ujson_obj fs_obj = { + .attrs = fs_attrs, + .attr_cnt =UJSON_ARRAY_SIZE (fs_attrs), +}; + +/* Must match the order of fs_attrs. */ +enum fs_ids { +MKFS_OPTS , +MKFS_SIZE_OPT , +MNT_FLAGS , +TYPE , +}; + +static int parse_mnt_flags (ujson_reader *reader,ujson_val *val) +{ +int ret =0 ; + + UJSON_ARR_FOREACH(reader, val) { +if (val->type !=UJSON_STR ) { +ujson_err (reader,"Expected string!" ); +return ret; + } + +if (!strcmp (val->val_str,"RDONLY" )) + ret |=MS_RDONLY ; +else if (!strcmp (val->val_str,"NOATIME" )) + ret |=MS_NOATIME ; +else if (!strcmp (val->val_str,"NOEXEC" )) + ret |=MS_NOEXEC ; +else if (!strcmp (val->val_str,"NOSUID" )) + ret |=MS_NOSUID ; +else +ujson_err (reader,"Invalid mount flag" ); + } + +return ret; +} + +struct tst_fs *parse_filesystems (ujson_reader *reader,ujson_val *val) +{ +unsigned int i =0 , cnt =0 ; +struct tst_fs *ret; + +ujson_reader_state state =ujson_reader_state_save (reader); + + UJSON_ARR_FOREACH(reader, val) { +if (val->type !=UJSON_OBJ ) { +ujson_err (reader,"Expected object!" ); +return NULL ; + } +ujson_obj_skip (reader); + cnt++; + } + +ujson_reader_state_load (reader, state); + + ret =SAFE_MALLOC (sizeof (struct tst_fs ) * (cnt +1 )); +memset (ret,0 ,sizeof (*ret) * (cnt+1 )); + + UJSON_ARR_FOREACH(reader, val) { + UJSON_OBJ_FOREACH_FILTER(reader, val, &fs_obj, ujson_empty_obj) { +switch ((enum fs_ids )val->idx) { +case MKFS_OPTS : + ret[i].mkfs_opts =parse_strarr (reader, val); +break ; +case MKFS_SIZE_OPT : + ret[i].mkfs_size_opt =strdup (val->val_str); +break ; +case MNT_FLAGS : + ret[i].mnt_flags =parse_mnt_flags (reader, val); +break ; +case TYPE : + ret[i].type =strdup (val->val_str); +break ; + } + + } + + i++; + } + +return ret; +} + +static void parse_metadata (void ) +{ +ujson_reader reader =UJSON_READER_INIT (metadata, metadata_used,UJSON_READER_STRICT ); +char str_buf[128 ]; +ujson_val val =UJSON_VAL_INIT (str_buf,sizeof (str_buf)); + + UJSON_OBJ_FOREACH_FILTER(&reader, &val, &test_obj, ujson_empty_obj) { +switch ((enum test_attr_ids )val.idx) { +case ALL_FILESYSTEMS : + test.all_filesystems = val.val_bool; +break ; +case DEV_MIN_SIZE : +if (val.val_int <=0 ) +ujson_err (&reader,"Device size must be > 0" ); +else + test.dev_min_size = val.val_int; +break ; +case FILESYSTEMS : + test.filesystems =parse_filesystems (&reader, &val); +break ; +case FORMAT_DEVICE : + test.format_device = val.val_bool; +break ; +case MIN_CPUS : +if (val.val_int <=0 ) +ujson_err (&reader,"Minimal number of cpus must be > 0" ); +else + test.min_cpus = val.val_int; +break ; +case MIN_MEM_AVAIL : +if (val.val_int <=0 ) +ujson_err (&reader,"Minimal available memory size must be > 0" ); +else + test.min_mem_avail = val.val_int; +break ; +case MIN_KVER : + test.min_kver =strdup (val.val_str); +break ; +case MIN_SWAP_AVAIL : +if (val.val_int <=0 ) +ujson_err (&reader,"Minimal available swap size must be > 0" ); +else + test.min_swap_avail = val.val_int; +break ; +case MNTPOINT : + test.mntpoint =strdup (val.val_str); +break ; +case MOUNT_DEVICE : + test.mount_device = val.val_bool; +break ; +case NEEDS_ABI_BITS : +if (val.val_int ==32 || val.val_int ==64 ) + test.needs_abi_bits = val.val_int; +else +ujson_err (&reader,"ABI bits must be 32 or 64" ); +break ; +case NEEDS_DEVFS : + test.needs_devfs = val.val_bool; +break ; +case NEEDS_DEVICE : + test.needs_device = val.val_bool; +break ; +case NEEDS_HUGETLBFS : + test.needs_hugetlbfs = val.val_bool; +break ; +case NEEDS_ROFS : + test.needs_rofs = val.val_bool; +break ; +case NEEDS_ROOT : + test.needs_root = val.val_bool; +break ; +case NEEDS_TMPDIR : + test.needs_tmpdir = val.val_bool; +break ; +case RESTORE_WALLCLOCK : + test.restore_wallclock = val.val_bool; +break ; +case SKIP_FILESYSTEMS : + test.skip_filesystems =parse_strarr (&reader, &val); +break ; +case SKIP_IN_COMPAT : + test.skip_in_compat = val.val_bool; +break ; +case SKIP_IN_LOCKDOWN : + test.skip_in_lockdown = val.val_bool; +break ; +case SKIP_IN_SECUREBOOT : + test.skip_in_secureboot = val.val_bool; +break ; +case SUPPORTED_ARCHS : + test.supported_archs =parse_strarr (&reader, &val); +break ; + } + } + +ujson_reader_finish (&reader); + +if (ujson_reader_err (&reader)) +tst_brk (TBROK ,"Invalid metadata" ); +} + +static void extract_metadata (void ) +{ +FILE *f; +char line[4096 ]; +char path[4096 ]; +int in_json =0 ; + +if (tst_get_path (shell_filename, path,sizeof (path)) ==-1 ) +tst_brk (TBROK ,"Failed to find %s in $PATH" , shell_filename); + + f =SAFE_FOPEN (path,"r" ); + +while (fgets (line,sizeof (line), f)) { +if (in_json) +metadata_append (line +2 );
testcases/lib/tst_run_shell.c line 349
Quoted 10 lines
testcases/lib/tst_run_shell.c new file +10
-0+ +if (in_json) { +if (!strcmp (line,"# }\n" )) + in_json =0 ; + }else { +if (!strcmp (line,"# TEST = {\n" )) { +metadata_append ("{\n" ); + in_json =1 ; + } + }
This is maybe a little bit too rigid, even if you want this exact formatting to be the only valid one. People will get frustrated when parsing fails due to whitespace and don't see a clear indication why it failed.
Alternativey you could adopt the standard markdown meta-data format where the meta-data is enclosed with three dashes like
---
"meta": "data"
---
at the top of the file. I suppose you could change it to have comments if you want the script to run outside LTP's runner.
In any case using three dots removes any whitespace between tokens which is harder to trim.
The whole patchset looks good though.
Signature · 6 lines
Thank you, Richard, richiejp.com. -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 3/3] testcaes/lib: Add shell loader
Hi!
Quoted 8 lines
testcases/lib/tst_run_shell.c line 38
Quoted 6 lines
testcases/lib/tst_run_shell.c new file +6
-0+static void metadata_append (const char *line) +{ +size_t linelen =strlen (line); + +if (metadata_size - metadata_used < linelen +1 ) { + metadata_size +=128 ;This seems like a very small amount to bother allocating.
That is what I have left there after the testing phase where I wanted to trigger the reallocation. I will change this to 4k.
Quoted 43 lines
Quoted 29 lines
testcases/lib/tst_run_shell.c new file +29
-0+ metadata =SAFE_REALLOC (metadata, metadata_size); + } + +strcpy (metadata + metadata_used, line); + metadata_used += linelen; +} + +static ujson_obj_attr test_attrs[] = { +UJSON_OBJ_ATTR ("all_filesystems" ,UJSON_BOOL ), +UJSON_OBJ_ATTR ("dev_min_size" ,UJSON_INT ), +UJSON_OBJ_ATTR ("filesystems" ,UJSON_ARR ), +UJSON_OBJ_ATTR ("format_device" ,UJSON_BOOL ), +UJSON_OBJ_ATTR ("min_cpus" ,UJSON_INT ), +UJSON_OBJ_ATTR ("min_mem_avail" ,UJSON_INT ), +UJSON_OBJ_ATTR ("min_kver" ,UJSON_STR ), +UJSON_OBJ_ATTR ("min_swap_avail" ,UJSON_INT ), +UJSON_OBJ_ATTR ("mntpoint" ,UJSON_STR ), +UJSON_OBJ_ATTR ("mount_device" ,UJSON_BOOL ), +UJSON_OBJ_ATTR ("needs_abi_bits" ,UJSON_INT ), +UJSON_OBJ_ATTR ("needs_devfs" ,UJSON_BOOL ), +UJSON_OBJ_ATTR ("needs_device" ,UJSON_BOOL ), +UJSON_OBJ_ATTR ("needs_hugetlbfs" ,UJSON_BOOL ), +UJSON_OBJ_ATTR ("needs_rofs" ,UJSON_BOOL ), +UJSON_OBJ_ATTR ("needs_root" ,UJSON_BOOL ), +UJSON_OBJ_ATTR ("needs_tmpdir" ,UJSON_BOOL ), +UJSON_OBJ_ATTR ("restore_wallclock" ,UJSON_BOOL ), +UJSON_OBJ_ATTR ("skip_filesystems" ,UJSON_ARR ), +UJSON_OBJ_ATTR ("skip_in_compat" ,UJSON_BOOL ), +UJSON_OBJ_ATTR ("skip_in_lockdown" ,UJSON_BOOL ),testcases/lib/tst_run_shell.c line 77
Quoted 10 lines
testcases/lib/tst_run_shell.c new file +10
-0+UJSON_OBJ_ATTR ("skip_in_secureboot" ,UJSON_BOOL ), + UJSON_OBJ_ATTR("supported_archs", UJSON_ARR), +}; + +static ujson_obj test_obj = { + .attrs = test_attrs, + .attr_cnt =UJSON_ARRAY_SIZE (test_attrs), +}; + +/* Must match the order of test_attrs. */ You could use the index syntax like [
ALL_FILESYSTEMS] =UJASON_OBJ_ATTR... IIRC.
Quoted 1 line
Then the order can't be messed up
Good idea, will do. Maybe it makes sense to embedded this into the UJSON_OBJ_ATTR() macro as a second parameter so that we have better syntax too.
Quoted 19 lines
testcases/lib/tst_run_shell.c line 349
Quoted 13 lines
testcases/lib/tst_run_shell.c new file +13
-0+while (fgets (line,sizeof (line), f)) { +if (in_json) +metadata_append (line +2 ); + +if (in_json) { +if (!strcmp (line,"# }\n" )) + in_json =0 ; + }else { +if (!strcmp (line,"# TEST = {\n" )) { +metadata_append ("{\n" ); + in_json =1 ; + } + }This is maybe a little bit too rigid, even if you want this exact formatting to be the only valid one. People will get frustrated when parsing fails due to whitespace and don't see a clear indication why it failed.
Quoted 6 lines
Alternativey you could adopt the standard markdown meta-data format where the meta-data is enclosed with three dashes like
---
"meta": "data"
---
That may be better, but we would need some markers for the asciidoc documentation as well, so we need some kind of identifier too.
So maybe:
# ---
# doc
#
# [Description]
#
# This test does bla bla.
# ---
#
# ---
# env
#
# {
# "needs_root": 1,
# }
# ---Getting the delimiters right when embedding data is hard...
Quoted 2 lines
at the top of the file. I suppose you could change it to have comments if you want the script to run outside LTP's runner.
We actually need it to be in the comments, because we run the script by bash first, which bootstraps the enviroment from the shell script we source first, then reexecutes the script in the right environment.
Quoted 4 lines
In any case using three dots removes any whitespace between tokens which is harder to trim.
The whole patchset looks good though.
Thanks a lot for the review, I will work on the small bits and send v3.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
And I've forget to amend the commit, so there is no support for tags etc. please ignore this, will send v2.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp