Skip to content

Commit d0d8a03

Browse files
Improved Unit-Test Coverage for Issue #703 (#736)
* copied files from unit-test branch (due to one of the commits not using userid for some reason) * updating blob.go, controllerserver.go, and nodeserver.go back to the origin master files * init.sh and dockerfile set back to master * reran go mod vendor * Fixing the gofmt issue * fixing boilerplate errors Co-authored-by: Akash Mukhopadhyay <[email protected]>
1 parent 9ab6579 commit d0d8a03

File tree

8 files changed

+1220
-92
lines changed

8 files changed

+1220
-92
lines changed

pkg/blob/blob_test.go

+403-8
Large diffs are not rendered by default.

pkg/blob/controllerserver_test.go

+629-81
Large diffs are not rendered by default.

pkg/blob/fake_mount_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,43 @@ import (
2424
mount "k8s.io/mount-utils"
2525
)
2626

27+
func TestMount(t *testing.T) {
28+
tests := []struct {
29+
desc string
30+
source string
31+
target string
32+
fstype string
33+
options []string
34+
expectedErr error
35+
}{
36+
{
37+
desc: "source error",
38+
source: "error_mount",
39+
expectedErr: fmt.Errorf("fake Mount: source error"),
40+
},
41+
{
42+
desc: "target error",
43+
target: "error_mount",
44+
expectedErr: fmt.Errorf("fake Mount: target error"),
45+
},
46+
{
47+
desc: "Success",
48+
expectedErr: nil,
49+
},
50+
}
51+
for _, test := range tests {
52+
d := NewFakeDriver()
53+
fakeMounter := &fakeMounter{}
54+
d.mounter = &mount.SafeFormatAndMount{
55+
Interface: fakeMounter,
56+
}
57+
err := d.mounter.Mount(test.source, test.target, test.fstype, test.options)
58+
if !reflect.DeepEqual(err, test.expectedErr) {
59+
t.Errorf("actualErr: (%v), expectedErr: (%v)", err, test.expectedErr)
60+
}
61+
}
62+
}
63+
2764
func TestMountSensitive(t *testing.T) {
2865
tests := []struct {
2966
desc string

pkg/blob/nodeserver_test.go

+107
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-09-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,102 @@ 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: "protocol = nfs",
491+
testFunc: func(t *testing.T) {
492+
req := &csi.NodeStageVolumeRequest{
493+
VolumeId: "rg#acc#cont#ns",
494+
StagingTargetPath: targetTest,
495+
VolumeCapability: &csi.VolumeCapability{AccessMode: &volumeCap},
496+
VolumeContext: map[string]string{
497+
mountPermissionsField: "0755",
498+
protocolField: "nfs",
499+
},
500+
Secrets: map[string]string{},
501+
}
502+
d := NewFakeDriver()
503+
d.cloud = provider.GetTestCloud(gomock.NewController(t))
504+
d.cloud.ResourceGroup = "rg"
505+
d.enableBlobMockMount = true
506+
fakeMounter := &fakeMounter{}
507+
fakeExec := &testingexec.FakeExec{}
508+
d.mounter = &mount.SafeFormatAndMount{
509+
Interface: fakeMounter,
510+
Exec: fakeExec,
511+
}
512+
513+
_, err := d.NodeStageVolume(context.TODO(), req)
514+
//expectedErr := nil
515+
if !reflect.DeepEqual(err, nil) {
516+
t.Errorf("actualErr: (%v), expectedErr: (%v)", err, nil)
517+
}
518+
},
519+
},
520+
{
521+
name: "BlobMockMount Enabled",
522+
testFunc: func(t *testing.T) {
523+
req := &csi.NodeStageVolumeRequest{
524+
VolumeId: "rg#acc#cont#ns",
525+
StagingTargetPath: targetTest,
526+
VolumeCapability: &csi.VolumeCapability{AccessMode: &volumeCap},
527+
VolumeContext: map[string]string{
528+
mountPermissionsField: "0755",
529+
protocolField: "protocol",
530+
},
531+
Secrets: map[string]string{},
532+
}
533+
d := NewFakeDriver()
534+
d.cloud = provider.GetTestCloud(gomock.NewController(t))
535+
d.cloud.ResourceGroup = "rg"
536+
d.enableBlobMockMount = true
537+
fakeMounter := &fakeMounter{}
538+
fakeExec := &testingexec.FakeExec{}
539+
d.mounter = &mount.SafeFormatAndMount{
540+
Interface: fakeMounter,
541+
Exec: fakeExec,
542+
}
543+
544+
keyList := make([]storage.AccountKey, 1)
545+
fakeKey := "fakeKey"
546+
fakeValue := "fakeValue"
547+
keyList[0] = (storage.AccountKey{
548+
KeyName: &fakeKey,
549+
Value: &fakeValue,
550+
})
551+
d.cloud.StorageAccountClient = NewMockSAClient(context.Background(), gomock.NewController(t), "subID", "unit-test", "unit-test", &keyList)
552+
553+
_, err := d.NodeStageVolume(context.TODO(), req)
554+
//expectedErr := nil
555+
if !reflect.DeepEqual(err, nil) {
556+
t.Errorf("actualErr: (%v), expectedErr: (%v)", err, nil)
557+
}
558+
},
559+
},
459560
}
460561
for _, tc := range testCases {
461562
t.Run(tc.name, tc.testFunc)
@@ -520,6 +621,12 @@ func TestNodeUnstageVolume(t *testing.T) {
520621
StagingTargetPath: "./unit-test",
521622
}
522623
d := NewFakeDriver()
624+
fakeMounter := &fakeMounter{}
625+
fakeExec := &testingexec.FakeExec{}
626+
d.mounter = &mount.SafeFormatAndMount{
627+
Interface: fakeMounter,
628+
Exec: fakeExec,
629+
}
523630
_, err := d.NodeUnstageVolume(context.TODO(), req)
524631
expectedErr := error(nil)
525632
if !reflect.DeepEqual(err, expectedErr) {

pkg/blobfuse-proxy/init.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,4 @@ then
6868
sed -i 's/PRUNEFS="NFS/PRUNEFS="fuse blobfuse NFS/g' ${updateDBConfigPath}
6969
echo "after change:"
7070
cat ${updateDBConfigPath}
71-
fi
71+
fi

pkg/blobplugin/Dockerfile

-1
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,3 @@ LABEL maintainers="andyzhangx"
3939
LABEL description="Azure Blob Storage CSI driver"
4040

4141
ENTRYPOINT ["/blobplugin"]
42-

pkg/csi-common/driver_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,11 @@ func TestValidateControllerServiceRequest(t *testing.T) {
124124
assert.NoError(t, err)
125125

126126
}
127+
128+
func TestAddNodeServiceCapabilities(t *testing.T) {
129+
d := NewFakeDriver()
130+
nl := []csi.NodeServiceCapability_RPC_Type{csi.NodeServiceCapability_RPC_UNKNOWN, csi.NodeServiceCapability_RPC_EXPAND_VOLUME}
131+
d.AddNodeServiceCapabilities(nl)
132+
expectedOutput := []*csi.NodeServiceCapability{NewNodeServiceCapability(nl[0]), NewNodeServiceCapability(nl[1])}
133+
assert.Equal(t, expectedOutput, d.NSCap, "NS Capabilities must Match")
134+
}

pkg/util/util_test.go

+35-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package util
1818

1919
import (
20+
"fmt"
2021
"os"
2122
"testing"
2223
"time"
@@ -147,7 +148,7 @@ func TestGetMountOptions(t *testing.T) {
147148
expected: "",
148149
},
149150
{
150-
options: []string{},
151+
options: make([]string, 0),
151152
expected: "",
152153
},
153154
}
@@ -172,6 +173,39 @@ func TestMakeDir(t *testing.T) {
172173
}
173174

174175
func TestConvertTagsToMap(t *testing.T) {
176+
tests := []struct {
177+
desc string
178+
tags string
179+
expectedOut map[string]string
180+
expectedErr error
181+
}{
182+
{
183+
desc: "Improper KeyValuePair",
184+
tags: "foo=bar=gar,lorem=ipsum",
185+
expectedOut: nil,
186+
expectedErr: fmt.Errorf("Tags '%s' are invalid, the format should like: 'key1=value1,key2=value2'", "foo=bar=gar,lorem=ipsum"),
187+
},
188+
{
189+
desc: "Missing Key",
190+
tags: "=bar,lorem=ipsum",
191+
expectedOut: nil,
192+
expectedErr: fmt.Errorf("Tags '%s' are invalid, the format should like: 'key1=value1,key2=value2'", "=bar,lorem=ipsum"),
193+
},
194+
{
195+
desc: "Successful Input/Output",
196+
tags: "foo=bar,lorem=ipsum",
197+
expectedOut: map[string]string{"foo": "bar", "lorem": "ipsum"},
198+
expectedErr: nil,
199+
},
200+
}
201+
202+
for _, test := range tests {
203+
output, err := ConvertTagsToMap(test.tags)
204+
assert.Equal(t, test.expectedOut, output, test.desc)
205+
assert.Equal(t, test.expectedErr, err, test.desc)
206+
}
207+
}
208+
func TestConvertTagsToMap2(t *testing.T) {
175209
type StringMap map[string]string
176210
tests := []struct {
177211
tags string

0 commit comments

Comments
 (0)