diff --git a/tests/bats.mk b/tests/bats.mk index 4a09e3af4..f5b601c01 100644 --- a/tests/bats.mk +++ b/tests/bats.mk @@ -34,8 +34,8 @@ ifdef TEST_COVERAGE else # the wrappers should work here too - it detects TEST_COVERAGE - but we allow # overriding the path to the binaries - CROWDSEC ?= "$(BIN_DIR)/crowdsec" - CSCLI ?= "$(BIN_DIR)/cscli" + CROWDSEC ?= $(BIN_DIR)/crowdsec + CSCLI ?= $(BIN_DIR)/cscli # any value is considered true BINCOVER_TESTING = endif diff --git a/tests/bats/01_crowdsec.bats b/tests/bats/01_crowdsec.bats index c9d5126a9..de974044e 100644 --- a/tests/bats/01_crowdsec.bats +++ b/tests/bats/01_crowdsec.bats @@ -54,3 +54,18 @@ declare stderr run -0 echo "${stderr}" assert_line --partial "You must run at least the API Server or crowdsec" } + +@test "crowdsec - print error on exit" { + # errors that cause program termination are printed to stderr, not only logs + yq e '.db_config.type="meh"' -i "${CONFIG_YAML}" + run -1 --separate-stderr "${BIN_DIR}/crowdsec" + refute_output + run -0 echo "${stderr}" + assert_output --partial "unable to create database client: unknown database type 'meh'" +} + +@test "CS_LAPI_SECRET not strong enough" { + CS_LAPI_SECRET=foo run -1 --separate-stderr timeout 2s "${CROWDSEC}" + run -0 echo "${stderr}" + assert_output --partial "api server init: unable to run local API: controller init: CS_LAPI_SECRET not strong enough" +} diff --git a/tests/bats/06_crowdsec.bats b/tests/bats/06_crowdsec.bats deleted file mode 100644 index d57d663f4..000000000 --- a/tests/bats/06_crowdsec.bats +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/env bats -# vim: ft=bats:list:ts=8:sts=4:sw=4:et:ai:si: - -set -u - -setup_file() { - load "../lib/setup_file.sh" -} - -teardown_file() { - load "../lib/teardown_file.sh" -} - -setup() { - load "../lib/setup.sh" - ./instance-data load -} - -teardown() { - ./instance-crowdsec stop -} - -# to silence shellcheck -declare stderr - -#---------- - -@test "${FILE} crowdsec - print error on exit" { - # errors that cause program termination are printed to stderr, not only logs - yq e '.db_config.type="meh"' -i "${CONFIG_YAML}" - run -1 --separate-stderr "${BIN_DIR}/crowdsec" - refute_output - run -0 echo "${stderr}" - assert_output --partial "unable to create database client: unknown database type 'meh'" -} - -@test "${FILE} CS_LAPI_SECRET not strong enough" { - CS_LAPI_SECRET=foo run -1 --separate-stderr timeout 2s "${CROWDSEC}" - run -0 echo "${stderr}" - assert_output --partial "api server init: unable to run local API: controller init: CS_LAPI_SECRET not strong enough" -} diff --git a/tests/bats/70_http_plugin.bats b/tests/bats/70_http_plugin.bats index 45f156ad4..c1aca6267 100644 --- a/tests/bats/70_http_plugin.bats +++ b/tests/bats/70_http_plugin.bats @@ -14,6 +14,8 @@ setup_file() { MOCK_URL="http://localhost:${MOCK_PORT}" export MOCK_URL PLUGIN_DIR=$(config_yq '.config_paths.plugin_dir') + # could have a trailing slash + PLUGIN_DIR=$(realpath -s "${PLUGIN_DIR}") export PLUGIN_DIR # https://mikefarah.gitbook.io/yq/operators/env-variable-operators @@ -41,8 +43,6 @@ setup_file() { teardown_file() { load "../lib/teardown_file.sh" - rm -f "${PLUGIN_DIR}"/badname - chmod go-w "${PLUGIN_DIR}"/notification-http ./instance-crowdsec stop ./instance-mock-http stop } diff --git a/tests/bats/71_dummy_plugin.bats b/tests/bats/71_dummy_plugin.bats index 4702dc438..abaad63f3 100644 --- a/tests/bats/71_dummy_plugin.bats +++ b/tests/bats/71_dummy_plugin.bats @@ -5,7 +5,7 @@ set -u setup_file() { load "../lib/setup_file.sh" - [ -n "${PACKAGE_TESTING}" ] && return + [[ -n "${PACKAGE_TESTING}" ]] && return ./instance-data load @@ -48,17 +48,16 @@ setup_file() { teardown_file() { load "../lib/teardown_file.sh" - rm -f "${tempfile}" "${tempfile2}" } setup() { - [ -n "${PACKAGE_TESTING}" ] && skip + [[ -n "${PACKAGE_TESTING}" ]] && skip load "../lib/setup.sh" } #---------- -@test "${FILE} add two bans" { +@test "add two bans" { run -0 cscli decisions add --ip 1.2.3.4 --duration 30s assert_output --partial 'Decision successfully added' @@ -67,13 +66,13 @@ setup() { sleep 2 } -@test "${FILE} expected 1 notification" { +@test "expected 1 notification" { run -0 cat "${tempfile}" assert_output --partial 1.2.3.4 assert_output --partial 1.2.3.5 } -@test "${FILE} second notification works too" { +@test "second notification works too" { run -0 cat "${tempfile2}" assert_output --partial secondfile } diff --git a/tests/bats/72_plugin_badconfig.bats b/tests/bats/72_plugin_badconfig.bats index eaccc79e9..04fc5637c 100644 --- a/tests/bats/72_plugin_badconfig.bats +++ b/tests/bats/72_plugin_badconfig.bats @@ -7,6 +7,8 @@ setup_file() { load "../lib/setup_file.sh" PLUGIN_DIR=$(config_yq '.config_paths.plugin_dir') + # could have a trailing slash + PLUGIN_DIR=$(realpath -s "${PLUGIN_DIR}") export PLUGIN_DIR PROFILES_PATH=$(config_yq '.api.server.profiles_path') diff --git a/tests/lib/init/crowdsec-systemd b/tests/lib/init/crowdsec-systemd index 041105f47..1037abd6d 100755 --- a/tests/lib/init/crowdsec-systemd +++ b/tests/lib/init/crowdsec-systemd @@ -33,8 +33,6 @@ fi start() { systemctl start crowdsec ./lib/util/wait-for-port 6060 - sleep 30s - "${CSCLI}" decisions delete --all } stop() { diff --git a/tests/lib/setup.sh b/tests/lib/setup.sh index a12ee0b59..03d207d87 100755 --- a/tests/lib/setup.sh +++ b/tests/lib/setup.sh @@ -5,3 +5,7 @@ load "../lib/bats-support/load.bash" load "../lib/bats-assert/load.bash" #load "../lib/bats-file/load.bash" + +# mark the start of each test in the logs, beware crowdsec might be running +# echo "time=\"$(date +"%d-%m-%Y %H:%M:%S")\" level=info msg=\"TEST: ${BATS_TEST_DESCRIPTION}\"" >> /var/log/crowdsec.log +