| 1 | |
| 2 | |
| 3 | |
| 4 | package hostagent |
| 5 | |
| 6 | import ( |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | |
| 10 | "gotest.tools/v3/assert" |
| 11 | ) |
| 12 | |
| 13 | func TestExpectRemove(t *testing.T) { |
| 14 | |
| 15 | host := filepath.FromSlash |
| 16 | var got []string |
| 17 | record := func(name string, supported bool) func(string) bool { |
| 18 | return func(hostPath string) bool { |
| 19 | got = append(got, name+":"+hostPath) |
| 20 | return supported |
| 21 | } |
| 22 | } |
| 23 | a := &HostAgent{mounts: []*mount{ |
| 24 | {location: host("/Users/user/project"), mountPoint: "/mnt/project", expectRemove: record("project", true)}, |
| 25 | {location: host("/Users/user/project/vendor"), mountPoint: "/mnt/project/vendor", expectRemove: record("vendor", true)}, |
| 26 | {location: host("/Users/user/other"), mountPoint: "/mnt/project/sub", expectRemove: record("other", true)}, |
| 27 | {location: host("/Users/user/9p"), mountPoint: "/Users/user/9p", expectRemove: record("9p", false)}, |
| 28 | }} |
| 29 | mountSymlinks = map[string]string{host("/private/Users/user/project"): host("/Users/user/project")} |
| 30 | t.Cleanup(func() { mountSymlinks = make(map[string]string) }) |
| 31 | |
| 32 | tests := []struct { |
| 33 | hostPath string |
| 34 | wantGuest string |
| 35 | wantCall string |
| 36 | }{ |
| 37 | {hostPath: "/Users/user/project/a.txt", wantGuest: "/mnt/project/a.txt", wantCall: "project:/Users/user/project/a.txt"}, |
| 38 | {hostPath: "/Users/user/project/vendor/b.txt", wantGuest: "/mnt/project/vendor/b.txt", wantCall: "vendor:/Users/user/project/vendor/b.txt"}, |
| 39 | {hostPath: "/private/Users/user/project/c.txt", wantGuest: "/mnt/project/c.txt", wantCall: "project:/Users/user/project/c.txt"}, |
| 40 | {hostPath: "/Users/user/9p/d.txt", wantCall: "9p:/Users/user/9p/d.txt"}, |
| 41 | {hostPath: "/Users/user/project"}, |
| 42 | {hostPath: "/Users/user/project2/e.txt"}, |
| 43 | |
| 44 | {hostPath: "/Users/user/project/sub/f.txt"}, |
| 45 | {hostPath: "/Users/user/project/sub"}, |
| 46 | {hostPath: "/Users/user/project/subdir/g.txt", wantGuest: "/mnt/project/subdir/g.txt", wantCall: "project:/Users/user/project/subdir/g.txt"}, |
| 47 | } |
| 48 | for _, tc := range tests { |
| 49 | t.Run(tc.hostPath, func(t *testing.T) { |
| 50 | got = nil |
| 51 | guestPath, ok := a.expectRemove(host(tc.hostPath)) |
| 52 | assert.Equal(t, guestPath, tc.wantGuest) |
| 53 | assert.Equal(t, ok, tc.wantGuest != "") |
| 54 | if tc.wantCall == "" { |
| 55 | assert.Equal(t, len(got), 0) |
| 56 | } else { |
| 57 | assert.DeepEqual(t, got, []string{host(tc.wantCall)}) |
| 58 | } |
| 59 | }) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func TestTranslateToGuestPath(t *testing.T) { |
| 64 | tests := []struct { |
| 65 | name string |
| 66 | hostPath string |
| 67 | symlinks map[string]string |
| 68 | locations map[string]string |
| 69 | expected string |
| 70 | }{ |
| 71 | { |
| 72 | name: "no translation needed - empty maps", |
| 73 | hostPath: "/Users/user/file.txt", |
| 74 | symlinks: map[string]string{}, |
| 75 | locations: map[string]string{}, |
| 76 | expected: "/Users/user/file.txt", |
| 77 | }, |
| 78 | { |
| 79 | name: "no translation needed - location equals mountPoint", |
| 80 | hostPath: "/Users/user/project/file.txt", |
| 81 | symlinks: map[string]string{}, |
| 82 | locations: map[string]string{"/Users/user": "/Users/user"}, |
| 83 | expected: "/Users/user/project/file.txt", |
| 84 | }, |
| 85 | { |
| 86 | name: "translate location to different mountPoint", |
| 87 | hostPath: "/Users/user/source/file.txt", |
| 88 | symlinks: map[string]string{}, |
| 89 | locations: map[string]string{"/Users/user/source": "/mnt/dest"}, |
| 90 | expected: "/mnt/dest/file.txt", |
| 91 | }, |
| 92 | { |
| 93 | name: "translate location to different mountPoint - nested path", |
| 94 | hostPath: "/Users/user/source/subdir/deep/file.txt", |
| 95 | symlinks: map[string]string{}, |
| 96 | locations: map[string]string{"/Users/user/source": "/mnt/dest"}, |
| 97 | expected: "/mnt/dest/subdir/deep/file.txt", |
| 98 | }, |
| 99 | { |
| 100 | name: "translate location to different mountPoint - root file", |
| 101 | hostPath: "/Users/user/source/file.txt", |
| 102 | symlinks: map[string]string{}, |
| 103 | locations: map[string]string{"/Users/user/source": "/mnt/dest"}, |
| 104 | expected: "/mnt/dest/file.txt", |
| 105 | }, |
| 106 | { |
| 107 | name: "symlink resolution only", |
| 108 | hostPath: "/private/tmp/file.txt", |
| 109 | symlinks: map[string]string{"/private/tmp": "/tmp"}, |
| 110 | locations: map[string]string{}, |
| 111 | expected: "/tmp/file.txt", |
| 112 | }, |
| 113 | { |
| 114 | name: "symlink resolution with location translation", |
| 115 | hostPath: "/private/var/folders/source/file.txt", |
| 116 | symlinks: map[string]string{"/private/var": "/var"}, |
| 117 | locations: map[string]string{"/var/folders/source": "/mnt/dest"}, |
| 118 | expected: "/mnt/dest/file.txt", |
| 119 | }, |
| 120 | { |
| 121 | name: "more specific location matches", |
| 122 | hostPath: "/Users/user/source/file.txt", |
| 123 | symlinks: map[string]string{}, |
| 124 | locations: map[string]string{"/Users/user/source": "/mnt/source"}, |
| 125 | expected: "/mnt/source/file.txt", |
| 126 | }, |
| 127 | { |
| 128 | name: "less specific location matches when more specific not present", |
| 129 | hostPath: "/Users/user/other/file.txt", |
| 130 | symlinks: map[string]string{}, |
| 131 | locations: map[string]string{"/Users/user": "/mnt/home"}, |
| 132 | expected: "/mnt/home/other/file.txt", |
| 133 | }, |
| 134 | { |
| 135 | name: "path not matching any location", |
| 136 | hostPath: "/other/path/file.txt", |
| 137 | symlinks: map[string]string{}, |
| 138 | locations: map[string]string{"/Users/user/source": "/mnt/dest"}, |
| 139 | expected: "/other/path/file.txt", |
| 140 | }, |
| 141 | { |
| 142 | name: "exact location match - file at mount root", |
| 143 | hostPath: "/Users/user/source", |
| 144 | symlinks: map[string]string{}, |
| 145 | locations: map[string]string{"/Users/user/source": "/mnt/dest"}, |
| 146 | expected: "/mnt/dest", |
| 147 | }, |
| 148 | { |
| 149 | name: "multiple locations - non-overlapping", |
| 150 | hostPath: "/Users/user/project/file.txt", |
| 151 | symlinks: map[string]string{}, |
| 152 | locations: map[string]string{ |
| 153 | "/Users/user/project": "/mnt/project", |
| 154 | "/Users/user/other": "/mnt/other", |
| 155 | }, |
| 156 | expected: "/mnt/project/file.txt", |
| 157 | }, |
| 158 | { |
| 159 | name: "multiple symlinks", |
| 160 | hostPath: "/private/var/tmp/file.txt", |
| 161 | symlinks: map[string]string{ |
| 162 | "/private/var": "/var", |
| 163 | "/private/tmp": "/tmp", |
| 164 | }, |
| 165 | locations: map[string]string{}, |
| 166 | expected: "/var/tmp/file.txt", |
| 167 | }, |
| 168 | { |
| 169 | name: "multiple locations and symlinks combined", |
| 170 | hostPath: "/private/var/folders/source/file.txt", |
| 171 | symlinks: map[string]string{ |
| 172 | "/private/var": "/var", |
| 173 | }, |
| 174 | locations: map[string]string{ |
| 175 | "/var/folders/source": "/mnt/dest1", |
| 176 | "/tmp/test": "/mnt/dest2", |
| 177 | }, |
| 178 | expected: "/mnt/dest1/file.txt", |
| 179 | }, |
| 180 | } |
| 181 | |
| 182 | for _, tc := range tests { |
| 183 | t.Run(tc.name, func(t *testing.T) { |
| 184 | result := translateToGuestPath(tc.hostPath, tc.symlinks, tc.locations) |
| 185 | assert.Equal(t, result, tc.expected) |
| 186 | }) |
| 187 | } |
| 188 | } |