Skip to content

Commit

Permalink
func tests: install dependencies from make, log test helpers (#2314)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc authored Jun 28, 2023
1 parent 893394e commit ebe25d7
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 49 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/bats-hub.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/bats-mysql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/bats-postgres.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/bats-sqlite-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*.test
*.cover

# Test dependencies
test/tools/*

# VMs used for dev/test

.vagrant
Expand Down
10 changes: 1 addition & 9 deletions test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/bats.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 6 additions & 28 deletions test/bin/check-requirements
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions test/lib/setup_file.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions test/tools/.do-not-remove
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this directory is populated by test dependencies, and is not checked in git

0 comments on commit ebe25d7

Please sign in to comment.