crowdsec/tests/lib/init/crowdsec-daemon
Manuel Sabban 16b9fd82f0
Update bats tests to work with systemd (#1394)
* update for making it work with systemd
* take DB_BACKEND from file name; reduce duplicated code; configure db_type=sqlite
* define PACKAGE_TESTING

Co-authored-by: sabban <15465465+sabban@users.noreply.github.com>
Co-authored-by: mmetc <92726601+mmetc@users.noreply.github.com>
2022-03-29 17:06:49 +02:00

70 lines
1.5 KiB
Plaintext
Executable file

#!/usr/bin/env bash
set -eu
script_name=$0
die() {
echo >&2 "$@"
exit 1
}
about() {
die "usage: $script_name [ start | stop ]"
}
#shellcheck disable=SC1007
THIS_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
cd "${THIS_DIR}"/../../
#shellcheck disable=SC1091
. ./.environment.sh
# you have not removed set -u above, have you?
[ -z "${CROWDSEC-}" ] && die "\$CROWDSEC must be defined."
[ -z "${LOG_DIR-}" ] && die "\$LOG_DIR must be defined."
[ -z "${PID_DIR-}" ] && die "\$PID_DIR must be defined."
if [ ! -f "${CROWDSEC}" ]; then
die "${CROWDSEC} is missing. Please build (with 'make bats-build') or install it."
fi
DAEMON_PID=${PID_DIR}/crowdsec.pid
start() {
OUT_FILE="${LOG_DIR}/crowdsec.out" \
DAEMON_PID="${DAEMON_PID}" \
"${TEST_DIR}/run-as-daemon" "${CROWDSEC}"
./lib/util/wait-for-port 6060
}
stop() {
if [ -f "${DAEMON_PID}" ]; then
# terminate quickly with extreme prejudice, all the application data will be
# thrown away anyway. also terminate the child processes (notification plugin).
PGID="$(ps -o pgid= -p "$(cat "${DAEMON_PID}")" | tr -d ' ')"
# ps above should work on linux, freebsd, busybox..
if [ -n "${PGID}" ]; then
kill -- "-${PGID}"
fi
rm -f -- "${DAEMON_PID}"
fi
}
# ---------------------------
[ $# -lt 1 ] && about
case "$1" in
start)
start
;;
stop)
stop
;;
*)
about
;;
esac;