| 1 | # SPDX-FileCopyrightText: Copyright The Lima Authors |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | load "../helpers/load" |
| 5 | |
| 6 | INSTANCE=bats-nomount |
| 7 | |
| 8 | setup() { |
| 9 | # Create a temporary test directory and files |
| 10 | TEST_SYNC_DIR="$BATS_TEST_TMPDIR/sync-test" |
| 11 | mkdir -p "$TEST_SYNC_DIR" |
| 12 | touch "$TEST_SYNC_DIR/foo.txt" |
| 13 | touch "$TEST_SYNC_DIR/bar.txt" |
| 14 | |
| 15 | # Create a simple script that makes changes to these files |
| 16 | cat > "$TEST_SYNC_DIR/modify.sh" << 'EOF' |
| 17 | #!/bin/sh |
| 18 | set -eu |
| 19 | echo "modified foo" > foo.txt |
| 20 | echo "modified bar" > bar.txt |
| 21 | EOF |
| 22 | chmod +x "$TEST_SYNC_DIR/modify.sh" |
| 23 | } |
| 24 | |
| 25 | teardown() { |
| 26 | # Clean up test directory |
| 27 | if [[ -d "$TEST_SYNC_DIR" ]]; then |
| 28 | rm -rf "$TEST_SYNC_DIR" |
| 29 | fi |
| 30 | } |
| 31 | |
| 32 | file_uid() { |
| 33 | local path="$1" |
| 34 | if [[ "$OSTYPE" == darwin* ]]; then |
| 35 | stat -f '%u' "$path" |
| 36 | else |
| 37 | stat -c '%u' "$path" |
| 38 | fi |
| 39 | } |
| 40 | |
| 41 | file_perm_octal() { |
| 42 | local path="$1" |
| 43 | if [[ "$OSTYPE" == darwin* ]]; then |
| 44 | stat -f '%A' "$path" |
| 45 | else |
| 46 | stat -c '%a' "$path" |
| 47 | fi |
| 48 | } |
| 49 | |
| 50 | @test 'shell --sync preserves working directory path from host to guest' { |
| 51 | cd "$TEST_SYNC_DIR" |
| 52 | |
| 53 | # Get path of the TEST_SYNC_DIR for verification |
| 54 | local path_test_dir |
| 55 | path_test_dir="$PWD" |
| 56 | |
| 57 | run -0 bash -c "limactl shell --sync . --yes '$INSTANCE' pwd && ./modify.sh" |
| 58 | |
| 59 | # Verify the guest working directory matches the host path structure |
| 60 | assert_output --regexp ".*${path_test_dir#/}" |
| 61 | |
| 62 | # Verify files were modified |
| 63 | run cat "$TEST_SYNC_DIR/foo.txt" |
| 64 | assert_output "modified foo" |
| 65 | run cat "$TEST_SYNC_DIR/bar.txt" |
| 66 | assert_output "modified bar" |
| 67 | } |
| 68 | |
| 69 | @test 'shell --sync with directory path containing spaces and quotes' { |
| 70 | # Create directory with spaces and quotes in name and move test directory to it |
| 71 | local special_dir="$BATS_TEST_TMPDIR/sync test 'with' \"quotes\"" |
| 72 | mkdir -p "$special_dir" |
| 73 | mv "$TEST_SYNC_DIR" "$special_dir" |
| 74 | cd "$special_dir/sync-test" |
| 75 | |
| 76 | # Count files before sync |
| 77 | local files_before |
| 78 | files_before=$(find . -type f | wc -l) |
| 79 | |
| 80 | run -0 bash -c "limactl shell --sync . --yes '$INSTANCE' ./modify.sh" |
| 81 | |
| 82 | # Verify files were modified |
| 83 | run cat "$special_dir/sync-test/foo.txt" |
| 84 | assert_output "modified foo" |
| 85 | run cat "$special_dir/sync-test/bar.txt" |
| 86 | assert_output "modified bar" |
| 87 | |
| 88 | # Count files after sync |
| 89 | local files_after |
| 90 | files_after=$(find "$special_dir/sync-test" -type f | wc -l) |
| 91 | [[ $files_after -eq $files_before ]] |
| 92 | |
| 93 | # Cleanup |
| 94 | rm -rf "$special_dir" |
| 95 | } |
| 96 | |
| 97 | @test 'shell --sync reflects file deletion from guest to host' { |
| 98 | cd "$TEST_SYNC_DIR" |
| 99 | |
| 100 | run -0 bash -c "limactl shell --sync . --yes '$INSTANCE' rm -f foo.txt" |
| 101 | |
| 102 | assert_file_not_exists "$TEST_SYNC_DIR/foo.txt" |
| 103 | } |
| 104 | |
| 105 | @test 'shell --sync reflects new directory and file creation from guest to host' { |
| 106 | cd "$TEST_SYNC_DIR" |
| 107 | |
| 108 | # Create a script that creates a new directory with a file |
| 109 | cat > "$TEST_SYNC_DIR/create_new.sh" << 'EOF' |
| 110 | #!/bin/sh |
| 111 | set -eu |
| 112 | mkdir -p new_directory |
| 113 | echo "foo bar baz" > new_directory/new_file.txt |
| 114 | EOF |
| 115 | chmod +x "$TEST_SYNC_DIR/create_new.sh" |
| 116 | |
| 117 | run -0 bash -c "limactl shell --sync . --yes '$INSTANCE' ./create_new.sh && ./modify.sh" |
| 118 | |
| 119 | # Verify new directory was created on host |
| 120 | assert_dir_exists "$TEST_SYNC_DIR/new_directory" |
| 121 | assert_file_exists "$TEST_SYNC_DIR/new_directory/new_file.txt" |
| 122 | |
| 123 | # Verify file content |
| 124 | run cat "$TEST_SYNC_DIR/new_directory/new_file.txt" |
| 125 | assert_output "foo bar baz" |
| 126 | run cat "$TEST_SYNC_DIR/foo.txt" |
| 127 | assert_output "modified foo" |
| 128 | run cat "$TEST_SYNC_DIR/bar.txt" |
| 129 | assert_output "modified bar" |
| 130 | } |
| 131 | |
| 132 | @test 'shell --sync preserves file permissions' { |
| 133 | cd "$TEST_SYNC_DIR" |
| 134 | |
| 135 | # Create a file with specific permissions |
| 136 | touch "$TEST_SYNC_DIR/executable.sh" |
| 137 | chmod 755 "$TEST_SYNC_DIR/executable.sh" |
| 138 | |
| 139 | # Modify the file in guest |
| 140 | run -0 bash -c "limactl shell --sync . --yes '$INSTANCE' ./modify.sh" |
| 141 | |
| 142 | # Verify file is still executable on host |
| 143 | run file_perm_octal "$TEST_SYNC_DIR/executable.sh" |
| 144 | assert_output "755" |
| 145 | |
| 146 | # Verify files were modified |
| 147 | run cat "$TEST_SYNC_DIR/foo.txt" |
| 148 | assert_output "modified foo" |
| 149 | run cat "$TEST_SYNC_DIR/bar.txt" |
| 150 | assert_output "modified bar" |
| 151 | } |
| 152 | |
| 153 | @test 'shell --sync reflects chmod metadata changes from guest to host' { |
| 154 | cd "$TEST_SYNC_DIR" |
| 155 | |
| 156 | touch "$TEST_SYNC_DIR/metadata-perms.sh" |
| 157 | chmod 644 "$TEST_SYNC_DIR/metadata-perms.sh" |
| 158 | |
| 159 | run -0 bash -c "limactl shell --sync . --yes '$INSTANCE' sh -ceu 'chmod 744 metadata-perms.sh'" |
| 160 | |
| 161 | run file_perm_octal "$TEST_SYNC_DIR/metadata-perms.sh" |
| 162 | assert_output "744" |
| 163 | } |
| 164 | |
| 165 | @test 'shell --sync reflects symlink target updates from guest to host' { |
| 166 | cd "$TEST_SYNC_DIR" |
| 167 | |
| 168 | echo "a" > "$TEST_SYNC_DIR/target_a.txt" |
| 169 | echo "b" > "$TEST_SYNC_DIR/target_b.txt" |
| 170 | ln -s target_a.txt "$TEST_SYNC_DIR/current_link.txt" |
| 171 | |
| 172 | run -0 bash -c "limactl shell --sync . --yes '$INSTANCE' ln -snf target_b.txt current_link.txt" |
| 173 | |
| 174 | assert_symlink_to "$TEST_SYNC_DIR/target_b.txt" "$TEST_SYNC_DIR/current_link.txt" |
| 175 | } |
| 176 | |
| 177 | @test 'shell --sync preserves host ownership after guest modification' { |
| 178 | cd "$TEST_SYNC_DIR" |
| 179 | |
| 180 | touch "$TEST_SYNC_DIR/ownership-test.txt" |
| 181 | orig_uid=$(file_uid "$TEST_SYNC_DIR/ownership-test.txt") |
| 182 | |
| 183 | run -0 bash -c "limactl shell --sync . --yes '$INSTANCE' sh -ceu 'sudo chown root ownership-test.txt'" |
| 184 | |
| 185 | new_uid=$(file_uid "$TEST_SYNC_DIR/ownership-test.txt") |
| 186 | assert_equal "$orig_uid" "$new_uid" |
| 187 | } |
| 188 | |
| 189 | @test 'shell --sync works without existing ControlMaster socket' { |
| 190 | cd "$TEST_SYNC_DIR" |
| 191 | |
| 192 | # Remove the ControlMaster socket |
| 193 | local sock_path="$LIMA_HOME/$INSTANCE/ssh.sock" |
| 194 | if [[ -S "$sock_path" ]]; then |
| 195 | rm "$sock_path" |
| 196 | fi |
| 197 | |
| 198 | run -0 bash -c "limactl shell --sync . --yes '$INSTANCE' ./modify.sh" |
| 199 | |
| 200 | # Verify files were modified |
| 201 | run cat "$TEST_SYNC_DIR/foo.txt" |
| 202 | assert_output "modified foo" |
| 203 | run cat "$TEST_SYNC_DIR/bar.txt" |
| 204 | assert_output "modified bar" |
| 205 | } |
| 206 | |
| 207 | @test 'shell --sync should preserve top-level subtree after clean up' { |
| 208 | local foo_dir="$TEST_SYNC_DIR/foo" |
| 209 | mkdir -p "$foo_dir" |
| 210 | |
| 211 | cd "$foo_dir" |
| 212 | run -0 bash -c "limactl shell --sync . --yes '$INSTANCE' sh -c 'pwd > pwd.txt'" |
| 213 | assert_file_exists "$foo_dir/pwd.txt" |
| 214 | |
| 215 | local guest_pwd |
| 216 | guest_pwd=$(cat "$foo_dir/pwd.txt") |
| 217 | |
| 218 | run -0 bash -c "limactl shell '$INSTANCE' test -d ${guest_pwd%/*}" |
| 219 | } |