Skip to content

Commit

Permalink
CI: disable log formatting for consistency (#2989)
Browse files Browse the repository at this point in the history
* CI: disable log formatting for consistency

* lint (shellharden)
  • Loading branch information
mmetc authored May 7, 2024
1 parent e5cd480 commit defa076
Show file tree
Hide file tree
Showing 22 changed files with 158 additions and 159 deletions.
40 changes: 20 additions & 20 deletions test/bats/01_crowdsec.bats
Original file line number Diff line number Diff line change
Expand Up @@ -24,52 +24,52 @@ teardown() {
#----------

@test "crowdsec (usage)" {
rune -0 wait-for --out "Usage of " "${CROWDSEC}" -h
rune -0 wait-for --out "Usage of " "${CROWDSEC}" --help
rune -0 wait-for --out "Usage of " "$CROWDSEC" -h
rune -0 wait-for --out "Usage of " "$CROWDSEC" --help
}

@test "crowdsec (unknown flag)" {
rune -0 wait-for --err "flag provided but not defined: -foobar" "$CROWDSEC" --foobar
}

@test "crowdsec (unknown argument)" {
rune -0 wait-for --err "argument provided but not defined: trololo" "${CROWDSEC}" trololo
rune -0 wait-for --err "argument provided but not defined: trololo" "$CROWDSEC" trololo
}

@test "crowdsec (no api and no agent)" {
rune -0 wait-for \
--err "you must run at least the API Server or crowdsec" \
"${CROWDSEC}" -no-api -no-cs
"$CROWDSEC" -no-api -no-cs
}

@test "crowdsec - print error on exit" {
# errors that cause program termination are printed to stderr, not only logs
config_set '.db_config.type="meh"'
rune -1 "${CROWDSEC}"
rune -1 "$CROWDSEC"
assert_stderr --partial "unable to create database client: unknown database type 'meh'"
}

@test "crowdsec - default logging configuration (empty/missing common section)" {
config_set '.common={}'
rune -0 wait-for \
--err "Starting processing data" \
"${CROWDSEC}"
"$CROWDSEC"
refute_output

config_set 'del(.common)'
rune -0 wait-for \
--err "Starting processing data" \
"${CROWDSEC}"
"$CROWDSEC"
refute_output
}

@test "CS_LAPI_SECRET not strong enough" {
CS_LAPI_SECRET=foo rune -1 wait-for "${CROWDSEC}"
CS_LAPI_SECRET=foo rune -1 wait-for "$CROWDSEC"
assert_stderr --partial "api server init: unable to run local API: controller init: CS_LAPI_SECRET not strong enough"
}

@test "crowdsec - reload (change of logfile, disabled agent)" {
logdir1=$(TMPDIR="${BATS_TEST_TMPDIR}" mktemp -u)
logdir1=$(TMPDIR="$BATS_TEST_TMPDIR" mktemp -u)
log_old="${logdir1}/crowdsec.log"
config_set ".common.log_dir=\"${logdir1}\""

Expand All @@ -81,7 +81,7 @@ teardown() {
assert_file_exists "$log_old"
assert_file_contains "$log_old" "Starting processing data"

logdir2=$(TMPDIR="${BATS_TEST_TMPDIR}" mktemp -u)
logdir2=$(TMPDIR="$BATS_TEST_TMPDIR" mktemp -u)
log_new="${logdir2}/crowdsec.log"
config_set ".common.log_dir=\"${logdir2}\""

Expand Down Expand Up @@ -137,7 +137,7 @@ teardown() {
ACQUIS_YAML=$(config_get '.crowdsec_service.acquisition_path')
rm -f "$ACQUIS_YAML"

rune -1 wait-for "${CROWDSEC}"
rune -1 wait-for "$CROWDSEC"
assert_stderr --partial "acquis.yaml: no such file or directory"
}

Expand All @@ -150,7 +150,7 @@ teardown() {
rm -f "$ACQUIS_DIR"

config_set '.common.log_media="stdout"'
rune -1 wait-for "${CROWDSEC}"
rune -1 wait-for "$CROWDSEC"
# check warning
assert_stderr --partial "no acquisition file found"
assert_stderr --partial "crowdsec init: while loading acquisition config: no datasource enabled"
Expand All @@ -166,7 +166,7 @@ teardown() {
config_set '.crowdsec_service.acquisition_dir=""'

config_set '.common.log_media="stdout"'
rune -1 wait-for "${CROWDSEC}"
rune -1 wait-for "$CROWDSEC"
# check warning
assert_stderr --partial "no acquisition_path or acquisition_dir specified"
assert_stderr --partial "crowdsec init: while loading acquisition config: no datasource enabled"
Expand All @@ -184,13 +184,13 @@ teardown() {

rune -0 wait-for \
--err "Starting processing data" \
"${CROWDSEC}"
"$CROWDSEC"

# now, if foo.yaml is empty instead, there won't be valid datasources.

cat /dev/null >"$ACQUIS_DIR"/foo.yaml

rune -1 wait-for "${CROWDSEC}"
rune -1 wait-for "$CROWDSEC"
assert_stderr --partial "crowdsec init: while loading acquisition config: no datasource enabled"
}

Expand All @@ -217,16 +217,16 @@ teardown() {

#shellcheck disable=SC2016
rune -0 wait-for \
--err 'datasource '\''journalctl'\'' is not available: exec: "journalctl": executable file not found in ' \
env PATH='' "${CROWDSEC}"
--err 'datasource '\''journalctl'\'' is not available: exec: \\"journalctl\\": executable file not found in ' \
env PATH='' "$CROWDSEC"

# if all datasources are disabled, crowdsec should exit

ACQUIS_YAML=$(config_get '.crowdsec_service.acquisition_path')
rm -f "$ACQUIS_YAML"
config_set '.crowdsec_service.acquisition_path=""'

rune -1 wait-for env PATH='' "${CROWDSEC}"
rune -1 wait-for env PATH='' "$CROWDSEC"
assert_stderr --partial "crowdsec init: while loading acquisition config: no datasource enabled"
}

Expand All @@ -237,11 +237,11 @@ teardown() {

# if filenames are missing, it won't be able to detect source type
config_set "$ACQUIS_YAML" '.source="file"'
rune -1 wait-for "${CROWDSEC}"
rune -1 wait-for "$CROWDSEC"
assert_stderr --partial "failed to configure datasource file: no filename or filenames configuration provided"

config_set "$ACQUIS_YAML" '.filenames=["file.log"]'
config_set "$ACQUIS_YAML" '.meh=3'
rune -1 wait-for "${CROWDSEC}"
rune -1 wait-for "$CROWDSEC"
assert_stderr --partial "field meh not found in type fileacquisition.FileConfiguration"
}
8 changes: 4 additions & 4 deletions test/bats/01_crowdsec_lapi.bats
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@ teardown() {

@test "lapi (.api.server.enable=false)" {
rune -0 config_set '.api.server.enable=false'
rune -1 "${CROWDSEC}" -no-cs
rune -1 "$CROWDSEC" -no-cs
assert_stderr --partial "you must run at least the API Server or crowdsec"
}

@test "lapi (no .api.server.listen_uri)" {
rune -0 config_set 'del(.api.server.listen_socket) | del(.api.server.listen_uri)'
rune -1 "${CROWDSEC}" -no-cs
rune -1 "$CROWDSEC" -no-cs
assert_stderr --partial "no listen_uri or listen_socket specified"
}

@test "lapi (bad .api.server.listen_uri)" {
rune -0 config_set 'del(.api.server.listen_socket) | .api.server.listen_uri="127.0.0.1:-80"'
rune -1 "${CROWDSEC}" -no-cs
rune -1 "$CROWDSEC" -no-cs
assert_stderr --partial "local API server stopped with error: listening on 127.0.0.1:-80: listen tcp: address -80: invalid port"
}

@test "lapi (listen on random port)" {
config_set '.common.log_media="stdout"'
rune -0 config_set 'del(.api.server.listen_socket) | .api.server.listen_uri="127.0.0.1:0"'
rune -0 wait-for --err "CrowdSec Local API listening on 127.0.0.1:" "${CROWDSEC}" -no-cs
rune -0 wait-for --err "CrowdSec Local API listening on 127.0.0.1:" "$CROWDSEC" -no-cs
}

40 changes: 20 additions & 20 deletions test/bats/01_cscli.bats
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ teardown() {
assert_stderr --partial "Constraint_acquis:"

# should work without configuration file
rm "${CONFIG_YAML}"
rm "$CONFIG_YAML"
rune -0 cscli version
assert_stderr --partial "version:"
}
Expand All @@ -62,7 +62,7 @@ teardown() {
assert_line --regexp ".* help .* Help about any command"

# should work without configuration file
rm "${CONFIG_YAML}"
rm "$CONFIG_YAML"
rune -0 cscli help
assert_line "Available Commands:"
}
Expand Down Expand Up @@ -132,34 +132,34 @@ teardown() {


@test "cscli - required configuration paths" {
config=$(cat "${CONFIG_YAML}")
config=$(cat "$CONFIG_YAML")
configdir=$(config_get '.config_paths.config_dir')

# required configuration paths with no defaults

config_set 'del(.config_paths)'
rune -1 cscli hub list
assert_stderr --partial 'no configuration paths provided'
echo "$config" > "${CONFIG_YAML}"
echo "$config" > "$CONFIG_YAML"

config_set 'del(.config_paths.data_dir)'
rune -1 cscli hub list
assert_stderr --partial "please provide a data directory with the 'data_dir' directive in the 'config_paths' section"
echo "$config" > "${CONFIG_YAML}"
echo "$config" > "$CONFIG_YAML"

# defaults

config_set 'del(.config_paths.hub_dir)'
rune -0 cscli hub list
rune -0 cscli config show --key Config.ConfigPaths.HubDir
assert_output "$configdir/hub"
echo "$config" > "${CONFIG_YAML}"
echo "$config" > "$CONFIG_YAML"

config_set 'del(.config_paths.index_path)'
rune -0 cscli hub list
rune -0 cscli config show --key Config.ConfigPaths.HubIndexFile
assert_output "$configdir/hub/.index.json"
echo "$config" > "${CONFIG_YAML}"
echo "$config" > "$CONFIG_YAML"
}

@test "cscli config show-yaml" {
Expand All @@ -182,30 +182,30 @@ teardown() {
assert_stderr --partial "failed to backup config: while creating /dev/null/blah: mkdir /dev/null/blah: not a directory"

# pick a dirpath
backupdir=$(TMPDIR="${BATS_TEST_TMPDIR}" mktemp -u)
backupdir=$(TMPDIR="$BATS_TEST_TMPDIR" mktemp -u)

# succeed the first time
rune -0 cscli config backup "${backupdir}"
rune -0 cscli config backup "$backupdir"
assert_stderr --partial "Starting configuration backup"

# don't overwrite an existing backup
rune -1 cscli config backup "${backupdir}"
rune -1 cscli config backup "$backupdir"
assert_stderr --partial "failed to backup config"
assert_stderr --partial "file exists"

SIMULATION_YAML="$(config_get '.config_paths.simulation_path')"

# restore
rm "${SIMULATION_YAML}"
rune -0 cscli config restore "${backupdir}"
assert_file_exists "${SIMULATION_YAML}"
rm "$SIMULATION_YAML"
rune -0 cscli config restore "$backupdir"
assert_file_exists "$SIMULATION_YAML"

# cleanup
rm -rf -- "${backupdir:?}"

# backup: detect missing files
rm "${SIMULATION_YAML}"
rune -1 cscli config backup "${backupdir}"
rm "$SIMULATION_YAML"
rune -1 cscli config backup "$backupdir"
assert_stderr --regexp "failed to backup config: failed copy .* to .*: stat .*: no such file or directory"
rm -rf -- "${backupdir:?}"
}
Expand All @@ -221,7 +221,7 @@ teardown() {

@test "cscli - missing LAPI credentials file" {
LOCAL_API_CREDENTIALS=$(config_get '.api.client.credentials_path')
rm -f "${LOCAL_API_CREDENTIALS}"
rm -f "$LOCAL_API_CREDENTIALS"
rune -1 cscli lapi status
assert_stderr --partial "loading api client: while reading yaml file: open ${LOCAL_API_CREDENTIALS}: no such file or directory"

Expand All @@ -234,7 +234,7 @@ teardown() {

@test "cscli - empty LAPI credentials file" {
LOCAL_API_CREDENTIALS=$(config_get '.api.client.credentials_path')
: > "${LOCAL_API_CREDENTIALS}"
: > "$LOCAL_API_CREDENTIALS"
rune -1 cscli lapi status
assert_stderr --partial "no credentials or URL found in api client configuration '${LOCAL_API_CREDENTIALS}'"

Expand All @@ -259,7 +259,7 @@ teardown() {

@test "cscli - malformed LAPI url" {
LOCAL_API_CREDENTIALS=$(config_get '.api.client.credentials_path')
config_set "${LOCAL_API_CREDENTIALS}" '.url="http://127.0.0.1:-80"'
config_set "$LOCAL_API_CREDENTIALS" '.url="http://127.0.0.1:-80"'

rune -1 cscli lapi status -o json
rune -0 jq -r '.msg' <(stderr)
Expand All @@ -269,7 +269,7 @@ teardown() {
@test "cscli - bad LAPI password" {
rune -0 ./instance-crowdsec start
LOCAL_API_CREDENTIALS=$(config_get '.api.client.credentials_path')
config_set "${LOCAL_API_CREDENTIALS}" '.password="meh"'
config_set "$LOCAL_API_CREDENTIALS" '.password="meh"'

rune -1 cscli lapi status -o json
rune -0 jq -r '.msg' <(stderr)
Expand All @@ -286,7 +286,7 @@ teardown() {
rune -0 cscli completion fish
assert_output --partial "# fish completion for cscli"

rm "${CONFIG_YAML}"
rm "$CONFIG_YAML"
rune -0 cscli completion bash
assert_output --partial "# bash completion for cscli"
}
Expand Down
12 changes: 6 additions & 6 deletions test/bats/02_nolapi.bats
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ teardown() {
config_set '.common.log_media="stdout"'
rune -0 wait-for \
--err "CrowdSec Local API listening" \
"${CROWDSEC}"
"$CROWDSEC"
}

@test "crowdsec should not run without LAPI (-no-api flag)" {
config_set '.common.log_media="stdout"'
rune -1 wait-for "${CROWDSEC}" -no-api
rune -1 wait-for "$CROWDSEC" -no-api
}

@test "crowdsec should not run without LAPI (no api.server in configuration file)" {
config_disable_lapi
config_log_stderr
rune -0 wait-for \
--err "crowdsec local API is disabled" \
"${CROWDSEC}"
"$CROWDSEC"
}

@test "capi status shouldn't be ok without api.server" {
Expand Down Expand Up @@ -68,10 +68,10 @@ teardown() {

@test "cscli config backup" {
config_disable_lapi
backupdir=$(TMPDIR="${BATS_TEST_TMPDIR}" mktemp -u)
rune -0 cscli config backup "${backupdir}"
backupdir=$(TMPDIR="$BATS_TEST_TMPDIR" mktemp -u)
rune -0 cscli config backup "$backupdir"
assert_stderr --partial "Starting configuration backup"
rune -1 cscli config backup "${backupdir}"
rune -1 cscli config backup "$backupdir"
rm -rf -- "${backupdir:?}"

assert_stderr --partial "failed to backup config"
Expand Down
12 changes: 6 additions & 6 deletions test/bats/03_noagent.bats
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ teardown() {
config_set '.common.log_media="stdout"'
rune -0 wait-for \
--err "Starting processing data" \
"${CROWDSEC}"
"$CROWDSEC"
}

@test "no agent: crowdsec LAPI should run (-no-cs flag)" {
config_set '.common.log_media="stdout"'
rune -0 wait-for \
--err "CrowdSec Local API listening" \
"${CROWDSEC}" -no-cs
"$CROWDSEC" -no-cs
}

@test "no agent: crowdsec LAPI should run (no crowdsec_service in configuration file)" {
config_disable_agent
config_log_stderr
rune -0 wait-for \
--err "crowdsec agent is disabled" \
"${CROWDSEC}"
"$CROWDSEC"
}

@test "no agent: cscli config show" {
Expand All @@ -62,10 +62,10 @@ teardown() {

@test "no agent: cscli config backup" {
config_disable_agent
backupdir=$(TMPDIR="${BATS_TEST_TMPDIR}" mktemp -u)
rune -0 cscli config backup "${backupdir}"
backupdir=$(TMPDIR="$BATS_TEST_TMPDIR" mktemp -u)
rune -0 cscli config backup "$backupdir"
assert_stderr --partial "Starting configuration backup"
rune -1 cscli config backup "${backupdir}"
rune -1 cscli config backup "$backupdir"

assert_stderr --partial "failed to backup config"
assert_stderr --partial "file exists"
Expand Down
Loading

0 comments on commit defa076

Please sign in to comment.