|
| 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 | + if command -v pwsh.exe; then |
| 13 | + # shellcheck disable=SC2016 # Don't expand PowerShell expansion |
| 14 | + local command=' |
| 15 | + $([System.IO.Directory]::CreateTempSubdirectory()).FullName |
| 16 | + ' |
| 17 | + run pwsh.exe -Command "$command" |
| 18 | + assert_success |
| 19 | + else |
| 20 | + # PowerShell 5.1 is built against .net Framework 4.x and doesn't have |
| 21 | + # [System.IO.Directory]::CreateTempSubdirectory(); create a temporary |
| 22 | + # file and use its name instead. |
| 23 | + # shellcheck disable=SC2016 # Don't expand PowerShell expansion |
| 24 | + local command=' |
| 25 | + $name = New-TemporaryFile |
| 26 | + Remove-Item -Path $name |
| 27 | + Start-Sleep -Seconds 1 # Allow anti-virus to do stuff |
| 28 | + New-Item -Type Directory -Path $name | Out-Null |
| 29 | + $name.FullName |
| 30 | + ' |
| 31 | + run powershell.exe -Command "$command" |
| 32 | + assert_success |
| 33 | + fi |
| 34 | + trace "Temporary directory: $output" |
| 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 | + # Check that the file is owned by the current user. |
| 113 | + if is_windows; then |
| 114 | + run powershell.exe -Command '[System.Security.Principal.WindowsIdentity]::GetCurrent().Name' |
| 115 | + assert_success |
| 116 | + local expected=$output |
| 117 | + local host_work_path |
| 118 | + host_work_path=$(host_path "$WORK_PATH") |
| 119 | + # shellcheck disable=SC2016 # Don't expand PowerShell expansion |
| 120 | + run powershell.exe -Command '$(Get-Item '"$host_work_path/foo"').GetAccessControl().Owner' |
| 121 | + assert_success |
| 122 | + assert_output "$expected" |
| 123 | + else |
| 124 | + stat_arg=-f # Assume BSD stat |
| 125 | + if stat --version | grep 'GNU coreutils'; then |
| 126 | + stat_arg=-c |
| 127 | + fi |
| 128 | + run stat "$stat_arg" '%u:%g' "$WORK_PATH/foo" |
| 129 | + assert_success |
| 130 | + assert_output "$(id -u):$(id -g)" |
| 131 | + fi |
| 132 | +} |
0 commit comments