bats changes to allow bouncer testing (#1345)
This commit is contained in:
parent
0dd37240a5
commit
b050d27e20
3
.github/workflows/ci_bats.yml
vendored
3
.github/workflows/ci_bats.yml
vendored
|
@ -35,9 +35,6 @@ jobs:
|
|||
- name: "BATS: build crowdsec"
|
||||
run: make bats-clean bats-build
|
||||
|
||||
- name: "BATS: prepare fixture config+data"
|
||||
run: make bats-instance-data
|
||||
|
||||
- name: "BATS: run tests"
|
||||
run: make bats-test
|
||||
|
||||
|
|
|
@ -21,18 +21,18 @@ export PID_DIR="$(PID_DIR)"
|
|||
export PLUGIN_DIR="$(PLUGIN_DIR)"
|
||||
endef
|
||||
|
||||
bats-all: bats-clean bats-build bats-instance-data bats-test
|
||||
bats-all: bats-clean bats-build bats-test
|
||||
|
||||
# Source this to run the scripts outside of the Makefile
|
||||
bats-environment:
|
||||
$(file >$(TEST_DIR)/.environment.sh,$(ENV))
|
||||
|
||||
# See if bats-core has been cloned from the repo
|
||||
check-bats-libs:
|
||||
@$(TEST_DIR)/lib/bats-core/bin/bats --version >/dev/null 2>&1 || (echo "ERROR: bats-core submodule is required. Please run 'git submodule init; git submodule update' and retry."; exit 1)
|
||||
# Verify dependencies and submodules
|
||||
bats-check-requirements:
|
||||
@$(TEST_DIR)/check-requirements
|
||||
|
||||
# Builds and installs crowdsec in a local directory
|
||||
bats-build: bats-environment
|
||||
bats-build: bats-environment bats-check-requirements
|
||||
@DEFAULT_CONFIGDIR=$(CONFIG_DIR) DEFAULT_DATADIR=$(DATA_DIR) $(MAKE) build
|
||||
@mkdir -p $(BIN_DIR) $(CONFIG_DIR) $(DATA_DIR) $(LOG_DIR) $(PID_DIR) $(LOCAL_INIT_DIR) $(PLUGIN_DIR)
|
||||
@install -m 0755 cmd/crowdsec/crowdsec $(BIN_DIR)/
|
||||
|
@ -41,26 +41,22 @@ bats-build: bats-environment
|
|||
@install -m 0755 plugins/notifications/http/notification-http $(PLUGIN_DIR)/
|
||||
@install -m 0755 plugins/notifications/slack/notification-slack $(PLUGIN_DIR)/
|
||||
@install -m 0755 plugins/notifications/splunk/notification-splunk $(PLUGIN_DIR)/
|
||||
|
||||
# Create a reusable package with initial configuration + data
|
||||
bats-instance-data: bats-environment
|
||||
$(TEST_DIR)/instance-data make
|
||||
@$(TEST_DIR)/instance-data make
|
||||
# Generate dynamic tests
|
||||
@$(TEST_DIR)/generate-hub-tests
|
||||
|
||||
# Removes the local crowdsec installation and the fixture config + data
|
||||
bats-clean:
|
||||
@$(RM) -r $(LOCAL_DIR) $(LOCAL_INIT_DIR) $(TEST_DIR)/dyn-bats/*.bats
|
||||
|
||||
# Creates the hub tests
|
||||
bats-generate-hubtests: bats-environment check-bats-libs
|
||||
${TEST_DIR}/generate-hub-tests
|
||||
|
||||
# Run the test suite
|
||||
bats-test: bats-environment check-bats-libs bats-generate-hubtests
|
||||
bats-test: bats-environment bats-check-requirements
|
||||
$(TEST_DIR)/run-tests
|
||||
|
||||
# Static checks for the test scripts.
|
||||
# Not failproof but they can catch bugs and improve learning of sh/bash
|
||||
bats-lint:
|
||||
@shellcheck --version >/dev/null 2>&1 || (echo "ERROR: shellcheck is required."; exit 1)
|
||||
@shellcheck -x ${TEST_DIR}/bats/*.bats
|
||||
@shellcheck -x $(TEST_DIR)/bats/*.bats
|
||||
|
||||
|
|
|
@ -9,9 +9,13 @@ setup_file() {
|
|||
|
||||
MOCK_OUT="${LOG_DIR}/mock-http.out"
|
||||
export MOCK_OUT
|
||||
MOCK_PORT="9999"
|
||||
MOCK_URL="http://localhost:${MOCK_PORT}"
|
||||
export MOCK_URL
|
||||
|
||||
# https://mikefarah.gitbook.io/yq/operators/env-variable-operators
|
||||
yq '
|
||||
.url="http://localhost:9999" |
|
||||
.url=strenv(MOCK_URL) |
|
||||
.group_wait="5s" |
|
||||
.group_threshold=2
|
||||
' -i "${CONFIG_DIR}/notifications/http.yaml"
|
||||
|
@ -29,7 +33,7 @@ setup_file() {
|
|||
rm -f -- "${MOCK_OUT}"
|
||||
|
||||
./instance-crowdsec start
|
||||
./instance-mock-http start 9999
|
||||
./instance-mock-http start "${MOCK_PORT}"
|
||||
}
|
||||
|
||||
teardown_file() {
|
||||
|
|
65
tests/check-requirements
Executable file
65
tests/check-requirements
Executable file
|
@ -0,0 +1,65 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
set -eu
|
||||
|
||||
die() {
|
||||
echo >&2 "$@"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# shellcheck disable=SC1007
|
||||
TEST_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
||||
# shellcheck source=./.environment.sh
|
||||
. "${TEST_DIR}/.environment.sh"
|
||||
|
||||
|
||||
check_bats_core() {
|
||||
if ! "${TEST_DIR}/lib/bats-core/bin/bats" --version >/dev/null 2>&1; then
|
||||
die "ERROR: bats-core submodule is required. Please run 'git submodule init; git submodule update' and retry."
|
||||
fi
|
||||
}
|
||||
|
||||
check_python3() {
|
||||
if ! command -v python3 >/dev/null; then
|
||||
die "missing required program 'python3'"
|
||||
fi
|
||||
}
|
||||
|
||||
check_nc() {
|
||||
if ! command -v nc >/dev/null; then
|
||||
die "missing required program 'nc' (package 'netcat-openbsd')"
|
||||
fi
|
||||
}
|
||||
|
||||
check_yq() {
|
||||
if ! command -v yq >/dev/null; then
|
||||
die "missing required program 'yq'. You can install it with 'GO111MODULE=on go get github.com/mikefarah/yq/v4' and add ~/go/bin to $PATH"
|
||||
fi
|
||||
}
|
||||
|
||||
check_daemonizer() {
|
||||
SYSTEM=$(uname -s)
|
||||
case "${SYSTEM,,}" in
|
||||
linux)
|
||||
if ! command -v daemonize >/dev/null; then
|
||||
die "missing required program 'daemonize' (package 'daemonize')"
|
||||
fi
|
||||
;;
|
||||
freebsd)
|
||||
if ! command -v daemon >/dev/null; then
|
||||
die "missing required program 'daemon'"
|
||||
fi
|
||||
;;
|
||||
*)
|
||||
die "unsupported system: $SYSTEM"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
|
||||
check_bats_core
|
||||
check_python3
|
||||
check_nc
|
||||
check_yq
|
||||
check_daemonizer
|
||||
|
|
@ -12,41 +12,7 @@ TEST_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
|
|||
# shellcheck source=./.environment.sh
|
||||
. "${TEST_DIR}/.environment.sh"
|
||||
|
||||
|
||||
|
||||
check_requirements() {
|
||||
if ! command -v python3 >/dev/null; then
|
||||
die "missing required program 'python3'"
|
||||
fi
|
||||
|
||||
if ! command -v nc >/dev/null; then
|
||||
die "missing required program 'nc' (package 'netcat-openbsd')"
|
||||
fi
|
||||
|
||||
if ! command -v yq >/dev/null; then
|
||||
die "missing required program 'yq'. You can install it with 'GO111MODULE=on go get github.com/mikefarah/yq/v4' and add ~/go/bin to $PATH"
|
||||
fi
|
||||
|
||||
SYSTEM=$(uname -s)
|
||||
case "${SYSTEM,,}" in
|
||||
linux)
|
||||
if ! command -v daemonize >/dev/null; then
|
||||
die "missing required program 'daemonize' (package 'daemonize')"
|
||||
fi
|
||||
;;
|
||||
freebsd)
|
||||
if ! command -v daemon >/dev/null; then
|
||||
die "missing required program 'daemon'"
|
||||
fi
|
||||
;;
|
||||
*)
|
||||
die "unsupported system: $SYSTEM"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
|
||||
check_requirements
|
||||
"${TEST_DIR}/check-requirements"
|
||||
|
||||
if [ $# -ge 1 ]; then
|
||||
"${TEST_DIR}/lib/bats-core/bin/bats" \
|
||||
|
|
Loading…
Reference in a new issue