From ebe25d7653379104b7488003ea498073e5bd3106 Mon Sep 17 00:00:00 2001 From: mmetc <92726601+mmetc@users.noreply.github.com> Date: Wed, 28 Jun 2023 10:07:05 +0200 Subject: [PATCH] func tests: install dependencies from make, log test helpers (#2314) --- .github/workflows/bats-hub.yml | 3 -- .github/workflows/bats-mysql.yml | 3 -- .github/workflows/bats-postgres.yml | 3 -- .github/workflows/bats-sqlite-coverage.yml | 3 -- .gitignore | 3 ++ test/README.md | 10 +------ test/bats.mk | 1 + test/bin/check-requirements | 34 ++++------------------ test/lib/setup_file.sh | 18 ++++++++++++ test/tools/.do-not-remove | 1 + 10 files changed, 30 insertions(+), 49 deletions(-) create mode 100644 test/tools/.do-not-remove diff --git a/.github/workflows/bats-hub.yml b/.github/workflows/bats-hub.yml index 68ef9ad95..c62229cbe 100644 --- a/.github/workflows/bats-hub.yml +++ b/.github/workflows/bats-hub.yml @@ -44,9 +44,6 @@ jobs: GOBIN: /usr/local/bin run: | sudo apt -qq -y -o=Dpkg::Use-Pty=0 install build-essential daemonize jq netcat-openbsd libre2-dev - go install github.com/mikefarah/yq/v4@latest - go install github.com/cloudflare/cfssl/cmd/cfssl@master - go install github.com/cloudflare/cfssl/cmd/cfssljson@master - name: "Build crowdsec and fixture" run: make bats-clean bats-build bats-fixture BUILD_STATIC=1 diff --git a/.github/workflows/bats-mysql.yml b/.github/workflows/bats-mysql.yml index 6b15ddacd..529c3f521 100644 --- a/.github/workflows/bats-mysql.yml +++ b/.github/workflows/bats-mysql.yml @@ -51,9 +51,6 @@ jobs: GOBIN: /usr/local/bin run: | sudo apt -qq -y -o=Dpkg::Use-Pty=0 install build-essential daemonize jq netcat-openbsd libre2-dev - go install github.com/mikefarah/yq/v4@latest - go install github.com/cloudflare/cfssl/cmd/cfssl@master - go install github.com/cloudflare/cfssl/cmd/cfssljson@master - name: "Build crowdsec and fixture" run: | diff --git a/.github/workflows/bats-postgres.yml b/.github/workflows/bats-postgres.yml index 7ee80cfa9..91e7ac103 100644 --- a/.github/workflows/bats-postgres.yml +++ b/.github/workflows/bats-postgres.yml @@ -52,9 +52,6 @@ jobs: GOBIN: /usr/local/bin run: | sudo apt -qq -y -o=Dpkg::Use-Pty=0 install build-essential daemonize jq netcat-openbsd libre2-dev - go install github.com/mikefarah/yq/v4@latest - go install github.com/cloudflare/cfssl/cmd/cfssl@master - go install github.com/cloudflare/cfssl/cmd/cfssljson@master - name: "Build crowdsec and fixture (DB_BACKEND: pgx)" run: | diff --git a/.github/workflows/bats-sqlite-coverage.yml b/.github/workflows/bats-sqlite-coverage.yml index c04fd5015..17b2aac09 100644 --- a/.github/workflows/bats-sqlite-coverage.yml +++ b/.github/workflows/bats-sqlite-coverage.yml @@ -41,9 +41,6 @@ jobs: GOBIN: /usr/local/bin run: | sudo apt -qq -y -o=Dpkg::Use-Pty=0 install build-essential daemonize jq netcat-openbsd libre2-dev - go install github.com/mikefarah/yq/v4@latest - go install github.com/cloudflare/cfssl/cmd/cfssl@master - go install github.com/cloudflare/cfssl/cmd/cfssljson@master - name: "Build crowdsec and fixture" run: | diff --git a/.gitignore b/.gitignore index bf0355f06..8fe1778ba 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,9 @@ *.test *.cover +# Test dependencies +test/tools/* + # VMs used for dev/test .vagrant diff --git a/test/README.md b/test/README.md index bbc40a388..7f34bd3db 100644 --- a/test/README.md +++ b/test/README.md @@ -56,9 +56,6 @@ architectures. ## pre-requisites - `git submodule init; git submodule update` - - `go install github.com/cloudflare/cfssl/cmd/cfssl@latest` - - `go install github.com/cloudflare/cfssl/cmd/cfssljson@latest` - - `go install github.com/mikefarah/yq/v4@latest` - `base64` - `bash>=4.4` - `curl` @@ -89,7 +86,7 @@ In BATS, you write tests in the form of Bash functions that have unique descriptions (the name of the test). You can do most things that you can normally do in a shell function. If there is any error condition, the test fails. A set of functions is provided to implement assertions, and a mechanism -of `setup`/`teardown` is provided a the level of individual tests (functions) +of `setup`/`teardown` is provided at the level of individual tests (functions) or group of tests (files). The stdout/stderr of the commands within the test function are captured by @@ -129,11 +126,6 @@ included in a larger test suite. The TAP specification is pretty minimalist and some glue may be needed. -Other tools that you can find useful: - - - [mikefarah/yq](https://github.com/mikefarah/yq) - to parse and update YAML files on the fly - - [aliou/bats.vim](https://github.com/aliou/bats.vim) - for syntax highlighting (use bash otherwise) - # setup and teardown If you have read the bats-core tutorial linked above, you are aware of the diff --git a/test/bats.mk b/test/bats.mk index 65bb4a286..0f96be8e1 100644 --- a/test/bats.mk +++ b/test/bats.mk @@ -51,6 +51,7 @@ export CONFIG_BACKEND="$(CONFIG_BACKEND)" export PACKAGE_TESTING="$(PACKAGE_TESTING)" export TEST_COVERAGE="$(TEST_COVERAGE)" export GOCOVERDIR="$(TEST_DIR)/coverage" +export PATH="$(TEST_DIR)/tools:$(PATH)" endef bats-all: bats-clean bats-build bats-fixture bats-test bats-test-hub diff --git a/test/bin/check-requirements b/test/bin/check-requirements index 351c0a01b..0563eec01 100755 --- a/test/bin/check-requirements +++ b/test/bin/check-requirements @@ -54,47 +54,25 @@ check_pkill() { fi } -check_yq() { - # shellcheck disable=SC2016 - howto_install='You can install it with your favorite package manager (including snap) or with "go install github.com/mikefarah/yq/v4@latest" and add ~/go/bin to $PATH.' - if ! command -v yq >/dev/null; then - die "Missing required program 'yq'. ${howto_install}" - fi - if ! (yq --version | grep mikefarah >/dev/null); then - die "yq exists but it's not the one we need (mikefarah/yq). ${howto_install}" - fi -} - check_daemonizer() { if ! command -v daemonize >/dev/null; then die "missing required program 'daemonize' (package 'daemonize' or 'https://github.com/bmc/daemonize')" fi } -check_cfssl() { - # shellcheck disable=SC2016 - howto_install='You can install it with "go install github.com/cloudflare/cfssl/cmd/cfssl@latest" and add ~/go/bin to $PATH.' - if ! command -v cfssl >/dev/null; then - die "Missing required program 'cfssl'. ${howto_install}" - fi -} +echo "Checking requirements..." -check_cfssljson() { - # shellcheck disable=SC2016 - howto_install='You can install it with "go install github.com/cloudflare/cfssl/cmd/cfssljson@latest" and add ~/go/bin to $PATH.' - if ! command -v cfssljson >/dev/null; then - die "Missing required program 'cfssljson'. ${howto_install}" - fi -} +GOBIN=${TEST_DIR}/tools +export GOBIN +go install github.com/mikefarah/yq/v4@latest +go install github.com/cloudflare/cfssl/cmd/cfssl@master +go install github.com/cloudflare/cfssl/cmd/cfssljson@master check_bats_core check_curl check_daemonizer -check_cfssl -check_cfssljson check_jq check_nc check_base64 check_python3 -check_yq check_pkill diff --git a/test/lib/setup_file.sh b/test/lib/setup_file.sh index 22a9bc303..a4231c98e 100755 --- a/test/lib/setup_file.sh +++ b/test/lib/setup_file.sh @@ -127,6 +127,24 @@ is_db_sqlite() { } export -f is_db_sqlite +crowdsec_log() { + echo "$(config_get .common.log_dir)"/crowdsec.log +} +export -f crowdsec_log + +truncate_log() { + true > "$(crowdsec_log)" +} +export -f truncate_log + +assert_log() { + local oldout="${output:-}" + output="$(cat "$(crowdsec_log)")" + assert_output "$@" + output="${oldout}" +} +export -f assert_log + # Compare ignoring the key order, and allow "expected" without quoted identifiers. # Preserve the output variable in case the following commands require it. assert_json() { diff --git a/test/tools/.do-not-remove b/test/tools/.do-not-remove new file mode 100644 index 000000000..311928170 --- /dev/null +++ b/test/tools/.do-not-remove @@ -0,0 +1 @@ +this directory is populated by test dependencies, and is not checked in git