| 1 |
/* |
| 2 |
Copyright 2018 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package mount |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"io/ioutil" |
| 22 |
"os" |
| 23 |
"path/filepath" |
| 24 |
"runtime" |
| 25 |
"syscall" |
| 26 |
"testing" |
| 27 |
) |
| 28 |
|
| 29 |
func TestDoCleanupMountPoint(t *testing.T) { |
| 30 |
|
| 31 |
if runtime.GOOS == "darwin" { |
| 32 |
t.Skipf("not supported on GOOS=%s", runtime.GOOS) |
| 33 |
} |
| 34 |
|
| 35 |
const testMount = "test-mount" |
| 36 |
const defaultPerm = 0750 |
| 37 |
|
| 38 |
tests := map[string]struct { |
| 39 |
corruptedMnt bool |
| 40 |
// Function that prepares the directory structure for the test under |
| 41 |
// the given base directory. |
| 42 |
// Returns a fake MountPoint, a fake error for the mount point, |
| 43 |
// and error if the prepare function encountered a fatal error. |
| 44 |
prepare func(base string) (MountPoint, error, error) |
| 45 |
expectErr bool |
| 46 |
}{ |
| 47 |
"mount-ok": { |
| 48 |
prepare: func(base string) (MountPoint, error, error) { |
| 49 |
path := filepath.Join(base, testMount) |
| 50 |
if err := os.MkdirAll(path, defaultPerm); err != nil { |
| 51 |
return MountPoint{}, nil, err |
| 52 |
} |
| 53 |
return MountPoint{Device: "/dev/sdb", Path: path}, nil, nil |
| 54 |
}, |
| 55 |
}, |
| 56 |
"mount-corrupted": { |
| 57 |
prepare: func(base string) (MountPoint, error, error) { |
| 58 |
path := filepath.Join(base, testMount) |
| 59 |
if err := os.MkdirAll(path, defaultPerm); err != nil { |
| 60 |
return MountPoint{}, nil, err |
| 61 |
} |
| 62 |
return MountPoint{Device: "/dev/sdb", Path: path}, os.NewSyscallError("fake", syscall.ESTALE), nil |
| 63 |
}, |
| 64 |
corruptedMnt: true, |
| 65 |
}, |
| 66 |
"mount-err-not-corrupted": { |
| 67 |
prepare: func(base string) (MountPoint, error, error) { |
| 68 |
path := filepath.Join(base, testMount) |
| 69 |
if err := os.MkdirAll(path, defaultPerm); err != nil { |
| 70 |
return MountPoint{}, nil, err |
| 71 |
} |
| 72 |
return MountPoint{Device: "/dev/sdb", Path: path}, os.NewSyscallError("fake", syscall.ETIMEDOUT), nil |
| 73 |
}, |
| 74 |
expectErr: true, |
| 75 |
}, |
| 76 |
} |
| 77 |
|
| 78 |
for name, tt := range tests { |
| 79 |
t.Run(name, func(t *testing.T) { |
| 80 |
|
| 81 |
tmpDir, err := ioutil.TempDir("", "unmount-mount-point-test") |
| 82 |
if err != nil { |
| 83 |
t.Fatalf("failed to create tmpdir: %v", err) |
| 84 |
} |
| 85 |
defer os.RemoveAll(tmpDir) |
| 86 |
|
| 87 |
if tt.prepare == nil { |
| 88 |
t.Fatalf("prepare function required") |
| 89 |
} |
| 90 |
|
| 91 |
mountPoint, mountError, err := tt.prepare(tmpDir) |
| 92 |
if err != nil { |
| 93 |
t.Fatalf("failed to prepare test: %v", err) |
| 94 |
} |
| 95 |
|
| 96 |
fake := NewFakeMounter( |
| 97 |
[]MountPoint{mountPoint}, |
| 98 |
) |
| 99 |
fake.MountCheckErrors = map[string]error{mountPoint.Path: mountError} |
| 100 |
|
| 101 |
err = doCleanupMountPoint(mountPoint.Path, fake, true, tt.corruptedMnt) |
| 102 |
if tt.expectErr { |
| 103 |
if err == nil { |
| 104 |
t.Errorf("test %s failed, expected error, got none", name) |
| 105 |
} |
| 106 |
if err := validateDirExists(mountPoint.Path); err != nil { |
| 107 |
t.Errorf("test %s failed, mount path doesn't exist: %v", name, err) |
| 108 |
} |
| 109 |
} |
| 110 |
if !tt.expectErr { |
| 111 |
if err != nil { |
| 112 |
t.Errorf("test %s failed: %v", name, err) |
| 113 |
} |
| 114 |
if err := validateDirNotExists(mountPoint.Path); err != nil { |
| 115 |
t.Errorf("test %s failed, mount path still exists: %v", name, err) |
| 116 |
} |
| 117 |
} |
| 118 |
}) |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
func validateDirExists(dir string) error { |
| 123 |
_, err := ioutil.ReadDir(dir) |
| 124 |
if err != nil { |
| 125 |
return err |
| 126 |
} |
| 127 |
return nil |
| 128 |
} |
| 129 |
|
| 130 |
func validateDirNotExists(dir string) error { |
| 131 |
_, err := ioutil.ReadDir(dir) |
| 132 |
if os.IsNotExist(err) { |
| 133 |
return nil |
| 134 |
} |
| 135 |
if err != nil { |
| 136 |
return err |
| 137 |
} |
| 138 |
return fmt.Errorf("dir %q still exists", dir) |
| 139 |
} |