func tests: install dependencies from make, log test helpers (#2314)

This commit is contained in:
mmetc 2023-06-28 10:07:05 +02:00 committed by GitHub
parent 893394ef5f
commit ebe25d7653
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 30 additions and 49 deletions

View file

@ -44,9 +44,6 @@ jobs:
GOBIN: /usr/local/bin GOBIN: /usr/local/bin
run: | run: |
sudo apt -qq -y -o=Dpkg::Use-Pty=0 install build-essential daemonize jq netcat-openbsd libre2-dev 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" - name: "Build crowdsec and fixture"
run: make bats-clean bats-build bats-fixture BUILD_STATIC=1 run: make bats-clean bats-build bats-fixture BUILD_STATIC=1

View file

@ -51,9 +51,6 @@ jobs:
GOBIN: /usr/local/bin GOBIN: /usr/local/bin
run: | run: |
sudo apt -qq -y -o=Dpkg::Use-Pty=0 install build-essential daemonize jq netcat-openbsd libre2-dev 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" - name: "Build crowdsec and fixture"
run: | run: |

View file

@ -52,9 +52,6 @@ jobs:
GOBIN: /usr/local/bin GOBIN: /usr/local/bin
run: | run: |
sudo apt -qq -y -o=Dpkg::Use-Pty=0 install build-essential daemonize jq netcat-openbsd libre2-dev 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)" - name: "Build crowdsec and fixture (DB_BACKEND: pgx)"
run: | run: |

View file

@ -41,9 +41,6 @@ jobs:
GOBIN: /usr/local/bin GOBIN: /usr/local/bin
run: | run: |
sudo apt -qq -y -o=Dpkg::Use-Pty=0 install build-essential daemonize jq netcat-openbsd libre2-dev 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" - name: "Build crowdsec and fixture"
run: | run: |

3
.gitignore vendored
View file

@ -15,6 +15,9 @@
*.test *.test
*.cover *.cover
# Test dependencies
test/tools/*
# VMs used for dev/test # VMs used for dev/test
.vagrant .vagrant

View file

@ -56,9 +56,6 @@ architectures.
## pre-requisites ## pre-requisites
- `git submodule init; git submodule update` - `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` - `base64`
- `bash>=4.4` - `bash>=4.4`
- `curl` - `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 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 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 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). or group of tests (files).
The stdout/stderr of the commands within the test function are captured by 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. 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 # setup and teardown
If you have read the bats-core tutorial linked above, you are aware of the If you have read the bats-core tutorial linked above, you are aware of the

View file

@ -51,6 +51,7 @@ export CONFIG_BACKEND="$(CONFIG_BACKEND)"
export PACKAGE_TESTING="$(PACKAGE_TESTING)" export PACKAGE_TESTING="$(PACKAGE_TESTING)"
export TEST_COVERAGE="$(TEST_COVERAGE)" export TEST_COVERAGE="$(TEST_COVERAGE)"
export GOCOVERDIR="$(TEST_DIR)/coverage" export GOCOVERDIR="$(TEST_DIR)/coverage"
export PATH="$(TEST_DIR)/tools:$(PATH)"
endef endef
bats-all: bats-clean bats-build bats-fixture bats-test bats-test-hub bats-all: bats-clean bats-build bats-fixture bats-test bats-test-hub

View file

@ -54,47 +54,25 @@ check_pkill() {
fi 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() { check_daemonizer() {
if ! command -v daemonize >/dev/null; then if ! command -v daemonize >/dev/null; then
die "missing required program 'daemonize' (package 'daemonize' or 'https://github.com/bmc/daemonize')" die "missing required program 'daemonize' (package 'daemonize' or 'https://github.com/bmc/daemonize')"
fi fi
} }
check_cfssl() { echo "Checking requirements..."
# 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
}
check_cfssljson() { GOBIN=${TEST_DIR}/tools
# shellcheck disable=SC2016 export GOBIN
howto_install='You can install it with "go install github.com/cloudflare/cfssl/cmd/cfssljson@latest" and add ~/go/bin to $PATH.' go install github.com/mikefarah/yq/v4@latest
if ! command -v cfssljson >/dev/null; then go install github.com/cloudflare/cfssl/cmd/cfssl@master
die "Missing required program 'cfssljson'. ${howto_install}" go install github.com/cloudflare/cfssl/cmd/cfssljson@master
fi
}
check_bats_core check_bats_core
check_curl check_curl
check_daemonizer check_daemonizer
check_cfssl
check_cfssljson
check_jq check_jq
check_nc check_nc
check_base64 check_base64
check_python3 check_python3
check_yq
check_pkill check_pkill

View file

@ -127,6 +127,24 @@ is_db_sqlite() {
} }
export -f 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. # Compare ignoring the key order, and allow "expected" without quoted identifiers.
# Preserve the output variable in case the following commands require it. # Preserve the output variable in case the following commands require it.
assert_json() { assert_json() {

View file

@ -0,0 +1 @@
this directory is populated by test dependencies, and is not checked in git