github.com/crowdsecurity/crowdsec@v1.6.1/test/bats.mk (about) 1 2 # contains scripts, bats submodules, local instances and functional test suite 3 TEST_DIR = $(CURDIR)/test 4 5 ifdef PACKAGE_TESTING 6 # define PACKAGE_TESTING to test the executables already installed with 7 # *.deb, *.rpm... 8 LOCAL_DIR = / 9 BIN_DIR = /usr/bin 10 INIT_BACKEND = systemd 11 CONFIG_BACKEND = global 12 else 13 # LOCAL_DIR contains a local instance of crowdsec, complete with 14 # configuration and data 15 LOCAL_DIR = $(TEST_DIR)/local 16 BIN_DIR = $(LOCAL_DIR)/bin 17 INIT_BACKEND = daemon 18 CONFIG_BACKEND = local 19 PACKAGE_TESTING = 20 endif 21 22 CONFIG_DIR = $(LOCAL_DIR)/etc/crowdsec 23 DATA_DIR = $(LOCAL_DIR)/var/lib/crowdsec/data 24 LOCAL_INIT_DIR = $(TEST_DIR)/local-init 25 LOG_DIR = $(LOCAL_DIR)/var/log 26 PID_DIR = $(LOCAL_DIR)/var/run 27 # do not shadow $(PLUGINS_DIR) from the main Makefile 28 BATS_PLUGIN_DIR = $(LOCAL_DIR)/lib/crowdsec/plugins 29 DB_BACKEND ?= sqlite 30 31 CROWDSEC ?= $(BIN_DIR)/crowdsec 32 CSCLI ?= $(BIN_DIR)/cscli 33 34 # If you change the name of the crowdsec executable, make sure the pgrep 35 # parameters are correct in $(TEST_DIR)/assert-crowdsec-not-running 36 37 define ENV := 38 export TEST_DIR="$(TEST_DIR)" 39 export LOCAL_DIR="$(LOCAL_DIR)" 40 export BIN_DIR="$(BIN_DIR)" 41 export CROWDSEC="$(CROWDSEC)" 42 export CSCLI="$(CSCLI)" 43 export CONFIG_YAML="$(CONFIG_DIR)/config.yaml" 44 export LOCAL_INIT_DIR="$(LOCAL_INIT_DIR)" 45 export LOG_DIR="$(LOG_DIR)" 46 export PID_DIR="$(PID_DIR)" 47 export PLUGIN_DIR="$(BATS_PLUGIN_DIR)" 48 export DB_BACKEND="$(DB_BACKEND)" 49 export INIT_BACKEND="$(INIT_BACKEND)" 50 export CONFIG_BACKEND="$(CONFIG_BACKEND)" 51 export PACKAGE_TESTING="$(PACKAGE_TESTING)" 52 export TEST_COVERAGE="$(TEST_COVERAGE)" 53 export GOCOVERDIR="$(TEST_DIR)/coverage" 54 export PATH="$(TEST_DIR)/tools:$(PATH)" 55 endef 56 57 bats-all: bats-clean bats-build bats-fixture bats-test bats-test-hub 58 59 # Source this to run the scripts outside of the Makefile 60 # Old versions of make don't have $(file) directive 61 bats-environment: export ENV:=$(ENV) 62 bats-environment: 63 @echo "$${ENV}" > $(TEST_DIR)/.environment.sh 64 65 bats-check-requirements: ## Check dependencies for functional tests 66 @$(TEST_DIR)/bin/check-requirements 67 68 bats-update-tools: ## Install/update tools required for functional tests 69 # yq v4.40.4 70 GOBIN=$(TEST_DIR)/tools go install github.com/mikefarah/yq/v4@1c3d55106075bd37df197b4bc03cb4a413fdb903 71 # cfssl v1.6.4 72 GOBIN=$(TEST_DIR)/tools go install github.com/cloudflare/cfssl/cmd/cfssl@b4d0d877cac528f63db39dfb62d5c96cd3a32a0b 73 GOBIN=$(TEST_DIR)/tools go install github.com/cloudflare/cfssl/cmd/cfssljson@b4d0d877cac528f63db39dfb62d5c96cd3a32a0b 74 75 # Build and installs crowdsec in a local directory. Rebuilds if already exists. 76 bats-build: bats-environment ## Build binaries for functional tests 77 @$(MKDIR) $(BIN_DIR) $(LOG_DIR) $(PID_DIR) $(BATS_PLUGIN_DIR) 78 @$(MAKE) build DEBUG=1 TEST_COVERAGE=$(TEST_COVERAGE) DEFAULT_CONFIGDIR=$(CONFIG_DIR) DEFAULT_DATADIR=$(DATA_DIR) 79 @install -m 0755 cmd/crowdsec/crowdsec cmd/crowdsec-cli/cscli $(BIN_DIR)/ 80 @install -m 0755 cmd/notification-*/notification-* $(BATS_PLUGIN_DIR)/ 81 82 # Create a reusable package with initial configuration + data 83 bats-fixture: bats-check-requirements bats-update-tools ## Build fixture for functional tests 84 @echo "Creating functional test fixture." 85 @$(TEST_DIR)/instance-data make 86 87 # Remove the local crowdsec installation and the fixture config + data 88 # Don't remove LOCAL_DIR directly because it could be / or anything else outside the repo 89 bats-clean: ## Remove functional test environment 90 @$(RM) $(TEST_DIR)/local $(WIN_IGNORE_ERR) 91 @$(RM) $(LOCAL_INIT_DIR) $(WIN_IGNORE_ERR) 92 @$(RM) $(TEST_DIR)/dyn-bats/*.bats $(WIN_IGNORE_ERR) 93 @$(RM) test/.environment.sh $(WIN_IGNORE_ERR) 94 @$(RM) test/coverage/* $(WIN_IGNORE_ERR) 95 96 bats-test: bats-environment ## Run functional tests 97 $(TEST_DIR)/run-tests $(TEST_DIR)/bats 98 99 bats-test-hub: bats-environment bats-check-requirements ## Run all hub tests 100 @$(TEST_DIR)/bin/generate-hub-tests 101 $(TEST_DIR)/run-tests $(TEST_DIR)/dyn-bats 102 103 # Not failproof but they can catch bugs and improve learning of sh/bash 104 bats-lint: ## Static checks for the test scripts. 105 @shellcheck --version >/dev/null 2>&1 || (echo "ERROR: shellcheck is required."; exit 1) 106 @shellcheck -x $(TEST_DIR)/bats/*.bats 107 108 bats-test-package: bats-environment ## CI only - test a binary package (deb, rpm, ...) 109 $(TEST_DIR)/instance-data make 110 $(TEST_DIR)/run-tests $(TEST_DIR)/bats 111 $(TEST_DIR)/run-tests $(TEST_DIR)/dyn-bats