2022-03-18 09:13:12 +00:00
|
|
|
|
|
|
|
# contains scripts, bats submodules, local instances and functional test suite
|
2022-03-15 10:19:35 +00:00
|
|
|
TEST_DIR = $(CURDIR)/tests
|
2022-03-18 09:13:12 +00:00
|
|
|
|
2022-03-29 15:06:49 +00:00
|
|
|
ifdef PACKAGE_TESTING
|
|
|
|
# define PACKAGE_TESTING to test the executables already installed with
|
|
|
|
# *.deb, *.rpm...
|
|
|
|
LOCAL_DIR = /
|
|
|
|
BIN_DIR = /usr/bin
|
|
|
|
INIT_BACKEND = systemd
|
|
|
|
CONFIG_BACKEND = global
|
|
|
|
else
|
|
|
|
# LOCAL_DIR will contain contains a local instance of crowdsec, complete with
|
|
|
|
# configuration and data
|
|
|
|
LOCAL_DIR = $(TEST_DIR)/local
|
|
|
|
BIN_DIR = $(LOCAL_DIR)/bin
|
|
|
|
INIT_BACKEND = daemon
|
|
|
|
CONFIG_BACKEND = local
|
|
|
|
PACKAGE_TESTING =
|
|
|
|
endif
|
2022-03-09 13:45:36 +00:00
|
|
|
|
|
|
|
CONFIG_DIR = $(LOCAL_DIR)/etc/crowdsec
|
|
|
|
DATA_DIR = $(LOCAL_DIR)/var/lib/crowdsec/data
|
|
|
|
LOCAL_INIT_DIR = $(TEST_DIR)/local-init
|
|
|
|
LOG_DIR = $(LOCAL_DIR)/var/log
|
|
|
|
PID_DIR = $(LOCAL_DIR)/var/run
|
|
|
|
PLUGIN_DIR = $(LOCAL_DIR)/lib/crowdsec/plugins
|
2022-03-29 15:06:49 +00:00
|
|
|
DB_BACKEND ?= sqlite
|
2022-03-09 13:45:36 +00:00
|
|
|
|
2022-04-05 09:00:11 +00:00
|
|
|
ifdef TEST_COVERAGE
|
2022-05-31 08:01:30 +00:00
|
|
|
CROWDSEC = $(TEST_DIR)/crowdsec-wrapper
|
|
|
|
CSCLI = $(TEST_DIR)/cscli-wrapper
|
2022-04-13 13:44:23 +00:00
|
|
|
BINCOVER_TESTING = true
|
2022-04-05 09:00:11 +00:00
|
|
|
else
|
|
|
|
# the wrappers should work here too - it detects TEST_COVERAGE - but we allow
|
|
|
|
# overriding the path to the binaries
|
2022-07-01 08:16:45 +00:00
|
|
|
CROWDSEC ?= $(BIN_DIR)/crowdsec
|
|
|
|
CSCLI ?= $(BIN_DIR)/cscli
|
2022-05-31 08:01:30 +00:00
|
|
|
# any value is considered true
|
|
|
|
BINCOVER_TESTING =
|
2022-04-05 09:00:11 +00:00
|
|
|
endif
|
|
|
|
|
|
|
|
# If you change the name of the crowdsec executable, make sure the pgrep
|
|
|
|
# parameters are correct in $(TEST_DIR)/assert-crowdsec-not-running
|
|
|
|
|
2022-03-09 13:45:36 +00:00
|
|
|
define ENV :=
|
|
|
|
export TEST_DIR="$(TEST_DIR)"
|
|
|
|
export LOCAL_DIR="$(LOCAL_DIR)"
|
2022-04-05 09:00:11 +00:00
|
|
|
export BIN_DIR="$(BIN_DIR)"
|
|
|
|
export CROWDSEC="$(CROWDSEC)"
|
|
|
|
export CSCLI="$(CSCLI)"
|
2022-03-18 09:13:12 +00:00
|
|
|
export CONFIG_YAML="$(CONFIG_DIR)/config.yaml"
|
2022-03-09 13:45:36 +00:00
|
|
|
export LOCAL_INIT_DIR="$(LOCAL_INIT_DIR)"
|
|
|
|
export LOG_DIR="$(LOG_DIR)"
|
|
|
|
export PID_DIR="$(PID_DIR)"
|
|
|
|
export PLUGIN_DIR="$(PLUGIN_DIR)"
|
2022-03-18 16:36:51 +00:00
|
|
|
export DB_BACKEND="$(DB_BACKEND)"
|
2022-03-29 15:06:49 +00:00
|
|
|
export INIT_BACKEND="$(INIT_BACKEND)"
|
|
|
|
export CONFIG_BACKEND="$(CONFIG_BACKEND)"
|
|
|
|
export PACKAGE_TESTING="$(PACKAGE_TESTING)"
|
2022-04-05 09:00:11 +00:00
|
|
|
export TEST_COVERAGE="$(TEST_COVERAGE)"
|
2022-03-09 13:45:36 +00:00
|
|
|
endef
|
|
|
|
|
2022-04-01 12:12:03 +00:00
|
|
|
bats-all: bats-clean bats-build bats-fixture bats-test bats-test-hub
|
2022-03-09 13:45:36 +00:00
|
|
|
|
|
|
|
# Source this to run the scripts outside of the Makefile
|
2022-04-19 09:25:27 +00:00
|
|
|
# Old versions of make don't have $(file) directive
|
2022-04-15 13:39:17 +00:00
|
|
|
bats-environment: export ENV:=$(ENV)
|
2022-03-21 14:51:05 +00:00
|
|
|
bats-environment:
|
2022-04-15 13:39:17 +00:00
|
|
|
@echo "$${ENV}" > $(TEST_DIR)/.environment.sh
|
2022-03-09 13:45:36 +00:00
|
|
|
|
2022-03-15 09:40:01 +00:00
|
|
|
# Verify dependencies and submodules
|
|
|
|
bats-check-requirements:
|
|
|
|
@$(TEST_DIR)/check-requirements
|
2022-03-09 13:45:36 +00:00
|
|
|
|
2022-04-01 12:12:03 +00:00
|
|
|
# Build and installs crowdsec in a local directory. Rebuilds if already exists.
|
2022-03-15 09:40:01 +00:00
|
|
|
bats-build: bats-environment bats-check-requirements
|
2022-03-18 09:13:12 +00:00
|
|
|
@mkdir -p $(BIN_DIR) $(LOG_DIR) $(PID_DIR) $(PLUGIN_DIR)
|
2022-04-13 13:44:23 +00:00
|
|
|
@BINCOVER_TESTING=$(BINCOVER_TESTING) DEFAULT_CONFIGDIR=$(CONFIG_DIR) DEFAULT_DATADIR=$(DATA_DIR) $(MAKE) goversion crowdsec cscli plugins
|
2022-03-18 09:13:12 +00:00
|
|
|
@install -m 0755 cmd/crowdsec/crowdsec cmd/crowdsec-cli/cscli $(BIN_DIR)/
|
|
|
|
@install -m 0755 plugins/notifications/*/notification-* $(PLUGIN_DIR)/
|
2022-04-13 13:44:23 +00:00
|
|
|
@BINCOVER_TESTING=$(BINCOVER_TESTING) DEFAULT_CONFIGDIR=$(CONFIG_DIR) DEFAULT_DATADIR=$(DATA_DIR) $(MAKE) goversion crowdsec-bincover cscli-bincover
|
2022-04-01 12:12:03 +00:00
|
|
|
@install -m 0755 cmd/crowdsec/crowdsec.cover cmd/crowdsec-cli/cscli.cover $(BIN_DIR)/
|
|
|
|
|
|
|
|
# Create a reusable package with initial configuration + data
|
|
|
|
bats-fixture:
|
2022-03-15 09:40:01 +00:00
|
|
|
@$(TEST_DIR)/instance-data make
|
2022-03-09 13:45:36 +00:00
|
|
|
|
2022-03-18 09:13:12 +00:00
|
|
|
# Remove the local crowdsec installation and the fixture config + data
|
2022-07-01 13:21:28 +00:00
|
|
|
# Don't remove LOCAL_DIR directly because it could be / or anything else outside the repo
|
2022-03-09 13:45:36 +00:00
|
|
|
bats-clean:
|
2022-07-01 13:21:28 +00:00
|
|
|
@$(RM) $(TEST_DIR)/local $(WIN_IGNORE_ERR)
|
2022-05-17 10:14:59 +00:00
|
|
|
@$(RM) $(LOCAL_INIT_DIR) $(WIN_IGNORE_ERR)
|
|
|
|
@$(RM) $(TEST_DIR)/dyn-bats/*.bats $(WIN_IGNORE_ERR)
|
|
|
|
@$(RM) tests/.environment.sh $(WIN_IGNORE_ERR)
|
2022-03-09 13:45:36 +00:00
|
|
|
|
|
|
|
# Run the test suite
|
2022-03-15 09:40:01 +00:00
|
|
|
bats-test: bats-environment bats-check-requirements
|
2022-03-21 14:51:05 +00:00
|
|
|
$(TEST_DIR)/run-tests $(TEST_DIR)/bats
|
|
|
|
|
|
|
|
# Generate dynamic tests
|
|
|
|
bats-test-hub: bats-environment bats-check-requirements
|
|
|
|
@$(TEST_DIR)/generate-hub-tests
|
|
|
|
$(TEST_DIR)/run-tests $(TEST_DIR)/dyn-bats
|
2022-03-09 13:45:36 +00:00
|
|
|
|
|
|
|
# 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)
|
2022-03-15 09:40:01 +00:00
|
|
|
@shellcheck -x $(TEST_DIR)/bats/*.bats
|
2022-03-09 13:45:36 +00:00
|
|
|
|
2022-03-29 15:06:49 +00:00
|
|
|
|
|
|
|
bats-test-package: bats-environment
|
|
|
|
$(TEST_DIR)/instance-data make
|
|
|
|
$(TEST_DIR)/run-tests $(TEST_DIR)/bats
|
|
|
|
$(TEST_DIR)/run-tests $(TEST_DIR)/dyn-bats
|
2022-04-15 13:39:17 +00:00
|
|
|
|
|
|
|
.PHONY: bats-environment
|