Skip to content

Commit b4fe9fe

Browse files
KP Singhanakryiko
KP Singh
authored andcommitted
selftests/bpf: Silence ima_setup.sh when not running in verbose mode.
Currently, ima_setup.sh spews outputs from commands like mkfs and dd on the terminal without taking into account the verbosity level of the test framework. Update test_progs to set the environment variable SELFTESTS_VERBOSE=1 when a verbose output is requested. This environment variable is then used by ima_setup.sh (and can be used by other similar scripts) to obey the verbosity level of the test harness without needing to re-implement command line options for verbosity. In "silent" mode, the script saves the output to a temporary file, the contents of which are echoed back to stderr when the script encounters an error. Fixes: 34b82d3 ("bpf: Add a selftest for bpf_ima_inode_hash") Reported-by: Andrii Nakryiko <[email protected]> Suggested-by: Andrii Nakryiko <[email protected]> Signed-off-by: KP Singh <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 89ad742 commit b4fe9fe

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

tools/testing/selftests/bpf/ima_setup.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ set -o pipefail
77

88
IMA_POLICY_FILE="/sys/kernel/security/ima/policy"
99
TEST_BINARY="/bin/true"
10+
VERBOSE="${SELFTESTS_VERBOSE:=0}"
11+
LOG_FILE="$(mktemp /tmp/ima_setup.XXXX.log)"
1012

1113
usage()
1214
{
@@ -75,6 +77,19 @@ run()
7577
exec "${copied_bin_path}"
7678
}
7779

80+
catch()
81+
{
82+
local exit_code="$1"
83+
local log_file="$2"
84+
85+
if [[ "${exit_code}" -ne 0 ]]; then
86+
cat "${log_file}" >&3
87+
fi
88+
89+
rm -f "${log_file}"
90+
exit ${exit_code}
91+
}
92+
7893
main()
7994
{
8095
[[ $# -ne 2 ]] && usage
@@ -96,4 +111,13 @@ main()
96111
fi
97112
}
98113

114+
trap 'catch "$?" "${LOG_FILE}"' EXIT
115+
116+
if [[ "${VERBOSE}" -eq 0 ]]; then
117+
# Save the stderr to 3 so that we can output back to
118+
# it incase of an error.
119+
exec 3>&2 1>"${LOG_FILE}" 2>&1
120+
fi
121+
99122
main "$@"
123+
rm -f "${LOG_FILE}"

tools/testing/selftests/bpf/test_progs.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,16 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
587587
return -EINVAL;
588588
}
589589
}
590+
591+
if (env->verbosity > VERBOSE_NONE) {
592+
if (setenv("SELFTESTS_VERBOSE", "1", 1) == -1) {
593+
fprintf(stderr,
594+
"Unable to setenv SELFTESTS_VERBOSE=1 (errno=%d)",
595+
errno);
596+
return -1;
597+
}
598+
}
599+
590600
break;
591601
case ARG_GET_TEST_CNT:
592602
env->get_test_cnt = true;

0 commit comments

Comments
 (0)