Skip to content

Commit c46b0ac

Browse files
committed
added testcases for nodstagevolume and fixed nullpointer error in test case for nodeunstagevolume
1 parent f4ed930 commit c46b0ac

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

Diff for: pkg/blob/nodeserver_test.go

+136
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ import (
3030
"google.golang.org/grpc"
3131
"google.golang.org/grpc/codes"
3232
"google.golang.org/grpc/status"
33+
"sigs.k8s.io/cloud-provider-azure/pkg/provider"
3334

35+
"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2021-02-01/storage"
3436
"github.com/container-storage-interface/spec/lib/go/csi"
37+
"github.com/golang/mock/gomock"
3538
"github.com/stretchr/testify/assert"
3639

3740
mount "k8s.io/mount-utils"
@@ -235,6 +238,7 @@ func TestNodePublishVolume(t *testing.T) {
235238
_ = makeDir(sourceTest)
236239
_ = makeDir(targetTest)
237240
d := NewFakeDriver()
241+
d.cloud = provider.GetTestCloud(gomock.NewController(t))
238242
fakeMounter := &fakeMounter{}
239243
fakeExec := &testingexec.FakeExec{ExactOrder: true}
240244
d.mounter = &mount.SafeFormatAndMount{
@@ -243,6 +247,7 @@ func TestNodePublishVolume(t *testing.T) {
243247
}
244248

245249
for _, test := range tests {
250+
d.cloud.ResourceGroup = "rg"
246251
if test.setup != nil {
247252
test.setup(d)
248253
}
@@ -456,6 +461,131 @@ func TestNodeStageVolume(t *testing.T) {
456461
}
457462
},
458463
},
464+
{
465+
name: "[Error] Could not mount to target",
466+
testFunc: func(t *testing.T) {
467+
req := &csi.NodeStageVolumeRequest{
468+
VolumeId: "unit-test",
469+
StagingTargetPath: "error_is_likely",
470+
VolumeCapability: &csi.VolumeCapability{AccessMode: &volumeCap},
471+
VolumeContext: map[string]string{
472+
mountPermissionsField: "0755",
473+
},
474+
}
475+
d := NewFakeDriver()
476+
fakeMounter := &fakeMounter{}
477+
fakeExec := &testingexec.FakeExec{}
478+
d.mounter = &mount.SafeFormatAndMount{
479+
Interface: fakeMounter,
480+
Exec: fakeExec,
481+
}
482+
_, err := d.NodeStageVolume(context.TODO(), req)
483+
expectedErr := status.Error(codes.Internal, fmt.Sprintf("Could not mount target %q: %v", req.StagingTargetPath, fmt.Errorf("fake IsLikelyNotMountPoint: fake error")))
484+
if !reflect.DeepEqual(err, expectedErr) {
485+
t.Errorf("actualErr: (%v), expectedErr: (%v)", err, expectedErr)
486+
}
487+
},
488+
},
489+
{
490+
name: "[Error] GetAuthEnv Error (could not find container name)",
491+
testFunc: func(t *testing.T) {
492+
req := &csi.NodeStageVolumeRequest{
493+
VolumeId: "unit-test",
494+
StagingTargetPath: targetTest,
495+
VolumeCapability: &csi.VolumeCapability{AccessMode: &volumeCap},
496+
VolumeContext: map[string]string{
497+
mountPermissionsField: "0755",
498+
protocolField: "protocol",
499+
},
500+
Secrets: map[string]string{},
501+
}
502+
d := NewFakeDriver()
503+
d.cloud = provider.GetTestCloud(gomock.NewController(t))
504+
d.cloud.ResourceGroup = "rg"
505+
fakeMounter := &fakeMounter{}
506+
fakeExec := &testingexec.FakeExec{}
507+
d.mounter = &mount.SafeFormatAndMount{
508+
Interface: fakeMounter,
509+
Exec: fakeExec,
510+
}
511+
_, err := d.NodeStageVolume(context.TODO(), req)
512+
expectedErr := fmt.Errorf("could not find containerName from attributes(%v) or volumeID(%v)", req.GetVolumeContext(), req.VolumeId)
513+
if !reflect.DeepEqual(err, expectedErr) {
514+
t.Errorf("actualErr: (%v), expectedErr: (%v)", err, expectedErr)
515+
}
516+
},
517+
},
518+
{
519+
name: "protocol = nfs",
520+
testFunc: func(t *testing.T) {
521+
req := &csi.NodeStageVolumeRequest{
522+
VolumeId: "rg#acc#cont#ns",
523+
StagingTargetPath: targetTest,
524+
VolumeCapability: &csi.VolumeCapability{AccessMode: &volumeCap},
525+
VolumeContext: map[string]string{
526+
mountPermissionsField: "0755",
527+
protocolField: "nfs",
528+
},
529+
Secrets: map[string]string{},
530+
}
531+
d := NewFakeDriver()
532+
d.cloud = provider.GetTestCloud(gomock.NewController(t))
533+
d.cloud.ResourceGroup = "rg"
534+
d.enableBlobMockMount = true
535+
fakeMounter := &fakeMounter{}
536+
fakeExec := &testingexec.FakeExec{}
537+
d.mounter = &mount.SafeFormatAndMount{
538+
Interface: fakeMounter,
539+
Exec: fakeExec,
540+
}
541+
542+
_, err := d.NodeStageVolume(context.TODO(), req)
543+
//expectedErr := nil
544+
if !reflect.DeepEqual(err, nil) {
545+
t.Errorf("actualErr: (%v), expectedErr: (%v)", err, nil)
546+
}
547+
},
548+
},
549+
{
550+
name: "BlobMockMount Enabled",
551+
testFunc: func(t *testing.T) {
552+
req := &csi.NodeStageVolumeRequest{
553+
VolumeId: "rg#acc#cont#ns",
554+
StagingTargetPath: targetTest,
555+
VolumeCapability: &csi.VolumeCapability{AccessMode: &volumeCap},
556+
VolumeContext: map[string]string{
557+
mountPermissionsField: "0755",
558+
protocolField: "protocol",
559+
},
560+
Secrets: map[string]string{},
561+
}
562+
d := NewFakeDriver()
563+
d.cloud = provider.GetTestCloud(gomock.NewController(t))
564+
d.cloud.ResourceGroup = "rg"
565+
d.enableBlobMockMount = true
566+
fakeMounter := &fakeMounter{}
567+
fakeExec := &testingexec.FakeExec{}
568+
d.mounter = &mount.SafeFormatAndMount{
569+
Interface: fakeMounter,
570+
Exec: fakeExec,
571+
}
572+
573+
keyList := make([]storage.AccountKey, 1)
574+
fakeKey := "fakeKey"
575+
fakeValue := "fakeValue"
576+
keyList[0] = (storage.AccountKey{
577+
KeyName: &fakeKey,
578+
Value: &fakeValue,
579+
})
580+
d.cloud.StorageAccountClient = NewMockSAClient(gomock.NewController(t), context.Background(), "subID", "unit-test", "unit-test", &keyList)
581+
582+
_, err := d.NodeStageVolume(context.TODO(), req)
583+
//expectedErr := nil
584+
if !reflect.DeepEqual(err, nil) {
585+
t.Errorf("actualErr: (%v), expectedErr: (%v)", err, nil)
586+
}
587+
},
588+
},
459589
}
460590
for _, tc := range testCases {
461591
t.Run(tc.name, tc.testFunc)
@@ -520,6 +650,12 @@ func TestNodeUnstageVolume(t *testing.T) {
520650
StagingTargetPath: "./unit-test",
521651
}
522652
d := NewFakeDriver()
653+
fakeMounter := &fakeMounter{}
654+
fakeExec := &testingexec.FakeExec{}
655+
d.mounter = &mount.SafeFormatAndMount{
656+
Interface: fakeMounter,
657+
Exec: fakeExec,
658+
}
523659
_, err := d.NodeUnstageVolume(context.TODO(), req)
524660
expectedErr := error(nil)
525661
if !reflect.DeepEqual(err, expectedErr) {

0 commit comments

Comments
 (0)