From: André Almeida Date: Wed, 30 Oct 2019 22:27:06 +0000 (-0300) Subject: check: Add configuration file option X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=4a5d2e87d654255dd5cda791bbc716a847046ec6;p=users%2Fhch%2Fblktests.git check: Add configuration file option Add an option to be possible to use a different configuration file rather than the default "config" file. Signed-off-by: André Almeida [Omar: rework so command line options still take precedence over config, document precedence] Signed-off-by: Omar Sandoval --- diff --git a/Documentation/running-tests.md b/Documentation/running-tests.md index 675dac7..cda75eb 100644 --- a/Documentation/running-tests.md +++ b/Documentation/running-tests.md @@ -20,8 +20,13 @@ will run all tests in the `loop` group and the `block/002` test. ## Configuration Test configuration goes in the `config` file at the top-level directory of the -blktests repository. Test configuration options can also be set as environment -variables instead of in the `config` file. +blktests repository. A different file can be specified with the `-c` command +line option. The `-c` option can be used multiple times; the files will all be +loaded in the order that they are specified on the command line. + +Test configuration options can also be set as environment variables. The +configuration file has precedence over environment variables, and command line +options have precedence over the configuration file. ### Test Devices diff --git a/check b/check index a19b9dc..5153dcd 100755 --- a/check +++ b/check @@ -636,6 +636,10 @@ Test runs: tests to run Miscellaneous: + -c, --config=FILE load configuration from FILE instead of the default + (\"./config\"); if this option is used multiple times, + all of the given files are loaded + -h, --help display this help message and exit" case "$1" in @@ -650,60 +654,39 @@ Miscellaneous: esac } -if ! TEMP=$(getopt -o 'do:q::x:h' --long 'device-only,quick::,exclude:,output:,help' -n "$0" -- "$@"); then +if ! TEMP=$(getopt -o 'do:q::x:c:h' --long 'device-only,quick::,exclude:,output:,config:,help' -n "$0" -- "$@"); then exit 1 fi eval set -- "$TEMP" unset TEMP -LOGGER_PROG="$(type -P logger)" || LOGGER_PROG=true - -if [[ -r config ]]; then - # shellcheck disable=SC1091 - . config -fi - -# Default configuration. -: "${DEVICE_ONLY:=0}" -: "${QUICK_RUN:=0}" -: "${RUN_ZONED_TESTS:=0}" -: "${OUTPUT:=results}" -if [[ "${EXCLUDE:-}" ]] && ! declare -p EXCLUDE | grep -q '^declare -a'; then - # If EXCLUDE was not defined as an array, convert it to one. - # shellcheck disable=SC2128,SC2190,SC2206 - EXCLUDE=($EXCLUDE) -elif [[ ! "${EXCLUDE:-}" ]]; then - EXCLUDE=() -fi -if [[ "${TEST_DEVS:-}" ]] && ! declare -p TEST_DEVS | grep -q '^declare -a'; then - # If TEST_DEVS was not defined as an array, convert it to one. - # shellcheck disable=SC2128,SC2206 - TEST_DEVS=($TEST_DEVS) -elif [[ ! "${TEST_DEVS:-}" ]]; then - TEST_DEVS=() -fi - +LOADED_CONFIG=0 +OPTION_EXCLUDE=() while true; do case "$1" in '-d'|'--device-only') - DEVICE_ONLY=1 + OPTION_DEVICE_ONLY=1 shift ;; '-o'|'--output') - OUTPUT="$2" + OPTION_OUTPUT="$2" shift 2 ;; '-q'|'--quick') - QUICK_RUN=1 - # Use the timeout specified on the command line, from - # the config, or the default. - TIMEOUT="${2:-${TIMEOUT:-30}}" + OPTION_QUICK_RUN=1 + OPTION_TIMEOUT="$2" shift 2 ;; '-x'|'--exclude') # shellcheck disable=SC2190 - EXCLUDE+=("$2") + OPTION_EXCLUDE+=("$2") + shift 2 + ;; + '-c'|'--config') + # shellcheck source=/dev/null + . "$2" + LOADED_CONFIG=1 shift 2 ;; '-h'|'--help') @@ -719,22 +702,60 @@ while true; do esac done -if [[ $QUICK_RUN -ne 0 && ! "${TIMEOUT:-}" ]]; then - _error "QUICK_RUN specified without TIMEOUT" +# Load the default configuration file if "-c" was not used. +if [[ $LOADED_CONFIG -eq 0 && -r config ]]; then + # shellcheck source=/dev/null + . config fi -if [[ $DEVICE_ONLY -ne 0 && ${#TEST_DEVS[@]} -eq 0 ]]; then - _error "DEVICE_ONLY specified without TEST_DEVS" +# Options on the command line take precedence over the configuration file. If +# neither set the option and we didn't get it from the environment, then we +# fall back to the default. +DEVICE_ONLY="${OPTION_DEVICE_ONLY:-${DEVICE_ONLY:-0}}" + +OUTPUT="${OPTION_OUTPUT:-${OUTPUT:-results}}" + +QUICK_RUN="${OPTION_QUICK_RUN:-${QUICK_RUN:-0}}" +if [[ -n $OPTION_QUICK_RUN && $OPTION_QUICK_RUN -ne 0 ]]; then + TIMEOUT="${OPTION_TIMEOUT:-${TIMEOUT:-30}}" fi -# Convert the exclude list to an associative array. -TEMP_EXCLUDE=("${EXCLUDE[@]}") +if [[ "${EXCLUDE:-}" ]] && ! declare -p EXCLUDE | grep -q '^declare -a'; then + # If EXCLUDE was not defined as an array, convert it to one. + # shellcheck disable=SC2128,SC2190,SC2206 + EXCLUDE=($EXCLUDE) +elif [[ ! "${EXCLUDE:-}" ]]; then + EXCLUDE=() +fi +# Convert the exclude list to an associative array. The lists from the +# configuration file and command line options are merged. +TEMP_EXCLUDE=("${EXCLUDE[@]}" "${OPTION_EXCLUDE[@]}") unset EXCLUDE declare -A EXCLUDE for filter in "${TEMP_EXCLUDE[@]}"; do EXCLUDE["$filter"]=1 done -unset TEMP_EXCLUDE +unset OPTION_EXCLUDE TEMP_EXCLUDE + +if [[ "${TEST_DEVS:-}" ]] && ! declare -p TEST_DEVS | grep -q '^declare -a'; then + # If TEST_DEVS was not defined as an array, convert it to one. + # shellcheck disable=SC2128,SC2206 + TEST_DEVS=($TEST_DEVS) +elif [[ ! "${TEST_DEVS:-}" ]]; then + TEST_DEVS=() +fi + +: "${LOGGER_PROG:="$(type -P logger || echo true)"}" +: "${RUN_ZONED_TESTS:=0}" + +# Sanity check options. +if [[ $QUICK_RUN -ne 0 && ! "${TIMEOUT:-}" ]]; then + _error "QUICK_RUN specified without TIMEOUT" +fi + +if [[ $DEVICE_ONLY -ne 0 && ${#TEST_DEVS[@]} -eq 0 ]]; then + _error "DEVICE_ONLY specified without TEST_DEVS" +fi mkdir -p "$OUTPUT" OUTPUT="$(realpath "$OUTPUT")"