Add a couple of helpers which can be used by tests which need to run a
specific bash command on a different target than the local system, be it
either another netns or a remote system accessible through ssh.

The run_cmd() function decides where to execute the command passed
through $@ based on the env variable TARGET value while run_on() will
receive the target through its first argument.

Signed-off-by: Ioana Ciornei <[email protected]>
---
Changes in v2:
- patch is new

tools/testing/selftests/net/lib.sh | 37 ++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/tools/testing/selftests/net/lib.sh 
b/tools/testing/selftests/net/lib.sh
index b40694573f4c..51d232552e37 100644
--- a/tools/testing/selftests/net/lib.sh
+++ b/tools/testing/selftests/net/lib.sh
@@ -28,6 +28,10 @@ EXIT_STATUS=0
 # Per-test return value. Clear at the beginning of each test.
 RET=0
 
+# If a specific command needs to be executed on another target than local, set
+# this appropriately before calling run_cmd
+TARGET="local:"
+
 ##############################################################################
 # Helpers
 
@@ -670,3 +674,36 @@ cmd_jq()
        # return success only in case of non-empty output
        [ ! -z "$output" ]
 }
+
+run_cmd()
+{
+       IFS=':' read -r type args <<< "$TARGET"
+
+       case "$type" in
+               local)
+                       # Execute command locally
+                       "$@"
+                       ;;
+               netns)
+                       # Execute command in network namespace
+                       # args contains the namespace name
+                       ip netns exec "$args" "$@"
+                       ;;
+               ssh)
+                       # Execute command via SSH args contains user@host
+                       ssh -n "$args" "$@"
+                       ;;
+               *)
+                       echo "Error: Unknown type '$type'" >&2
+                       return 1
+                       ;;
+       esac
+}
+
+run_on()
+{
+       local iface=$1; shift
+
+       TARGET="${TARGETS[$iface]}"
+       run_cmd $@
+}
-- 
2.25.1


Reply via email to