Skip to content

Commit c10157d

Browse files
committed
test: add unit-test in nodeserver and csi-common.
1 parent fce8466 commit c10157d

File tree

6 files changed

+332
-0
lines changed

6 files changed

+332
-0
lines changed

pkg/blob/fake_mount.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Copyright 2020 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 blob
18+
19+
import (
20+
"fmt"
21+
"strings"
22+
23+
"k8s.io/utils/mount"
24+
)
25+
26+
type fakeMounter struct {
27+
mount.FakeMounter
28+
}
29+
30+
// MountSensitive overrides mount.FakeMounter.MountSensitive.
31+
func (f *fakeMounter) MountSensitive(source string, target string, fstype string, options []string, sensitiveOptions []string) error {
32+
if strings.Contains(source, "ut-container") {
33+
return fmt.Errorf("fake MountSensitive: source error")
34+
} else if strings.Contains(target, "error_mount_sens") {
35+
return fmt.Errorf("fake MountSensitive: target error")
36+
}
37+
38+
return nil
39+
}

pkg/blob/fake_mount_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Copyright 2020 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 blob
18+
19+
import (
20+
"fmt"
21+
"reflect"
22+
"testing"
23+
24+
"k8s.io/utils/mount"
25+
)
26+
27+
func TestMountSensitive(t *testing.T) {
28+
tests := []struct {
29+
desc string
30+
source string
31+
target string
32+
expectedErr error
33+
}{
34+
{
35+
desc: "[Error] Mocked source error",
36+
source: "ut-container",
37+
target: targetTest,
38+
expectedErr: fmt.Errorf("fake MountSensitive: source error"),
39+
},
40+
{
41+
desc: "[Error] Mocked target error",
42+
source: "container",
43+
target: "error_mount_sens",
44+
expectedErr: fmt.Errorf("fake MountSensitive: target error"),
45+
},
46+
{
47+
desc: "[Error] Mocked target error",
48+
source: "container",
49+
target: "error_mount",
50+
expectedErr: nil,
51+
},
52+
}
53+
54+
d := NewFakeDriver()
55+
fakeMounter := &fakeMounter{}
56+
d.mounter = &mount.SafeFormatAndMount{
57+
Interface: fakeMounter,
58+
}
59+
for _, test := range tests {
60+
err := d.mounter.MountSensitive(test.source, test.target, "", nil, nil)
61+
if !reflect.DeepEqual(err, test.expectedErr) {
62+
t.Errorf("actualErr: (%v), expectedErr: (%v)", err, test.expectedErr)
63+
}
64+
}
65+
}

pkg/blob/nodeserver_test.go

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,144 @@ func TestNodeUnpublishVolume(t *testing.T) {
240240
assert.NoError(t, err)
241241
}
242242

