Skip to content

Commit 38245cc

Browse files
committed
fix(libcontainer): bats test for rootfs propagation
add bat integration test for rootfs propagation test, expect to see the mount propagation is slave, the test will create a isolate mntns to run the test as the test will mutate the rootfs propagation Signed-off-by: sean <xujihui1985@gmail.com>
1 parent ec170d8 commit 38245cc

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/env bats
2+
3+
load helpers
4+
5+
function require_mount_namespace_tools() {
6+
command -v unshare >/dev/null || skip "test requires unshare"
7+
command -v nsenter >/dev/null || skip "test requires nsenter"
8+
}
9+
10+
function in_mount_namespace() {
11+
local cwd
12+
cwd="$(pwd)"
13+
nsenter --mount="$ISOLATED_MNTNS" -- sh -c "cd \"\$1\" && shift && exec \"\$@\"" sh "$cwd" "$@"
14+
}
15+
16+
function setup_isolated_mount_namespace() {
17+
ISOLATED_MNTNS_DIR="$(mktemp -d "$BATS_RUN_TMPDIR/mntns.XXXXXX")"
18+
mount --bind "$ISOLATED_MNTNS_DIR" "$ISOLATED_MNTNS_DIR"
19+
mount --make-private "$ISOLATED_MNTNS_DIR"
20+
21+
ISOLATED_MNTNS="$ISOLATED_MNTNS_DIR/testns"
22+
touch "$ISOLATED_MNTNS"
23+
if ! unshare --mount="$ISOLATED_MNTNS" mount --make-rprivate /; then
24+
rm -f "$ISOLATED_MNTNS"
25+
umount "$ISOLATED_MNTNS_DIR" 2>/dev/null || true
26+
rmdir "$ISOLATED_MNTNS_DIR" 2>/dev/null || true
27+
fail "failed to bind isolated mount namespace"
28+
fi
29+
}
30+
31+
function teardown_isolated_mount_namespace() {
32+
if [ -n "${ISOLATED_MNTNS_DIR:-}" ]; then
33+
umount -l "$ISOLATED_MNTNS_DIR" 2>/dev/null || true
34+
rmdir "$ISOLATED_MNTNS_DIR" 2>/dev/null || true
35+
fi
36+
}
37+
38+
function __runc_in_mount_namespace() {
39+
setup_runc_cmdline
40+
in_mount_namespace "${RUNC_CMDLINE[@]}" "$@"
41+
}
42+
43+
function make_rootfs_shared() {
44+
in_mount_namespace mount --make-rshared /
45+
}
46+
47+
function runc_in_mount_namespace() {
48+
CMDNAME="$(basename "$RUNC")" sane_run __runc_in_mount_namespace "$@"
49+
}
50+
51+
function setup() {
52+
requires root
53+
require_mount_namespace_tools
54+
55+
setup_isolated_mount_namespace
56+
make_rootfs_shared
57+
setup_debian
58+
}
59+
60+
function teardown() {
61+
teardown_bundle
62+
teardown_isolated_mount_namespace
63+
}
64+
65+
@test "runc run [rootfsPropagation slave]" {
66+
# make sure the rootfs mount is slave before running the test
67+
update_config ' .linux.rootfsPropagation = "slave" '
68+
69+
update_config ' .process.args = ["findmnt", "--noheadings", "-o", "PROPAGATION", "/"] '
70+
71+
runc_in_mount_namespace run test_slave_rootfs
72+
[ "$status" -eq 0 ]
73+
[ "$output" = "private,slave" ]
74+
}

0 commit comments

Comments
 (0)