diff --git a/tests/bats/01_base.bats b/tests/bats/01_base.bats index 62e2b4637..b9788ccf0 100644 --- a/tests/bats/01_base.bats +++ b/tests/bats/01_base.bats @@ -47,14 +47,21 @@ 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 - sleep 2 - run -0 cscli alerts list -o json - [ "$output" != "null" ] && break - done + is_db_postgres && skip + # it should have been received while preparing the fixture + run -0 cscli alerts list -o json run -0 jq -r '. | length' <(output) refute_output 0 + + # if we want to trigger it here, we'll have to remove decisions, restart crowdsec and wait like this: + # loop_max=15 + # for ((i = 0; i <= loop_max; i++)); do + # sleep 2 + # run -0 cscli alerts list -o json + # [ "$output" != "null" ] && break + # done + # run -0 jq -r '. | length' <(output) + # refute_output 0 } @test "$FILE cscli capi status" { @@ -116,7 +123,7 @@ declare stderr } @test "$FILE cscli lapi status" { - [[ "$DB_BACKEND" =~ ^postgres|pgx$ ]] && sleep 4 + if is_db_postgres; then sleep 4; fi run -0 --separate-stderr cscli lapi status run -0 echo "$stderr" diff --git a/tests/bats/30_machines.bats b/tests/bats/30_machines.bats index efca6b73a..41a0cb68c 100644 --- a/tests/bats/30_machines.bats +++ b/tests/bats/30_machines.bats @@ -60,7 +60,7 @@ teardown() { } @test "$FILE register, validate and then remove a machine" { - [[ "$DB_BACKEND" =~ ^postgres|pgx$ ]] && skip + if is_db_postgres; then sleep 4; fi run -0 cscli lapi register --machine CiTestMachineRegister -f /dev/null -o human assert_output --partial "Successfully registered to Local API (LAPI)" assert_output --partial "Local API credentials dumped to '/dev/null'" diff --git a/tests/bats/70_http_plugin.bats b/tests/bats/70_http_plugin.bats index 57b060886..ce691dbea 100644 --- a/tests/bats/70_http_plugin.bats +++ b/tests/bats/70_http_plugin.bats @@ -54,7 +54,6 @@ setup() { run -0 cscli decisions add --ip 1.2.3.5 --duration 30s assert_output --partial 'Decision successfully added' - sleep 2 } @test "$FILE expected 1 log line from http server" { diff --git a/tests/bats/80_alerts.bats b/tests/bats/80_alerts.bats index b565f4d3c..0861f8047 100644 --- a/tests/bats/80_alerts.bats +++ b/tests/bats/80_alerts.bats @@ -24,7 +24,7 @@ teardown() { #---------- @test "$FILE cscli alerts list, with and without --machine" { - [[ "$DB_BACKEND" =~ ^postgres|pgx$ ]] && skip + is_db_postgres && skip run -0 cscli decisions add -i 10.20.30.40 -t ban run -0 cscli alerts list diff --git a/tests/bats/90_decisions.bats b/tests/bats/90_decisions.bats index bd43d5609..b287f9148 100644 --- a/tests/bats/90_decisions.bats +++ b/tests/bats/90_decisions.bats @@ -38,7 +38,7 @@ declare stderr } @test "$FILE cscli decisions list, with and without --machine" { - [[ "$DB_BACKEND" =~ ^postgres|pgx$ ]] && skip + is_db_postgres && skip run -0 cscli decisions add -i 10.20.30.40 -t ban run -0 cscli decisions list diff --git a/tests/bats/97_ipv4_single.bats b/tests/bats/97_ipv4_single.bats index 069583b50..9c3912b44 100644 --- a/tests/bats/97_ipv4_single.bats +++ b/tests/bats/97_ipv4_single.bats @@ -19,6 +19,7 @@ teardown_file() { setup() { load "../lib/setup.sh" + if is_db_mysql; then sleep 0.3; fi } api() { @@ -29,6 +30,8 @@ api() { #---------- @test "$FILE cli - first decisions list: must be empty" { + # delete community pull + run -0 cscli decisions delete --all run -0 cscli decisions list -o json assert_output 'null' } diff --git a/tests/bats/97_ipv6_single.bats b/tests/bats/97_ipv6_single.bats index 7dba0e5f6..74c66ab9e 100644 --- a/tests/bats/97_ipv6_single.bats +++ b/tests/bats/97_ipv6_single.bats @@ -19,17 +19,23 @@ teardown_file() { setup() { load "../lib/setup.sh" - # some environments in CI require more time (mysql, test coverage) - sleep 0.3 + if is_db_mysql; then sleep 0.3; fi } -#---------- - api() { URI="$1" curl -s -H "X-Api-Key: ${API_KEY}" "${CROWDSEC_API_URL}${URI}" } +#---------- + +@test "$FILE cli - first decisions list: must be empty" { + # delete community pull + run -0 cscli decisions delete --all + run -0 cscli decisions list -o json + assert_output 'null' +} + @test "$FILE adding decision for ip 1111:2222:3333:4444:5555:6666:7777:8888" { run -0 cscli decisions add -i '1111:2222:3333:4444:5555:6666:7777:8888' assert_output --partial 'Decision successfully added' diff --git a/tests/bats/98_ipv4_range.bats b/tests/bats/98_ipv4_range.bats index dd29b6517..10c1634dc 100644 --- a/tests/bats/98_ipv4_range.bats +++ b/tests/bats/98_ipv4_range.bats @@ -19,6 +19,7 @@ teardown_file() { setup() { load "../lib/setup.sh" + if is_db_mysql; then sleep 0.3; fi } api() { @@ -28,6 +29,13 @@ api() { #---------- +@test "$FILE cli - first decisions list: must be empty" { + # delete community pull + run -0 cscli decisions delete --all + run -0 cscli decisions list -o json + assert_output 'null' +} + @test "$FILE adding decision for range 4.4.4.0/24" { run -0 cscli decisions add -r '4.4.4.0/24' assert_output --partial 'Decision successfully added' diff --git a/tests/bats/98_ipv6_range.bats b/tests/bats/98_ipv6_range.bats index 5004aabaf..b2173bcbe 100644 --- a/tests/bats/98_ipv6_range.bats +++ b/tests/bats/98_ipv6_range.bats @@ -19,17 +19,23 @@ teardown_file() { setup() { load "../lib/setup.sh" - # some environments in CI require more time (mysql, test coverage) - sleep 0.3 + if is_db_mysql; then sleep 0.3; fi } -#---------- - api() { URI="$1" curl -s -H "X-Api-Key: ${API_KEY}" "${CROWDSEC_API_URL}${URI}" } +#---------- + +@test "$FILE cli - first decisions list: must be empty" { + # delete community pull + run -0 cscli decisions delete --all + run -0 cscli decisions list -o json + assert_output 'null' +} + @test "$FILE adding decision for range aaaa:2222:3333:4444::/64" { run -0 cscli decisions add -r 'aaaa:2222:3333:4444::/64' assert_output --partial 'Decision successfully added' diff --git a/tests/bats/99_lapi-stream-mode.bats b/tests/bats/99_lapi-stream-mode.bats index 1fa93fade..2b39014bb 100644 --- a/tests/bats/99_lapi-stream-mode.bats +++ b/tests/bats/99_lapi-stream-mode.bats @@ -37,7 +37,7 @@ api() { @test "$FILE stream start" { run -0 api "/v1/decisions/stream?startup=true" - [[ "$DB_BACKEND" == "mysql" ]] && sleep 3 + if is_db_mysql; then sleep 3; fi run -0 jq -r '.new' <(output) assert_output --partial '1111:2222:3333:4444:5555:6666:7777:8888' assert_output --partial '1.2.3.4' @@ -46,7 +46,7 @@ api() { @test "$FILE stream cont (add)" { run -0 cscli decisions add -i '1.2.3.5' - [[ "$DB_BACKEND" == "mysql" ]] && sleep 3 + if is_db_mysql; then sleep 3; fi run -0 api "/v1/decisions/stream" run -0 jq -r '.new' <(output) assert_output --partial '1.2.3.5' @@ -54,7 +54,7 @@ api() { @test "$FILE stream cont (del)" { run -0 cscli decisions delete -i '1.2.3.4' - [[ "$DB_BACKEND" == "mysql" ]] && sleep 3 + if is_db_mysql; then sleep 3; fi run -0 api "/v1/decisions/stream" run -0 jq -r '.deleted' <(output) assert_output --partial '1.2.3.4' diff --git a/tests/lib/config/config-local b/tests/lib/config/config-local index d19edcb9a..4e75b0f19 100755 --- a/tests/lib/config/config-local +++ b/tests/lib/config/config-local @@ -98,6 +98,19 @@ make_init_data() { "${TEST_DIR}/instance-crowdsec" start [[ "$DB_BACKEND" =~ ^postgres|pgx$ ]] && sleep 4 "${CSCLI}" lapi status + + # a restart is required to receive community pull + "${TEST_DIR}/instance-crowdsec" stop + sleep 2 + "${TEST_DIR}/instance-crowdsec" start + + loop_max=15 + for ((i = 0; i <= loop_max; i++)); do + sleep 2 + [[ $("${CSCLI}" alerts list -o json) != "null" ]] && break + done + [[ $("${CSCLI}" alerts list -o json) != "null" ]] || die "could not get community data" + "${TEST_DIR}/instance-crowdsec" stop mkdir -p "${LOCAL_INIT_DIR}" diff --git a/tests/lib/setup_file.sh b/tests/lib/setup_file.sh index 19d03cd57..dbbd8caeb 100644 --- a/tests/lib/setup_file.sh +++ b/tests/lib/setup_file.sh @@ -58,3 +58,18 @@ output() { } export -f output +is_db_postgres() { + [[ "$DB_BACKEND" =~ ^postgres|pgx$ ]] +} +export -f is_db_postgres + +is_db_mysql() { + [[ "$DB_BACKEND" == "mysql" ]] +} +export -f is_db_mysql + +is_db_sqlite() { + [[ "$DB_BACKEND" == "sqlite" ]] +} +export -f is_db_sqlite +