Skip to content

Commit a87964c

Browse files
committed
tests/common.sh: fix nsenter flags
`nsenter` should not be called when `unshare` was called without `-r`. Close rootless-containers#235 Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
1 parent 77bd1c7 commit a87964c

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

tests/common.sh

+27-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,33 @@
11
#!/bin/bash
22

3+
function nsenter_flags {
4+
pid=$1
5+
flags="--target=${pid}"
6+
userns="$(readlink /proc/${pid}/ns/user)"
7+
mntns="$(readlink /proc/${pid}/ns/mnt)"
8+
netns="$(readlink /proc/${pid}/ns/net)"
9+
10+
self_userns="$(readlink /proc/self/ns/user)"
11+
self_mntns="$(readlink /proc/self/ns/mnt)"
12+
self_netns="$(readlink /proc/self/ns/net)"
13+
14+
if [ "${userns}" != "${self_userns}" ]; then
15+
flags="$flags --preserve-credentials -U"
16+
fi
17+
if [ "${mntns}" != "${self_mntns}" ]; then
18+
flags="$flags -m"
19+
fi
20+
if [ "${netns}" != "${self_netns}" ]; then
21+
flags="$flags -n"
22+
fi
23+
echo "${flags}"
24+
}
25+
326
function wait_for_network_namespace {
427
# Wait that the namespace is ready.
528
COUNTER=0
629
while [ $COUNTER -lt 40 ]; do
7-
if nsenter --preserve-credentials -U -n --target=$1 true; then
30+
if nsenter $(nsenter_flags $1) true; then
831
break
932
else
1033
sleep 0.5
@@ -17,7 +40,7 @@ function wait_for_network_device {
1740
# Wait that the device appears.
1841
COUNTER=0
1942
while [ $COUNTER -lt 40 ]; do
20-
if nsenter --preserve-credentials -U -n --target=$1 ip addr show $2; then
43+
if nsenter $(nsenter_flags $1) ip addr show $2; then
2144
break
2245
else
2346
sleep 0.5
@@ -41,7 +64,7 @@ function wait_process_exits {
4164
function wait_for_ping_connectivity {
4265
COUNTER=0
4366
while [ $COUNTER -lt 40 ]; do
44-
if nsenter --preserve-credentials -U -n --target=$1 ping -c 1 -w 1 $2; then
67+
if nsenter $(nsenter_flags $1) ping -c 1 -w 1 $2; then
4568
break
4669
else
4770
sleep 0.5
@@ -53,7 +76,7 @@ function wait_for_ping_connectivity {
5376
function wait_for_connectivity {
5477
COUNTER=0
5578
while [ $COUNTER -lt 40 ]; do
56-
if echo "wait_for_connectivity" | nsenter --preserve-credentials -U -n --target=$1 ncat -v $2 $3; then
79+
if echo "wait_for_connectivity" | nsenter $(nsenter_flags $1) ncat -v $2 $3; then
5780
break
5881
else
5982
sleep 0.5

0 commit comments

Comments
 (0)