]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Use jq in system tests inspecting JSON data
authorMichał Kępień <michal@isc.org>
Sat, 25 Oct 2025 05:37:48 +0000 (07:37 +0200)
committerMichał Kępień <michal@isc.org>
Sat, 25 Oct 2025 06:01:46 +0000 (08:01 +0200)
Inspecting JSON data using grep is error-prone, overly lax in some ways,
overly strict in others, and neither accurate nor expressive.  Use jq
for inspecting JSON data in the "statschannel" and "synthfromdnssec"
system tests to address these deficiencies.

(cherry picked from commit b494e02761e9996a6619ed31a17ff0b6f32647eb)

bin/tests/system/statschannel/tests.sh
bin/tests/system/synthfromdnssec/tests.sh

index 314eb0ab19931a794ed5ceb46811365d1db69e12..c535e85de21715130520aca53dd8fca5032c2ba7 100644 (file)
@@ -734,19 +734,19 @@ _wait_for_transfers() {
     if [ $count != 1 ]; then return 1; fi
   fi
 
-  if [ "$PERL_JSON" ]; then
+  if [ "$PERL_JSON" ] && [ -x "$JQ" ]; then
     getxfrins json j$n || return 1
 
     # We expect 4 transfers
-    count=$(grep -c -E '"state":"(Zone Transfer Request|First Data|Receiving AXFR Data)"' xfrins.json.j$n)
+    count=$("$JQ" '.views._default.xfrins | length' <xfrins.json.j$n)
     if [ $count != 4 ]; then return 1; fi
 
     # We expect 3 of 4 to be retransfers
-    count=$(grep -c -F '"firstrefresh":"No"' xfrins.json.j$n)
+    count=$("$JQ" '.views._default.xfrins | map(select(.firstrefresh == "No")) | length' <xfrins.json.j$n)
     if [ $count != 3 ]; then return 1; fi
 
     # We expect 1 of 4 to be a new transfer
-    count=$(grep -c -F '"firstrefresh":"Yes"' xfrins.json.j$n)
+    count=$("$JQ" '.views._default.xfrins | map(select(.firstrefresh == "Yes")) | length' <xfrins.json.j$n)
     if [ $count != 1 ]; then return 1; fi
   fi
 }
@@ -766,7 +766,7 @@ if [ $ret != 0 ]; then echo_i "failed"; fi
 status=$((status + ret))
 n=$((n + 1))
 
-if [ "$PERL_JSON" ]; then
+if [ "$PERL_JSON" ] && [ -x "$JQ" ]; then
   echo_i "Checking zone transfer transports ($n)"
   ret=0
   cp xfrins.json.j$((n - 2)) xfrins.json.j$n
index e599b0d2a71b87a0ccc8ad32f3e31add37710e60..7d011ce7e32470ba305df80e7d29d4f9952f127d 100644 (file)
@@ -776,7 +776,7 @@ for ns in 2 4 5 6; do
     echo_i "Skipping XML statistics checks"
   fi
 
-  if $FEATURETEST --have-json-c && [ -x "${CURL}" ]; then
+  if $FEATURETEST --have-json-c && [ -x "${CURL}" ] && [ -x "${JQ}" ]; then
     echo_i "getting JSON statisistcs for (synth-from-dnssec ${description};) ($n)"
     ret=0
     json=json.out$n
@@ -787,9 +787,9 @@ for ns in 2 4 5 6; do
 
     echo_i "check JSON for 'CoveringNSEC' with (synth-from-dnssec ${description};) ($n)"
     ret=0
-    count=$(grep '"CoveringNSEC":' $json | wc -l)
+    count=$("${JQ}" '.views | map(select(.resolver.cachestats | has("CoveringNSEC"))) | length' <$json)
     test $count = 2 || ret=1
-    zero=$(grep '"CoveringNSEC":0' $json | wc -l)
+    zero=$("${JQ}" '.views | map(select(.resolver.cachestats.CoveringNSEC == 0)) | length' <$json)
     if [ ${synth} = yes ]; then
       test $zero = 1 || ret=1
     else
@@ -801,9 +801,9 @@ for ns in 2 4 5 6; do
 
     echo_i "check JSON for 'CacheNSECNodes' with (synth-from-dnssec ${description};) ($n)"
     ret=0
-    count=$(grep '"CacheNSECNodes":' $json | wc -l)
+    count=$("${JQ}" '.views | map(select(.resolver.cachestats | has("CacheNSECNodes"))) | length' <$json)
     test $count = 2 || ret=1
-    zero=$(grep '"CacheNSECNodes":0' $json | wc -l)
+    zero=$("${JQ}" '.views | map(select(.resolver.cachestats.CacheNSECNodes == 0)) | length' <$json)
     if [ ${ad} = yes ]; then
       test $zero = 1 || ret=1
     else
@@ -823,9 +823,9 @@ for ns in 2 4 5 6; do
       echo_i "check JSON for '$synthesized}' with (synth-from-dnssec ${description};) ($n)"
       ret=0
       if [ ${synth} = yes ]; then
-        grep '"'$synthesized'":'$count'' $json >/dev/null || ret=1
+        test $("${JQ}" ".nsstats.${synthesized}" <$json) -eq $count || ret=1
       else
-        grep '"'$synthesized'":' $json >/dev/null && ret=1
+        "${JQ}" -e '.nsstats | has("'"${synthesized}"'")' <$json >/dev/null && ret=1
       fi
       n=$((n + 1))
       if [ $ret != 0 ]; then echo_i "failed"; fi