save fixture with community data (#1440)

This commit is contained in:
mmetc 2022-04-13 15:17:49 +02:00 committed by GitHub
parent 0190614342
commit 17ed27052c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 79 additions and 22 deletions

View file

@ -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"

View file

@ -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'"

View file

@ -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" {

View file

@ -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

View file

@ -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

View file

@ -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'
}

View file

@ -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'

View file

@ -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'

View file

@ -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'

View file

@ -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'

View file

@ -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}"

View file

@ -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