1 # #-- stat_values.test --#
2 # source the master var file when it's there
3 [ -f ../.tpkg.var.master ] && source ../.tpkg.var.master
4 # use .tpkg.var.test for in test variable passing
5 [ -f .tpkg.var.test ] && source .tpkg.var.test
6 # We need kill_pid for the serve-expired-client-timeout test
11 # Individual thread stats.
12 STATS_IGNORE_THREAD="\
16 STATS_IGNORE_HISTOGRAM="\
19 # Time dependent stats.
20 STATS_IGNORE_TIME_SPECIFIC="\
21 ^total.recursion.time.avg=
22 ^total.recursion.time.median=
27 # Usage dependent stats.
28 STATS_IGNORE_USAGE_SPECIFIC="\
29 ^total.requestlist.avg=
30 ^total.requestlist.max=
31 ^total.requestlist.overwritten=
32 ^total.requestlist.exceeded=
33 ^total.requestlist.current.all=
34 ^total.requestlist.current.user=
38 # Stats to ignore by default.
39 STATS_IGNORE_DEFAULT="\
41 $STATS_IGNORE_HISTOGRAM
42 $STATS_IGNORE_TIME_SPECIFIC
43 $STATS_IGNORE_USAGE_SPECIFIC"
45 # Various files to be used while testing.
47 EXPECTED_STATS_FILE=expected_stats.$$
48 IGNORE_REGEX_FILE=ignore_regex.$$
49 FILTERED_STATS_FILE=filtered_stats.$$
50 FOUND_STATS_FILE=found_stats.$$
51 REST_STATS_FILE=rest_stats.$$
55 # Write stats to $STATS_FILE.
56 # Call this when you want to get stats from unbound.
58 echo "> Getting stats"
59 echo "$PRE/unbound-control -c ub.conf stats"
60 $PRE/unbound-control -c ub.conf stats > $STATS_FILE
61 if test $? -ne 0; then
62 echo "wrong exit value after success"
67 # Set the expected stat values by writing to $EXPECTED_STATS_FILE.
68 # sort is used for proper diff later.
69 set_expected_stats () {
70 echo "$1" | sort > $EXPECTED_STATS_FILE
73 # Set the regex to ignore stats by writing to $IGNORE_REGEX_FILE.
74 set_ignore_regex_stats () {
75 echo "$1" > $IGNORE_REGEX_FILE
78 # Filter the stats by removing any matched regex from $IGNORE_REGEX_FILE,
79 # sorts and writes the left over stats to $FILTERED_STATS_FILE.
81 grep -v -f $IGNORE_REGEX_FILE $STATS_FILE | sort > $FILTERED_STATS_FILE
84 # Check that the stats in $FILTERED_STATS_FILE include the expected stats in
85 # $EXPECTED_STATS_FILE.
86 check_expected_stats () {
87 echo "> Checking expected stats"
88 grep -F -x -f $EXPECTED_STATS_FILE $FILTERED_STATS_FILE > $FOUND_STATS_FILE
89 if test $DEBUG -ne 0; then
93 if diff $EXPECTED_STATS_FILE $FOUND_STATS_FILE; then
96 echo "! bad expected stats:"
97 cat $FILTERED_STATS_FILE
102 # Check that the rest (unspecified) stats are all 0 (no surprises).
103 check_rest_stats () {
104 echo "> Checking rest stats"
105 grep -F -x -v -f $EXPECTED_STATS_FILE $FILTERED_STATS_FILE > $REST_STATS_FILE
106 if test $DEBUG -ne 0; then
110 if grep -v "=0$" $REST_STATS_FILE; then
111 echo "! bad rest stats"
118 # Main function to check stats by:
119 # - Getting stats from unbound
120 # - Filtering out the stats we are not interested in
121 # - Checking that the expected stats are part of the filtered stats
122 # - The rest of the stats have 0 values.
124 set_expected_stats "$1"
125 if test $DEBUG -ne 0; then
127 cat $EXPECTED_STATS_FILE
131 if test $DEBUG -ne 0; then
133 cat $FILTERED_STATS_FILE
139 # Convenient function to set an option through unbound-control.
143 echo "$PRE/unbound-control -c ub.conf set_option $name: $value"
144 $PRE/unbound-control -c ub.conf set_option $name: $value
145 if test $? -ne 0; then
146 echo "wrong exit value after success"
151 # Convenient function to exit the test.
153 echo "> cat logfiles"
156 if test $1 -eq 1; then
164 # Ignore all run specific stats.
165 set_ignore_regex_stats "$STATS_IGNORE_DEFAULT"
167 # Check if the server is up.
168 echo "> dig 1ttl.example.com."
169 dig @127.0.0.1 -p $UNBOUND_PORT 1ttl.example.com. | tee outfile
170 echo "> check answer"
171 if grep "1.1.1.1" outfile; then
178 echo "[ Check initial stats based on first query. ]"
181 total.num.cachemiss=1
182 total.num.recursivereplies=1
185 num.query.opcode.QUERY=1
188 num.query.edns.present=1
192 num.answer.rcode.NOERROR=1"
195 echo "[ Check stat reset. ]"
203 echo "[ Enable serve-expired and check. ]"
204 set_ub_option serve-expired yes
205 sleep 2 # make sure the TTL has expired.
206 echo "> dig 1ttl.example.com."
207 dig @127.0.0.1 -p $UNBOUND_PORT 1ttl.example.com. | tee outfile
208 echo "> check answer"
209 if grep "1.1.1.1" outfile; then
217 total.num.cachehits=1
219 num.answer.rcode.NOERROR=1
221 num.query.edns.present=1
224 num.query.opcode.QUERY=1
232 echo "[ Enable serve-expired-client-timeout and check. ]"
233 set_ub_option serve-expired-client-timeout 1
234 echo "> dig servfail.expired."
235 dig @127.0.0.1 -p $UNBOUND_PORT servfail.expired. | tee outfile
236 echo "> check answer"
237 if grep "192.0.2.1" outfile; then
244 total.num.cachemiss=1
245 total.num.recursivereplies=1
248 num.query.opcode.QUERY=1
251 num.query.edns.present=1
255 num.answer.rcode.NOERROR=1"
256 kill_pid $FWD_EXPIRED_PID # kill the expired forwarder to force a servfail from upstream.
257 sleep 2 # make sure the TTL has expired.
258 echo "> dig servfail.expired."
259 dig @127.0.0.1 -p $UNBOUND_PORT servfail.expired. | tee outfile
260 echo "> check answer"
261 if grep "192.0.2.1" outfile; then
269 total.num.recursivereplies=1
270 num.answer.rcode.NOERROR=1
272 num.query.edns.present=1
275 num.query.opcode.QUERY=1
277 total.num.cachemiss=1
283 # Disable serve-expired
284 set_ub_option serve-expired no
288 echo "[ Check REFUSED; try without RD flag. ]"
289 echo "> dig somethingelse.example.com."
290 dig @127.0.0.1 -p $UNBOUND_PORT +nordflag somethingelse.example.com. | tee outfile
291 echo "> check answer"
292 if grep "REFUSED" outfile; then
298 num.answer.rcode.REFUSED=1
299 total.num.cachehits=1
301 num.query.edns.present=1
303 num.query.opcode.QUERY=1
312 echo "[ Check the AD flag. ]"
313 echo "> dig www.example.com."
314 dig @127.0.0.1 -p $UNBOUND_PORT +noadflag www.example.com. | tee outfile
315 echo "> check answer"
316 if grep "10.20.30.40" outfile; then
323 total.num.cachemiss=1
324 num.answer.rcode.NOERROR=1
326 num.query.edns.present=1
328 num.query.opcode.QUERY=1
331 total.num.recursivereplies=1
337 echo "[ Check local zone. ]"
338 echo "> dig www.local.zone."
339 dig @127.0.0.1 -p $UNBOUND_PORT www.local.zone. | tee outfile
340 echo "> check answer"
341 if grep "192.0.2.1" outfile; then
347 num.answer.rcode.NOERROR=1
348 total.num.cachehits=1
350 num.query.edns.present=1
353 num.query.opcode.QUERY=1
362 echo "[ Check NXDOMAIN (with local data). ]"
363 echo "> dig mail.local.zone."
364 dig @127.0.0.1 -p $UNBOUND_PORT mail.local.zone. | tee outfile
365 echo "> check answer"
366 if grep "NXDOMAIN" outfile; then
372 num.answer.rcode.NXDOMAIN=1
373 total.num.cachehits=1
375 num.query.edns.present=1
378 num.query.opcode.QUERY=1
387 echo "[ Check CHAOS. ]"
388 echo "> dig id.server. ch txt"
389 dig @127.0.0.1 -p $UNBOUND_PORT id.server. ch txt | tee outfile
390 echo "> check answer"
391 if grep "stat_values" outfile; then
398 total.num.cachehits=1
399 num.answer.rcode.NOERROR=1
400 num.query.edns.present=1
403 num.query.opcode.QUERY=1