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 d17a54b

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

File tree

Expand file treeCollapse file tree

1 file changed

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

1 file changed

+309
-0
lines changed
+309Lines changed: 309 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,309 @@
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 := os.TempDir()
29+
testCases := []struct {
30+
name string
31+
devicePath string
32+
mapPath string
33+
linkName string
34+
bindMount bool
35+
expectError bool
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: false,
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: true,
52+
},
53+
{
54+
name: "empty map path",
55+
devicePath: "/dev/fakeDevice",
56+
mapPath: "",
57+
linkName: "emptyMapLink",
58+
bindMount: false,
59+
expectError: true,
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+
defer func() {
70+
if err := os.RemoveAll(tc.mapPath); err != nil {
71+
t.Fatalf("failed to remove mapPath: %v", err)
72+
}
73+
}()
74+
}
75+
handler := VolumePathHandler{}
76+
err := handler.MapDevice(tc.devicePath, tc.mapPath, tc.linkName, tc.bindMount)
77+
if (err != nil) != tc.expectError {
78+
t.Fatalf("expected error: %v, got: %v", tc.expectError, err)
79+
}
80+
if !tc.expectError {
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 := os.TempDir()
92+
testCases := []struct {
93+
name string
94+
mapPath string
95+
linkName string
96+
bindMount bool
97+
setup func(string, string)
98+
expectError bool
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: false,
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: false,
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+
defer func() {
136+
if err := os.RemoveAll(tc.mapPath); err != nil {
137+
t.Fatalf("failed to remove mapPath: %v", err)
138+
}
139+
}()
140+
141+
err := handler.UnmapDevice(tc.mapPath, tc.linkName, tc.bindMount)
142+
if (err != nil) != tc.expectError {
143+
t.Fatalf("expected error: %v, got: %v", tc.expectError, err)
144+
}
145+
if !tc.expectError {
146+
linkPath := filepath.Join(tc.mapPath, tc.linkName)
147+
if _, err := os.Lstat(linkPath); !os.IsNotExist(err) {
148+
t.Fatalf("expected symlink to be removed, got error: %v", err)
149+
}
150+
}
151+
})
152+
}
153+
}
154+
155+
func TestRemoveMapPath(t *testing.T) {
156+
tempDir := os.TempDir()
157+
mapPath := filepath.Join(tempDir, "test-remove-map-path")
158+
159+
if err := os.MkdirAll(mapPath, 0755); err != nil {
160+
t.Fatalf("failed to create map path: %v", err)
161+
}
162+
163+
testCases := []struct {
164+
name string
165+
mapPath string
166+
expectError bool
167+
}{
168+
{
169+
name: "Remove Existing Map Path",
170+
mapPath: mapPath,
171+
expectError: false,
172+
},
173+
{
174+
name: "Remove Non-existing Map Path",
175+
mapPath: filepath.Join(tempDir, "non-existing-path"),
176+
expectError: false,
177+
},
178+
}
179+
180+
for _, tc := range testCases {
181+
t.Run(tc.name, func(t *testing.T) {
182+
handler := VolumePathHandler{}
183+
err := handler.RemoveMapPath(tc.mapPath)
184+
if (err != nil) != tc.expectError {
185+
t.Fatalf("expected error: %v, got: %v", tc.expectError, err)
186+
}
187+
if !tc.expectError {
188+
if _, err := os.Stat(tc.mapPath); !os.IsNotExist(err) {
189+
t.Fatalf("expected map path to be deleted, got error: %v", err)
190+
}
191+
}
192+
})
193+
}
194+
}
195+
196+
func TestIsSymlinkExist(t *testing.T) {
197+
tempDir := os.TempDir()
198+
mapPath := filepath.Join(tempDir, "test-symlink")
199+
linkName := "test-symlink-link"
200+
201+
if err := os.MkdirAll(mapPath, 0755); err != nil {
202+
t.Fatalf("failed to create test directory: %v", err)
203+
}
204+
205+
defer func() {
206+
if err := os.RemoveAll(mapPath); err != nil {
207+
t.Fatalf("failed to remove mapPath: %v", err)
208+
}
209+
}()
210+
211+
if err := os.Symlink("/dev/fakeDevice", filepath.Join(mapPath, linkName)); err != nil {
212+
t.Fatalf("failed to create symlink: %v", err)
213+
}
214+
215+
testCases := []struct {
216+
name string
217+
mapPath string
218+
expectedExists bool
219+
}{
220+
{
221+
name: "Check Existing Symlink",
222+
mapPath: filepath.Join(mapPath, linkName),
223+
expectedExists: true,
224+
},
225+
{
226+
name: "Check Non-existing Symlink",
227+
mapPath: filepath.Join(mapPath, "non-existing-symlink"),
228+
expectedExists: false,
229+
},
230+
}
231+
232+
for _, tc := range testCases {
233+
t.Run(tc.name, func(t *testing.T) {
234+
exists, err := VolumePathHandler{}.IsSymlinkExist(tc.mapPath)
235+
if err != nil {
236+
t.Fatalf("expected no error, got: %v", err)
237+
}
238+
if exists != tc.expectedExists {
239+
t.Fatalf("expected symlink existence to be: %v, got: %v", tc.expectedExists, exists)
240+
}
241+
})
242+
}
243+
}
244+
245+
func checkMountSupport() error {
246+
// Check if the operating system supports mounting
247+
if runtime.GOOS != "linux" {
248+
return fmt.Errorf("mount is not supported on %s", runtime.GOOS)
249+
}
250+
return nil
251+
}
252+
253+
func TestIsDeviceBindMountExist(t *testing.T) {
254+
tempDir := os.TempDir()
255+
mapPath := filepath.Join(tempDir, "test-bind-mount")
256+
linkName := "test-bind-mount-link"
257+
devicePath := "/dev/fakeDevice"
258+
259+
if err := os.MkdirAll(mapPath, 0755); err != nil {
260+
t.Fatalf("failed to create test directory: %v", err)
261+
}
262+
defer func() {
263+
if err := os.RemoveAll(mapPath); err != nil {
264+
t.Fatalf("failed to remove mapPath: %v", err)
265+
}
266+
}()
267+
268+
if err := checkMountSupport(); err != nil {
269+
t.Skipf("skipping test because mount is not supported: %v", err)
270+
}
271+
272+
if _, err := os.Stat(devicePath); os.IsNotExist(err) {
273+
t.Skipf("skipping test because the device %s does not exist: %v", devicePath, err)
274+
}
275+
276+
err := VolumePathHandler{}.MapDevice(devicePath, mapPath, linkName, true)
277+
if err != nil {
278+
t.Fatalf("failed to create bind mount: %v", err)
279+
}
280+
281+
testCases := []struct {
282+
name string
283+
mapPath string
284+
expectedExists bool
285+
}{
286+
{
287+
name: "Check Existing Bind Mount",
288+
mapPath: filepath.Join(mapPath, linkName),
289+
expectedExists: true,
290+
},
291+
{
292+
name: "Check Non-existing Bind Mount",
293+
mapPath: filepath.Join(mapPath, "non-existing-bind-mount"),
294+
expectedExists: false,
295+
},
296+
}
297+
298+
for _, tc := range testCases {
299+
t.Run(tc.name, func(t *testing.T) {
300+
exists, err := VolumePathHandler{}.IsDeviceBindMountExist(tc.mapPath)
301+
if err != nil {
302+
t.Fatalf("expected no error, got: %v", err)
303+
}
304+
if exists != tc.expectedExists {
305+
t.Fatalf("expected bind mount existence to be: %v, got: %v", tc.expectedExists, exists)
306+
}
307+
})
308+
}
309+
}

0 commit comments

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