]> 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 05:37:48 +0000 (07:37 +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.

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 0ce3046f9f72adf7f03d0707ac1bd4b384944b1d..7086d67212e03bd905d4320fbf17a0bbc8d7f655 100644 (file)
@@ -747,7 +747,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
@@ -758,9 +758,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
@@ -780,9 +780,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