From 82b268798b354c35a4b41a2027f4106771611f73 Mon Sep 17 00:00:00 2001 From: Adrian Reber Date: Wed, 6 May 2020 08:04:45 +0000 Subject: [PATCH] test: test external namespace support Adapt netns_ext tests to also work with pid namespaces and move it from test/others/netns_ext/ to test/others/ns_ext/. Also enable ns_ext tests in Travis runs. Signed-off-by: Adrian Reber --- scripts/travis/travis-tests | 3 + test/others/netns_ext/Makefile | 2 - test/others/netns_ext/run.sh | 40 ---------- test/others/ns_ext/Makefile | 7 ++ test/others/{netns_ext => ns_ext}/_run.sh | 1 + test/others/ns_ext/run.sh | 94 +++++++++++++++++++++++ 6 files changed, 105 insertions(+), 42 deletions(-) delete mode 100644 test/others/netns_ext/Makefile delete mode 100755 test/others/netns_ext/run.sh create mode 100644 test/others/ns_ext/Makefile rename test/others/{netns_ext => ns_ext}/_run.sh (63%) create mode 100755 test/others/ns_ext/run.sh diff --git a/scripts/travis/travis-tests b/scripts/travis/travis-tests index 4cb842c973..5ae5072d06 100755 --- a/scripts/travis/travis-tests +++ b/scripts/travis/travis-tests @@ -196,4 +196,7 @@ ip net add test # libcriu testing make -C test/others/libcriu run +# external namespace testing +make -C test/others/ns_ext run + make -C test/others/shell-job diff --git a/test/others/netns_ext/Makefile b/test/others/netns_ext/Makefile deleted file mode 100644 index 7779a9903c..0000000000 --- a/test/others/netns_ext/Makefile +++ /dev/null @@ -1,2 +0,0 @@ -run: - ./run.sh diff --git a/test/others/netns_ext/run.sh b/test/others/netns_ext/run.sh deleted file mode 100755 index 6de6c5ad82..0000000000 --- a/test/others/netns_ext/run.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/bin/bash - -set -e - -CRIU=../../../criu/criu -setsid unshare -n bash -c 'unshare -n sh _run.sh pidfile2 & unshare -n sh _run.sh pidfile3 & ip link add xxx type veth && ip link add mymacvlan1 link xxx type macvlan mode bridge && . _run.sh pidfile' < /dev/zero &> output & -sleep 1 -while :; do - test -f pidfile && test -f pidfile2 && break; - sleep 0.1 -done - -pid=$(cat pidfile) -pid2=$(cat pidfile2) - -touch test_netns -mount --bind /proc/$pid/ns/net test_netns -touch test_netns2 -mount --bind /proc/$pid2/ns/net test_netns2 -mkdir -p images -ino=$(ls -iL test_netns | awk '{ print $1 }') -ino2=$(ls -iL test_netns2 | awk '{ print $1 }') -exec 33< test_netns -exec 34< test_netns2 -$CRIU dump -t $pid -o dump.log -D images --external net[$ino]:test_netns --external net[$ino2]:test_netns2 -cat images/dump.log | grep -B 5 Error || echo ok -$CRIU restore -o restore.log -D images --inherit-fd fd[33]:test_netns --inherit-fd fd[34]:test_netns2 -d -cat images/restore.log | grep -B 5 Error || echo ok -new_ino=$(ls -iL /proc/$pid/ns/net | awk '{ print $1 }') -new_ino2=$(ls -iL /proc/$pid2/ns/net | awk '{ print $1 }') -[ "$ino" -ne "$new_ino" ] && { - echo FAIL - exit 1 -} -[ "$ino2" -ne "$new_ino2" ] && { - echo FAIL - exit 1 -} -echo PASS -exit 0 diff --git a/test/others/ns_ext/Makefile b/test/others/ns_ext/Makefile new file mode 100644 index 0000000000..f859b89bca --- /dev/null +++ b/test/others/ns_ext/Makefile @@ -0,0 +1,7 @@ +run: + ./run.sh net + ./run.sh pid + +clean: + rm -rf images output pidfile pidfile2 pidfile3 +.PHONY: clean run diff --git a/test/others/netns_ext/_run.sh b/test/others/ns_ext/_run.sh similarity index 63% rename from test/others/netns_ext/_run.sh rename to test/others/ns_ext/_run.sh index 59070fb1ba..8ac1022b4b 100644 --- a/test/others/netns_ext/_run.sh +++ b/test/others/ns_ext/_run.sh @@ -1,3 +1,4 @@ +ip link set up dev lo echo $$ > $1 while :; do sleep 1 diff --git a/test/others/ns_ext/run.sh b/test/others/ns_ext/run.sh new file mode 100755 index 0000000000..796c3642d7 --- /dev/null +++ b/test/others/ns_ext/run.sh @@ -0,0 +1,94 @@ +#!/bin/bash + +set -x + +if [[ "$1" == "pid" ]]; then + NS=pid +else + NS=net +fi + +MNT1=test_ns1 +MNT2=test_ns2 + +trap "cleanup" QUIT TERM INT HUP EXIT + +function cleanup() +{ + kill -9 $pid $pid2 + sleep 0.5 + umount -lf $MNT1 $MNT2 || : + rm -f $MNT1 $MNT2 + rm -f pidfile pidfile2 pidfile3 +} + +CRIU=../../../criu/criu +if [[ "$NS" == "net" ]]; then + setsid unshare -n bash -c 'unshare -n sh _run.sh pidfile2 & unshare -n sh _run.sh pidfile3 & ip link add xxx type veth && ip link add mymacvlan1 link xxx type macvlan mode bridge && . _run.sh pidfile' < /dev/zero &> output & +elif [[ "$NS" == "pid" ]]; then + # Adding some random values to the command-line to easily grep the correct process later + RND1=$RANDOM + RND2=$RANDOM + unshare -p -f bash -c "setsid sh _run.sh pidfile2 $RND2 & . _run.sh pidfile $RND1" < /dev/zero &> output & +fi +sleep 1 +while :; do + test -f pidfile && test -f pidfile2 && break; + sleep 0.1 +done + +# Figure out the PIDs of the relevant processes +if [[ "$NS" == "net" ]]; then + pid=$(cat pidfile) + pid2=$(cat pidfile2) +elif [[ "$NS" == "pid" ]]; then + # Unfortunately we cannot read out 'pidfile' as it contains the PID + # from within the PID namespace. We need to know the outside PID. + pid2=$(pgrep -f ". _run.sh pidfile $RND1" -n) + pid=$(pgrep -f "sh _run.sh pidfile2 $RND2" -n) +fi + +touch $MNT1 +mount --bind /proc/$pid/ns/$NS $MNT1 +touch $MNT2 +mount --bind /proc/$pid2/ns/$NS $MNT2 +mkdir -p images +ino=$(ls -iL $MNT1 | awk '{ print $1 }') +ino2=$(ls -iL $MNT2 | awk '{ print $1 }') +exec 33< $MNT1 +exec 34< $MNT2 +$CRIU dump -v4 -t $pid -o dump.log -D images --external $NS[$ino]:test_ns --external $NS[$ino2]:test_ns2 +RESULT=$? +cat images/dump.log | grep -B 5 Error || echo ok +[ "$RESULT" != "0" ] && { + echo "CRIU dump failed" + echo FAIL + exit 1 +} + +$CRIU restore -v4 -o restore.log -D images --inherit-fd fd[33]:test_ns --inherit-fd fd[34]:test_ns2 -d +RESULT=$? +cat images/restore.log | grep -B 5 Error || echo ok +[ "$RESULT" != "0" ] && { + echo "CRIU restore failed" + echo FAIL + exit 1 +} + +if [[ "$NS" == "pid" ]]; then + pid=$(pgrep -f "^sh _run.sh pidfile2 $RND2") +fi +new_ino=$(ls -iL /proc/$pid/ns/$NS | awk '{ print $1 }') +new_ino2=$(ls -iL /proc/$pid2/ns/$NS | awk '{ print $1 }') +[ "$ino" != "$new_ino" ] && { + echo "Inode of new NS is different" + echo FAIL + exit 1 +} +[ "$ino2" != "$new_ino2" ] && { + echo "Inode of new NS is different" + echo FAIL + exit 1 +} +echo PASS +exit 0