Debian Codegraph @

tests/lib/config/config-global

raw symbols SCIP
1 #!/usr/bin/envscip-shell filesystem . . `/usr/bin/env`. bash
2
3 set -eu
4 script_name=$0
5
6 diescip-shell shell crowdsec 1.4.6-10.1 die().() {
7 echoscip-shell system . . echo. >&2 "$@"
8 exit 1
9 }
10
11 aboutscip-shell shell crowdsec 1.4.6-10.1 about().() {
12 diescip-shell shell crowdsec 1.4.6-10.1 die(). "usage: ${script_name} [make | load | clean]"
13 }
14
15 #shellcheck disable=SC1007
16 THIS_DIR=$(CDPATH= cd -- "$(dirnamescip-shell system . . dirname. -- "$0")" && pwdscip-shell system . . pwd.)
17 cd "${THIS_DIR}"/../../
18 #shellcheck disable=SC1091
19 . ./.environment.shscip-shell source . . `./.environment.sh`.
20
21 # you have not removed set -u above, have you?
22
23 [[ -z "${TEST_DIR-}" ]] && diescip-shell shell crowdsec 1.4.6-10.1 die(). "\$TEST_DIR must be defined."
24 [[ -z "${LOCAL_DIR-}" ]] && diescip-shell shell crowdsec 1.4.6-10.1 die(). "\$LOCAL_DIR must be defined."
25 [[ -z "${CSCLI-}" ]] && diescip-shell shell crowdsec 1.4.6-10.1 die(). "\$CSCLI must be defined."
26 [[ -z "${LOCAL_INIT_DIR-}" ]] && diescip-shell shell crowdsec 1.4.6-10.1 die(). "\$LOCAL_INIT_DIR must be defined."
27 [[ -z "${PLUGIN_DIR-}" ]] && diescip-shell shell crowdsec 1.4.6-10.1 die(). "\$PLUGIN_DIR must be defined."
28 [[ -z "${DB_BACKEND-}" ]] && diescip-shell shell crowdsec 1.4.6-10.1 die(). "\$DB_BACKEND must be defined."
29
30 if [[ ! -f "${CSCLI}" ]]; then
31 diescip-shell shell crowdsec 1.4.6-10.1 die(). "${CSCLI} is missing. Please build (with 'make bats-build') or install it."
32 fi
33
34 REL_CONFIG_DIR="etc/crowdsec"
35 REL_DATA_DIR="var/lib/crowdsec/data"
36
37 DATA_DIR="${LOCAL_DIR}/${REL_DATA_DIR}"
38 export DATA_DIR
39 CONFIG_DIR="${LOCAL_DIR}/${REL_CONFIG_DIR}"
40 export CONFIG_DIR
41
42 if [[ $(uname) == "OpenBSD" ]]; then
43 TAR=gtar
44 else
45 TAR=tar
46 fi
47
48 remove_init_datascip-shell shell crowdsec 1.4.6-10.1 remove_init_data().() {
49 ./bin/assert-crowdsec-not-running || diescip-shell shell crowdsec 1.4.6-10.1 die(). "Cannot remove fixture data."
50 rmscip-shell system . . rm. -rf -- "${LOCAL_DIR:?}/${REL_CONFIG_DIR}"/* "${LOCAL_DIR:?}/${REL_DATA_DIR:?}"/*
51 }
52
53 # we need a separate function for initializing config when testing package
54 # because we want to test the configuration as well
55 make_init_datascip-shell shell crowdsec 1.4.6-10.1 make_init_data().() {
56 ./bin/assert-crowdsec-not-running || diescip-shell shell crowdsec 1.4.6-10.1 die(). "Cannot create fixture data."
57
58 ./instance-db config-yaml
59 ./instance-db setup
60
61 # when installed packages are always using sqlite, so no need to regenerate
62 # local credz for sqlite
63
64 [[ "${DB_BACKEND}" == "sqlite" ]] || ${CSCLI} machines add --auto
65
66 mkdirscip-shell system . . mkdir. -p "${LOCAL_INIT_DIR}"
67
68 ./instance-db dump "${LOCAL_INIT_DIR}/database"
69
70 echoscip-shell system . . echo. "${DB_BACKEND}" > "${LOCAL_INIT_DIR}/.backend"
71
72 # disable CAPI by default
73 yq e 'del(.api.server.online_client)' -i "${CONFIG_DIR}/config.yaml"
74
75 "${TAR}" -C "${LOCAL_DIR}" --create \
76 --exclude "${REL_DATA_DIR}"/crowdsec.db \
77 --file "${LOCAL_INIT_DIR}/init-config-data.tar" "${REL_CONFIG_DIR}" "${REL_DATA_DIR}"
78 }
79
80 load_init_datascip-shell shell crowdsec 1.4.6-10.1 load_init_data().() {
81 ./bin/assert-crowdsec-not-running || diescip-shell shell crowdsec 1.4.6-10.1 die(). "Cannot load fixture data."
82
83 if [[ ! -f "${LOCAL_INIT_DIR}/init-config-data.tar" ]]; then
84 diescip-shell shell crowdsec 1.4.6-10.1 die(). "Initial data not found; did you run '${script_name} make' ?"
85 fi
86
87 dump_backend="$(catscip-shell system . . cat. "${LOCAL_INIT_DIR}/.backend")"
88 if [[ "${DB_BACKEND}" != "${dump_backend}" ]]; then
89 diescip-shell shell crowdsec 1.4.6-10.1 die(). "Can't run with backend '${DB_BACKEND}' because the test data was built with '${dump_backend}'"
90 fi
91
92 remove_init_datascip-shell shell crowdsec 1.4.6-10.1 remove_init_data().
93
94 "${TAR}" -C "${LOCAL_DIR}" --extract --file "${LOCAL_INIT_DIR}/init-config-data.tar"
95
96 ./instance-db restore "${LOCAL_INIT_DIR}/database"
97 }
98
99
100 # ---------------------------
101
102 [[ $# -lt 1 ]] && aboutscip-shell shell crowdsec 1.4.6-10.1 about().
103
104 case "$1" in
105 make)
106 "${TEST_DIR}/instance-crowdsec" stop
107 make_init_datascip-shell shell crowdsec 1.4.6-10.1 make_init_data().
108 ;;
109 load)
110 load_init_datascip-shell shell crowdsec 1.4.6-10.1 load_init_data().
111 ;;
112 clean)
113 remove_init_datascip-shell shell crowdsec 1.4.6-10.1 remove_init_data().
114 ;;
115 *)
116 aboutscip-shell shell crowdsec 1.4.6-10.1 about().
117 ;;
118 esac;
119