1
16
17 package removeall
18
19 import (
20 "errors"
21 "os"
22 "path/filepath"
23 "strings"
24 "testing"
25
26 utiltesting "k8s.io/client-go/util/testing"
27 "k8s.io/mount-utils"
28 )
29
30 type fakeMounter struct {
31 mount.FakeMounter
32 }
33
34
35 func (f *fakeMounter) IsLikelyNotMountPoint(file string) (bool, error) {
36 name := filepath.Base(file)
37 if strings.HasPrefix(name, "mount") {
38 return false, nil
39 }
40 if strings.HasPrefix(name, "err") {
41 return false, errors.New("mock error")
42 }
43 return true, nil
44 }
45
46 func TestRemoveAllOneFilesystem(t *testing.T) {
47 tests := []struct {
48 name string
49
50
51
52
53 items []string
54 expectError bool
55 }{
56 {
57 "empty dir",
58 []string{},
59 false,
60 },
61 {
62 "non-mount",
63 []string{
64 "dir/",
65 "dir/file",
66 "dir2/",
67 "file2",
68 },
69 false,
70 },
71 {
72 "mount",
73 []string{
74 "dir/",
75 "dir/file",
76 "dir2/",
77 "file2",
78 "mount/",
79 "mount/file3",
80 },
81 true,
82 },
83 {
84 "innermount",
85 []string{
86 "dir/",
87 "dir/file",
88 "dir/dir2/",
89 "dir/dir2/file2",
90 "dir/dir2/mount/",
91 "dir/dir2/mount/file3",
92 },
93 true,
94 },
95 {
96 "error",
97 []string{
98 "dir/",
99 "dir/file",
100 "dir2/",
101 "file2",
102 "err/",
103 "err/file3",
104 },
105 true,
106 },
107 }
108
109 for _, test := range tests {
110 tmpDir, err := utiltesting.MkTmpdir("removeall-" + test.name + "-")
111 if err != nil {
112 t.Fatalf("Can't make a tmp dir: %v", err)
113 }
114 defer os.RemoveAll(tmpDir)
115
116 for _, item := range test.items {
117 if strings.HasSuffix(item, "/") {
118 item = strings.TrimRight(item, "/")
119 if err = os.Mkdir(filepath.Join(tmpDir, item), 0777); err != nil {
120 t.Fatalf("error creating %s: %v", item, err)
121 }
122 } else {
123 f, err := os.Create(filepath.Join(tmpDir, item))
124 if err != nil {
125 t.Fatalf("error creating %s: %v", item, err)
126 }
127 f.Close()
128 }
129 }
130
131 mounter := &fakeMounter{}
132 err = RemoveAllOneFilesystem(mounter, tmpDir)
133 if err == nil && test.expectError {
134 t.Errorf("test %q failed: expected error and got none", test.name)
135 }
136 if err != nil && !test.expectError {
137 t.Errorf("test %q failed: unexpected error: %v", test.name, err)
138 }
139 }
140 }
141
142 func TestRemoveDirsOneFilesystem(t *testing.T) {
143 tests := []struct {
144 name string
145
146
147
148
149 items []string
150 expectError bool
151 }{
152 {
153 "empty dir",
154 []string{},
155 false,
156 },
157 {
158 "non-mount-no-files",
159 []string{
160 "dir/",
161 "dir/subdir1/",
162 "dir2/",
163 "dir2/subdir2/",
164 "dir2/subdir2/subdir3/",
165 "dir3/",
166 },
167 false,
168 },
169 {
170 "non-mount-with-files",
171 []string{
172 "dir/",
173 "dir/file",
174 "dir2/",
175 "file2",
176 },
177 true,
178 },
179 {
180 "mount-no-files",
181 []string{
182 "dir/",
183 "dir/subdir1/",
184 "dir2/",
185 "dir2/subdir2/",
186 "dir2/subdir2/subdir3/",
187 "mount/",
188 "mount/dir3/",
189 },
190 true,
191 },
192 {
193 "mount-with-files",
194 []string{
195 "dir/",
196 "dir/file",
197 "dir2/",
198 "file2",
199 "mount/",
200 "mount/file3",
201 },
202 true,
203 },
204 {
205 "innermount",
206 []string{
207 "dir/",
208 "dir/subdir1/",
209 "dir/dir2/",
210 "dir/dir2/subdir2/",
211 "dir/dir2/mount/",
212 "dir/dir2/mount/subdir3/",
213 },
214 true,
215 },
216 {
217 "error",
218 []string{
219 "dir/",
220 "dir/subdir1/",
221 "dir2/",
222 "err/",
223 "err/subdir3/",
224 },
225 true,
226 },
227 }
228
229 for _, test := range tests {
230 tmpDir, err := utiltesting.MkTmpdir("removeall-" + test.name + "-")
231 if err != nil {
232 t.Fatalf("Can't make a tmp dir: %v", err)
233 }
234 defer os.RemoveAll(tmpDir)
235
236 for _, item := range test.items {
237 if strings.HasSuffix(item, "/") {
238 item = strings.TrimRight(item, "/")
239 if err = os.Mkdir(filepath.Join(tmpDir, item), 0777); err != nil {
240 t.Fatalf("error creating %s: %v", item, err)
241 }
242 } else {
243 f, err := os.Create(filepath.Join(tmpDir, item))
244 if err != nil {
245 t.Fatalf("error creating %s: %v", item, err)
246 }
247 f.Close()
248 }
249 }
250
251 mounter := &fakeMounter{}
252 err = RemoveDirsOneFilesystem(mounter, tmpDir)
253 if err == nil && test.expectError {
254 t.Errorf("test %q failed: expected error and got none", test.name)
255 }
256 if err != nil && !test.expectError {
257 t.Errorf("test %q failed: unexpected error: %v", test.name, err)
258 }
259 }
260 }
261
View as plain text