|
| 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 PowerShell 5.1 (which |
| 10 | + # is more widely available) is built on .net Framework 4.x and does not |
| 11 | + # support `[System.IO.Directory]::CreateTempSubdirectory()` |
| 12 | + # shellcheck disable=SC2016 # Don't expand PowerShell expansion |
| 13 | + local command=' |
| 14 | + $name = New-TemporaryFile |
| 15 | + Remove-Item -Path $name |
| 16 | + Start-Sleep -Seconds 1 # Allow anti-virus to do stuff |
| 17 | + New-Item -Type Directory -Path $name | Out-Null |
| 18 | + $name.FullName |
| 19 | + ' |
| 20 | + run powershell.exe -Command "$command" |
| 21 | + assert_success |
| 22 | + run wslpath -u "$output" |
| 23 | + assert_success |
| 24 | + echo "$output" | tr -d "\r" |
| 25 | +} |
| 26 | + |
| 27 | +local_setup() { |
| 28 | + unset PSModulePath # On Windows, fix running PowerShell 5.1 within pwsh 7 |
| 29 | + run get_tempdir |
| 30 | + assert_success |
| 31 | + export WORK_PATH=$output |
| 32 | +} |
| 33 | + |
| 34 | +local_teardown() { |
| 35 | + # Only do manual deletion on Windows; elsewhere we use BATS_TEST_TMPDIR so |
| 36 | + # BATS is expected to do the cleanup. |
| 37 | + if is_windows && [[ -n $WORK_PATH ]]; then |
| 38 | + local host_work_path |
| 39 | + host_work_path=$(host_path "$WORK_PATH") |
| 40 | + powershell.exe -Command "Remove-Item -Recurse -LiteralPath '$host_work_path'" |
| 41 | + fi |
| 42 | +} |
| 43 | + |
| 44 | +@test 'factory reset' { |
| 45 | + factory_reset |
| 46 | +} |
| 47 | + |
| 48 | +@test 'start container engine' { |
| 49 | + if is_linux; then |
| 50 | + # On linux, mount BATS_RUN_TMPDIR into the VM so that we can use |
| 51 | + # BATS_TEST_TMPDIR as a volume. |
| 52 | + local override_dir="${HOME}/.local/share/rancher-desktop/lima/_config" |
| 53 | + mkdir -p "$override_dir" |
| 54 | + { |
| 55 | + echo "mounts:" |
| 56 | + echo "- location: ${BATS_RUN_TMPDIR}" |
| 57 | + echo " writable: true" |
| 58 | + } >"$override_dir/override.yaml" |
| 59 | + fi |
| 60 | + start_container_engine |
| 61 | + wait_for_container_engine |
| 62 | +} |
| 63 | + |
| 64 | +@test 'read-only volume mount' { |
| 65 | + # Read a file that was created outside the container. |
| 66 | + create_file "$WORK_PATH/foo" <<<hello |
| 67 | + # Use `--separate-stderr` to avoid image pull messages. |
| 68 | + run --separate-stderr \ |
| 69 | + ctrctl run --volume "$(host_path "$WORK_PATH"):/mount:ro" \ |
| 70 | + "$IMAGE_BUSYBOX" cat /mount/foo |
| 71 | + assert_success |
| 72 | + assert_output hello |
| 73 | +} |
| 74 | + |
| 75 | +@test 'read-write volume mount' { |
| 76 | + # Create a file from the container. |
| 77 | + ctrctl run --volume "$(host_path "$WORK_PATH"):/mount:rw" \ |
| 78 | + "$IMAGE_BUSYBOX" sh -c 'echo hello > /mount/foo' |
| 79 | + # Check that the file was written to. |
| 80 | + run cat "$WORK_PATH/foo" |
| 81 | + assert_success |
| 82 | + assert_output hello |
| 83 | +} |
| 84 | + |
| 85 | +@test 'read-write volume mount as user' { |
| 86 | + # Create a file from within the container. |
| 87 | + ctrctl run --volume "$(host_path "$WORK_PATH"):/mount:rw" \ |
| 88 | + --user 1000:1000 "$IMAGE_BUSYBOX" sh -c 'echo hello > /mount/foo' |
| 89 | + run cat "$WORK_PATH/foo" |
| 90 | + assert_success |
| 91 | + assert_output hello |
| 92 | + # Try to append to the file. |
| 93 | + ctrctl run --volume "$(host_path "$WORK_PATH"):/mount:rw" \ |
| 94 | + --user 1000:1000 "$IMAGE_BUSYBOX" sh -c 'echo hello | tee -a /mount/foo' |
| 95 | + # Check that the file was modified. |
| 96 | + run cat "$WORK_PATH/foo" |
| 97 | + assert_success |
| 98 | + assert_output hello$'\n'hello |
| 99 | + # Check that the file is owned by the current user. |
| 100 | + if is_windows; then |
| 101 | + run powershell.exe -Command '[System.Security.Principal.WindowsIdentity]::GetCurrent().Name' |
| 102 | + assert_success |
| 103 | + local expected=$output |
| 104 | + local host_work_path |
| 105 | + host_work_path=$(host_path "$WORK_PATH") |
| 106 | + # shellcheck disable=SC2016 # Don't expand PowerShell expansion |
| 107 | + run powershell.exe -Command '$(Get-Item '"$host_work_path/foo"').GetAccessControl().Owner' |
| 108 | + assert_success |
| 109 | + assert_output "$expected" |
| 110 | + else |
| 111 | + stat_arg=-f # Assume BSD stat |
| 112 | + if stat --version | grep 'GNU coreutils'; then |
| 113 | + stat_arg=-c |
| 114 | + fi |
| 115 | + run stat "$stat_arg" '%u:%g' "$WORK_PATH/foo" |
| 116 | + assert_success |
| 117 | + assert_output "$(id -u):$(id -g)" |
| 118 | + fi |
| 119 | +} |
0 commit comments