Skip to content

Commit 5721146

Browse files
committed
BATS: Add test for volume mounts
This creates an initial test for volume mounts, mostly to figure out the correct behaviour around file ownership. Signed-off-by: Mark Yen <[email protected]>
1 parent d1f7e58 commit 5721146

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

bats/tests/containers/volumes.bats

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
load '../helpers/load'
2+
3+
get_tempdir() {
4+
if ! is_windows; then
5+
echo "$BATS_TEST_TMPDIR"
6+
return
7+
fi
8+
# On Windows, create a temporary directory that is in the Windows temporary
9+
# directory so that it mounts correctly. Note that in CI we end up running
10+
# with PSModulePath set to pwsh (7.x) paths, and that breaks the code for
11+
# PowerShell 5.1. So we need to have alternative code in that case.
12+
# See also https://github.com/PowerShell/PowerShell/issues/14100
13+
if command -v pwsh.exe &>/dev/null; then
14+
# shellcheck disable=SC2016 # Don't expand PowerShell expansion
15+
local command='
16+
$([System.IO.Directory]::CreateTempSubdirectory()).FullName
17+
'
18+
run pwsh.exe -Command "$command"
19+
assert_success
20+
else
21+
# PowerShell 5.1 is built against .net Framework 4.x and doesn't have
22+
# [System.IO.Directory]::CreateTempSubdirectory(); create a temporary
23+
# file and use its name instead.
24+
# shellcheck disable=SC2016 # Don't expand PowerShell expansion
25+
local command='
26+
$name = New-TemporaryFile
27+
Remove-Item -Path $name
28+
Start-Sleep -Seconds 1 # Allow anti-virus to do stuff
29+
New-Item -Type Directory -Path $name | Out-Null
30+
$name.FullName
31+
'
32+
run powershell.exe -Command "$command"
33+
assert_success
34+
fi
35+
run wslpath -u "$output"
36+
assert_success
37+
echo "$output" | tr -d "\r"
38+
}
39+
40+
local_setup() {
41+
unset PSModulePath # On Windows, fix running PowerShell 5.1 within pwsh 7
42+
run get_tempdir
43+
assert_success
44+
export WORK_PATH=$output
45+
}
46+
47+
local_teardown() {
48+
# Only do manual deletion on Windows; elsewhere we use BATS_TEST_TMPDIR so
49+
# BATS is expected to do the cleanup.
50+
if is_windows && [[ -n $WORK_PATH ]]; then
51+
local host_work_path
52+
host_work_path=$(host_path "$WORK_PATH")
53+
powershell.exe -Command "Remove-Item -Recurse -LiteralPath '$host_work_path'"
54+
fi
55+
}
56+
57+
@test 'factory reset' {
58+
factory_reset
59+
}
60+
61+
@test 'start container engine' {
62+
if is_linux; then
63+
# On linux, mount BATS_RUN_TMPDIR into the VM so that we can use
64+
# BATS_TEST_TMPDIR as a volume.
65+
local override_dir="${HOME}/.local/share/rancher-desktop/lima/_config"
66+
mkdir -p "$override_dir"
67+
{
68+
echo "mounts:"
69+
echo "- location: ${BATS_RUN_TMPDIR}"
70+
echo " writable: true"
71+
} >"$override_dir/override.yaml"
72+
fi
73+
start_container_engine
74+
wait_for_container_engine
75+
}
76+
77+
@test 'read-only volume mount' {
78+
# Read a file that was created outside the container.
79+
create_file "$WORK_PATH/foo" <<<hello
80+
# Use `--separate-stderr` to avoid image pull messages.
81+
run --separate-stderr \
82+
ctrctl run --volume "$(host_path "$WORK_PATH"):/mount:ro" \
83+
"$IMAGE_BUSYBOX" cat /mount/foo
84+
assert_success
85+
assert_output hello
86+
}
87+
88+
@test 'read-write volume mount' {
89+
# Create a file from the container.
90+
ctrctl run --volume "$(host_path "$WORK_PATH"):/mount:rw" \
91+
"$IMAGE_BUSYBOX" sh -c 'echo hello > /mount/foo'
92+
# Check that the file was written to.
93+
run cat "$WORK_PATH/foo"
94+
assert_success
95+
assert_output hello
96+
}
97+
98+
@test 'read-write volume mount as user' {
99+
# Create a file from within the container.
100+
ctrctl run --volume "$(host_path "$WORK_PATH"):/mount:rw" \
101+
--user 1000:1000 "$IMAGE_BUSYBOX" sh -c 'echo hello > /mount/foo'
102+
run cat "$WORK_PATH/foo"
103+
assert_success
104+
assert_output hello
105+
# Try to append to the file.
106+
ctrctl run --volume "$(host_path "$WORK_PATH"):/mount:rw" \
107+
--user 1000:1000 "$IMAGE_BUSYBOX" sh -c 'echo hello | tee -a /mount/foo'
108+
# Check that the file was modified.
109+
run cat "$WORK_PATH/foo"
110+
assert_success
111+
assert_output hello$'\n'hello
112+
if is_windows && using_windows_exe; then
113+
# On Windows, the directory may be owned by a group that the user is in;
114+
# additionally, there isn't an easy API to get effective access (!?).
115+
# shellcheck disable=SC2016 # Don't expand PowerShell expansion
116+
local command='
117+
$type = [System.Type]::GetType("System.Security.Principal.SecurityIdentifier")
118+
$owner = $(Get-Acl '"'$host_work_path/foo'"').GetOwner($type)
119+
$owner.Value
120+
'
121+
run powershell.exe -Command "$command"
122+
assert_success
123+
local owner=${output//$'\r'/}
124+
# shellcheck disable=SC2016 # Don't expand PowerShell expansion
125+
command='
126+
$identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
127+
$groups = $identity.Groups
128+
$groups.Add($identity.User)
129+
$groups | ForEach-Object { $_.Value }
130+
'
131+
run powershell.exe -Command "$command"
132+
assert_success
133+
assert_line "$owner"
134+
else
135+
# Check that the file is owned by the current user.
136+
stat_arg=-f # Assume BSD stat
137+
if stat --version | grep 'GNU coreutils'; then
138+
stat_arg=-c
139+
fi
140+
run stat "$stat_arg" '%u:%g' "$WORK_PATH/foo"
141+
assert_success
142+
assert_output "$(id -u):$(id -g)"
143+
fi
144+
}

0 commit comments

Comments
 (0)