2022-06-06 13:24:48 +00:00
|
|
|
#!/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"
|
2022-10-14 13:48:41 +00:00
|
|
|
load "../lib/bats-file/load.bash"
|
2022-06-06 13:24:48 +00:00
|
|
|
./instance-data load
|
|
|
|
}
|
|
|
|
|
|
|
|
teardown() {
|
|
|
|
./instance-crowdsec stop
|
|
|
|
}
|
|
|
|
|
|
|
|
#----------
|
|
|
|
|
2022-06-17 14:12:49 +00:00
|
|
|
@test "crowdsec (usage)" {
|
2022-06-06 13:24:48 +00:00
|
|
|
run -0 --separate-stderr timeout 2s "${CROWDSEC}" -h
|
2022-08-05 08:54:49 +00:00
|
|
|
assert_stderr_line --regexp "Usage of .*:"
|
2022-06-06 13:24:48 +00:00
|
|
|
|
|
|
|
run -0 --separate-stderr timeout 2s "${CROWDSEC}" --help
|
2022-08-05 08:54:49 +00:00
|
|
|
assert_stderr_line --regexp "Usage of .*:"
|
2022-06-06 13:24:48 +00:00
|
|
|
}
|
|
|
|
|
2022-06-17 14:12:49 +00:00
|
|
|
@test "crowdsec (unknown flag)" {
|
2022-06-06 13:24:48 +00:00
|
|
|
run -2 --separate-stderr timeout 2s "${CROWDSEC}" --foobar
|
2022-08-05 08:54:49 +00:00
|
|
|
assert_stderr_line "flag provided but not defined: -foobar"
|
|
|
|
assert_stderr_line --regexp "Usage of .*"
|
2022-06-06 13:24:48 +00:00
|
|
|
}
|
|
|
|
|
2022-06-17 14:12:49 +00:00
|
|
|
@test "crowdsec (unknown argument)" {
|
2022-06-06 13:24:48 +00:00
|
|
|
run -2 --separate-stderr timeout 2s "${CROWDSEC}" trololo
|
2022-08-05 08:54:49 +00:00
|
|
|
assert_stderr_line "argument provided but not defined: trololo"
|
|
|
|
assert_stderr_line --regexp "Usage of .*"
|
2022-06-06 13:24:48 +00:00
|
|
|
}
|
|
|
|
|
2022-06-17 14:12:49 +00:00
|
|
|
@test "crowdsec (no api and no agent)" {
|
2022-06-06 13:24:48 +00:00
|
|
|
run -1 --separate-stderr timeout 2s "${CROWDSEC}" -no-api -no-cs
|
2022-08-05 08:54:49 +00:00
|
|
|
assert_stderr_line --partial "You must run at least the API Server or crowdsec"
|
2022-06-06 13:24:48 +00:00
|
|
|
}
|
2022-07-01 08:16:45 +00:00
|
|
|
|
|
|
|
@test "crowdsec - print error on exit" {
|
|
|
|
# errors that cause program termination are printed to stderr, not only logs
|
2022-07-01 20:45:55 +00:00
|
|
|
config_set '.db_config.type="meh"'
|
2022-10-14 13:48:41 +00:00
|
|
|
run -1 --separate-stderr "${CROWDSEC}"
|
2022-07-01 08:16:45 +00:00
|
|
|
refute_output
|
2022-08-05 08:54:49 +00:00
|
|
|
assert_stderr --partial "unable to create database client: unknown database type 'meh'"
|
2022-07-01 08:16:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@test "CS_LAPI_SECRET not strong enough" {
|
|
|
|
CS_LAPI_SECRET=foo run -1 --separate-stderr timeout 2s "${CROWDSEC}"
|
2022-08-05 08:54:49 +00:00
|
|
|
assert_stderr --partial "api server init: unable to run local API: controller init: CS_LAPI_SECRET not strong enough"
|
2022-07-01 08:16:45 +00:00
|
|
|
}
|
2022-10-14 13:48:41 +00:00
|
|
|
|
|
|
|
@test "crowdsec - reload (change of logfile, disabled agent)" {
|
|
|
|
logdir1=$(TMPDIR="${BATS_TEST_TMPDIR}" mktemp -u)
|
|
|
|
log_old="${logdir1}/crowdsec.log"
|
|
|
|
config_set ".common.log_dir=\"${logdir1}\""
|
|
|
|
|
|
|
|
run -0 ./instance-crowdsec start
|
|
|
|
# PID="$output"
|
|
|
|
assert_file_exist "$log_old"
|
|
|
|
assert_file_contains "$log_old" "Starting processing data"
|
|
|
|
|
|
|
|
logdir2=$(TMPDIR="${BATS_TEST_TMPDIR}" mktemp -u)
|
|
|
|
log_new="${logdir2}/crowdsec.log"
|
|
|
|
config_set ".common.log_dir=\"${logdir2}\""
|
|
|
|
|
|
|
|
config_disable_agent
|
|
|
|
|
|
|
|
sleep 5
|
|
|
|
|
|
|
|
# this won't work as crowdsec-wrapper does not relay the signal
|
|
|
|
# run -0 kill -HUP "$PID"
|
|
|
|
|
|
|
|
run killall -HUP "$BIN_DIR/crowdsec.cover"
|
|
|
|
run killall -HUP "$BIN_DIR/crowdsec"
|
|
|
|
|
|
|
|
for ((i=0; i<20; i++)); do
|
|
|
|
sleep 1
|
|
|
|
grep -q "killing all plugins" <"$log_old" && break
|
|
|
|
done
|
|
|
|
|
|
|
|
echo "waited $i seconds"
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo "OLD LOG"
|
|
|
|
echo
|
|
|
|
ls -la "$log_old" || true
|
|
|
|
cat "$log_old" || true
|
|
|
|
|
|
|
|
assert_file_contains "$log_old" "SIGHUP received, reloading"
|
|
|
|
assert_file_contains "$log_old" "Crowdsec engine shutting down"
|
|
|
|
assert_file_contains "$log_old" "Killing parser routines"
|
|
|
|
assert_file_contains "$log_old" "Bucket routine exiting"
|
|
|
|
assert_file_contains "$log_old" "serve: shutting down api server"
|
|
|
|
assert_file_contains "$log_old" "plugingTomb dying"
|
|
|
|
assert_file_contains "$log_old" "killing all plugins"
|
|
|
|
|
|
|
|
sleep 5
|
|
|
|
|
|
|
|
assert_file_exist "$log_new"
|
|
|
|
|
|
|
|
for ((i=0; i<20; i++)); do
|
|
|
|
sleep 1
|
|
|
|
grep -q "Reload is finished" <"$log_old" && break
|
|
|
|
done
|
|
|
|
|
|
|
|
echo "waited $i seconds"
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo "NEW LOG"
|
|
|
|
echo
|
|
|
|
ls -la "$log_new" || true
|
|
|
|
cat "$log_new" || true
|
|
|
|
|
|
|
|
assert_file_contains "$log_new" "CrowdSec Local API listening on 127.0.0.1:8080"
|
|
|
|
assert_file_contains "$log_new" "Reload is finished"
|
|
|
|
|
|
|
|
run -0 ./instance-crowdsec stop
|
|
|
|
}
|
|
|
|
|