From c2708ab6c0ee4742e87402ca95000ee7c5c78d41 Mon Sep 17 00:00:00 2001 From: mmetc <92726601+mmetc@users.noreply.github.com> Date: Fri, 18 Mar 2022 14:46:05 +0100 Subject: [PATCH] Bats: "reformat" script for shfmt, longer timeout for crowdsec (#1373) --- tests/bats/01_base.bats | 4 ++-- tests/bats/02_nolapi.bats | 6 +++--- tests/bats/03_noagent.bats | 6 +++--- tests/bats/80_alerts.bats | 1 - tests/bats/90_decisions.bats | 1 - tests/bats/98_ipv4_range.bats | 2 -- tests/bats/99_lapi-stream-mode-scopes.bats | 2 -- tests/bats/99_lapi-stream-mode.bats | 1 - tests/bats/reformat | 16 ++++++++++++++++ 9 files changed, 24 insertions(+), 15 deletions(-) create mode 100755 tests/bats/reformat diff --git a/tests/bats/01_base.bats b/tests/bats/01_base.bats index 664480d9c..013721b01 100644 --- a/tests/bats/01_base.bats +++ b/tests/bats/01_base.bats @@ -41,10 +41,10 @@ declare stderr @test "$FILE cscli alerts list: at startup returns at least one entry: community pull" { loop_max=15 - for ((i=0; i<=loop_max; i++)); do + for ((i = 0; i <= loop_max; i++)); do sleep 2 run -0 cscli alerts list -o json - [[ "$output" != "null" ]] && break + [ "$output" != "null" ] && break done run -0 jq -r '. | length' <(output) refute_output 0 diff --git a/tests/bats/02_nolapi.bats b/tests/bats/02_nolapi.bats index 2a46e5917..0db1ace7a 100644 --- a/tests/bats/02_nolapi.bats +++ b/tests/bats/02_nolapi.bats @@ -26,17 +26,17 @@ declare stderr #---------- @test "$FILE test without -no-api flag" { - run -124 --separate-stderr timeout 1s "${CROWDSEC}" + run -124 --separate-stderr timeout 2s "${CROWDSEC}" # from `man timeout`: If the command times out, and --preserve-status is not set, then exit with status 124. } @test "$FILE crowdsec should not run without LAPI (-no-api flag)" { - run -1 --separate-stderr timeout 1s "${CROWDSEC}" -no-api + run -1 --separate-stderr timeout 2s "${CROWDSEC}" -no-api } @test "$FILE crowdsec should not run without LAPI (no api.server in configuration file)" { yq 'del(.api.server)' -i "${CONFIG_YAML}" - run -1 --separate-stderr timeout 1s "${CROWDSEC}" + run -1 --separate-stderr timeout 2s "${CROWDSEC}" run -0 echo "$stderr" assert_output --partial "crowdsec local API is disabled" diff --git a/tests/bats/03_noagent.bats b/tests/bats/03_noagent.bats index e1ff05950..fbfa97219 100644 --- a/tests/bats/03_noagent.bats +++ b/tests/bats/03_noagent.bats @@ -29,17 +29,17 @@ config_disable_agent() { } @test "$FILE with agent: test without -no-cs flag" { - run -124 timeout 1s "${CROWDSEC}" + run -124 timeout 2s "${CROWDSEC}" # from `man timeout`: If the command times out, and --preserve-status is not set, then exit with status 124. } @test "$FILE no agent: crowdsec LAPI should run (-no-cs flag)" { - run -124 timeout 1s "${CROWDSEC}" -no-cs + run -124 timeout 2s "${CROWDSEC}" -no-cs } @test "$FILE no agent: crowdsec LAPI should run (no crowdsec_service in configuration file)" { config_disable_agent - run -124 --separate-stderr timeout 1s "${CROWDSEC}" + run -124 --separate-stderr timeout 2s "${CROWDSEC}" run -0 echo "$stderr" assert_output --partial "crowdsec agent is disabled" diff --git a/tests/bats/80_alerts.bats b/tests/bats/80_alerts.bats index 8153778ce..254404b26 100644 --- a/tests/bats/80_alerts.bats +++ b/tests/bats/80_alerts.bats @@ -42,4 +42,3 @@ teardown() { assert_output --partial "| 'githubciXXXXXXXXXXXXXXXXXXXXXXXX' |" assert_output --partial "| githubciXXXXXXXXXXXXXXXXXXXXXXXX |" } - diff --git a/tests/bats/90_decisions.bats b/tests/bats/90_decisions.bats index a36aa921a..87f2b2ceb 100644 --- a/tests/bats/90_decisions.bats +++ b/tests/bats/90_decisions.bats @@ -56,4 +56,3 @@ declare stderr assert_output --partial "| 'githubciXXXXXXXXXXXXXXXXXXXXXXXX' |" assert_output --partial "| githubciXXXXXXXXXXXXXXXXXXXXXXXX |" } - diff --git a/tests/bats/98_ipv4_range.bats b/tests/bats/98_ipv4_range.bats index 0c2551a7e..dd29b6517 100644 --- a/tests/bats/98_ipv4_range.bats +++ b/tests/bats/98_ipv4_range.bats @@ -37,8 +37,6 @@ api() { run -0 cscli decisions list -o json run -0 jq -r '.[0].decisions[0].value' <(output) assert_output '4.4.4.0/24' -# run -0 jq -c '[ .[0].decisions[0].value, .[1].decisions[0].value ]' <(output) -# assert_output '["4.4.4.0/24","1.2.3.4"]' } @test "$FILE API - all decisions" { diff --git a/tests/bats/99_lapi-stream-mode-scopes.bats b/tests/bats/99_lapi-stream-mode-scopes.bats index b4ec4d7ff..d35a95f05 100644 --- a/tests/bats/99_lapi-stream-mode-scopes.bats +++ b/tests/bats/99_lapi-stream-mode-scopes.bats @@ -49,7 +49,6 @@ api() { refute_output --partial 'toto' } - @test "$FILE stream start (user scope)" { run -0 api "/v1/decisions/stream?startup=true&scopes=user" run -0 jq -r '.new' <(output) @@ -57,7 +56,6 @@ api() { assert_output --partial 'toto' } - @test "$FILE stream start (user+ip scope)" { run -0 api "/v1/decisions/stream?startup=true&scopes=user,ip" run -0 jq -r '.new' <(output) diff --git a/tests/bats/99_lapi-stream-mode.bats b/tests/bats/99_lapi-stream-mode.bats index bd2dbb83d..f1190d52a 100644 --- a/tests/bats/99_lapi-stream-mode.bats +++ b/tests/bats/99_lapi-stream-mode.bats @@ -52,7 +52,6 @@ api() { assert_output --partial '1.2.3.5' } - @test "$FILE stream cont (del)" { sleep 1 run -0 cscli decisions delete -i '1.2.3.4' diff --git a/tests/bats/reformat b/tests/bats/reformat new file mode 100755 index 000000000..46ced82d3 --- /dev/null +++ b/tests/bats/reformat @@ -0,0 +1,16 @@ + +# from https://github.com/bats-core/bats-core/issues/192#issuecomment-528315083 +# thanks Sean Leather + +# Rewrite the Bats scripts in-place to look more like Bash scripts to shfmt +perl -pi -e 's/^(\@test.*) \{$/$1\n{/' ./*.bats + +tmpfile=$(mktemp) +for file in *bats; do + shfmt -i 4 -ln bash -s $file >$tmpfile + mv $tmpfile $file +done +rm -f $tmpfile + +# Undo the changes to the Bats scripts in-place so that they work with Bats +perl -pi -e 's/^\{\R//; s/(\@test.*$)/$1 {/' ./*.bats