github.com/crowdsecurity/crowdsec@v1.6.1/test/lib/setup_file.sh (about) 1 #!/usr/bin/env bash 2 3 # this should have effect globally, for all tests 4 # https://github.com/bats-core/bats-core/blob/master/docs/source/warnings/BW02.rst 5 bats_require_minimum_version 1.5.0 6 7 debug() { 8 echo 'exec 1<&-; exec 2<&-; exec 1>&3; exec 2>&1' 9 } 10 export -f debug 11 12 # redirects stdout and stderr to &3 otherwise the errors in setup, teardown would 13 # go unreported. 14 # BUT - don't do this in test functions. Everything written to stdout and 15 # stderr after this line will go to the terminal, but in the tests, these 16 # are supposed to be collected and shown only in case of test failure 17 # (see options --print-output-on-failure and --show-output-of-passing-tests) 18 eval "$(debug)" 19 20 # Allow tests to use relative paths for helper scripts. 21 # shellcheck disable=SC2164 22 cd "${TEST_DIR}" 23 export PATH="${TEST_DIR}/bin:${PATH}" 24 25 # complain if there's a crowdsec running system-wide or leftover from a previous test 26 ./bin/assert-crowdsec-not-running 27 28 # we can prepend the filename to the test descriptions (useful to feed a TAP consumer) 29 if [[ "${PREFIX_TEST_NAMES_WITH_FILE:-false}" == "true" ]]; then 30 BATS_TEST_NAME_PREFIX="$(basename "${BATS_TEST_FILENAME}" .bats): " 31 export BATS_TEST_NAME_PREFIX 32 fi 33 34 # before bats 1.7, we did that by hand 35 FILE= 36 export FILE 37 38 # the variables exported here can be seen in other setup/teardown/test functions 39 # MYVAR=something 40 # export MYVAR 41 42 # functions too 43 cscli() { 44 "${CSCLI}" "$@" 45 } 46 export -f cscli 47 48 config_get() { 49 local cfg="${CONFIG_YAML}" 50 if [[ $# -ge 2 ]]; then 51 cfg="$1" 52 shift 53 fi 54 55 yq e "$1" "${cfg}" 56 } 57 export -f config_get 58 59 config_set() { 60 local cfg="${CONFIG_YAML}" 61 if [[ $# -ge 2 ]]; then 62 cfg="$1" 63 shift 64 fi 65 66 yq e "$1" -i "${cfg}" 67 } 68 export -f config_set 69 70 config_disable_agent() { 71 config_set '.crowdsec_service.enable=false' 72 # this should be equivalent to: 73 # config_set 'del(.crowdsec_service)' 74 } 75 export -f config_disable_agent 76 77 config_log_stderr() { 78 config_set '.common.log_media="stdout"' 79 } 80 export -f config_log_stderr 81 82 config_disable_lapi() { 83 config_set '.api.server.enable=false' 84 # this should be equivalent to: 85 # config_set 'del(.api.server)' 86 } 87 export -f config_disable_lapi 88 89 config_disable_capi() { 90 config_set 'del(.api.server.online_client)' 91 } 92 export -f config_disable_capi 93 94 config_enable_capi() { 95 online_api_credentials="$(dirname "${CONFIG_YAML}")/online_api_credentials.yaml" \ 96 config_set '.api.server.online_client.credentials_path=strenv(online_api_credentials)' 97 } 98 export -f config_enable_capi 99 100 # We use these functions like this: 101 # somecommand <(stderr) 102 # to provide a standard input to "somecommand". 103 # The alternatives echo "$stderr" or <<<"$stderr" 104 # ("here string" in bash jargon) 105 # are worse because they add a newline, 106 # even if the variable is empty. 107 108 # shellcheck disable=SC2154 109 stderr() { 110 printf '%s' "${stderr}" 111 } 112 export -f stderr 113 114 # shellcheck disable=SC2154 115 output() { 116 printf '%s' "${output}" 117 } 118 export -f output 119 120 is_package_testing() { 121 [[ "$PACKAGE_TESTING" != "" ]] 122 } 123 export -f is_package_testing 124 125 is_db_postgres() { 126 [[ "$DB_BACKEND" =~ ^postgres|pgx$ ]] 127 } 128 export -f is_db_postgres 129 130 is_db_mysql() { 131 [[ "$DB_BACKEND" == "mysql" ]] 132 } 133 export -f is_db_mysql 134 135 is_db_sqlite() { 136 [[ "$DB_BACKEND" == "sqlite" ]] 137 } 138 export -f is_db_sqlite 139 140 crowdsec_log() { 141 echo "$(config_get .common.log_dir)"/crowdsec.log 142 } 143 export -f crowdsec_log 144 145 truncate_log() { 146 true > "$(crowdsec_log)" 147 } 148 export -f truncate_log 149 150 assert_log() { 151 local oldout="${output:-}" 152 output="$(cat "$(crowdsec_log)")" 153 assert_output "$@" 154 output="${oldout}" 155 } 156 export -f assert_log 157 158 # Compare ignoring the key order, and allow "expected" without quoted identifiers. 159 # Preserve the output variable in case the following commands require it. 160 assert_json() { 161 local oldout="${output}" 162 # validate actual, sort 163 run -0 jq -Sen "${output}" 164 local actual="${output}" 165 166 # handle stdin, quote identifiers, sort 167 local expected="$1" 168 if [[ "${expected}" == "-" ]]; then 169 expected="$(cat)" 170 fi 171 run -0 jq -Sn "${expected}" 172 expected="${output}" 173 174 #shellcheck disable=SC2016 175 run jq -ne --argjson a "${actual}" --argjson b "${expected}" '$a == $b' 176 #shellcheck disable=SC2154 177 if [[ "${status}" -ne 0 ]]; then 178 echo "expect: $(jq -c <<<"${expected}")" 179 echo "actual: $(jq -c <<<"${actual}")" 180 diff <(echo "${actual}") <(echo "${expected}") 181 fail "json does not match" 182 fi 183 output="${oldout}" 184 } 185 export -f assert_json 186 187 # Check if there's something on stdin by consuming it. Only use this as a way 188 # to check if something was passed by mistake, since if you read it, it will be 189 # incomplete. 190 is_stdin_empty() { 191 if read -r -t 0.1; then 192 return 1 193 fi 194 return 0 195 } 196 export -f is_stdin_empty 197 198 assert_stderr() { 199 # it is never useful to call this without arguments 200 if [[ "$#" -eq 0 ]]; then 201 # maybe the caller forgot to use '-' with an heredoc 202 if ! is_stdin_empty; then 203 fail "${FUNCNAME[0]}: called with stdin and no arguments (heredoc?)" 204 fi 205 fail "${FUNCNAME[0]}: called with no arguments" 206 fi 207 208 local oldout="${output}" 209 run -0 echo "${stderr}" 210 assert_output "$@" 211 output="${oldout}" 212 } 213 export -f assert_stderr 214 215 # like refute_output, but for stderr 216 refute_stderr() { 217 # calling this without arguments is ok, as long as stdin in empty 218 if ! is_stdin_empty; then 219 fail "${FUNCNAME[0]}: called with stdin (heredoc?)" 220 fi 221 222 local oldout="${output}" 223 run -0 echo "${stderr}" 224 refute_output "$@" 225 output="${oldout}" 226 } 227 export -f refute_stderr 228 229 # like assert_output, but for stderr 230 assert_stderr_line() { 231 if [[ "$#" -eq 0 ]]; then 232 fail "${FUNCNAME[0]}: called with no arguments" 233 fi 234 235 local oldout="${output}" 236 run -0 echo "${stderr}" 237 assert_line "$@" 238 output="${oldout}" 239 } 240 export -f assert_stderr_line 241 242 # remove all installed items and data 243 hub_purge_all() { 244 local CONFIG_DIR 245 local itemtype 246 CONFIG_DIR=$(dirname "$CONFIG_YAML") 247 for itemtype in $(cscli hub types -o raw); do 248 rm -rf "$CONFIG_DIR"/"${itemtype:?}"/* "$CONFIG_DIR"/hub/"${itemtype:?}"/* 249 done 250 local DATA_DIR 251 DATA_DIR=$(config_get .config_paths.data_dir) 252 # should remove everything except the db (find $DATA_DIR -not -name "crowdsec.db*" -delete), 253 # but don't play with fire if there is a misconfiguration 254 rm -rfv "$DATA_DIR"/GeoLite* 255 } 256 export -f hub_purge_all 257 258 # remove unused data from the index, to make sure we don't rely on it in any way 259 hub_strip_index() { 260 local INDEX 261 INDEX=$(config_get .config_paths.index_path) 262 local hub_min 263 hub_min=$(jq <"$INDEX" 'del(..|.content?) | del(..|.long_description?) | del(..|.deprecated?) | del (..|.labels?)') 264 echo "$hub_min" >"$INDEX" 265 } 266 export -f hub_strip_index 267 268 # remove color and style sequences from stdin 269 plaintext() { 270 sed -E 's/\x1B\[[0-9;]*[JKmsu]//g' 271 } 272 export -f plaintext 273 274 # like run but defaults to separate stderr and stdout 275 rune() { 276 run --separate-stderr "$@" 277 } 278 export -f rune