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