From a50a3362bd3f4c0962b19a2763eb20836b751211 Mon Sep 17 00:00:00 2001 From: mmetc <92726601+mmetc@users.noreply.github.com> Date: Fri, 7 Oct 2022 16:17:36 +0200 Subject: [PATCH] bats helper fixes (#1792) --- .../experimental/alpine-3.16/bootstrap | 4 +- tests/ansible/vagrant/freebsd-12/skip | 4 +- tests/ansible/vagrant/freebsd-13/skip | 4 +- tests/lib/config/config-global | 6 +-- tests/lib/config/config-local | 6 +-- tests/lib/setup_file.sh | 45 ++++++++++++++++--- 6 files changed, 53 insertions(+), 16 deletions(-) diff --git a/tests/ansible/vagrant/experimental/alpine-3.16/bootstrap b/tests/ansible/vagrant/experimental/alpine-3.16/bootstrap index 45349ed78..7fb806bb5 100755 --- a/tests/ansible/vagrant/experimental/alpine-3.16/bootstrap +++ b/tests/ansible/vagrant/experimental/alpine-3.16/bootstrap @@ -2,4 +2,6 @@ unset IFS set -euf -sudo apk add python3 go tar procps netcat-openbsd +# coreutils -> for timeout (busybox is not enough) +sudo apk add python3 go tar procps netcat-openbsd coreutils + diff --git a/tests/ansible/vagrant/freebsd-12/skip b/tests/ansible/vagrant/freebsd-12/skip index d43fe3aff..a72dde8e4 100755 --- a/tests/ansible/vagrant/freebsd-12/skip +++ b/tests/ansible/vagrant/freebsd-12/skip @@ -6,5 +6,7 @@ die() { } [ "${PACKAGE_TESTING}" = "true" ] && die "no package available for this distribution" -[ "${DB_BACKEND}" = "mysql" ] && die "mysql role does not support freebsd" +[ "${DB_BACKEND}" = "postgres" ] && die "postgres role does not support this distribution" +[ "${DB_BACKEND}" = "pgx" ] && die "postgres role does not support this distribution" +[ "${DB_BACKEND}" = "mysql" ] && die "mysql role does not support this distribution" exit 0 diff --git a/tests/ansible/vagrant/freebsd-13/skip b/tests/ansible/vagrant/freebsd-13/skip index d43fe3aff..a72dde8e4 100755 --- a/tests/ansible/vagrant/freebsd-13/skip +++ b/tests/ansible/vagrant/freebsd-13/skip @@ -6,5 +6,7 @@ die() { } [ "${PACKAGE_TESTING}" = "true" ] && die "no package available for this distribution" -[ "${DB_BACKEND}" = "mysql" ] && die "mysql role does not support freebsd" +[ "${DB_BACKEND}" = "postgres" ] && die "postgres role does not support this distribution" +[ "${DB_BACKEND}" = "pgx" ] && die "postgres role does not support this distribution" +[ "${DB_BACKEND}" = "mysql" ] && die "mysql role does not support this distribution" exit 0 diff --git a/tests/lib/config/config-global b/tests/lib/config/config-global index cee519410..c2f0767c9 100755 --- a/tests/lib/config/config-global +++ b/tests/lib/config/config-global @@ -46,14 +46,14 @@ else fi remove_init_data() { - ./bin/assert-crowdsec-not-running + ./bin/assert-crowdsec-not-running || die "Cannot remove fixture data." rm -rf -- "${LOCAL_DIR:?}/${REL_CONFIG_DIR}"/* "${LOCAL_DIR:?}/${REL_DATA_DIR:?}"/* } # we need a separate function for initializing config when testing package # because we want to test the configuration as well make_init_data() { - ./bin/assert-crowdsec-not-running + ./bin/assert-crowdsec-not-running || die "Cannot create fixture data." ./instance-db config-yaml ./instance-db setup @@ -78,7 +78,7 @@ make_init_data() { } load_init_data() { - ./bin/assert-crowdsec-not-running + ./bin/assert-crowdsec-not-running || die "Cannot load fixture data." if [[ ! -f "${LOCAL_INIT_DIR}/init-config-data.tar" ]]; then die "Initial data not found; did you run '${script_name} make' ?" diff --git a/tests/lib/config/config-local b/tests/lib/config/config-local index 0dbbebb16..bf3ebd8a8 100755 --- a/tests/lib/config/config-local +++ b/tests/lib/config/config-local @@ -46,7 +46,7 @@ else fi remove_init_data() { - ./bin/assert-crowdsec-not-running + ./bin/assert-crowdsec-not-running || die "Cannot remove fixture data." rm -rf -- "${LOCAL_DIR:?}/${REL_CONFIG_DIR}"/* "${LOCAL_DIR:?}/${REL_DATA_DIR:?}"/* } @@ -91,7 +91,7 @@ config_generate() { make_init_data() { - ./bin/assert-crowdsec-not-running + ./bin/assert-crowdsec-not-running || die "Cannot create fixture data." remove_init_data mkdir -p "${DATA_DIR}" @@ -125,7 +125,7 @@ make_init_data() { } load_init_data() { - ./bin/assert-crowdsec-not-running + ./bin/assert-crowdsec-not-running || die "Cannot load fixture data." if [[ ! -f "${LOCAL_INIT_DIR}/init-config-data.tar" ]]; then die "Initial data not found; did you run '${script_name} make' ?" diff --git a/tests/lib/setup_file.sh b/tests/lib/setup_file.sh index e89fc1127..8002dbaed 100755 --- a/tests/lib/setup_file.sh +++ b/tests/lib/setup_file.sh @@ -45,7 +45,7 @@ cscli() { export -f cscli config_get() { - cfg="${CONFIG_YAML}" + local cfg="${CONFIG_YAML}" if [[ $# -ge 2 ]]; then cfg="$1" shift @@ -56,7 +56,7 @@ config_get() { export -f config_get config_set() { - cfg="${CONFIG_YAML}" + local cfg="${CONFIG_YAML}" if [[ $# -ge 2 ]]; then cfg="$1" shift @@ -122,8 +122,10 @@ is_db_sqlite() { } export -f is_db_sqlite -# compare ignoring the key order, and allow "expected" without quoted identifiers +# Compare ignoring the key order, and allow "expected" without quoted identifiers. +# Preserve the output variable in case the following commands require it. assert_json() { + local oldout="${output}" # validate actual, sort run -0 jq -Sen "${output}" local actual="${output}" @@ -145,12 +147,32 @@ assert_json() { diff <(echo "${actual}") <(echo "${expected}") fail "json does not match" fi + output="${oldout}" } export -f assert_json -# like assert_output, but for stderr +# Check if there's something on stdin by consuming it. Only use this as a way +# to check if something was passed by mistake, since if you read it, it will be +# incomplete. +is_stdin_empty() { + if read -t 0.1; then + return 1 + fi + return 0 +} +export -f is_stdin_empty + assert_stderr() { - oldout="${output}" + # it is never useful to call this without arguments + if [[ "$#" -eq 0 ]]; then + # maybe the caller forgot to use '-' with an heredoc + if ! is_stdin_empty; then + fail "${FUNCNAME[0]}: called with stdin and no arguments (heredoc?)" + fi + fail "${FUNCNAME[0]}: called with no arguments" + fi + + local oldout="${output}" run -0 echo "${stderr}" assert_output "$@" output="${oldout}" @@ -159,7 +181,12 @@ export -f assert_stderr # like refute_output, but for stderr refute_stderr() { - oldout="${output}" + # calling this without arguments is ok, as long as stdin in empty + if ! is_stdin_empty; then + fail "${FUNCNAME[0]}: called with stdin (heredoc?)" + fi + + local oldout="${output}" run -0 echo "${stderr}" refute_output "$@" output="${oldout}" @@ -168,7 +195,11 @@ export -f refute_stderr # like assert_output, but for stderr assert_stderr_line() { - oldout="${output}" + if [[ "$#" -eq 0 ]]; then + fail "${FUNCNAME[0]}: called with no arguments" + fi + + local oldout="${output}" run -0 echo "${stderr}" assert_line "$@" output="${oldout}"