Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 73ded98

Browse filesBrowse files
committed
add UT for volumepathhandler
Signed-off-by: liyuerich <yue.li@daocloud.io>
1 parent 9ec52fc commit 73ded98
Copy full SHA for 73ded98

File tree

Expand file treeCollapse file tree

1 file changed

+308
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+308
-0
lines changed
+308Lines changed: 308 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,308 @@
1+
/*
2+
Copyright 2024 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 volumepathhandler
18+
19+
import (
20+
"fmt"
21+
"os"
22+
"path/filepath"
23+
"runtime"
24+
"testing"
25+
)
26+
27+
func TestMapDevice(t *testing.T) {
28+
tempDir := t.TempDir()
29+
testCases := []struct {
30+
name string
31+
devicePath string
32+
mapPath string
33+
linkName string
34+
bindMount bool
35+
expectError string
36+
}{
37+
{
38+
name: "valid symlink map",
39+
devicePath: "/dev/fakeDevice",
40+
mapPath: filepath.Join(tempDir, "test-map-valid"),
41+
linkName: "validLink",
42+
bindMount: false,
43+
expectError: "",
44+
},
45+
{
46+
name: "empty device path",
47+
devicePath: "",
48+
mapPath: filepath.Join(tempDir, "test-map-empty-device"),
49+
linkName: "emptyDeviceLink",
50+
bindMount: false,
51+
expectError: "failed to map device to map path. devicePath is empty",
52+
},
53+
{
54+
name: "empty map path",
55+
devicePath: "/dev/fakeDevice",
56+
mapPath: "",
57+
linkName: "emptyMapLink",
58+
bindMount: false,
59+
expectError: "failed to map device to map path. mapPath is empty",
60+
},
61+
}
62+
63+
for _, tc := range testCases {
64+
t.Run(tc.name, func(t *testing.T) {
65+
if tc.mapPath != "" {
66+
if err := os.MkdirAll(tc.mapPath, 0750); err != nil {
67+
t.Fatalf("failed to create mapPath: %v", err)
68+
}
69+
}
70+
handler := VolumePathHandler{}
71+
err := handler.MapDevice(tc.devicePath, tc.mapPath, tc.linkName, tc.bindMount)
72+
73+
if tc.expectError != "" {
74+
if err == nil || err.Error() != tc.expectError {
75+
t.Fatalf("expected error: %v, got: %v", tc.expectError, err)
76+
}
77+
} else {
78+
if err != nil {
79+
t.Fatalf("expected no error, got: %v", err)
80+
}
81+
linkPath := filepath.Join(tc.mapPath, tc.linkName)
82+
if _, err := os.Lstat(linkPath); os.IsNotExist(err) {
83+
t.Fatalf("expected symlink to exist, got error: %v", err)
84+
}
85+
}
86+
})
87+
}
88+
}
89+
90+
func TestUnmapDevice(t *testing.T) {
91+
tempDir := t.TempDir()
92+
testCases := []struct {
93+
name string
94+
mapPath string
95+
linkName string
96+
bindMount bool
97+
setup func(string, string)
98+
expectError string
99+
}{
100+
{
101+
name: "valid symlink unmap",
102+
mapPath: filepath.Join(tempDir, "test-unmap-valid"),
103+
linkName: "validLink",
104+
bindMount: false,
105+
setup: func(mapPath, linkName string) {
106+
if err := os.MkdirAll(mapPath, 0750); err != nil {
107+
t.Fatalf("failed to create mapPath in setup: %v", err)
108+
}
109+
if err := os.Symlink("/dev/fakeDevice", filepath.Join(mapPath, linkName)); err != nil {
110+
t.Fatalf("failed to create symlink in setup: %v", err)
111+
}
112+
},
113+
expectError: "",
114+
},
115+
{
116+
name: "symlink does not exist",
117+
mapPath: filepath.Join(tempDir, "test-unmap-nonexistent"),
118+
linkName: "nonexistentLink",
119+
bindMount: false,
120+
setup: func(mapPath, linkName string) {
121+
if err := os.MkdirAll(mapPath, 0750); err != nil {
122+
t.Fatalf("failed to create mapPath in setup: %v", err)
123+
}
124+
},
125+
expectError: "",
126+
},
127+
}
128+
129+
for _, tc := range testCases {
130+
t.Run(tc.name, func(t *testing.T) {
131+
handler := VolumePathHandler{}
132+
if tc.setup != nil {
133+
tc.setup(tc.mapPath, tc.linkName)
134+
}
135+
136+
err := handler.UnmapDevice(tc.mapPath, tc.linkName, tc.bindMount)
137+
if tc.expectError != "" {
138+
if err == nil || err.Error() != tc.expectError {
139+
t.Fatalf("expected error: %v, got: %v", tc.expectError, err)
140+
}
141+
} else {
142+
if err != nil {
143+
t.Fatalf("expected no error, got: %v", err)
144+
}
145+
linkPath := filepath.Join(tc.mapPath, tc.linkName)
146+
if _, err := os.Lstat(linkPath); !os.IsNotExist(err) {
147+
t.Fatalf("expected symlink to be removed, got error: %v", err)
148+
}
149+
}
150+
})
151+
}
152+
}
153+
154+
func TestRemoveMapPath(t *testing.T) {
155+
tempDir := t.TempDir()
156+
mapPath := filepath.Join(tempDir, "test-remove-map-path")
157+
158+
if err := os.MkdirAll(mapPath, 0755); err != nil {
159+
t.Fatalf("failed to create map path: %v", err)
160+
}
161+
162+
testCases := []struct {
163+
name string
164+
mapPath string
165+
expectError string
166+
}{
167+
{
168+
name: "Remove Existing Map Path",
169+
mapPath: mapPath,
170+
expectError: "",
171+
},
172+
{
173+
name: "Remove Non-existing Map Path",
174+
mapPath: filepath.Join(tempDir, "non-existing-path"),
175+
expectError: "",
176+
},
177+
}
178+
179+
for _, tc := range testCases {
180+
t.Run(tc.name, func(t *testing.T) {
181+
handler := VolumePathHandler{}
182+
err := handler.RemoveMapPath(tc.mapPath)
183+
if tc.expectError != "" {
184+
if err == nil || err.Error() != tc.expectError {
185+
t.Fatalf("expected error: %v, got: %v", tc.expectError, err)
186+
}
187+
} else {
188+
if err != nil {
189+
t.Fatalf("expected no error, got: %v", err)
190+
}
191+
if _, err := os.Stat(tc.mapPath); !os.IsNotExist(err) {
192+
t.Fatalf("expected map path to be deleted, got error: %v", err)
193+
}
194+
}
195+
})
196+
}
197+
}
198+
199+
func TestIsSymlinkExist(t *testing.T) {
200+
tempDir := t.TempDir()
201+
mapPath := filepath.Join(tempDir, "test-symlink")
202+
linkName := "test-symlink-link"
203+
204+
if err := os.MkdirAll(mapPath, 0755); err != nil {
205+
t.Fatalf("failed to create test directory: %v", err)
206+
}
207+
208+
if err := os.Symlink("/dev/fakeDevice", filepath.Join(mapPath, linkName)); err != nil {
209+
t.Fatalf("failed to create symlink: %v", err)
210+
}
211+
212+
testCases := []struct {
213+
name string
214+
mapPath string
215+
expectedExists bool
216+
}{
217+
{
218+
name: "Check Existing Symlink",
219+
mapPath: filepath.Join(mapPath, linkName),
220+
expectedExists: true,
221+
},
222+
{
223+
name: "Check Non-existing Symlink",
224+
mapPath: filepath.Join(mapPath, "non-existing-symlink"),
225+
expectedExists: false,
226+
},
227+
}
228+
229+
for _, tc := range testCases {
230+
t.Run(tc.name, func(t *testing.T) {
231+
exists, err := VolumePathHandler{}.IsSymlinkExist(tc.mapPath)
232+
if err != nil {
233+
t.Fatalf("expected no error, got: %v", err)
234+
}
235+
if exists != tc.expectedExists {
236+
t.Fatalf("expected symlink existence to be: %v, got: %v", tc.expectedExists, exists)
237+
}
238+
})
239+
}
240+
}
241+
242+
func checkMountSupport() error {
243+
// Check if the operating system supports mounting
244+
if runtime.GOOS != "linux" {
245+
return fmt.Errorf("mount is not supported on %s", runtime.GOOS)
246+
}
247+
return nil
248+
}
249+
250+
func TestIsDeviceBindMountExist(t *testing.T) {
251+
tempDir := t.TempDir()
252+
mapPath := filepath.Join(tempDir, "test-bind-mount")
253+
linkName := "test-bind-mount-link"
254+
devicePath := filepath.Join(tempDir, "fakeDevice")
255+
256+
if err := os.MkdirAll(mapPath, 0755); err != nil {
257+
t.Fatalf("failed to create test directory: %v", err)
258+
}
259+
260+
if err := checkMountSupport(); err != nil {
261+
t.Skipf("skipping test because mount is not supported: %v", err)
262+
}
263+
264+
if _, err := os.Stat(devicePath); os.IsNotExist(err) {
265+
if err := os.WriteFile(devicePath, []byte{}, 0644); err != nil {
266+
t.Fatalf("failed to create fake device: %v", err)
267+
}
268+
defer func() {
269+
if err := os.Remove(devicePath); err != nil {
270+
t.Fatalf("failed to remove fake device: %v", err)
271+
}
272+
}()
273+
}
274+
275+
err := VolumePathHandler{}.MapDevice(devicePath, mapPath, linkName, true)
276+
if err != nil {
277+
t.Fatalf("failed to create bind mount: %v", err)
278+
}
279+
280+
testCases := []struct {
281+
name string
282+
mapPath string
283+
expectedExists bool
284+
}{
285+
{
286+
name: "Check Existing Bind Mount",
287+
mapPath: filepath.Join(mapPath, linkName),
288+
expectedExists: true,
289+
},
290+
{
291+
name: "Check Non-existing Bind Mount",
292+
mapPath: filepath.Join(mapPath, "non-existing-bind-mount"),
293+
expectedExists: false,
294+
},
295+
}
296+
297+
for _, tc := range testCases {
298+
t.Run(tc.name, func(t *testing.T) {
299+
exists, err := VolumePathHandler{}.IsDeviceBindMountExist(tc.mapPath)
300+
if err != nil {
301+
t.Fatalf("expected no error, got: %v", err)
302+
}
303+
if exists != tc.expectedExists {
304+
t.Fatalf("expected bind mount existence to be: %v, got: %v", tc.expectedExists, exists)
305+
}
306+
})
307+
}
308+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.