|
| 1 | +// SPDX-FileCopyrightText: Copyright The Lima Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package hostagent |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "gotest.tools/v3/assert" |
| 10 | +) |
| 11 | + |
| 12 | +func TestTranslateToGuestPath(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + hostPath string |
| 16 | + symlinks map[string]string |
| 17 | + locations map[string]string |
| 18 | + expected string |
| 19 | + }{ |
| 20 | + { |
| 21 | + name: "no translation needed - empty maps", |
| 22 | + hostPath: "/Users/user/file.txt", |
| 23 | + symlinks: map[string]string{}, |
| 24 | + locations: map[string]string{}, |
| 25 | + expected: "/Users/user/file.txt", |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "no translation needed - location equals mountPoint", |
| 29 | + hostPath: "/Users/user/project/file.txt", |
| 30 | + symlinks: map[string]string{}, |
| 31 | + locations: map[string]string{"/Users/user": "/Users/user"}, |
| 32 | + expected: "/Users/user/project/file.txt", |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "translate location to different mountPoint", |
| 36 | + hostPath: "/Users/user/source/file.txt", |
| 37 | + symlinks: map[string]string{}, |
| 38 | + locations: map[string]string{"/Users/user/source": "/mnt/dest"}, |
| 39 | + expected: "/mnt/dest/file.txt", |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "translate location to different mountPoint - nested path", |
| 43 | + hostPath: "/Users/user/source/subdir/deep/file.txt", |
| 44 | + symlinks: map[string]string{}, |
| 45 | + locations: map[string]string{"/Users/user/source": "/mnt/dest"}, |
| 46 | + expected: "/mnt/dest/subdir/deep/file.txt", |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "translate location to different mountPoint - root file", |
| 50 | + hostPath: "/Users/user/source/file.txt", |
| 51 | + symlinks: map[string]string{}, |
| 52 | + locations: map[string]string{"/Users/user/source": "/mnt/dest"}, |
| 53 | + expected: "/mnt/dest/file.txt", |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "symlink resolution only", |
| 57 | + hostPath: "/private/tmp/file.txt", |
| 58 | + symlinks: map[string]string{"/private/tmp": "/tmp"}, |
| 59 | + locations: map[string]string{}, |
| 60 | + expected: "/tmp/file.txt", |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "symlink resolution with location translation", |
| 64 | + hostPath: "/private/var/folders/source/file.txt", |
| 65 | + symlinks: map[string]string{"/private/var": "/var"}, |
| 66 | + locations: map[string]string{"/var/folders/source": "/mnt/dest"}, |
| 67 | + expected: "/mnt/dest/file.txt", |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "more specific location matches", |
| 71 | + hostPath: "/Users/user/source/file.txt", |
| 72 | + symlinks: map[string]string{}, |
| 73 | + locations: map[string]string{"/Users/user/source": "/mnt/source"}, |
| 74 | + expected: "/mnt/source/file.txt", |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "less specific location matches when more specific not present", |
| 78 | + hostPath: "/Users/user/other/file.txt", |
| 79 | + symlinks: map[string]string{}, |
| 80 | + locations: map[string]string{"/Users/user": "/mnt/home"}, |
| 81 | + expected: "/mnt/home/other/file.txt", |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "path not matching any location", |
| 85 | + hostPath: "/other/path/file.txt", |
| 86 | + symlinks: map[string]string{}, |
| 87 | + locations: map[string]string{"/Users/user/source": "/mnt/dest"}, |
| 88 | + expected: "/other/path/file.txt", |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "exact location match - file at mount root", |
| 92 | + hostPath: "/Users/user/source", |
| 93 | + symlinks: map[string]string{}, |
| 94 | + locations: map[string]string{"/Users/user/source": "/mnt/dest"}, |
| 95 | + expected: "/mnt/dest", |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "multiple locations - non-overlapping", |
| 99 | + hostPath: "/Users/user/project/file.txt", |
| 100 | + symlinks: map[string]string{}, |
| 101 | + locations: map[string]string{ |
| 102 | + "/Users/user/project": "/mnt/project", |
| 103 | + "/Users/user/other": "/mnt/other", |
| 104 | + }, |
| 105 | + expected: "/mnt/project/file.txt", |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "multiple symlinks", |
| 109 | + hostPath: "/private/var/tmp/file.txt", |
| 110 | + symlinks: map[string]string{ |
| 111 | + "/private/var": "/var", |
| 112 | + "/private/tmp": "/tmp", |
| 113 | + }, |
| 114 | + locations: map[string]string{}, |
| 115 | + expected: "/var/tmp/file.txt", |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "multiple locations and symlinks combined", |
| 119 | + hostPath: "/private/var/folders/source/file.txt", |
| 120 | + symlinks: map[string]string{ |
| 121 | + "/private/var": "/var", |
| 122 | + }, |
| 123 | + locations: map[string]string{ |
| 124 | + "/var/folders/source": "/mnt/dest1", |
| 125 | + "/tmp/test": "/mnt/dest2", |
| 126 | + }, |
| 127 | + expected: "/mnt/dest1/file.txt", |
| 128 | + }, |
| 129 | + } |
| 130 | + |
| 131 | + for _, tc := range tests { |
| 132 | + t.Run(tc.name, func(t *testing.T) { |
| 133 | + result := translateToGuestPath(tc.hostPath, tc.symlinks, tc.locations) |
| 134 | + assert.Equal(t, result, tc.expected) |
| 135 | + }) |
| 136 | + } |
| 137 | +} |
0 commit comments