bit of doc + tests for lapi stream mode (#1356)

* bit of doc + tests for lapi stream mode
This commit is contained in:
Thibault "bui" Koechlin 2022-03-15 17:16:33 +01:00 committed by GitHub
parent b09339ae91
commit 548b0b5518
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 158 additions and 1 deletions

View file

@ -49,15 +49,32 @@ repositories).
| :----------------- | :--------------- | :----------- |
| alerts GET/POST | `9[78]_ipv[46]*` | |
| decisions GET/POST | `9[78]_ipv[46]*` | |
| stream mode | `99_lapi-stream-mode | |
# How to use it
Run `make clean bats-all` to perform a test build + run.
## pre-requisites
- `git submodule init; git submodule update`
- `daemonize bash, python3, openbsd-netcat`
- `yq` from https://github.com/mikefarah/yq
## Running all tests
Run `make clean bats-all` to perform a test build + run.
To repeat test runs without rebuilding crowdsec, use `make bats-test`.
## Troubleshooting tests
See `./tests/run-tests` usage to run/debug single test.
# How does it work?
In BATS, you write tests in the form of Bash functions that have unique

View file

@ -0,0 +1,66 @@
#!/usr/bin/env bats
# vim: ft=bats:list:ts=8:sts=4:sw=4:et:ai:si:
set -u
setup_file() {
load "../lib/setup_file.sh" >&3 2>&1
./instance-data load
./instance-crowdsec start
API_KEY=$(cscli bouncers add testbouncer -o raw)
export API_KEY
CROWDSEC_API_URL="http://localhost:8080"
export CROWDSEC_API_URL
}
teardown_file() {
load "../lib/teardown_file.sh" >&3 2>&1
}
setup() {
load "../lib/setup.sh"
}
#----------
api() {
URI="$1"
curl -s -H "X-Api-Key: ${API_KEY}" "${CROWDSEC_API_URL}${URI}"
}
@test "$FILE adding decisions for multiple scopes" {
run -0 cscli decisions add -i '1.2.3.6'
assert_output --partial 'Decision successfully added'
run -0 cscli decisions add --scope user --value toto
assert_output --partial 'Decision successfully added'
}
@test "$FILE stream start (implicit ip scope)" {
run -0 api "/v1/decisions/stream?startup=true"
run -0 jq -r '.new' <(output)
assert_output --partial '1.2.3.6'
refute_output --partial 'toto'
}
@test "$FILE stream start (explicit ip scope)" {
run -0 api "/v1/decisions/stream?startup=true&scopes=ip"
run -0 jq -r '.new' <(output)
assert_output --partial '1.2.3.6'
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)
refute_output --partial '1.2.3.6'
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)
assert_output --partial '1.2.3.6'
assert_output --partial 'toto'
}

View file

@ -0,0 +1,74 @@
#!/usr/bin/env bats
# vim: ft=bats:list:ts=8:sts=4:sw=4:et:ai:si:
set -u
setup_file() {
load "../lib/setup_file.sh" >&3 2>&1
./instance-data load
./instance-crowdsec start
API_KEY=$(cscli bouncers add testbouncer -o raw)
export API_KEY
CROWDSEC_API_URL="http://localhost:8080"
export CROWDSEC_API_URL
}
teardown_file() {
load "../lib/teardown_file.sh" >&3 2>&1
}
setup() {
load "../lib/setup.sh"
}
#----------
api() {
URI="$1"
curl -s -H "X-Api-Key: ${API_KEY}" "${CROWDSEC_API_URL}${URI}"
}
@test "$FILE adding decisions for multiple ips" {
run -0 cscli decisions add -i '1111:2222:3333:4444:5555:6666:7777:8888'
run -0 cscli decisions add -i '1.2.3.4'
run -0 cscli decisions add -r '1.2.4.0/24'
assert_output --partial 'Decision successfully added'
}
@test "$FILE stream start" {
run -0 api "/v1/decisions/stream?startup=true"
run -0 jq -r '.new' <(output)
assert_output --partial '1111:2222:3333:4444:5555:6666:7777:8888'
assert_output --partial '1.2.3.4'
assert_output --partial '1.2.4.0/24'
}
@test "$FILE stream cont (add)" {
sleep 1
run -0 cscli decisions add -i '1.2.3.5'
run -0 api "/v1/decisions/stream"
run -0 jq -r '.new' <(output)
assert_output --partial '1.2.3.5'
}
@test "$FILE stream cont (del)" {
sleep 1
run -0 cscli decisions delete -i '1.2.3.4'
run -0 api "/v1/decisions/stream"
run -0 jq -r '.deleted' <(output)
assert_output --partial '1.2.3.4'
}
@test "$FILE stream restart" {
run -0 api "/v1/decisions/stream?startup=true"
api_out=$output
run -0 jq -r '.deleted' <(output)
assert_output --partial '1.2.3.4'
output=$api_out
run -0 jq -r '.new' <(output)
assert_output --partial '1111:2222:3333:4444:5555:6666:7777:8888'
assert_output --partial '1.2.3.5'
assert_output --partial '1.2.4.0/24'
}