| 1 | # SPDX-FileCopyrightText: Copyright The Lima Authors |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | load "../helpers/load" |
| 5 | |
| 6 | INSTANCE=bats |
| 7 | |
| 8 | setup() { |
| 9 | limactl shell "$INSTANCE" -- mkdir -p /tmp/test_limactl_copy |
| 10 | } |
| 11 | |
| 12 | teardown() { |
| 13 | limactl shell "$INSTANCE" -- rm -rf /tmp/test_limactl_copy |
| 14 | } |
| 15 | |
| 16 | test_copy_dir_from_host_to_instance() { |
| 17 | backend="$1" |
| 18 | mkdir -p "$BATS_TEST_TMPDIR/foo/bar" |
| 19 | |
| 20 | # SRC DST |
| 21 | limactl copy --backend="$backend" -r "$BATS_TEST_TMPDIR/foo" "$INSTANCE":/tmp/test_limactl_copy/foo |
| 22 | limactl shell "$INSTANCE" -- test -d /tmp/test_limactl_copy/foo/bar |
| 23 | limactl shell "$INSTANCE" -- rm -rf /tmp/test_limactl_copy/foo |
| 24 | |
| 25 | # SRC/ DST |
| 26 | limactl shell "$INSTANCE" -- mkdir -p /tmp/test_limactl_copy/foo_src_with_slash/ |
| 27 | limactl copy --backend="$backend" -r "$BATS_TEST_TMPDIR/foo/" "$INSTANCE":/tmp/test_limactl_copy/foo_src_with_slash |
| 28 | limactl shell "$INSTANCE" -- test -d /tmp/test_limactl_copy/foo_src_with_slash/foo |
| 29 | limactl shell "$INSTANCE" -- rm -rf /tmp/test_limactl_copy/foo_src_with_slash |
| 30 | |
| 31 | # SRC DST/ |
| 32 | limactl shell "$INSTANCE" -- mkdir -p /tmp/test_limactl_copy/foo_dst_with_slash/ |
| 33 | limactl copy --backend="$backend" -r "$BATS_TEST_TMPDIR/foo" "$INSTANCE":/tmp/test_limactl_copy/foo_dst_with_slash/ |
| 34 | limactl shell "$INSTANCE" -- test -d /tmp/test_limactl_copy/foo_dst_with_slash/foo |
| 35 | limactl shell "$INSTANCE" -- rm -rf /tmp/test_limactl_copy/foo_dst_with_slash |
| 36 | |
| 37 | # SRC/ DST/ |
| 38 | limactl shell "$INSTANCE" -- mkdir -p /tmp/test_limactl_copy/foo_src_dst_with_slash/ |
| 39 | limactl copy --backend="$backend" -r "$BATS_TEST_TMPDIR/foo/" "$INSTANCE":/tmp/test_limactl_copy/foo_src_dst_with_slash/ |
| 40 | limactl shell "$INSTANCE" -- test -d /tmp/test_limactl_copy/foo_src_dst_with_slash/foo |
| 41 | limactl shell "$INSTANCE" -- rm -rf /tmp/test_limactl_copy/foo_src_dst_with_slash |
| 42 | |
| 43 | # SRC DST, with DST existing: SRC has to be copied into DST, like `cp -r` does. |
| 44 | # https://github.com/lima-vm/lima/issues/5500 |
| 45 | limactl shell "$INSTANCE" -- mkdir -p /tmp/test_limactl_copy/existing_dst |
| 46 | limactl copy --backend="$backend" -r "$BATS_TEST_TMPDIR/foo" "$INSTANCE":/tmp/test_limactl_copy/existing_dst |
| 47 | limactl shell "$INSTANCE" -- test -d /tmp/test_limactl_copy/existing_dst/foo/bar |
| 48 | limactl shell "$INSTANCE" -- rm -rf /tmp/test_limactl_copy/existing_dst |
| 49 | } |
| 50 | |
| 51 | @test "copy directory from host to Lima instance (scp)" { |
| 52 | test_copy_dir_from_host_to_instance scp |
| 53 | } |
| 54 | |
| 55 | # https://github.com/lima-vm/lima/issues/4468 |
| 56 | @test "copy directory from host to Lima instance (rsync)" { |
| 57 | test_copy_dir_from_host_to_instance rsync |
| 58 | } |
| 59 | |
| 60 | test_copy_dir_from_instance_to_host() { |
| 61 | backend="$1" |
| 62 | |
| 63 | limactl shell "$INSTANCE" -- mkdir -p /tmp/test_limactl_copy/foo/bar |
| 64 | |
| 65 | # SRC DST |
| 66 | limactl copy --backend="$backend" -r "$INSTANCE":/tmp/test_limactl_copy/foo "$BATS_TEST_TMPDIR/foo" |
| 67 | assert_dir_exists "$BATS_TEST_TMPDIR/foo/bar" |
| 68 | |
| 69 | # SRC/ DST |
| 70 | limactl copy --backend="$backend" -r "$INSTANCE":/tmp/test_limactl_copy/foo/ "$BATS_TEST_TMPDIR/foo_src_with_slash" |
| 71 | assert_dir_exists "$BATS_TEST_TMPDIR/foo_src_with_slash/bar" |
| 72 | |
| 73 | # SRC DST/ |
| 74 | limactl copy --backend="$backend" -r "$INSTANCE":/tmp/test_limactl_copy/foo "$BATS_TEST_TMPDIR/foo_dst_with_slash/" |
| 75 | assert_dir_exists "$BATS_TEST_TMPDIR/foo_dst_with_slash/bar" |
| 76 | |
| 77 | # SRC/ DST/ |
| 78 | limactl copy --backend="$backend" -r "$INSTANCE":/tmp/test_limactl_copy/foo/ "$BATS_TEST_TMPDIR/foo_src_dst_with_slash/" |
| 79 | assert_dir_exists "$BATS_TEST_TMPDIR/foo_src_dst_with_slash/bar" |
| 80 | |
| 81 | # SRC DST, with DST existing: SRC has to be copied into DST, like `cp -r` does. |
| 82 | # https://github.com/lima-vm/lima/issues/5500 |
| 83 | mkdir -p "$BATS_TEST_TMPDIR/existing_dst" |
| 84 | limactl copy --backend="$backend" -r "$INSTANCE":/tmp/test_limactl_copy/foo "$BATS_TEST_TMPDIR/existing_dst" |
| 85 | assert_dir_exists "$BATS_TEST_TMPDIR/existing_dst/foo/bar" |
| 86 | } |
| 87 | |
| 88 | @test "copy directory from Lima instance to host (scp)" { |
| 89 | test_copy_dir_from_instance_to_host scp |
| 90 | } |
| 91 | |
| 92 | @test "copy directory from Lima instance to host (rsync)" { |
| 93 | test_copy_dir_from_instance_to_host rsync |
| 94 | } |
| 95 | |
| 96 | test_copy_file_from_host_to_instance() { |
| 97 | backend="$1" |
| 98 | echo "hello" > "$BATS_TEST_TMPDIR/hello.txt" |
| 99 | |
| 100 | limactl copy --backend="$backend" "$BATS_TEST_TMPDIR/hello.txt" "$INSTANCE":/tmp/test_limactl_copy/hello.txt |
| 101 | |
| 102 | run -0 limactl shell "$INSTANCE" -- cat /tmp/test_limactl_copy/hello.txt |
| 103 | assert_output "hello" |
| 104 | |
| 105 | limactl shell "$INSTANCE" -- rm -f /tmp/test_limactl_copy/hello.txt |
| 106 | } |
| 107 | |
| 108 | @test "copy file from host to Lima instance (scp)" { |
| 109 | test_copy_file_from_host_to_instance scp |
| 110 | } |
| 111 | |
| 112 | @test "copy file from host to Lima instance (rsync)" { |
| 113 | test_copy_file_from_host_to_instance rsync |
| 114 | } |
| 115 | |
| 116 | test_copy_file_from_instance_to_host() { |
| 117 | backend="$1" |
| 118 | limactl shell "$INSTANCE" -- bash -c 'echo "hello" > /tmp/test_limactl_copy/hello.txt' |
| 119 | |
| 120 | limactl copy --backend="$backend" "$INSTANCE":/tmp/test_limactl_copy/hello.txt "$BATS_TEST_TMPDIR/hello.txt" |
| 121 | |
| 122 | run -0 cat "$BATS_TEST_TMPDIR/hello.txt" |
| 123 | assert_output "hello" |
| 124 | |
| 125 | limactl shell "$INSTANCE" -- rm -f /tmp/hello.txt |
| 126 | } |
| 127 | |
| 128 | @test "copy file from Lima instance to host (scp)" { |
| 129 | test_copy_file_from_instance_to_host scp |
| 130 | } |
| 131 | |
| 132 | @test "copy file from Lima instance to host (rsync)" { |
| 133 | test_copy_file_from_instance_to_host rsync |
| 134 | } |
| 135 | |
| 136 | @test "copy file from Lima instance to Lima instance (auto)" { |
| 137 | limactl shell "$INSTANCE" -- bash -c 'echo "hello" > /tmp/test_limactl_copy/hello.txt' |
| 138 | |
| 139 | limactl copy --backend=auto \ |
| 140 | "$INSTANCE":/tmp/test_limactl_copy/hello.txt \ |
| 141 | "$INSTANCE":/tmp |
| 142 | |
| 143 | run -0 limactl shell "$INSTANCE" -- cat /tmp/hello.txt |
| 144 | assert_output "hello" |
| 145 | run -0 limactl shell "$INSTANCE" -- rm -f /tmp/hello.txt |
| 146 | } |