243+
func TestNodeStageVolume(t *testing.T) {
244+
testCases := []struct {
245+
name string
246+
testFunc func(t *testing.T)
247+
}{
248+
{
249+
name: "Volume ID missing",
250+
testFunc: func(t *testing.T) {
251+
req := &csi.NodeStageVolumeRequest{}
252+
d := NewFakeDriver()
253+
_, err := d.NodeStageVolume(context.TODO(), req)
254+
expectedErr := status.Error(codes.InvalidArgument, "Volume ID missing in request")
255+
if !reflect.DeepEqual(err, expectedErr) {
256+
t.Errorf("actualErr: (%v), expectedErr: (%v)", err, expectedErr)
257+
}
258+
},
259+
},
260+
{
261+
name: "Staging target not provided",
262+
testFunc: func(t *testing.T) {
263+
req := &csi.NodeStageVolumeRequest{
264+
VolumeId: "unit-test",
265+
}
266+
d := NewFakeDriver()
267+
_, err := d.NodeStageVolume(context.TODO(), req)
268+
expectedErr := status.Error(codes.InvalidArgument, "Staging target not provided")
269+
if !reflect.DeepEqual(err, expectedErr) {
270+
t.Errorf("actualErr: (%v), expectedErr: (%v)", err, expectedErr)
271+
}
272+
},
273+
},
274+
{
275+
name: "Volume capability missing",
276+
testFunc: func(t *testing.T) {
277+
req := &csi.NodeStageVolumeRequest{
278+
VolumeId: "unit-test",
279+
StagingTargetPath: "unit-test",
280+
}
281+
d := NewFakeDriver()
282+
_, err := d.NodeStageVolume(context.TODO(), req)
283+
expectedErr := status.Error(codes.InvalidArgument, "Volume capability not provided")
284+
if !reflect.DeepEqual(err, expectedErr) {
285+
t.Errorf("actualErr: (%v), expectedErr: (%v)", err, expectedErr)
286+
}
287+
},
288+
},
289+
{
290+
name: "volume mount sensitive fail",
291+
testFunc: func(t *testing.T) {
292+
attrib := make(map[string]string)
293+
attrib["containername"] = "ut-container"
294+
attrib["server"] = "blob-endpoint"
295+
attrib["protocol"] = "nfs"
296+
secret := make(map[string]string)
297+
secret["accountname"] = "unit-test"
298+
accessType := csi.VolumeCapability_Mount{}
299+
req := &csi.NodeStageVolumeRequest{
300+
VolumeId: "rg#f5713de20cde511e8ba4900#pvc-fuse-dynamic-17e43f84-f474-11e8-acd0-000d3a00df41",
301+
StagingTargetPath: "right_mount",
302+
VolumeContext: attrib,
303+
Secrets: secret,
304+
VolumeCapability: &csi.VolumeCapability{
305+
AccessType: &accessType,
306+
},
307+
}
308+
d := NewFakeDriver()
309+
fakeMounter := &fakeMounter{}
310+
d.mounter = &mount.SafeFormatAndMount{
311+
Interface: fakeMounter,
312+
}
313+
_, err := d.NodeStageVolume(context.TODO(), req)
314+
expectedErr := status.Error(codes.Internal, "volume(rg#f5713de20cde511e8ba4900#pvc-fuse-dynamic-17e43f84-f474-11e8-acd0-000d3a00df41) mount \"blob-endpoint:/unit-test/ut-container\" on \"right_mount\" failed with fake MountSensitive: source error")
315+
if !reflect.DeepEqual(err, expectedErr) {
316+
t.Errorf("actualErr: (%v), expectedErr: (%v)", err, expectedErr)
317+
}
318+
// Clean up
319+
err = os.RemoveAll(req.StagingTargetPath)
320+
assert.NoError(t, err)
321+
},
322+
},
323+
}
324+
for _, tc := range testCases {
325+
t.Run(tc.name, tc.testFunc)
326+
}
327+
}
328+
329+
func TestNodeUnstageVolume(t *testing.T) {
330+
testCases := []struct {
331+
name string
332+
testFunc func(t *testing.T)
333+
}{
334+
{
335+
name: "Volume ID missing",
336+
testFunc: func(t *testing.T) {
337+
req := &csi.NodeUnstageVolumeRequest{}
338+
d := NewFakeDriver()
339+
_, err := d.NodeUnstageVolume(context.TODO(), req)
340+
expectedErr := status.Error(codes.InvalidArgument, "Volume ID not provided")
341+
if !reflect.DeepEqual(err, expectedErr) {
342+
t.Errorf("actualErr: (%v), expectedErr: (%v)", err, expectedErr)
343+
}
344+
},
345+
},
346+
{
347+
name: "staging target missing",
348+
testFunc: func(t *testing.T) {
349+
req := &csi.NodeUnstageVolumeRequest{
350+
VolumeId: "unit-test",
351+
}
352+
d := NewFakeDriver()
353+
_, err := d.NodeUnstageVolume(context.TODO(), req)
354+
expectedErr := status.Error(codes.InvalidArgument, "Staging target not provided")
355+
if !reflect.DeepEqual(err, expectedErr) {
356+
t.Errorf("actualErr: (%v), expectedErr: (%v)", err, expectedErr)
357+
}
358+
},
359+
},
360+
{
361+
name: "mount point not exist ",
362+
testFunc: func(t *testing.T) {
363+
req := &csi.NodeUnstageVolumeRequest{
364+
VolumeId: "unit-test",
365+
StagingTargetPath: "./unit-test",
366+
}
367+
d := NewFakeDriver()
368+
_, err := d.NodeUnstageVolume(context.TODO(), req)
369+
expectedErr := error(nil)
370+
if !reflect.DeepEqual(err, expectedErr) {
371+
t.Errorf("actualErr: (%v), expectedErr: (%v)", err, expectedErr)
372+
}
373+
},
374+
},
375+
}
376+
for _, tc := range testCases {
377+
t.Run(tc.name, tc.testFunc)
378+
}
379+
}
380+
243381
func makeDir(pathname string) error {
244382
err := os.MkdirAll(pathname, os.FileMode(0755))
245383
if err != nil {
@@ -278,3 +416,23 @@ func TestNewSafeMounter(t *testing.T) {
278416
assert.NotNil(t, resp)
279417
assert.Nil(t, err)
280418
}
419+
420+
func TestNodeGetVolumeStats(t *testing.T) {
421+
d := NewFakeDriver()
422+
req := csi.NodeGetVolumeStatsRequest{}
423+
resp, err := d.NodeGetVolumeStats(context.Background(), &req)
424+
assert.Nil(t, resp)
425+
if !reflect.DeepEqual(err, status.Error(codes.Unimplemented, "")) {
426+
t.Errorf("Unexpected error: %v", err)
427+
}
428+
}
429+
430+
func TestNodeExpandVolume(t *testing.T) {
431+
d := NewFakeDriver()
432+
req := csi.NodeExpandVolumeRequest{}
433+
resp, err := d.NodeExpandVolume(context.Background(), &req)
434+
assert.Nil(t, resp)
435+
if !reflect.DeepEqual(err, status.Error(codes.Unimplemented, "NodeExpandVolume is not yet implemented")) {
436+
t.Errorf("Unexpected error: %v", err)
437+
}
438+
}

pkg/csi-common/driver_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ var (
3434
vendorVersion = "0.3.0"
3535
)
3636

37+
func TestNewCSIDriver(t *testing.T) {
38+
name := ""
39+
str := ""
40+
nodeID := ""
41+
assert.Nil(t, NewCSIDriver(name, str, nodeID))
42+
name = "unit-test"
43+
assert.Nil(t, NewCSIDriver(name, str, nodeID))
44+
nodeID = "unit-test"
45+
driver := CSIDriver{
46+
Name: name,
47+
NodeID: nodeID,
48+
Version: str,
49+
}
50+
assert.Equal(t, &driver, NewCSIDriver(name, str, nodeID))
51+
}
52+
3753
func NewFakeDriver() *CSIDriver {
3854

3955
driver := NewCSIDriver(fakeDriverName, vendorVersion, fakeNodeID)
@@ -47,6 +63,13 @@ func TestNewFakeDriver(t *testing.T) {
4763
assert.Nil(t, d)
4864
}
4965

66+
func TestAddControllerServiceCapabilities(t *testing.T) {
67+
d := NewFakeDriver()
68+
var cl []csi.ControllerServiceCapability_RPC_Type
69+
cl = append(cl, csi.ControllerServiceCapability_RPC_UNKNOWN)
70+
d.AddControllerServiceCapabilities(cl)
71+
}
72+
5073
func TestGetVolumeCapabilityAccessModes(t *testing.T) {
5174

5275
d := NewFakeDriver()

pkg/csi-common/identityserver-default_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,23 @@ func TestGetPluginInfo(t *testing.T) {
3535
assert.Equal(t, resp.GetName(), fakeDriverName)
3636
assert.Equal(t, resp.GetVendorVersion(), vendorVersion)
3737
}
38+
39+
func TestProbe(t *testing.T) {
40+
d := NewFakeDriver()
41+
ids := NewDefaultIdentityServer(d)
42+
req := csi.ProbeRequest{}
43+
resp, err := ids.Probe(context.Background(), &req)
44+
assert.NoError(t, err)
45+
assert.NotNil(t, resp)
46+
assert.Equal(t, resp.XXX_sizecache, int32(0))
47+
}
48+
49+
func TestGetPluginCapabilities(t *testing.T) {
50+
d := NewFakeDriver()
51+
ids := NewDefaultIdentityServer(d)
52+
req := csi.GetPluginCapabilitiesRequest{}
53+
resp, err := ids.GetPluginCapabilities(context.Background(), &req)
54+
assert.NoError(t, err)
55+
assert.NotNil(t, resp)
56+
assert.Equal(t, resp.XXX_sizecache, int32(0))
57+
}

pkg/csi-common/utils_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,30 @@ func TestLogGRPC(t *testing.T) {
145145
})
146146
}
147147
}
148+
149+
func TestNewNodeServiceCapability(t *testing.T) {
150+
tests := []struct {
151+
cap csi.ControllerServiceCapability_RPC_Type
152+
}{
153+
{
154+
cap: csi.ControllerServiceCapability_RPC_UNKNOWN,
155+
},
156+
{
157+
cap: csi.ControllerServiceCapability_RPC_CREATE_DELETE_VOLUME,
158+
},
159+
{
160+
cap: csi.ControllerServiceCapability_RPC_PUBLISH_UNPUBLISH_VOLUME,
161+
},
162+
{
163+
cap: csi.ControllerServiceCapability_RPC_LIST_VOLUMES,
164+
},
165+
{
166+
cap: csi.ControllerServiceCapability_RPC_GET_CAPACITY,
167+
},
168+
}
169+
for _, test := range tests {
170+
resp := NewControllerServiceCapability(test.cap)
171+
assert.NotNil(t, resp)
172+
assert.Equal(t, resp.XXX_sizecache, int32(0))
173+
}
174+
}

0 commit comments

Comments
 (0)