Skip to content

Commit

Permalink
selftests: forwarding: lib: Add in_ns()
Browse files Browse the repository at this point in the history
In order to run a certain command inside another network namespace, it's
possible to use "ip netns exec ns command". However then one can't use
functions defined in lib.sh or a test suite.

One option is to do "ip netns exec ns bash -c command", provided that
all functions that one wishes to use (and their dependencies) are
published using "export -f". That may not be practical.

Therefore, introduce a helper in_ns(), which wraps a given command in a
boilerplate of "ip netns exec" and "source lib.sh", thus making all
library functions available. (Custom functions that a script wishes to
run within a namespace still need to be exported.)

Because quotes in "$@" aren't recognized in heredoc, hand-expand the
array in an explicit for loop, leveraging printf %q to handle proper
quoting.

Signed-off-by: Petr Machata <petrm@mellanox.com>
Signed-off-by: Ido Schimmel <idosch@mellanox.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
pmachata authored and davem330 committed Nov 20, 2018
1 parent 601bc1c commit d0540d1
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tools/testing/selftests/net/forwarding/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,17 @@ multipath_eval()
log_info "Expected ratio $weights_ratio Measured ratio $packets_ratio"
}

in_ns()
{
local name=$1; shift

ip netns exec $name bash <<-EOF
NUM_NETIFS=0
source lib.sh
$(for a in "$@"; do printf "%q${IFS:0:1}" "$a"; done)
EOF
}

##############################################################################
# Tests

Expand Down

0 comments on commit d0540d1

Please sign in to comment.