Skip to content

Commit 9d0b4ad

Browse files
liuhangbindavem330
authored andcommitted
kselftest/runner.sh: add netns support
Add a variable RUN_IN_NETNS if the user wants to run all the selected tests in namespace in parallel. With this, we can save a lot of testing time. Note that some tests may not fit to run in namespace, e.g. net/drop_monitor_tests.sh, as the dwdump needs to be run in init ns. I also added another parameter -p to make all the logs reported separately instead of mixing them in the stdout or output.log. Nit: the NUM in run_one is not used, rename it to test_num. Acked-by: David Ahern <[email protected]> Signed-off-by: Hangbin Liu <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 378f082 commit 9d0b4ad

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

tools/testing/selftests/kselftest/runner.sh

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export skip_rc=4
66
export timeout_rc=124
77
export logfile=/dev/stdout
88
export per_test_logging=
9+
export RUN_IN_NETNS=
910

1011
# Defaults for "settings" file fields:
1112
# "timeout" how many seconds to let each test run before running
@@ -47,7 +48,7 @@ run_one()
4748
{
4849
DIR="$1"
4950
TEST="$2"
50-
NUM="$3"
51+
local test_num="$3"
5152

5253
BASENAME_TEST=$(basename $TEST)
5354

@@ -141,6 +142,33 @@ run_one()
141142
fi
142143
}
143144

145+
in_netns()
146+
{
147+
local name=$1
148+
ip netns exec $name bash <<-EOF
149+
BASE_DIR=$BASE_DIR
150+
source $BASE_DIR/kselftest/runner.sh
151+
logfile=$logfile
152+
run_one $DIR $TEST $test_num
153+
EOF
154+
}
155+
156+
run_in_netns()
157+
{
158+
local netns=$(mktemp -u ${BASENAME_TEST}-XXXXXX)
159+
local tmplog="/tmp/$(mktemp -u ${BASENAME_TEST}-XXXXXX)"
160+
ip netns add $netns
161+
if [ $? -ne 0 ]; then
162+
echo "# Warning: Create namespace failed for $BASENAME_TEST"
163+
echo "not ok $test_num selftests: $DIR: $BASENAME_TEST # Create NS failed"
164+
fi
165+
ip -n $netns link set lo up
166+
in_netns $netns &> $tmplog
167+
ip netns del $netns &> /dev/null
168+
cat $tmplog
169+
rm -f $tmplog
170+
}
171+
144172
run_many()
145173
{
146174
echo "TAP version 13"
@@ -155,6 +183,12 @@ run_many()
155183
logfile="/tmp/$BASENAME_TEST"
156184
cat /dev/null > "$logfile"
157185
fi
158-
run_one "$DIR" "$TEST" "$test_num"
186+
if [ -n "$RUN_IN_NETNS" ]; then
187+
run_in_netns &
188+
else
189+
run_one "$DIR" "$TEST" "$test_num"
190+
fi
159191
done
192+
193+
wait
160194
}

tools/testing/selftests/run_kselftest.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ usage()
2020
{
2121
cat <<EOF
2222
Usage: $0 [OPTIONS]
23-
-s | --summary Print summary with detailed log in output.log
23+
-s | --summary Print summary with detailed log in output.log (conflict with -p)
24+
-p | --per_test_log Print test log in /tmp with each test name (conflict with -s)
2425
-t | --test COLLECTION:TEST Run TEST from COLLECTION
2526
-c | --collection COLLECTION Run all tests from COLLECTION
2627
-l | --list List the available collection:test entries
2728
-d | --dry-run Don't actually run any tests
29+
-n | --netns Run each test in namespace
2830
-h | --help Show this usage info
2931
-o | --override-timeout Number of seconds after which we timeout
3032
EOF
@@ -41,6 +43,9 @@ while true; do
4143
logfile="$BASE_DIR"/output.log
4244
cat /dev/null > $logfile
4345
shift ;;
46+
-p | --per-test-log)
47+
per_test_logging=1
48+
shift ;;
4449
-t | --test)
4550
TESTS="$TESTS $2"
4651
shift 2 ;;
@@ -53,6 +58,9 @@ while true; do
5358
-d | --dry-run)
5459
dryrun="echo"
5560
shift ;;
61+
-n | --netns)
62+
RUN_IN_NETNS=1
63+
shift ;;
5664
-o | --override-timeout)
5765
kselftest_override_timeout="$2"
5866
shift 2 ;;

0 commit comments

Comments
 (0)