Debian Codegraph @

mount_helper_test.go

raw
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 "os"
22 "path/filepath"
23 "runtime"
24 "syscall"
25 "testing"
26 )
27
28 func TestDoCleanupMountPoint(t *testing.T) {
29 if runtime.GOOS == "darwin" {
30 t.Skipf("not supported on GOOS=%s", runtime.GOOS)
31 }
32
33 const testMount = "test-mount"
34 const defaultPerm = 0o750
35
36 tests := map[string]struct {
37 corruptedMnt bool
38 // Function that prepares the directory structure for the test under
39 // the given base directory.
40 // Returns a fake MountPoint, a fake error for the mount point,
41 // and error if the prepare function encountered a fatal error.
42 prepareMnt func(base string) (MountPoint, error, error)
43 // Function that prepares the FakeMounter for the test.
44 prepareMntr func(mntr *FakeMounter)
45 expectErr bool
46 }{
47 "mount-ok": {
48 prepareMnt: 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 corruptedMnt: false,
56 expectErr: false,
57 },
58 "path-not-exist": {
59 prepareMnt: func(base string) (MountPoint, error, error) {
60 path := filepath.Join(base, testMount)
61 return MountPoint{Device: "/dev/sdb", Path: path}, nil, nil
62 },
63 corruptedMnt: false,
64 expectErr: false,
65 },
66 "mount-corrupted": {
67 prepareMnt: 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.ESTALE), nil
73 },
74 corruptedMnt: true,
75 expectErr: false,
76 },
77 "mount-err-not-corrupted": {
78 prepareMnt: func(base string) (MountPoint, error, error) {
79 path := filepath.Join(base, testMount)
80 if err := os.MkdirAll(path, defaultPerm); err != nil {
81 return MountPoint{}, nil, err
82 }
83 return MountPoint{Device: "/dev/sdb", Path: path}, os.NewSyscallError("fake", syscall.ETIMEDOUT), nil
84 },
85 corruptedMnt: false,
86 expectErr: true,
87 },
88 "skip-mount-point-check": {
89 prepareMnt: func(base string) (MountPoint, error, error) {
90 path := filepath.Join(base, testMount)
91 if err := os.MkdirAll(path, defaultPerm); err != nil {
92 return MountPoint{Device: "/dev/sdb", Path: path}, nil, err
93 }
94 return MountPoint{Device: "/dev/sdb", Path: path}, nil, nil
95 },
96 prepareMntr: func(mntr *FakeMounter) {
97 mntr.WithSkipMountPointCheck()
98 },
99 expectErr: false,
100 },
101 }
102
103 for name, tt := range tests {
104 t.Run(name, func(t *testing.T) {
105 tmpDir := t.TempDir()
106
107 if tt.prepareMnt == nil {
108 t.Fatalf("prepareMnt function required")
109 }
110
111 mountPoint, mountError, err := tt.prepareMnt(tmpDir)
112 if err != nil {
113 t.Fatalf("failed to prepareMnt for test: %v", err)
114 }
115
116 fake := NewFakeMounter(
117 []MountPoint{mountPoint},
118 )
119 fake.MountCheckErrors = map[string]error{mountPoint.Path: mountError}
120 if tt.prepareMntr != nil {
121 tt.prepareMntr(fake)
122 }
123
124 err = doCleanupMountPoint(mountPoint.Path, fake, true, tt.corruptedMnt)
125 if tt.expectErr {
126 if err == nil {
127 t.Errorf("test %s failed, expected error, got none", name)
128 }
129 if err := validateDirExists(mountPoint.Path); err != nil {
130 t.Errorf("test %s failed, mount path doesn't exist: %v", name, err)
131 }
132 }
133 if !tt.expectErr {
134 if err != nil {
135 t.Errorf("test %s failed: %v", name, err)
136 }
137 if err := validateDirNotExists(mountPoint.Path); err != nil {
138 t.Errorf("test %s failed, mount path still exists: %v", name, err)
139 }
140 }
141 })
142 }
143 }
144
145 func validateDirExists(dir string) error {
146 _, err := os.ReadDir(dir)
147 return err
148 }
149
150 func validateDirNotExists(dir string) error {
151 _, err := os.ReadDir(dir)
152 if os.IsNotExist(err) {
153 return nil
154 }
155 if err != nil {
156 return err
157 }
158 return fmt.Errorf("dir %q still exists", dir)
159 }