| 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 |
"os" |
| 24 |
"os/exec" |
| 25 |
"path/filepath" |
| 26 |
"testing" |
| 27 |
|
| 28 |
"github.com/stretchr/testify/assert" |
| 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 |
assert.Equal(t, result, test.expectedResult, "Expect result not equal with PathWithinBase(%s, %s) return: %q, expected: %q", |
| 130 |
test.fullPath, test.basePath, result, test.expectedResult) |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
func TestIsLikelyNotMountPoint(t *testing.T) { |
| 135 |
mounter := Mounter{"fake/path"} |
| 136 |
|
| 137 |
tests := []struct { |
| 138 |
fileName string |
| 139 |
targetLinkName string |
| 140 |
setUp func(base, fileName, targetLinkName string) error |
| 141 |
expectedResult bool |
| 142 |
expectError bool |
| 143 |
}{ |
| 144 |
{ |
| 145 |
"Dir", |
| 146 |
"", |
| 147 |
func(base, fileName, targetLinkName string) error { |
| 148 |
return os.Mkdir(filepath.Join(base, fileName), 0o750) |
| 149 |
}, |
| 150 |
true, |
| 151 |
false, |
| 152 |
}, |
| 153 |
{ |
| 154 |
"InvalidDir", |
| 155 |
"", |
| 156 |
func(base, fileName, targetLinkName string) error { |
| 157 |
return nil |
| 158 |
}, |
| 159 |
true, |
| 160 |
true, |
| 161 |
}, |
| 162 |
{ |
| 163 |
"ValidSymLink", |
| 164 |
"targetSymLink", |
| 165 |
func(base, fileName, targetLinkName string) error { |
| 166 |
targeLinkPath := filepath.Join(base, targetLinkName) |
| 167 |
if err := os.Mkdir(targeLinkPath, 0o750); err != nil { |
| 168 |
return err |
| 169 |
} |
| 170 |
|
| 171 |
filePath := filepath.Join(base, fileName) |
| 172 |
if err := makeLink(filePath, targeLinkPath); err != nil { |
| 173 |
return err |
| 174 |
} |
| 175 |
return nil |
| 176 |
}, |
| 177 |
false, |
| 178 |
false, |
| 179 |
}, |
| 180 |
{ |
| 181 |
"InvalidSymLink", |
| 182 |
"targetSymLink2", |
| 183 |
func(base, fileName, targetLinkName string) error { |
| 184 |
targeLinkPath := filepath.Join(base, targetLinkName) |
| 185 |
if err := os.Mkdir(targeLinkPath, 0o750); err != nil { |
| 186 |
return err |
| 187 |
} |
| 188 |
|
| 189 |
filePath := filepath.Join(base, fileName) |
| 190 |
if err := makeLink(filePath, targeLinkPath); err != nil { |
| 191 |
return err |
| 192 |
} |
| 193 |
return removeLink(targeLinkPath) |
| 194 |
}, |
| 195 |
false, |
| 196 |
false, |
| 197 |
}, |
| 198 |
{ |
| 199 |
"junction", |
| 200 |
"targetDir", |
| 201 |
func(base, fileName, targetLinkName string) error { |
| 202 |
target := filepath.Join(base, targetLinkName) |
| 203 |
if err := os.Mkdir(target, 0o750); err != nil { |
| 204 |
return err |
| 205 |
} |
| 206 |
|
| 207 |
// create a Junction file type on Windows |
| 208 |
junction := filepath.Join(base, fileName) |
| 209 |
if output, err := exec.Command("cmd", "/c", "mklink", "/J", junction, target).CombinedOutput(); err != nil { |
| 210 |
return fmt.Errorf("mklink failed: %v, link(%q) target(%q) output: %q", err, junction, target, string(output)) |
| 211 |
} |
| 212 |
return nil |
| 213 |
}, |
| 214 |
false, |
| 215 |
false, |
| 216 |
}, |
| 217 |
} |
| 218 |
|
| 219 |
for _, test := range tests { |
| 220 |
base := t.TempDir() |
| 221 |
|
| 222 |
if err := test.setUp(base, test.fileName, test.targetLinkName); err != nil { |
| 223 |
t.Fatalf("unexpected error in setUp(%s, %s): %v", test.fileName, test.targetLinkName, err) |
| 224 |
} |
| 225 |
|
| 226 |
filePath := filepath.Join(base, test.fileName) |
| 227 |
result, err := mounter.IsLikelyNotMountPoint(filePath) |
| 228 |
assert.Equal(t, test.expectedResult, result, "Expect result not equal with IsLikelyNotMountPoint(%s) return: %q, expected: %q", |
| 229 |
filePath, result, test.expectedResult) |
| 230 |
|
| 231 |
if test.expectError { |
| 232 |
assert.NotNil(t, err, "Expect error during IsLikelyNotMountPoint(%s)", filePath) |
| 233 |
} else { |
| 234 |
assert.Nil(t, err, "Expect error is nil during IsLikelyNotMountPoint(%s)", filePath) |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
func TestFormatAndMount(t *testing.T) { |
| 240 |
tests := []struct { |
| 241 |
device string |
| 242 |
target string |
| 243 |
fstype string |
| 244 |
execScripts []ExecArgs |
| 245 |
mountOptions []string |
| 246 |
expectError bool |
| 247 |
}{ |
| 248 |
{ |
| 249 |
device: "0", |
| 250 |
target: "disk", |
| 251 |
fstype: "NTFS", |
| 252 |
execScripts: []ExecArgs{ |
| 253 |
{"powershell", []string{"/c", "Get-Disk", "-Number"}, "0", nil}, |
| 254 |
{"powershell", []string{"/c", "Get-Partition", "-DiskNumber"}, "0", nil}, |
| 255 |
{"cmd", []string{"/c", "mklink", "/D"}, "", nil}, |
| 256 |
}, |
| 257 |
mountOptions: []string{}, |
| 258 |
expectError: false, |
| 259 |
}, |
| 260 |
{ |
| 261 |
device: "0", |
| 262 |
target: "disk", |
| 263 |
fstype: "", |
| 264 |
execScripts: []ExecArgs{ |
| 265 |
{"powershell", []string{"/c", "Get-Disk", "-Number"}, "0", nil}, |
| 266 |
{"powershell", []string{"/c", "Get-Partition", "-DiskNumber"}, "0", nil}, |
| 267 |
{"cmd", []string{"/c", "mklink", "/D"}, "", nil}, |
| 268 |
}, |
| 269 |
mountOptions: []string{}, |
| 270 |
expectError: false, |
| 271 |
}, |
| 272 |
{ |
| 273 |
device: "invalidDevice", |
| 274 |
target: "disk", |
| 275 |
fstype: "NTFS", |
| 276 |
mountOptions: []string{}, |
| 277 |
expectError: true, |
| 278 |
}, |
| 279 |
} |
| 280 |
|
| 281 |
for _, test := range tests { |
| 282 |
fakeMounter := ErrorMounter{NewFakeMounter(nil), 0, nil} |
| 283 |
fakeExec := &testingexec.FakeExec{} |
| 284 |
for _, script := range test.execScripts { |
| 285 |
fakeCmd := &testingexec.FakeCmd{} |
| 286 |
cmdAction := makeFakeCmd(fakeCmd, script.command, script.args...) |
| 287 |
outputAction := makeFakeOutput(script.output, script.err) |
| 288 |
fakeCmd.CombinedOutputScript = append(fakeCmd.CombinedOutputScript, outputAction) |
| 289 |
fakeExec.CommandScript = append(fakeExec.CommandScript, cmdAction) |
| 290 |
} |
| 291 |
mounter := SafeFormatAndMount{ |
| 292 |
Interface: &fakeMounter, |
| 293 |
Exec: fakeExec, |
| 294 |
} |
| 295 |
target := filepath.Join(t.TempDir(), test.target) |
| 296 |
err := mounter.FormatAndMount(test.device, target, test.fstype, test.mountOptions) |
| 297 |
if test.expectError { |
| 298 |
assert.NotNil(t, err, "Expect error during FormatAndMount(%s, %s, %s, %v)", test.device, test.target, test.fstype, test.mountOptions) |
| 299 |
} else { |
| 300 |
assert.Nil(t, err, "Expect error is nil during FormatAndMount(%s, %s, %s, %v)", test.device, test.target, test.fstype, test.mountOptions) |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
func TestNewSMBMapping(t *testing.T) { |
| 306 |
tests := []struct { |
| 307 |
username string |
| 308 |
password string |
| 309 |
remotepath string |
| 310 |
expectError bool |
| 311 |
}{ |
| 312 |
{ |
| 313 |
"", |
| 314 |
"password", |
| 315 |
`\\remotepath`, |
| 316 |
true, |
| 317 |
}, |
| 318 |
{ |
| 319 |
"username", |
| 320 |
"", |
| 321 |
`\\remotepath`, |
| 322 |
true, |
| 323 |
}, |
| 324 |
{ |
| 325 |
"username", |
| 326 |
"password", |
| 327 |
"", |
| 328 |
true, |
| 329 |
}, |
| 330 |
} |
| 331 |
|
| 332 |
for _, test := range tests { |
| 333 |
_, err := newSMBMapping(test.username, test.password, test.remotepath) |
| 334 |
if test.expectError { |
| 335 |
assert.NotNil(t, err, "Expect error during newSMBMapping(%s, %s, %s, %v)", test.username, test.password, test.remotepath) |
| 336 |
} else { |
| 337 |
assert.Nil(t, err, "Expect error is nil during newSMBMapping(%s, %s, %s, %v)", test.username, test.password, test.remotepath) |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
func TestIsValidPath(t *testing.T) { |
| 343 |
tests := []struct { |
| 344 |
remotepath string |
| 345 |
expectedResult bool |
| 346 |
expectError bool |
| 347 |
}{ |
| 348 |
{ |
| 349 |
"c:", |
| 350 |
true, |
| 351 |
false, |
| 352 |
}, |
| 353 |
{ |
| 354 |
"invalid-path", |
| 355 |
false, |
| 356 |
false, |
| 357 |
}, |
| 358 |
} |
| 359 |
|
| 360 |
for _, test := range tests { |
| 361 |
result, err := isValidPath(test.remotepath) |
| 362 |
assert.Equal(t, result, test.expectedResult, "Expect result not equal with isValidPath(%s) return: %q, expected: %q, error: %v", |
| 363 |
test.remotepath, result, test.expectedResult, err) |
| 364 |
if test.expectError { |
| 365 |
assert.NotNil(t, err, "Expect error during isValidPath(%s)", test.remotepath) |
| 366 |
} else { |
| 367 |
assert.Nil(t, err, "Expect error is nil during isValidPath(%s)", test.remotepath) |
| 368 |
} |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
func TestIsAccessDeniedError(t *testing.T) { |
| 373 |
tests := []struct { |
| 374 |
err error |
| 375 |
expectedResult bool |
| 376 |
}{ |
| 377 |
{ |
| 378 |
nil, |
| 379 |
false, |
| 380 |
}, |
| 381 |
{ |
| 382 |
fmt.Errorf("other error message"), |
| 383 |
false, |
| 384 |
}, |
| 385 |
{ |
| 386 |
fmt.Errorf(`PathValid(\\xxx\share) failed with returned output: Test-Path : Access is denied`), |
| 387 |
true, |
| 388 |
}, |
| 389 |
} |
| 390 |
|
| 391 |
for _, test := range tests { |
| 392 |
result := isAccessDeniedError(test.err) |
| 393 |
assert.Equal(t, result, test.expectedResult, "Expect result not equal with isAccessDeniedError(%v) return: %q, expected: %q", |
| 394 |
test.err, result, test.expectedResult) |
| 395 |
} |
| 396 |
} |