Use jq in system tests inspecting JSON data

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 b494e02761)
This commit is contained in:
Michał Kępień 2025-10-25 07:37:48 +02:00
parent 0ca76b6716
commit 1f0ed3c479
No known key found for this signature in database
2 changed files with 12 additions and 12 deletions

View 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

View 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