fix some tests in pipeline (#1622)

This commit is contained in:
mmetc 2022-07-01 10:16:45 +02:00 committed by GitHub
parent ca4cd6d559
commit 69dd8d2892
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 30 additions and 53 deletions

View file

@ -34,8 +34,8 @@ ifdef TEST_COVERAGE
else else
# the wrappers should work here too - it detects TEST_COVERAGE - but we allow # the wrappers should work here too - it detects TEST_COVERAGE - but we allow
# overriding the path to the binaries # overriding the path to the binaries
CROWDSEC ?= "$(BIN_DIR)/crowdsec" CROWDSEC ?= $(BIN_DIR)/crowdsec
CSCLI ?= "$(BIN_DIR)/cscli" CSCLI ?= $(BIN_DIR)/cscli
# any value is considered true # any value is considered true
BINCOVER_TESTING = BINCOVER_TESTING =
endif endif

View file

@ -54,3 +54,18 @@ declare stderr
run -0 echo "${stderr}" run -0 echo "${stderr}"
assert_line --partial "You must run at least the API Server or crowdsec" 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"
}

View file

@ -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"
}

View file

@ -14,6 +14,8 @@ setup_file() {
MOCK_URL="http://localhost:${MOCK_PORT}" MOCK_URL="http://localhost:${MOCK_PORT}"
export MOCK_URL export MOCK_URL
PLUGIN_DIR=$(config_yq '.config_paths.plugin_dir') PLUGIN_DIR=$(config_yq '.config_paths.plugin_dir')
# could have a trailing slash
PLUGIN_DIR=$(realpath -s "${PLUGIN_DIR}")
export PLUGIN_DIR export PLUGIN_DIR
# https://mikefarah.gitbook.io/yq/operators/env-variable-operators # https://mikefarah.gitbook.io/yq/operators/env-variable-operators
@ -41,8 +43,6 @@ setup_file() {
teardown_file() { teardown_file() {
load "../lib/teardown_file.sh" load "../lib/teardown_file.sh"
rm -f "${PLUGIN_DIR}"/badname
chmod go-w "${PLUGIN_DIR}"/notification-http
./instance-crowdsec stop ./instance-crowdsec stop
./instance-mock-http stop ./instance-mock-http stop
} }

View file

@ -5,7 +5,7 @@ set -u
setup_file() { setup_file() {
load "../lib/setup_file.sh" load "../lib/setup_file.sh"
[ -n "${PACKAGE_TESTING}" ] && return [[ -n "${PACKAGE_TESTING}" ]] && return
./instance-data load ./instance-data load
@ -48,17 +48,16 @@ setup_file() {
teardown_file() { teardown_file() {
load "../lib/teardown_file.sh" load "../lib/teardown_file.sh"
rm -f "${tempfile}" "${tempfile2}"
} }
setup() { setup() {
[ -n "${PACKAGE_TESTING}" ] && skip [[ -n "${PACKAGE_TESTING}" ]] && skip
load "../lib/setup.sh" 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 run -0 cscli decisions add --ip 1.2.3.4 --duration 30s
assert_output --partial 'Decision successfully added' assert_output --partial 'Decision successfully added'
@ -67,13 +66,13 @@ setup() {
sleep 2 sleep 2
} }
@test "${FILE} expected 1 notification" { @test "expected 1 notification" {
run -0 cat "${tempfile}" run -0 cat "${tempfile}"
assert_output --partial 1.2.3.4 assert_output --partial 1.2.3.4
assert_output --partial 1.2.3.5 assert_output --partial 1.2.3.5
} }
@test "${FILE} second notification works too" { @test "second notification works too" {
run -0 cat "${tempfile2}" run -0 cat "${tempfile2}"
assert_output --partial secondfile assert_output --partial secondfile
} }

View file

@ -7,6 +7,8 @@ setup_file() {
load "../lib/setup_file.sh" load "../lib/setup_file.sh"
PLUGIN_DIR=$(config_yq '.config_paths.plugin_dir') PLUGIN_DIR=$(config_yq '.config_paths.plugin_dir')
# could have a trailing slash
PLUGIN_DIR=$(realpath -s "${PLUGIN_DIR}")
export PLUGIN_DIR export PLUGIN_DIR
PROFILES_PATH=$(config_yq '.api.server.profiles_path') PROFILES_PATH=$(config_yq '.api.server.profiles_path')

View file

@ -33,8 +33,6 @@ fi
start() { start() {
systemctl start crowdsec systemctl start crowdsec
./lib/util/wait-for-port 6060 ./lib/util/wait-for-port 6060
sleep 30s
"${CSCLI}" decisions delete --all
} }
stop() { stop() {

View file

@ -5,3 +5,7 @@
load "../lib/bats-support/load.bash" load "../lib/bats-support/load.bash"
load "../lib/bats-assert/load.bash" load "../lib/bats-assert/load.bash"
#load "../lib/bats-file/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