Vendor import of Unbound 1.12.0.
[freebsd.git] / testdata / stat_values.tdir / stat_values.test
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
7 . ../common.sh
8
9 PRE="../.."
10
11 # Individual thread stats.
12 STATS_IGNORE_THREAD="\
13 ^thread"
14
15 # Histogram stats.
16 STATS_IGNORE_HISTOGRAM="\
17 ^histogram"
18
19 # Time dependent stats.
20 STATS_IGNORE_TIME_SPECIFIC="\
21 ^total.recursion.time.avg=
22 ^total.recursion.time.median=
23 ^time.now=
24 ^time.up=
25 ^time.elapsed="
26
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=
35 ^total.tcpusage=
36 ^mem\."
37
38 # Stats to ignore by default.
39 STATS_IGNORE_DEFAULT="\
40 $STATS_IGNORE_THREAD
41 $STATS_IGNORE_HISTOGRAM
42 $STATS_IGNORE_TIME_SPECIFIC
43 $STATS_IGNORE_USAGE_SPECIFIC"
44
45 # Various files to be used while testing.
46 STATS_FILE=stats.$$
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.$$
52
53 DEBUG=0
54
55 # Write stats to $STATS_FILE.
56 # Call this when you want to get stats from unbound.
57 get_stats () {
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"
63                 exit 1
64         fi
65 }
66
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
71 }
72
73 # Set the regex to ignore stats by writing to $IGNORE_REGEX_FILE.
74 set_ignore_regex_stats () {
75         echo "$1" > $IGNORE_REGEX_FILE
76 }
77
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.
80 filter_stats () {
81         grep -v -f $IGNORE_REGEX_FILE $STATS_FILE | sort > $FILTERED_STATS_FILE
82 }
83
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
90                 echo "Found:"
91                 cat $FOUND_STATS_FILE
92         fi
93         if diff $EXPECTED_STATS_FILE $FOUND_STATS_FILE; then
94                 echo "OK"
95         else
96                 echo "! bad expected stats:"
97         cat $FILTERED_STATS_FILE
98                 exit 1
99         fi
100 }
101
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
107                 echo "Rest:"
108                 cat $REST_STATS_FILE
109         fi
110         if grep -v "=0$" $REST_STATS_FILE; then
111                 echo "! bad rest stats"
112                 exit 1
113         else
114                 echo "OK"
115         fi
116 }
117
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.
123 check_stats () {
124         set_expected_stats "$1"
125         if test $DEBUG -ne 0; then
126                 echo "Expected:"
127                 cat $EXPECTED_STATS_FILE
128         fi
129         get_stats
130         filter_stats
131         if test $DEBUG -ne 0; then
132                 echo "Filtered:"
133                 cat $FILTERED_STATS_FILE
134         fi
135         check_expected_stats
136         check_rest_stats
137 }
138
139 # Convenient function to set an option through unbound-control.
140 set_ub_option () {
141         name=$1
142         value=$2
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"
147                 exit 1
148         fi
149 }
150
151 # Convenient function to exit the test.
152 end () {
153         echo "> cat logfiles"
154         cat fwd.log
155         cat unbound.log
156         if test $1 -eq 1; then
157                 echo "Not OK"
158         else
159                 echo "> OK"
160         fi
161         exit $1
162 }
163
164 # Ignore all run specific stats.
165 set_ignore_regex_stats "$STATS_IGNORE_DEFAULT"
166
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
172         echo "OK"
173 else
174         end 1
175 fi
176
177 echo
178 echo "[ Check initial stats based on first query. ]"
179 check_stats "\
180 total.num.queries=1
181 total.num.cachemiss=1
182 total.num.recursivereplies=1
183 num.query.type.A=1
184 num.query.class.IN=1
185 num.query.opcode.QUERY=1
186 num.query.flags.RD=1
187 num.query.flags.AD=1
188 num.query.edns.present=1
189 msg.cache.count=1
190 rrset.cache.count=1
191 infra.cache.count=1
192 num.answer.rcode.NOERROR=1"
193
194 echo
195 echo "[ Check stat reset. ]"
196 check_stats "\
197 msg.cache.count=1
198 rrset.cache.count=1
199 infra.cache.count=1"
200
201
202 echo
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
210         echo "OK"
211 else
212         end 1
213 fi
214 check_stats "\
215 total.num.queries=1
216 total.num.expired=1
217 total.num.cachehits=1
218 total.num.prefetch=1
219 num.answer.rcode.NOERROR=1
220 num.query.class.IN=1
221 num.query.edns.present=1
222 num.query.flags.AD=1
223 num.query.flags.RD=1
224 num.query.opcode.QUERY=1
225 num.query.type.A=1
226 msg.cache.count=1
227 rrset.cache.count=1
228 infra.cache.count=1"
229
230
231 echo
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
238         echo "OK"
239 else
240         end 1
241 fi
242 check_stats "\
243 total.num.queries=1
244 total.num.cachemiss=1
245 total.num.recursivereplies=1
246 num.query.type.A=1
247 num.query.class.IN=1
248 num.query.opcode.QUERY=1
249 num.query.flags.RD=1
250 num.query.flags.AD=1
251 num.query.edns.present=1
252 msg.cache.count=2
253 rrset.cache.count=2
254 infra.cache.count=2
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
262         echo "OK"
263 else
264         end 1
265 fi
266 check_stats "\
267 total.num.queries=1
268 total.num.expired=1
269 total.num.recursivereplies=1
270 num.answer.rcode.NOERROR=1
271 num.query.class.IN=1
272 num.query.edns.present=1
273 num.query.flags.AD=1
274 num.query.flags.RD=1
275 num.query.opcode.QUERY=1
276 num.query.type.A=1
277 total.num.cachemiss=1
278 msg.cache.count=2
279 rrset.cache.count=2
280 infra.cache.count=2"
281
282
283 # Disable serve-expired
284 set_ub_option serve-expired no
285
286
287 echo
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
293         echo "OK"
294 else
295         end 1
296 fi
297 check_stats "\
298 num.answer.rcode.REFUSED=1
299 total.num.cachehits=1
300 num.query.class.IN=1
301 num.query.edns.present=1
302 num.query.flags.AD=1
303 num.query.opcode.QUERY=1
304 num.query.type.A=1
305 total.num.queries=1
306 msg.cache.count=2
307 rrset.cache.count=2
308 infra.cache.count=2"
309
310
311 echo
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
317         echo "OK"
318 else
319         end 1
320 fi
321 check_stats "\
322 num.query.flags.AD=0
323 total.num.cachemiss=1
324 num.answer.rcode.NOERROR=1
325 num.query.class.IN=1
326 num.query.edns.present=1
327 num.query.flags.RD=1
328 num.query.opcode.QUERY=1
329 num.query.type.A=1
330 total.num.queries=1
331 total.num.recursivereplies=1
332 msg.cache.count=3
333 rrset.cache.count=3
334 infra.cache.count=2"
335
336 echo
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
342         echo "OK"
343 else
344         end 1
345 fi
346 check_stats "\
347 num.answer.rcode.NOERROR=1
348 total.num.cachehits=1
349 num.query.class.IN=1
350 num.query.edns.present=1
351 num.query.flags.AD=1
352 num.query.flags.RD=1
353 num.query.opcode.QUERY=1
354 num.query.type.A=1
355 total.num.queries=1
356 msg.cache.count=3
357 rrset.cache.count=3
358 infra.cache.count=2"
359
360
361 echo
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
367         echo "OK"
368 else
369         end 1
370 fi
371 check_stats "\
372 num.answer.rcode.NXDOMAIN=1
373 total.num.cachehits=1
374 num.query.class.IN=1
375 num.query.edns.present=1
376 num.query.flags.AD=1
377 num.query.flags.RD=1
378 num.query.opcode.QUERY=1
379 num.query.type.A=1
380 total.num.queries=1
381 msg.cache.count=3
382 rrset.cache.count=3
383 infra.cache.count=2"
384
385
386 echo
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
392         echo "OK"
393 else
394         end 1
395 fi
396 check_stats "\
397 num.query.class.CH=1
398 total.num.cachehits=1
399 num.answer.rcode.NOERROR=1
400 num.query.edns.present=1
401 num.query.flags.AD=1
402 num.query.flags.RD=1
403 num.query.opcode.QUERY=1
404 num.query.type.TXT=1
405 total.num.queries=1
406 msg.cache.count=3
407 rrset.cache.count=3
408 infra.cache.count=2"
409
410
411 end 0