2022-03-18 09:13:12 +00:00
|
|
|
|
|
|
|
# contains scripts, bats submodules, local instances and functional test suite
|
2023-03-03 14:54:49 +00:00
|
|
|
TEST_DIR = $(CURDIR)/test
|
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
|
2023-06-29 14:35:19 +00:00
|
|
|
# LOCAL_DIR contains a local instance of crowdsec, complete with
|
2022-03-29 15:06:49 +00:00
|
|
|
# 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
|
2023-06-05 21:15:18 +00:00
|
|
|
# do not shadow $(PLUGINS_DIR) from the main Makefile
|
|
|
|
BATS_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
|
|
|
|
2023-03-30 13:05:09 +00:00
|
|
|
CROWDSEC ?= $(BIN_DIR)/crowdsec
|
|
|
|
CSCLI ?= $(BIN_DIR)/cscli
|
2022-04-05 09:00:11 +00:00
|
|
|
|
|
|
|
# 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)"
|
2023-06-05 21:15:18 +00:00
|
|
|
export PLUGIN_DIR="$(BATS_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)"
|
2023-03-30 13:05:09 +00:00
|
|
|
export GOCOVERDIR="$(TEST_DIR)/coverage"
|
2023-06-28 08:07:05 +00:00
|
|
|
export PATH="$(TEST_DIR)/tools:$(PATH)"
|
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
|
|
|
|
2023-12-18 09:13:08 +00:00
|
|
|
bats-check-requirements: ## Check dependencies for functional tests
|
2022-10-04 13:58:10 +00:00
|
|
|
@$(TEST_DIR)/bin/check-requirements
|
2022-03-09 13:45:36 +00:00
|
|
|
|
2023-12-18 09:13:08 +00:00
|
|
|
bats-update-tools: ## Install/update tools required for functional tests
|
2024-04-22 15:19:00 +00:00
|
|
|
# yq v4.43.1
|
|
|
|
GOBIN=$(TEST_DIR)/tools go install github.com/mikefarah/yq/v4@c35ec752e38ea0c096d3c44e13cfc0797ac394d8
|
|
|
|
# cfssl v1.6.5
|
|
|
|
GOBIN=$(TEST_DIR)/tools go install github.com/cloudflare/cfssl/cmd/cfssl@96259aa29c9cc9b2f4e04bad7d4bc152e5405dda
|
|
|
|
GOBIN=$(TEST_DIR)/tools go install github.com/cloudflare/cfssl/cmd/cfssljson@96259aa29c9cc9b2f4e04bad7d4bc152e5405dda
|
2023-07-05 15:45:31 +00:00
|
|
|
|
2022-04-01 12:12:03 +00:00
|
|
|
# Build and installs crowdsec in a local directory. Rebuilds if already exists.
|
2023-12-18 09:13:08 +00:00
|
|
|
bats-build: bats-environment ## Build binaries for functional tests
|
2023-06-05 21:15:18 +00:00
|
|
|
@$(MKDIR) $(BIN_DIR) $(LOG_DIR) $(PID_DIR) $(BATS_PLUGIN_DIR)
|
2023-08-28 13:58:26 +00:00
|
|
|
@$(MAKE) build DEBUG=1 TEST_COVERAGE=$(TEST_COVERAGE) DEFAULT_CONFIGDIR=$(CONFIG_DIR) DEFAULT_DATADIR=$(DATA_DIR)
|
2022-03-18 09:13:12 +00:00
|
|
|
@install -m 0755 cmd/crowdsec/crowdsec cmd/crowdsec-cli/cscli $(BIN_DIR)/
|
2023-08-24 07:46:25 +00:00
|
|
|
@install -m 0755 cmd/notification-*/notification-* $(BATS_PLUGIN_DIR)/
|
2022-04-01 12:12:03 +00:00
|
|
|
|
|
|
|
# Create a reusable package with initial configuration + data
|
2023-12-18 09:13:08 +00:00
|
|
|
bats-fixture: bats-check-requirements bats-update-tools ## Build fixture for functional tests
|
2023-12-07 15:20:13 +00:00
|
|
|
@echo "Creating functional test 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
|
2023-12-18 09:13:08 +00:00
|
|
|
bats-clean: ## Remove functional test environment
|
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)
|
2023-03-03 14:54:49 +00:00
|
|
|
@$(RM) test/.environment.sh $(WIN_IGNORE_ERR)
|
2023-06-05 11:03:18 +00:00
|
|
|
@$(RM) test/coverage/* $(WIN_IGNORE_ERR)
|
2022-03-09 13:45:36 +00:00
|
|
|
|
2023-12-18 09:13:08 +00:00
|
|
|
bats-test: bats-environment ## Run functional tests
|
2022-03-21 14:51:05 +00:00
|
|
|
$(TEST_DIR)/run-tests $(TEST_DIR)/bats
|
|
|
|
|
2023-12-18 09:13:08 +00:00
|
|
|
bats-test-hub: bats-environment bats-check-requirements ## Run all hub tests
|
2022-10-04 13:58:10 +00:00
|
|
|
@$(TEST_DIR)/bin/generate-hub-tests
|
2022-03-21 14:51:05 +00:00
|
|
|
$(TEST_DIR)/run-tests $(TEST_DIR)/dyn-bats
|
2022-03-09 13:45:36 +00:00
|
|
|
|
|
|
|
# Not failproof but they can catch bugs and improve learning of sh/bash
|
2023-12-18 09:13:08 +00:00
|
|
|
bats-lint: ## Static checks for the test scripts.
|
2022-03-09 13:45:36 +00:00
|
|
|
@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
|
|
|
|
2023-12-18 09:13:08 +00:00
|
|
|
bats-test-package: bats-environment ## CI only - test a binary package (deb, rpm, ...)
|
2022-03-29 15:06:49 +00:00
|
|
|
$(TEST_DIR)/instance-data make
|
|
|
|
$(TEST_DIR)/run-tests $(TEST_DIR)/bats
|
|
|
|
$(TEST_DIR)/run-tests $(TEST_DIR)/dyn-bats
|