From 0c35263c295011bfed46a4ea38f4b7af59956c1c Mon Sep 17 00:00:00 2001 From: mmetc <92726601+mmetc@users.noreply.github.com> Date: Thu, 10 Mar 2022 12:19:02 +0100 Subject: [PATCH] fix bats-test for freebsd (#1333) --- tests/bats/01_base.bats | 8 ++++---- tests/bats/02_nolapi.bats | 8 ++++---- tests/bats/03_noagent.bats | 8 ++++---- tests/bats/04_nocapi.bats | 8 ++++---- tests/bats/40_live-ban.bats | 2 +- tests/instance-crowdsec | 5 +++-- tests/run-tests | 2 +- 7 files changed, 21 insertions(+), 20 deletions(-) diff --git a/tests/bats/01_base.bats b/tests/bats/01_base.bats index 0c24d11ae..a1b067a80 100644 --- a/tests/bats/01_base.bats +++ b/tests/bats/01_base.bats @@ -97,15 +97,15 @@ declare stderr } @test "$FILE cscli config backup" { - tempdir=$(mktemp -u -p "${BATS_TEST_TMPDIR}") - run -0 cscli config backup "${tempdir}" + backupdir=$(TMPDIR="${BATS_TEST_TMPDIR}" mktemp -u) + run -0 cscli config backup "${backupdir}" assert_output --partial "Starting configuration backup" - run -1 --separate-stderr cscli config backup "${tempdir}" + run -1 --separate-stderr cscli config backup "${backupdir}" run -0 echo "$stderr" assert_output --partial "Failed to backup configurations" assert_output --partial "file exists" - rm -rf -- "${tempdir:?}" + rm -rf -- "${backupdir:?}" } @test "$FILE cscli lapi status" { diff --git a/tests/bats/02_nolapi.bats b/tests/bats/02_nolapi.bats index 4eb049a67..ac8382086 100644 --- a/tests/bats/02_nolapi.bats +++ b/tests/bats/02_nolapi.bats @@ -61,11 +61,11 @@ declare stderr @test "$FILE cscli config backup" { yq 'del(.api.server)' -i "${CONFIG_DIR}/config.yaml" - tempdir=$(mktemp -u -p "${BATS_TEST_TMPDIR}") - run -0 cscli config backup "${tempdir}" + backupdir=$(TMPDIR="${BATS_TEST_TMPDIR}" mktemp -u) + run -0 cscli config backup "${backupdir}" assert_output --partial "Starting configuration backup" - run -1 --separate-stderr cscli config backup "${tempdir}" - rm -rf -- "${tempdir:?}" + run -1 --separate-stderr cscli config backup "${backupdir}" + rm -rf -- "${backupdir:?}" run -0 echo "$stderr" assert_output --partial "Failed to backup configurations" diff --git a/tests/bats/03_noagent.bats b/tests/bats/03_noagent.bats index 5b0c21c94..8f7f1dead 100644 --- a/tests/bats/03_noagent.bats +++ b/tests/bats/03_noagent.bats @@ -66,15 +66,15 @@ config_disable_agent() { @test "$FILE no agent: cscli config backup" { config_disable_agent - tempdir=$(mktemp -u -p "${BATS_TEST_TMPDIR}") - run -0 cscli config backup "${tempdir}" + backupdir=$(TMPDIR="${BATS_TEST_TMPDIR}" mktemp -u) + run -0 cscli config backup "${backupdir}" assert_output --partial "Starting configuration backup" - run -1 --separate-stderr cscli config backup "${tempdir}" + run -1 --separate-stderr cscli config backup "${backupdir}" run -0 echo "$stderr" assert_output --partial "Failed to backup configurations" assert_output --partial "file exists" - rm -rf -- "${tempdir:?}" + rm -rf -- "${backupdir:?}" } @test "$FILE no agent: lapi status should be ok" { diff --git a/tests/bats/04_nocapi.bats b/tests/bats/04_nocapi.bats index f5957767e..f3b06f711 100644 --- a/tests/bats/04_nocapi.bats +++ b/tests/bats/04_nocapi.bats @@ -57,15 +57,15 @@ config_disable_capi() { @test "$FILE no agent: cscli config backup" { config_disable_capi - tempdir=$(mktemp -u -p "${BATS_TEST_TMPDIR}") - run -0 cscli config backup "${tempdir}" + backupdir=$(TMPDIR="${BATS_TEST_TMPDIR}" mktemp -u) + run -0 cscli config backup "${backupdir}" assert_output --partial "Starting configuration backup" - run -1 --separate-stderr cscli config backup "${tempdir}" + run -1 --separate-stderr cscli config backup "${backupdir}" run -0 echo "$stderr" assert_output --partial "Failed to backup configurations" assert_output --partial "file exists" - rm -rf -- "${tempdir:?}" + rm -rf -- "${backupdir:?}" } @test "$FILE without capi: cscli lapi status -> success" { diff --git a/tests/bats/40_live-ban.bats b/tests/bats/40_live-ban.bats index 5657f7fb8..3ae9af53e 100644 --- a/tests/bats/40_live-ban.bats +++ b/tests/bats/40_live-ban.bats @@ -30,7 +30,7 @@ teardown() { #---------- @test "$FILE 1.1.1.172 has been banned" { - tmpfile=$(mktemp -p "${BATS_TEST_TMPDIR}") + tmpfile=$(TMPDIR="${BATS_TEST_TMPDIR}" mktemp) touch "${tmpfile}" echo -e "---\nfilename: $tmpfile\nlabels:\n type: syslog\n" >>"${CONFIG_DIR}/acquis.yaml" diff --git a/tests/instance-crowdsec b/tests/instance-crowdsec index 52021f256..bcd86e517 100755 --- a/tests/instance-crowdsec +++ b/tests/instance-crowdsec @@ -28,7 +28,7 @@ fi wait_for_port() { for _ in $(seq 40); do - nc -z localhost "$1" && return + nc -z localhost "$1" >/dev/null 2>&1 && return sleep .05 done @@ -59,7 +59,8 @@ stop_instance() { if [ -f "${DAEMON_PID}" ]; then # terminate quickly with extreme prejudice, all the application data will be # thrown away anyway. also terminate the child processes (notification plugin). - PGID="$(ps --no-headers -p "$(cat "${DAEMON_PID}")" -o pgid | tr -d ' ')" + PGID="$(ps -o pgid= -p "$(cat "${DAEMON_PID}")" | tr -d ' ')" + # ps above should work on linux, freebsd, busybox.. if [ -n "${PGID}" ]; then kill -- "-${PGID}" fi diff --git a/tests/run-tests b/tests/run-tests index 369f637fc..d759fa210 100755 --- a/tests/run-tests +++ b/tests/run-tests @@ -20,7 +20,7 @@ check_requirements() { fi if ! command -v yq >/dev/null; then - die "missing required program 'yq'. You can install it with 'GO111MODULE=on go get github.com/mikefarah/yq/v4'" + die "missing required program 'yq'. You can install it with 'GO111MODULE=on go get github.com/mikefarah/yq/v4' and add ~/go/bin to $PATH" fi SYSTEM=$(uname -s)