Debian Codegraph @

mount/mount_windows_test.go

raw
1 //go:build windows
2
3 /*
4 Copyright 2017 The Kubernetes Authors.
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 */
18
19 package mount
20
21 import (
22 "fmt"
23 "io/ioutil"
24 "os"
25 "os/exec"
26 "path/filepath"
27 "testing"
28
29 testingexec "k8s.io/utils/exec/testing"
30 )
31
32 func makeLink(link, target string) error {
33 if output, err := exec.Command("cmd", "/c", "mklink", "/D", link, target).CombinedOutput(); err != nil {
34 return fmt.Errorf("mklink failed: %v, link(%q) target(%q) output: %q", err, link, target, string(output))
35 }
36 return nil
37 }
38
39 func removeLink(link string) error {
40 if output, err := exec.Command("cmd", "/c", "rmdir", link).CombinedOutput(); err != nil {
41 return fmt.Errorf("rmdir failed: %v, output: %q", err, string(output))
42 }
43 return nil
44 }
45
46 func setEquivalent(set1, set2 []string) bool {
47 map1 := make(map[string]bool)
48 map2 := make(map[string]bool)
49 for _, s := range set1 {
50 map1[s] = true
51 }
52 for _, s := range set2 {
53 map2[s] = true
54 }
55
56 for s := range map1 {
57 if !map2[s] {
58 return false
59 }
60 }
61 for s := range map2 {
62 if !map1[s] {
63 return false
64 }
65 }
66 return true
67 }
68
69 // this func must run in admin mode, otherwise it will fail
70 func TestGetMountRefs(t *testing.T) {
71 tests := []struct {
72 mountPath string
73 expectedRefs []string
74 }{
75 {
76 mountPath: `c:\windows`,
77 expectedRefs: []string{`c:\windows`},
78 },
79 {
80 mountPath: `c:\doesnotexist`,
81 expectedRefs: []string{},
82 },
83 }
84
85 mounter := Mounter{"fake/path"}
86
87 for _, test := range tests {
88 if refs, err := mounter.GetMountRefs(test.mountPath); err != nil || !setEquivalent(test.expectedRefs, refs) {
89 t.Errorf("getMountRefs(%q) = %v, error: %v; expected %v", test.mountPath, refs, err, test.expectedRefs)
90 }
91 }
92 }
93
94 func TestPathWithinBase(t *testing.T) {
95 tests := []struct {
96 fullPath string
97 basePath string
98 expectedResult bool
99 }{
100 {
101 fullPath: `c:\tmp\a\b\c`,
102 basePath: `c:\tmp`,
103 expectedResult: true,
104 },
105 {
106 fullPath: `c:\tmp1`,
107 basePath: `c:\tmp2`,
108 expectedResult: false,
109 },
110 {
111 fullPath: `c:\tmp`,
112 basePath: `c:\tmp`,
113 expectedResult: true,
114 },
115 {
116 fullPath: `c:\tmp`,
117 basePath: `c:\tmp\a\b\c`,
118 expectedResult: false,
119 },
120 {
121 fullPath: `c:\kubelet\pods\uuid\volumes\kubernetes.io~configmap\config\..timestamp\file.txt`,
122 basePath: `c:\kubelet\pods\uuid\volumes\kubernetes.io~configmap\config`,
123 expectedResult: true,
124 },
125 }
126
127 for _, test := range tests {
128 result := PathWithinBase(test.fullPath, test.basePath)
129 if result != test.expectedResult {
130 t.Fatalf("Expect result not equal with PathWithinBase(%s, %s) return: %v, expected: %v", test.fullPath, test.basePath, result, test.expectedResult)
131 }
132 }
133 }
134
135 func TestIsLikelyNotMountPoint(t *testing.T) {
136 if os.Getenv("GH_ACTION") == "TRUE" {
137 // Skip this test on Github Action due to:
138 // Creating a `SymbolicLink` type on Windows requires elevation as administrator
139 // https://github.com/MicrosoftDocs/PowerShell-Docs/pull/3473
140 t.Skip("Skipping test on Github Action")
141 }
142 mounter := Mounter{"fake/path"}
143
144 tests := []struct {
145 fileName string
146 targetLinkName string
147 setUp func(base, fileName, targetLinkName string) error
148 expectedResult bool
149 expectError bool
150 }{
151 {
152 "Dir",
153 "",
154 func(base, fileName, targetLinkName string) error {
155 return os.Mkdir(filepath.Join(base, fileName), 0750)
156 },
157 true,
158 false,
159 },
160 {
161 "InvalidDir",
162 "",
163 func(base, fileName, targetLinkName string) error {
164 return nil
165 },
166 true,
167 true,
168 },
169 {
170 "ValidSymLink",
171 "targetSymLink",
172 func(base, fileName, targetLinkName string) error {
173 targeLinkPath := filepath.Join(base, targetLinkName)
174 if err := os.Mkdir(targeLinkPath, 0750); err != nil {
175 return err
176 }
177
178 filePath := filepath.Join(base, fileName)
179 if err := makeLink(filePath, targeLinkPath); err != nil {
180 return err
181 }
182 return nil
183 },
184 false,
185 false,
186 },
187 {
188 "InvalidSymLink",
189 "targetSymLink2",
190 func(base, fileName, targetLinkName string) error {
191 targeLinkPath := filepath.Join(base, targetLinkName)
192 if err := os.Mkdir(targeLinkPath, 0750); err != nil {
193 return err
194 }
195
196 filePath := filepath.Join(base, fileName)
197 if err := makeLink(filePath, targeLinkPath); err != nil {
198 return err
199 }
200 return removeLink(targeLinkPath)
201 },
202 true,
203 false,
204 },
205 }
206
207 for _, test := range tests {
208 base, err := ioutil.TempDir("", test.fileName)
209 if err != nil {
210 t.Fatal(err.Error())
211 }
212
213 defer os.RemoveAll(base)
214
215 if err := test.setUp(base, test.fileName, test.targetLinkName); err != nil {
216 t.Fatalf("unexpected error in setUp(%s, %s): %v", test.fileName, test.targetLinkName, err)
217 }
218
219 filePath := filepath.Join(base, test.fileName)
220 result, err := mounter.IsLikelyNotMountPoint(filePath)
221 if result != test.expectedResult {
222 t.Fatalf("Expect result not equal with IsLikelyNotMountPoint(%s) return: %v, expected: %v", filePath, result, test.expectedResult)
223 }
224
225 if test.expectError {
226 if err == nil {
227 t.Fatalf("expected error, got none during IsLikelyNotMountPoint(%s)", filePath)
228 }
229 } else {
230 if err != nil {
231 t.Fatalf("unexpected error %v during IsLikelyNotMountPoint(%s)", err, filePath)
232 }
233 }
234 }
235 }
236
237 func TestFormatAndMount(t *testing.T) {
238 tests := []struct {
239 device string
240 target string
241 fstype string
242 execScripts []ExecArgs
243 mountOptions []string
244 expectError bool
245 }{
246 {
247 device: "0",
248 target: "disk",
249 fstype: "NTFS",
250 execScripts: []ExecArgs{
251 {"powershell", []string{"/c", "Get-Disk", "-Number"}, "0", nil},
252 {"powershell", []string{"/c", "Get-Partition", "-DiskNumber"}, "0", nil},
253 {"cmd", []string{"/c", "mklink", "/D"}, "", nil},
254 },
255 mountOptions: []string{},
256 expectError: false,
257 },
258 {
259 device: "0",
260 target: "disk",
261 fstype: "",
262 execScripts: []ExecArgs{
263 {"powershell", []string{"/c", "Get-Disk", "-Number"}, "0", nil},
264 {"powershell", []string{"/c", "Get-Partition", "-DiskNumber"}, "0", nil},
265 {"cmd", []string{"/c", "mklink", "/D"}, "", nil},
266 },
267 mountOptions: []string{},
268 expectError: false,
269 },
270 {
271 device: "invalidDevice",
272 target: "disk",
273 fstype: "NTFS",
274 mountOptions: []string{},
275 expectError: true,
276 },
277 }
278
279 for _, test := range tests {
280 fakeMounter := ErrorMounter{NewFakeMounter(nil), 0, nil}
281 fakeExec := &testingexec.FakeExec{}
282 for _, script := range test.execScripts {
283 fakeCmd := &testingexec.FakeCmd{}
284 cmdAction := makeFakeCmd(fakeCmd, script.command, script.args...)
285 outputAction := makeFakeOutput(script.output, script.err)
286 fakeCmd.CombinedOutputScript = append(fakeCmd.CombinedOutputScript, outputAction)
287 fakeExec.CommandScript = append(fakeExec.CommandScript, cmdAction)
288 }
289 mounter := SafeFormatAndMount{
290 Interface: &fakeMounter,
291 Exec: fakeExec,
292 }
293 base, err := ioutil.TempDir("", test.device)
294 if err != nil {
295 t.Fatal(err.Error())
296 }
297 defer os.RemoveAll(base)
298
299 target := filepath.Join(base, test.target)
300 err = mounter.FormatAndMount(test.device, target, test.fstype, test.mountOptions)
301
302 if test.expectError {
303 if err == nil {
304 t.Fatalf("expected error, got none during FormatAndMount(%s, %s, %s, %v)", test.device, test.target, test.fstype, test.mountOptions)
305 }
306 } else {
307 if err != nil {
308 t.Fatalf("unexpected error %v during FormatAndMount(%s, %s, %s, %v)", err, test.device, test.target, test.fstype, test.mountOptions)
309 }
310 }
311 }
312 }
313
314 func TestNewSMBMapping(t *testing.T) {
315 tests := []struct {
316 username string
317 password string
318 remotepath string
319 expectError bool
320 }{
321 {
322 "",
323 "password",
324 `\\remotepath`,
325 true,
326 },
327 {
328 "username",
329 "",
330 `\\remotepath`,
331 true,
332 },
333 {
334 "username",
335 "password",
336 "",
337 true,
338 },
339 }
340
341 for _, test := range tests {
342 _, err := newSMBMapping(test.username, test.password, test.remotepath)
343 if test.expectError {
344 if err == nil {
345 t.Fatalf("expected error, got none during newSMBMapping(%s, %s, %s)", test.username, test.password, test.remotepath)
346 }
347 } else {
348 if err != nil {
349 t.Fatalf("unexpected error %v during newSMBMapping(%s, %s, %s)", err, test.username, test.password, test.remotepath)
350 }
351 }
352 }
353 }
354
355 func TestIsValidPath(t *testing.T) {
356 tests := []struct {
357 remotepath string
358 expectedResult bool
359 expectError bool
360 }{
361 {
362 "c:",
363 true,
364 false,
365 },
366 {
367 "invalid-path",
368 false,
369 false,
370 },
371 }
372
373 for _, test := range tests {
374 result, err := isValidPath(test.remotepath)
375 if result != test.expectedResult {
376 t.Fatalf("Expect result not equal with isValidPath(%s) return: %v, expected: %v, error: %v", test.remotepath, result, test.expectedResult, err)
377 }
378 if test.expectError {
379 if err == nil {
380 t.Fatalf("expected error, got none during isValidPath(%s)", test.remotepath)
381 }
382 } else {
383 if err != nil {
384 t.Fatalf("unexpected error %v during isValidPath(%s)", err, test.remotepath)
385 }
386 }
387 }
388 }
389
390 func TestIsAccessDeniedError(t *testing.T) {
391 tests := []struct {
392 err error
393 expectedResult bool
394 }{
395 {
396 nil,
397 false,
398 },
399 {
400 fmt.Errorf("other error message"),
401 false,
402 },
403 {
404 fmt.Errorf(`PathValid(\\xxx\share) failed with returned output: Test-Path : Access is denied`),
405 true,
406 },
407 }
408
409 for _, test := range tests {
410 result := isAccessDeniedError(test.err)
411 if result != test.expectedResult {
412 t.Fatalf("Expect result not equal with isAccessDeniedError(%v) return: %v, expected: %v", test.err, result, test.expectedResult)
413 }
414 }
415 }