소스 검색

bats helper fixes (#1792)

mmetc 2 년 전
부모
커밋
a50a3362bd

+ 3 - 1
tests/ansible/vagrant/experimental/alpine-3.16/bootstrap

@@ -2,4 +2,6 @@
 unset IFS
 unset IFS
 set -euf
 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
+

+ 3 - 1
tests/ansible/vagrant/freebsd-12/skip

@@ -6,5 +6,7 @@ die() {
 }
 }
 
 
 [ "${PACKAGE_TESTING}" = "true" ] && die "no package available for this distribution"
 [ "${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
 exit 0

+ 3 - 1
tests/ansible/vagrant/freebsd-13/skip

@@ -6,5 +6,7 @@ die() {
 }
 }
 
 
 [ "${PACKAGE_TESTING}" = "true" ] && die "no package available for this distribution"
 [ "${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
 exit 0

+ 3 - 3
tests/lib/config/config-global

@@ -46,14 +46,14 @@ else
 fi
 fi
 
 
 remove_init_data() {
 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:?}"/*
     rm -rf -- "${LOCAL_DIR:?}/${REL_CONFIG_DIR}"/* "${LOCAL_DIR:?}/${REL_DATA_DIR:?}"/*
 }
 }
 
 
 # we need a separate function for initializing config when testing package
 # we need a separate function for initializing config when testing package
 # because we want to test the configuration as well
 # because we want to test the configuration as well
 make_init_data() {
 make_init_data() {
-    ./bin/assert-crowdsec-not-running
+    ./bin/assert-crowdsec-not-running || die "Cannot create fixture data."
 
 
     ./instance-db config-yaml
     ./instance-db config-yaml
     ./instance-db setup
     ./instance-db setup
@@ -78,7 +78,7 @@ make_init_data() {
 }
 }
 
 
 load_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
     if [[ ! -f "${LOCAL_INIT_DIR}/init-config-data.tar" ]]; then
         die "Initial data not found; did you run '${script_name} make' ?"
         die "Initial data not found; did you run '${script_name} make' ?"

+ 3 - 3
tests/lib/config/config-local

@@ -46,7 +46,7 @@ else
 fi
 fi
 
 
 remove_init_data() {
 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:?}"/*
     rm -rf -- "${LOCAL_DIR:?}/${REL_CONFIG_DIR}"/* "${LOCAL_DIR:?}/${REL_DATA_DIR:?}"/*
 }
 }
 
 
@@ -91,7 +91,7 @@ config_generate() {
 
 
 
 
 make_init_data() {
 make_init_data() {
-    ./bin/assert-crowdsec-not-running
+    ./bin/assert-crowdsec-not-running || die "Cannot create fixture data."
 
 
     remove_init_data
     remove_init_data
     mkdir -p "${DATA_DIR}"
     mkdir -p "${DATA_DIR}"
@@ -125,7 +125,7 @@ make_init_data() {
 }
 }
 
 
 load_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
     if [[ ! -f "${LOCAL_INIT_DIR}/init-config-data.tar" ]]; then
         die "Initial data not found; did you run '${script_name} make' ?"
         die "Initial data not found; did you run '${script_name} make' ?"

+ 38 - 7
tests/lib/setup_file.sh

@@ -45,7 +45,7 @@ cscli() {
 export -f cscli
 export -f cscli
 
 
 config_get() {
 config_get() {
-    cfg="${CONFIG_YAML}"
+    local cfg="${CONFIG_YAML}"
     if [[ $# -ge 2 ]]; then
     if [[ $# -ge 2 ]]; then
         cfg="$1"
         cfg="$1"
         shift
         shift
@@ -56,7 +56,7 @@ config_get() {
 export -f config_get
 export -f config_get
 
 
 config_set() {
 config_set() {
-    cfg="${CONFIG_YAML}"
+    local cfg="${CONFIG_YAML}"
     if [[ $# -ge 2 ]]; then
     if [[ $# -ge 2 ]]; then
         cfg="$1"
         cfg="$1"
         shift
         shift
@@ -122,8 +122,10 @@ is_db_sqlite() {
 }
 }
 export -f 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() {
 assert_json() {
+    local oldout="${output}"
     # validate actual, sort
     # validate actual, sort
     run -0 jq -Sen "${output}"
     run -0 jq -Sen "${output}"
     local actual="${output}"
     local actual="${output}"
@@ -145,12 +147,32 @@ assert_json() {
         diff <(echo "${actual}") <(echo "${expected}")
         diff <(echo "${actual}") <(echo "${expected}")
         fail "json does not match"
         fail "json does not match"
     fi
     fi
+    output="${oldout}"
 }
 }
 export -f assert_json
 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() {
 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}"
     run -0 echo "${stderr}"
     assert_output "$@"
     assert_output "$@"
     output="${oldout}"
     output="${oldout}"
@@ -159,7 +181,12 @@ export -f assert_stderr
 
 
 # like refute_output, but for stderr
 # like refute_output, but for stderr
 refute_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}"
     run -0 echo "${stderr}"
     refute_output "$@"
     refute_output "$@"
     output="${oldout}"
     output="${oldout}"
@@ -168,7 +195,11 @@ export -f refute_stderr
 
 
 # like assert_output, but for stderr
 # like assert_output, but for stderr
 assert_stderr_line() {
 assert_stderr_line() {
-    oldout="${output}"
+    if [[ "$#" -eq 0 ]]; then
+        fail "${FUNCNAME[0]}: called with no arguments"
+    fi
+
+    local oldout="${output}"
     run -0 echo "${stderr}"
     run -0 echo "${stderr}"
     assert_line "$@"
     assert_line "$@"
     output="${oldout}"
     output="${oldout}"