selftests: net: push jq workaround into separate helper
authorJiri Pirko <jiri@mellanox.com>
Thu, 15 Aug 2019 13:42:28 +0000 (15:42 +0200)
committerDavid S. Miller <davem@davemloft.net>
Thu, 15 Aug 2019 19:01:21 +0000 (12:01 -0700)
Push the jq return value workaround code into a separate helper so it
could be used by the rest of the code.

Signed-off-by: Jiri Pirko <jiri@mellanox.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
tools/testing/selftests/net/forwarding/lib.sh
tools/testing/selftests/net/forwarding/tc_common.sh

index 9385dc97126993642bf7a24b183d397bf686b2f6..85c587a03c8a5e48dba0cb3b1956c26bbccac7ea 100644 (file)
@@ -250,6 +250,25 @@ setup_wait()
        sleep $WAIT_TIME
 }
 
+cmd_jq()
+{
+       local cmd=$1
+       local jq_exp=$2
+       local ret
+       local output
+
+       output="$($cmd)"
+       # it the command fails, return error right away
+       ret=$?
+       if [[ $ret -ne 0 ]]; then
+               return $ret
+       fi
+       output=$(echo $output | jq -r "$jq_exp")
+       echo $output
+       # return success only in case of non-empty output
+       [ ! -z "$output" ]
+}
+
 lldpad_app_wait_set()
 {
        local dev=$1; shift
index 9d3b64a2a26427280ecb46b2c64a08f9752a06b2..315e934358d4ab248b8a25a54fdc484a998fb89c 100644 (file)
@@ -8,18 +8,9 @@ tc_check_packets()
        local id=$1
        local handle=$2
        local count=$3
-       local ret
 
-       output="$(tc -j -s filter show $id)"
-       # workaround the jq bug which causes jq to return 0 in case input is ""
-       ret=$?
-       if [[ $ret -ne 0 ]]; then
-               return $ret
-       fi
-       echo $output | \
-               jq -e ".[] \
-               | select(.options.handle == $handle) \
-               | select(.options.actions[0].stats.packets == $count)" \
-               &> /dev/null
-       return $?
+       cmd_jq "tc -j -s filter show $id" \
+              ".[] | select(.options.handle == $handle) | \
+                     select(.options.actions[0].stats.packets == $count)" \
+              &> /dev/null
 }