Quellcode durchsuchen

save fixture with community data (#1440)

mmetc vor 3 Jahren
Ursprung
Commit
17ed27052c

+ 14 - 7
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"

+ 1 - 1
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'"

+ 0 - 1
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" {

+ 1 - 1
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

+ 1 - 1
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

+ 3 - 0
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'
 }

+ 10 - 4
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'

+ 8 - 0
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'

+ 10 - 4
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'

+ 3 - 3
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'

+ 13 - 0
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}"

+ 15 - 0
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
+