forked from fluid-cloudnative/fluid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmount_test.go
More file actions
308 lines (286 loc) · 8.13 KB
/
mount_test.go
File metadata and controls
308 lines (286 loc) · 8.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
Copyright 2023 The Fluid Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package utils
import (
"errors"
"os"
"os/exec"
"reflect"
"testing"
. "github.com/agiledragon/gomonkey/v2"
. "github.com/smartystreets/goconvey/convey"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/mount"
)
func TestMountRootWithEnvSet(t *testing.T) {
var testCases = []struct {
input string
expected string
}{
{"/var/lib/mymount", "/var/lib/mymount"},
}
for _, tc := range testCases {
t.Setenv(MountRoot, tc.input)
mountRoot, err := GetMountRoot()
if err != nil {
t.Errorf("Get error %v", err)
}
if tc.expected != mountRoot {
t.Errorf("expected %#v, got %#v",
tc.expected, mountRoot)
}
}
}
func TestMountRootWithoutEnvSet(t *testing.T) {
var testCases = []struct {
input string
expected string
}{
{"/var/lib/mymount", ""},
}
for _, tc := range testCases {
_ = os.Unsetenv(MountRoot)
mountRoot, err := GetMountRoot()
if err == nil {
t.Errorf("Expected error happened, but no error")
}
if err.Error() != "invalid mount root path '': the mount root path is empty" {
t.Errorf("Get unexpected error %v", err)
}
if tc.expected != mountRoot {
t.Errorf("Unexpected result %s", tc.expected)
}
}
}
func TestCheckMountReady(t *testing.T) {
Convey("TestCheckMountReady", t, func() {
Convey("CheckMountReady success", func() {
cmd := &exec.Cmd{}
patch1 := ApplyMethod(reflect.TypeOf(cmd), "CombinedOutput", func(_ *exec.Cmd) ([]byte, error) {
return nil, nil
})
defer patch1.Reset()
err := CheckMountReadyAndSubPathExist("/test", "test", "")
So(err, ShouldBeNil)
})
Convey("CheckMountReady false", func() {
cmd := &exec.Cmd{}
patch1 := ApplyMethod(reflect.TypeOf(cmd), "CombinedOutput", func(_ *exec.Cmd) ([]byte, error) {
return nil, errors.New("test")
})
defer patch1.Reset()
err := CheckMountReadyAndSubPathExist("/test", "test", "")
So(err, ShouldNotBeNil)
})
Convey("fluidPath nil", func() {
err := CheckMountReadyAndSubPathExist("", "test", "")
So(err, ShouldNotBeNil)
})
Convey("illegal subpath", func() {
err := CheckMountReadyAndSubPathExist("/test", "test", "$(echo)")
So(err, ShouldNotBeNil)
})
})
}
func TestIsMounted(t *testing.T) {
Convey("TestIsMounted", t, func() {
Convey("IsMounted success", func() {
patch2 := ApplyFunc(os.Stat, func(filename string) (os.FileInfo, error) {
return nil, nil
})
defer patch2.Reset()
patch1 := ApplyFunc(os.ReadFile, func(filename string) ([]byte, error) {
return []byte("JuiceFS:minio /var/lib/kubelet/pods/4781fc5b-72f9-4175-9321-2e1f169880ce/volumes/kubernetes.io~csi/default-jfsdemo/mount fuse.juicefs rw,relatime,user_id=0,group_id=0,default_permissions,allow_other 0 0"), nil
})
defer patch1.Reset()
absPath := "/var/lib/kubelet/pods/4781fc5b-72f9-4175-9321-2e1f169880ce/volumes/kubernetes.io~csi/default-jfsdemo/mount"
mounted, err := IsMounted(absPath)
So(err, ShouldBeNil)
So(mounted, ShouldBeTrue)
})
Convey("IsMounted false", func() {
patch1 := ApplyFunc(os.ReadFile, func(filename string) ([]byte, error) {
return []byte("JuiceFS:minio /var/lib/kubelet/pods/4781fc5b-72f9-4175-9321-2e1f169880ce/volumes/kubernetes.io~csi/default-jfsdemo/mount fuse.juicefs rw,relatime,user_id=0,group_id=0,default_permissions,allow_other 0 0"), nil
})
defer patch1.Reset()
patch2 := ApplyFunc(os.Stat, func(filename string) (os.FileInfo, error) {
return nil, nil
})
defer patch2.Reset()
absPath := "/test"
mounted, err := IsMounted(absPath)
So(err, ShouldBeNil)
So(mounted, ShouldBeFalse)
})
Convey("token len is 1", func() {
patch1 := ApplyFunc(os.ReadFile, func(filename string) ([]byte, error) {
return []byte("JuiceFS:minio "), nil
})
defer patch1.Reset()
patch2 := ApplyFunc(os.Stat, func(filename string) (os.FileInfo, error) {
return nil, nil
})
defer patch2.Reset()
absPath := "/test"
mounted, err := IsMounted(absPath)
So(err, ShouldBeNil)
So(mounted, ShouldBeFalse)
})
Convey("IsMounted error", func() {
patch1 := ApplyFunc(os.ReadFile, func(filename string) ([]byte, error) {
return []byte("JuiceFS:minio"), errors.New("test")
})
defer patch1.Reset()
absPath := "/test"
mounted, err := IsMounted(absPath)
So(err, ShouldNotBeNil)
So(mounted, ShouldBeFalse)
})
})
}
func TestCheckMountPointBroken(t *testing.T) {
Convey("TestCheckMountPointBroken", t, func() {
Convey("CheckMountPointBroken success", func() {
patch1 := ApplyFunc(mount.PathExists, func(path string) (bool, error) {
return true, errors.New("test")
})
defer patch1.Reset()
patch2 := ApplyFunc(mount.IsCorruptedMnt, func(err error) bool {
return true
})
defer patch2.Reset()
broken, err := CheckMountPointBroken("/test")
So(err, ShouldBeNil)
So(broken, ShouldBeTrue)
})
Convey("CheckMountPointBroken not broken", func() {
patch1 := ApplyFunc(mount.PathExists, func(path string) (bool, error) {
return true, nil
})
defer patch1.Reset()
broken, err := CheckMountPointBroken("/test")
So(err, ShouldBeNil)
So(broken, ShouldBeFalse)
})
Convey("CheckMountPointBroken not exist", func() {
patch1 := ApplyFunc(mount.PathExists, func(path string) (bool, error) {
return false, nil
})
defer patch1.Reset()
patch2 := ApplyFunc(mount.IsCorruptedMnt, func(err error) bool {
return false
})
defer patch2.Reset()
broken, err := CheckMountPointBroken("/test")
So(err, ShouldNotBeNil)
So(broken, ShouldBeFalse)
})
Convey("CheckMountPointBroken error", func() {
patch1 := ApplyFunc(mount.PathExists, func(path string) (bool, error) {
return false, errors.New("test")
})
defer patch1.Reset()
patch2 := ApplyFunc(mount.IsCorruptedMnt, func(err error) bool {
return false
})
defer patch2.Reset()
broken, err := CheckMountPointBroken("/test")
So(err, ShouldNotBeNil)
So(broken, ShouldBeFalse)
})
Convey("CheckMountPointBroken nil", func() {
broken, err := CheckMountPointBroken("")
So(err, ShouldNotBeNil)
So(broken, ShouldBeFalse)
})
})
}
func TestGetRuntimeNameFromFusePod(t *testing.T) {
type args struct {
pod v1.Pod
}
tests := []struct {
name string
args args
wantRuntimeName string
wantErr bool
}{
{
name: "test-right",
args: args{
pod: v1.Pod{
ObjectMeta: metav1.ObjectMeta{Name: "test-fuse-123"},
},
},
wantRuntimeName: "test",
wantErr: false,
},
{
name: "test-error",
args: args{
pod: v1.Pod{
ObjectMeta: metav1.ObjectMeta{Name: "test"},
},
},
wantRuntimeName: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotRuntimeName, err := GetRuntimeNameFromFusePod(tt.args.pod)
if (err != nil) != tt.wantErr {
t.Errorf("GetRuntimeNameFromFusePod() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotRuntimeName != tt.wantRuntimeName {
t.Errorf("GetRuntimeNameFromFusePod() gotRuntimeName = %v, want %v", gotRuntimeName, tt.wantRuntimeName)
}
})
}
}
func TestIsFusePod(t *testing.T) {
type args struct {
pod v1.Pod
}
tests := []struct {
name string
args args
want bool
}{
{
name: "test-true",
args: args{
pod: v1.Pod{
ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"role": "juicefs-fuse"}},
},
},
want: true,
},
{
name: "test-false",
args: args{
pod: v1.Pod{},
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsFusePod(tt.args.pod); got != tt.want {
t.Errorf("IsFusePod() = %v, want %v", got, tt.want)
}
})
}
}