Simplify health cmdapi tester - no setup/cleanup needed (#6210)
Chris Akritidis committed
Jun 4, 2019 at 11:51 UTC
fd976d40e2d90e31af4a6c34726f13dcf562e824
3 files changed
+34
-125
tests/health_mgmtapi/README.md
+2
-3
@@ -4,9 +4,8 @@ The directory `tests/health_cmdapi` contains the test script `health-cmdapi-test
4
5
The script can be executed with options to prepare the system for the tests, run them and restore the system to its previous state.
6
7
-It depends on the management API being accessible and on the responses to the api/v1/alarms?all requests being functional.
8
-
9
-Run it with `tests/health_mgmtapi/health-cmdapi-test.sh -h` to see the options.
7
+It depends on the management API being accessible on localhost:19999 and on the responses to the api/v1/alarms?all requests being functional.
8
+It also requires read access to the management API key that is usually under `/var/lib/netdata/netdata.api.key` (`@varlibdir_POST@/netdata.api.key`).
9
10
[]()
11
tests/health_mgmtapi/health-cmdapi-test.sh.in
+32
-106
@@ -1,50 +1,52 @@
1
#!/usr/bin/env bash
2
# shellcheck disable=SC1117,SC2034,SC2059,SC2086,SC2181
3
4
-NETDATA_USER_CONFIG_DIR="@configdir_POST@"
5
-NETDATA_STOCK_CONFIG_DIR="@libconfigdir_POST@"
4
NETDATA_VARLIB_DIR="@varlibdir_POST@"
5
8
-printhelp () {
9
- echo "Usage: health-cmdapi-test.sh [OPTIONS]
10
- -s SETUP config files for python example tests
11
- -c CLEANUP config files from python example tests
12
- -r RESTART netdata after SETUP and CLEANUP, using systemctl restart netdata.
13
- -t TEST scenarios execution
14
- -u <URL> changes the host:port from localhost:19999 to <URL>
15
- "
16
-}
17
-
6
check () {
19
- echo -e "${GRAY}Check: '${1}' in 2 sec"
20
- sleep 2
21
- resp=$(curl -s "http://$URL/api/v1/alarms?all")
7
+ sec=1
8
+ echo -e " ${GRAY}Check: '${1}' in $sec sec"
9
+ sleep $sec
10
+ number=$RANDOM
11
+ resp=$(curl -s "http://$URL/api/v1/alarms?all&$number")
12
r=$(echo "${resp}" | \
13
python3 -c "import sys, json; d=json.load(sys.stdin); \
14
print(\
25
- d['alarms']['example.random.example_alarm1']['disabled'], \
26
- d['alarms']['example.random.example_alarm1']['silenced'] , \
27
- d['alarms']['example.random.example_alarm2']['disabled'], \
28
- d['alarms']['example.random.example_alarm2']['silenced'], \
15
+ d['alarms']['system.cpu.10min_cpu_usage']['disabled'], \
16
+ d['alarms']['system.cpu.10min_cpu_usage']['silenced'] , \
17
+ d['alarms']['system.cpu.10min_cpu_iowait']['disabled'], \
18
+ d['alarms']['system.cpu.10min_cpu_iowait']['silenced'], \
19
d['alarms']['system.load.load_trigger']['disabled'], \
20
d['alarms']['system.load.load_trigger']['silenced'], \
21
);" 2>&1)
22
if [ $? -ne 0 ] ; then
33
- echo -e "${RED}ERROR: Unexpected response '$resp'"
23
+ echo -e " ${RED}ERROR: Unexpected response stored in /tmp/resp-$number.json"
24
+ echo "$resp" > /tmp/resp-$number.json
25
err=$((err+1))
26
+ iter=0
27
elif [ "${r}" != "${2}" ] ; then
36
- echo -e "${RED}ERROR: 'Got ${r}'. Expected '${2}'"
37
- err=$((err+1))
28
+ echo -e " ${GRAY}WARNING: 'Got ${r}'. Expected '${2}'"
29
+ iter=$((iter+1))
30
+ if [ $iter -lt 10 ] ; then
31
+ echo -e " ${GRAY}Repeating test "
32
+ check "$1" "$2"
33
+ else
34
+ echo -e " ${RED}ERROR: 'Got ${r}'. Expected '${2}'"
35
+ iter=0
36
+ err=$((err+1))
37
+ fi
38
else
39
- echo -e "${GREEN}Success"
39
+ echo -e " ${GREEN}Success"
40
+ iter=0
41
fi
42
}
43
44
cmd () {
44
- echo -e "${WHITE}Cmd '${1}', expecting '${2}'"
45
+ echo -e "${WHITE}Cmd '${1}'"
46
+ echo -en " ${GRAY}Expecting '${2}' : "
47
RESPONSE=$(curl -s "http://$URL/api/v1/manage/health?${1}" -H "X-Auth-Token: $TOKEN" 2>&1)
48
if [ "${RESPONSE}" != "${2}" ] ; then
47
- echo -e "${RED}ERROR: Response '${RESPONSE}' != '${2}'"
49
+ echo -e "${RED}ERROR: Response '${RESPONSE}'"
50
err=$((err+1))
51
else
52
echo -e "${GREEN}Success"
@@ -62,63 +64,8 @@ CLEANUP=0
64
TEST=0
65
URL="localhost:19999"
66
65
-while getopts :srctu: option
66
-do
67
- case "$option" in
68
- s)
69
- SETUP=1
70
- ;;
71
- r)
72
- RESTART=1
73
- ;;
74
- c)
75
- CLEANUP=1
76
- ;;
77
- t)
78
- TEST=1
79
- ;;
80
- u)
81
- URL=$OPTARG
82
- ;;
83
- *)
84
- printhelp
85
- exit 1
86
- ;;
87
- esac
88
-done
89
-
90
-if [ $SETUP -eq 1 ] ; then
91
- echo "Preparing netdata configuration for testing"
92
- # Prep netdata for tests
93
- if [ -f "${NETDATA_USER_CONFIG_DIR}/python.d.conf" ] ; then
94
- cp -f "${NETDATA_USER_CONFIG_DIR}/python.d.conf" /tmp/python.d.conf
95
- else
96
- cp "${NETDATA_STOCK_CONFIG_DIR}/python.d.conf" "${NETDATA_USER_CONFIG_DIR}/"
97
- fi
98
- sed -i -e "s/example: no/example: yes/g" "${NETDATA_USER_CONFIG_DIR}/python.d.conf"
99
-
100
- mypath=$(cd ${0%/*} && echo $PWD)
101
-
102
- cp -f "${mypath}/python-example.conf" "${NETDATA_USER_CONFIG_DIR}/health.d/"
103
-
104
- # netdata.conf
105
- if [ -f "${NETDATA_USER_CONFIG_DIR}/netdata.conf" ] ; then
106
- cp -f "${NETDATA_USER_CONFIG_DIR}/netdata.conf" /tmp/netdata.conf
107
- fi
108
- printf "[health]\nrun at least every seconds = 1\n" > "${NETDATA_USER_CONFIG_DIR}/netdata.conf"
109
-
110
- chmod +r "${NETDATA_USER_CONFIG_DIR}/python.d.conf" "${NETDATA_USER_CONFIG_DIR}/netdata.conf" "${NETDATA_USER_CONFIG_DIR}/health.d/python-example.conf" "${NETDATA_STOCK_CONFIG_DIR}/health.d/load.conf"
111
- # Restart netdata
112
- if [ $RESTART -eq 1 ] ; then
113
- echo "Restarting netdata"
114
- systemctl restart netdata
115
- fi
116
-fi
117
-
67
err=0
68
120
-# Execute tests
121
-if [ $TEST -eq 1 ] ; then
69
70
HEALTH_CMDAPI_MSG_AUTHERROR="Auth Error"
71
HEALTH_CMDAPI_MSG_SILENCEALL="All alarm notifications are silenced"
@@ -170,7 +117,7 @@ if [ $TEST -eq 1 ] ; then
117
118
# Add silencer by name
119
printf -v resp "$HEALTH_CMDAPI_MSG_SILENCE\n$HEALTH_CMDAPI_MSG_ADDED"
173
- cmd "cmd=SILENCE&alarm=*example_alarm1 *load_trigger" "${resp}"
120
+ cmd "cmd=SILENCE&alarm=*10min_cpu_usage *load_trigger" "${resp}"
121
check "Silence notifications for alarm1 and load_trigger" "False True False False False True"
122
123
# Convert to disable health checks
@@ -182,7 +129,7 @@ if [ $TEST -eq 1 ] ; then
129
check "Silence notifications for alarm1 and load_trigger" "False True False False False True"
130
131
# Add second silencer by name
185
- cmd "alarm=*example_alarm2" "$HEALTH_CMDAPI_MSG_ADDED"
132
+ cmd "alarm=*10min_cpu_iowait" "$HEALTH_CMDAPI_MSG_ADDED"
133
check "Silence notifications for alarm1,alarm2 and load_trigger" "False True False True False True"
134
135
# Reset
@@ -194,7 +141,7 @@ if [ $TEST -eq 1 ] ; then
141
check "Default State" "False False False False True False"
142
143
# Add silencer by context
197
- cmd "context=random" "$HEALTH_CMDAPI_MSG_ADDED"
144
+ cmd "context=system.cpu" "$HEALTH_CMDAPI_MSG_ADDED"
145
check "Default State" "True False True False True False"
146
147
# Reset
@@ -202,11 +149,11 @@ if [ $TEST -eq 1 ] ; then
149
150
# Add second condition to a selector (AND)
151
printf -v resp "$HEALTH_CMDAPI_MSG_SILENCE\n$HEALTH_CMDAPI_MSG_ADDED"
205
- cmd "cmd=SILENCE&alarm=*example_alarm1 *load_trigger&chart=system.load" "${resp}"
152
+ cmd "cmd=SILENCE&alarm=*10min_cpu_usage *load_trigger&chart=system.load" "${resp}"
153
check "Silence notifications load_trigger" "False False False False False True"
154
155
# Add second selector with two conditions
209
- cmd "alarm=*example_alarm1 *load_trigger&context=random" "$HEALTH_CMDAPI_MSG_ADDED"
156
+ cmd "alarm=*10min_cpu_usage *load_trigger&context=system.cpu" "$HEALTH_CMDAPI_MSG_ADDED"
157
check "Silence notifications load_trigger" "False True False False False True"
158
159
# Reset
@@ -236,27 +183,6 @@ if [ $TEST -eq 1 ] ; then
183
# Reset
184
cmd "cmd=RESET" "$HEALTH_CMDAPI_MSG_RESET"
185
239
-fi
240
-
241
-# Cleanup
242
-if [ $CLEANUP -eq 1 ] ; then
243
- echo -e "${WHITE}Restoring netdata configuration"
244
- for f in "python.d.conf" "netdata.conf" ; do
245
- if [ -f "/tmp/$f" ] ; then
246
- mv -f "/tmp/$f" "${NETDATA_USER_CONFIG_DIR}/"
247
- else
248
- rm -f "${NETDATA_USER_CONFIG_DIR}/$f"
249
- fi
250
- done
251
-
252
- rm -f "${NETDATA_USER_CONFIG_DIR}/health.d/python-example.conf"
253
-
254
- # Restart netdata
255
- if [ $RESTART -eq 1 ] ; then
256
- echo "Restarting netdata"
257
- systemctl restart netdata
258
- fi
259
-fi
186
187
if [ $err -gt 0 ] ; then
188
echo "$err error(s) found"
tests/health_mgmtapi/python-example.conf
deleted
-16
@@ -1,16 +0,0 @@
1
-alarm: example_alarm1
2
- on: example.random
3
- every: 2s
4
- warn: $random1 > (($status >= $WARNING) ? (55) : (75))
5
- crit: $random1 > (($status == $CRITICAL) ? (75) : (95))
6
- info: random
7
- to: sysadmin
8
-
9
-alarm: example_alarm2
10
- on: example.random
11
- every: 2s
12
- warn: $random2 > (($status >= $WARNING) ? (55) : (75))
13
- crit: $random2 > (($status == $CRITICAL) ? (75) : (95))
14
- info: random
15
- to: sysadmin
16
-