2022-06-13 19:54:47 +00:00
|
|
|
#!/usr/bin/env bash
|
2022-03-09 13:45:36 +00:00
|
|
|
|
2022-06-15 13:36:36 +00:00
|
|
|
# this should have effect globally, for all tests
|
|
|
|
# https://github.com/bats-core/bats-core/blob/master/docs/source/warnings/BW02.rst
|
2022-06-22 08:29:02 +00:00
|
|
|
bats_require_minimum_version 1.5.0
|
|
|
|
|
|
|
|
# this should have effect globally, for all tests
|
|
|
|
# https://github.com/bats-core/bats-core/blob/master/docs/source/warnings/BW02.rst
|
2022-06-15 13:36:36 +00:00
|
|
|
bats_require_minimum_version 1.5.0
|
|
|
|
|
2022-03-18 09:13:12 +00:00
|
|
|
debug() {
|
|
|
|
echo 'exec 1<&-; exec 2<&-; exec 1>&3; exec 2>&1'
|
|
|
|
}
|
|
|
|
export -f debug
|
2022-03-09 13:45:36 +00:00
|
|
|
|
2022-03-18 09:13:12 +00:00
|
|
|
# redirects stdout and stderr to &3 otherwise the errors in setup, teardown would
|
|
|
|
# go unreported.
|
|
|
|
# BUT - don't do this in test functions. Everything written to stdout and
|
|
|
|
# stderr after this line will go to the terminal, but in the tests, these
|
|
|
|
# are supposed to be collected and shown only in case of test failure
|
|
|
|
# (see options --print-output-on-failure and --show-output-of-passing-tests)
|
|
|
|
eval "$(debug)"
|
|
|
|
|
|
|
|
# Allow tests to use relative paths for helper scripts.
|
2022-03-09 13:45:36 +00:00
|
|
|
# shellcheck disable=SC2164
|
2022-03-18 09:13:12 +00:00
|
|
|
cd "${TEST_DIR}"
|
2022-03-09 13:45:36 +00:00
|
|
|
|
|
|
|
# complain if there's a crowdsec running system-wide or leftover from a previous test
|
|
|
|
./assert-crowdsec-not-running
|
|
|
|
|
2022-06-17 14:12:49 +00:00
|
|
|
# we can prepend the filename to the test descriptions (useful to feed a TAP consumer)
|
|
|
|
if [[ "${PREFIX_TEST_NAMES_WITH_FILE:-false}" == "true" ]]; then
|
|
|
|
BATS_TEST_NAME_PREFIX="$(basename "${BATS_TEST_FILENAME}" .bats): "
|
|
|
|
export BATS_TEST_NAME_PREFIX
|
|
|
|
fi
|
|
|
|
|
|
|
|
# before bats 1.7, we did that by hand
|
|
|
|
FILE=
|
2022-03-09 13:45:36 +00:00
|
|
|
export FILE
|
|
|
|
|
|
|
|
# the variables exported here can be seen in other setup/teardown/test functions
|
2022-03-18 09:13:12 +00:00
|
|
|
# MYVAR=something
|
|
|
|
# export MYVAR
|
2022-03-09 13:45:36 +00:00
|
|
|
|
|
|
|
# functions too
|
|
|
|
cscli() {
|
|
|
|
"${CSCLI}" "$@"
|
|
|
|
}
|
|
|
|
export -f cscli
|
|
|
|
|
2022-07-01 20:45:55 +00:00
|
|
|
config_get() {
|
|
|
|
cfg="${CONFIG_YAML}"
|
|
|
|
if [[ $# -ge 2 ]]; then
|
|
|
|
cfg="$1"
|
|
|
|
shift
|
|
|
|
fi
|
|
|
|
|
|
|
|
yq e "$1" "${cfg}"
|
2022-03-18 09:13:12 +00:00
|
|
|
}
|
2022-07-01 20:45:55 +00:00
|
|
|
export -f config_get
|
|
|
|
|
|
|
|
config_set() {
|
|
|
|
cfg="${CONFIG_YAML}"
|
|
|
|
if [[ $# -ge 2 ]]; then
|
|
|
|
cfg="$1"
|
|
|
|
shift
|
|
|
|
fi
|
|
|
|
|
|
|
|
yq e "$1" -i "${cfg}"
|
|
|
|
}
|
|
|
|
export -f config_set
|
2022-03-18 09:13:12 +00:00
|
|
|
|
2022-03-09 13:45:36 +00:00
|
|
|
# We use these functions like this:
|
|
|
|
# somecommand <(stderr)
|
|
|
|
# to provide a standard input to "somecommand".
|
|
|
|
# The alternatives echo "$stderr" or <<<"$stderr"
|
|
|
|
# ("here string" in bash jargon)
|
|
|
|
# are worse because they add a newline,
|
|
|
|
# even if the variable is empty.
|
|
|
|
|
|
|
|
# shellcheck disable=SC2154
|
|
|
|
stderr() {
|
2022-06-13 19:54:47 +00:00
|
|
|
printf '%s' "${stderr}"
|
2022-03-09 13:45:36 +00:00
|
|
|
}
|
|
|
|
export -f stderr
|
|
|
|
|
|
|
|
# shellcheck disable=SC2154
|
|
|
|
output() {
|
2022-06-13 19:54:47 +00:00
|
|
|
printf '%s' "${output}"
|
2022-03-09 13:45:36 +00:00
|
|
|
}
|
|
|
|
export -f output
|
2022-03-18 09:13:12 +00:00
|
|
|
|
2022-04-13 13:17:49 +00:00
|
|
|
is_db_postgres() {
|
2022-06-13 19:54:47 +00:00
|
|
|
[[ "${DB_BACKEND}" =~ ^postgres|pgx$ ]]
|
2022-04-13 13:17:49 +00:00
|
|
|
}
|
|
|
|
export -f is_db_postgres
|
|
|
|
|
|
|
|
is_db_mysql() {
|
2022-06-13 19:54:47 +00:00
|
|
|
[[ "${DB_BACKEND}" == "mysql" ]]
|
2022-04-13 13:17:49 +00:00
|
|
|
}
|
|
|
|
export -f is_db_mysql
|
|
|
|
|
|
|
|
is_db_sqlite() {
|
2022-06-13 19:54:47 +00:00
|
|
|
[[ "${DB_BACKEND}" == "sqlite" ]]
|
2022-04-13 13:17:49 +00:00
|
|
|
}
|
|
|
|
export -f is_db_sqlite
|
|
|
|
|