Skip to content

Commit

Permalink
Merge pull request #479 from Sakuralbj/unit-test
Browse files Browse the repository at this point in the history
test: add unit-test in controllerserver and nodeserver
  • Loading branch information
andyzhangx authored Aug 7, 2020
2 parents 3c59872 + acd260a commit f6de053
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 15 deletions.
208 changes: 196 additions & 12 deletions pkg/azuredisk/controllerserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,27 @@ func TestGetCachingMode(t *testing.T) {
}

func TestGetEntriesAndNextToken(t *testing.T) {
provisioningState := "succeeded"
DiskSize := int32(10)
snapshotID := "test"
sourceVolumeID := "unit-test"
creationdate := compute.CreationData{
SourceResourceID: &sourceVolumeID,
}
snapshot := compute.Snapshot{
SnapshotProperties: &compute.SnapshotProperties{
TimeCreated: &date.Time{},
ProvisioningState: &provisioningState,
DiskSizeGB: &DiskSize,
CreationData: &creationdate,
},
ID: &snapshotID,
}
snapshots := []compute.Snapshot{}
snapshots = append(snapshots, snapshot)
entries := []*csi.ListSnapshotsResponse_Entry{}
csiSnapshot, _ := generateCSISnapshot(sourceVolumeID, &snapshot)
entries = append(entries, &csi.ListSnapshotsResponse_Entry{Snapshot: csiSnapshot})
tests := []struct {
request *csi.ListSnapshotsRequest
snapshots []compute.Snapshot
Expand Down Expand Up @@ -149,11 +170,23 @@ func TestGetEntriesAndNextToken(t *testing.T) {
nil,
status.Errorf(codes.Aborted, "ListSnapshots starting token(-1) can not be negative"),
},
{
&csi.ListSnapshotsRequest{
MaxEntries: 2,
SourceVolumeId: sourceVolumeID,
},
snapshots,
&csi.ListSnapshotsResponse{
Entries: entries,
NextToken: "1",
},
error(nil),
},
}

for _, test := range tests {
resultResponse, resultError := getEntriesAndNextToken(test.request, test.snapshots)
if resultResponse != test.expectedResponse || resultError.Error() != test.expectedError.Error() {
if !reflect.DeepEqual(resultResponse, test.expectedResponse) || (!reflect.DeepEqual(resultError, test.expectedError)) {
t.Errorf("request: %v, snapshotListPage: %v, resultResponse: %v, expectedResponse: %v, resultError: %v, expectedError: %v", test.request, test.snapshots, resultResponse, test.expectedResponse, resultError, test.expectedError)
}
}
Expand All @@ -164,6 +197,20 @@ func TestCreateVolume(t *testing.T) {
name string
testFunc func(t *testing.T)
}{
{
name: " invalid ",
testFunc: func(t *testing.T) {
d, _ := NewFakeDriver(t)
d.Cap = []*csi.ControllerServiceCapability{}

req := &csi.CreateVolumeRequest{}
_, err := d.CreateVolume(context.Background(), req)
expectedErr := status.Error(codes.InvalidArgument, "CREATE_DELETE_VOLUME")
if !reflect.DeepEqual(err, expectedErr) {
t.Errorf("actualErr: (%v), expectedErr: (%v)", err, expectedErr)
}
},
},
{
name: " volume name missing",
testFunc: func(t *testing.T) {
Expand Down Expand Up @@ -216,16 +263,6 @@ func TestCreateVolume(t *testing.T) {
d, _ := NewFakeDriver(t)
mp := make(map[string]string)
mp["maxshares"] = "aaa"
mp["skuname"] = "ut"
mp["location"] = "ut"
mp["storageaccount"] = "ut"
mp["resourcegroup"] = "ut"
mp["diskiopsreadwrite"] = "ut"
mp["diskmbpsreadwrite"] = "ut"
mp["diskname"] = "ut"
mp["diskencryptionsetid"] = "ut"
mp["writeacceleratorenabled"] = "ut"

req := &csi.CreateVolumeRequest{
Name: "unit-test",
VolumeCapabilities: createVolumeCapabilities(csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER),
Expand Down Expand Up @@ -262,6 +299,16 @@ func TestCreateVolume(t *testing.T) {
d, _ := NewFakeDriver(t)
mp := make(map[string]string)
mp["maxshares"] = "1"
mp["skuname"] = "ut"
mp["location"] = "ut"
mp["storageaccount"] = "ut"
mp["storageaccounttype"] = "ut"
mp["resourcegroup"] = "ut"
mp["diskiopsreadwrite"] = "ut"
mp["diskmbpsreadwrite"] = "ut"
mp["diskname"] = "ut"
mp["diskencryptionsetid"] = "ut"
mp["writeacceleratorenabled"] = "ut"
req := &csi.CreateVolumeRequest{
Name: "unit-test",
VolumeCapabilities: createVolumeCapabilities(csi.VolumeCapability_AccessMode_MULTI_NODE_READER_ONLY),
Expand Down Expand Up @@ -409,7 +456,7 @@ func TestCreateVolume(t *testing.T) {
testFunc: func(t *testing.T) {
d, _ := NewFakeDriver(t)
mp := make(map[string]string)
mp["tags"] = "unit=test"
mp["unit-test"] = "unit=test"
volumeContentSourceSnapshotSource := &csi.VolumeContentSource_Snapshot{}
volumecontensource := csi.VolumeContentSource{
Type: volumeContentSourceSnapshotSource,
Expand Down Expand Up @@ -830,6 +877,23 @@ func TestControllerExpandVolume(t *testing.T) {
}
},
},
{
name: "disk type is not managedDisk",
testFunc: func(t *testing.T) {
req := &csi.ControllerExpandVolumeRequest{
VolumeId: "httptest",
CapacityRange: stdCapRange,
}
ctx := context.Background()
d, _ := NewFakeDriver(t)

expectedErr := status.Error(codes.InvalidArgument, "the disk type(httptest) is not ManagedDisk")
_, err := d.ControllerExpandVolume(ctx, req)
if !reflect.DeepEqual(err, expectedErr) {
t.Errorf("Unexpected error: %v", err)
}
},
},
{
name: "Disk URI not valid",
testFunc: func(t *testing.T) {
Expand Down Expand Up @@ -1363,7 +1427,51 @@ func TestListSnapshots(t *testing.T) {
}
},
},
{
name: "List resource error",
testFunc: func(t *testing.T) {
req := csi.ListSnapshotsRequest{}
d, _ := NewFakeDriver(t)
snapshot := compute.Snapshot{}
snapshots := []compute.Snapshot{}
snapshots = append(snapshots, snapshot)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
rerr := &retry.Error{
RawError: fmt.Errorf("test"),
}
mockSnapshotClient := mocksnapshotclient.NewMockInterface(ctrl)
d.cloud.SnapshotsClient = mockSnapshotClient
mockSnapshotClient.EXPECT().ListByResourceGroup(gomock.Any(), gomock.Any()).Return(snapshots, rerr).AnyTimes()
expectedErr := status.Error(codes.Internal, "Unknown list snapshot error: Retriable: false, RetryAfter: 0s, HTTPStatusCode: 0, RawError: test")
_, err := d.ListSnapshots(context.TODO(), &req)
if !reflect.DeepEqual(err, expectedErr) {
t.Errorf("actualErr: (%v), expectedErr: (%v)", err, expectedErr)
}
},
},
{
name: "snapshot property nil",
testFunc: func(t *testing.T) {
req := csi.ListSnapshotsRequest{}
d, _ := NewFakeDriver(t)
snapshot := compute.Snapshot{}
snapshots := []compute.Snapshot{}
snapshots = append(snapshots, snapshot)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockSnapshotClient := mocksnapshotclient.NewMockInterface(ctrl)
d.cloud.SnapshotsClient = mockSnapshotClient
mockSnapshotClient.EXPECT().ListByResourceGroup(gomock.Any(), gomock.Any()).Return(snapshots, nil).AnyTimes()
expectedErr := fmt.Errorf("failed to generate snapshot entry: snapshot property is nil")
_, err := d.ListSnapshots(context.TODO(), &req)
if !reflect.DeepEqual(err, expectedErr) {
t.Errorf("actualErr: (%v), expectedErr: (%v)", err, expectedErr)
}
},
},
}

for _, tc := range testCases {
t.Run(tc.name, tc.testFunc)
}
Expand Down Expand Up @@ -1486,3 +1594,79 @@ func TestValidateVolumeCapabilities(t *testing.T) {
t.Run(tc.name, tc.testFunc)
}
}

func TestPickAvailabilityZone(t *testing.T) {
testCases := []struct {
name string
testFunc func(t *testing.T)
}{
{
name: "requirement missing ",
testFunc: func(t *testing.T) {
expectedresponse := ""
region := "test"
actualresponse := pickAvailabilityZone(nil, region)
if !reflect.DeepEqual(expectedresponse, actualresponse) {
t.Errorf("actualresponse: (%v), expectedresponse: (%v)", actualresponse, expectedresponse)
}
},
},
{
name: "valid get preferred",
testFunc: func(t *testing.T) {
expectedresponse := "test-01"
region := "test"
mp := make(map[string]string)
mp["N/A"] = "test-01"
topology := &csi.Topology{
Segments: mp,
}
topologies := []*csi.Topology{}
topologies = append(topologies, topology)
req := &csi.TopologyRequirement{
Preferred: topologies,
}
actualresponse := pickAvailabilityZone(req, region)
if !reflect.DeepEqual(expectedresponse, actualresponse) {
t.Errorf("actualresponse: (%v), expectedresponse: (%v)", actualresponse, expectedresponse)
}
},
},
{
name: "valid get requisite",
testFunc: func(t *testing.T) {
expectedresponse := "test-01"
region := "test"
mp := make(map[string]string)
mp["N/A"] = "test-01"
topology := &csi.Topology{
Segments: mp,
}
topologies := []*csi.Topology{}
topologies = append(topologies, topology)
req := &csi.TopologyRequirement{
Requisite: topologies,
}
actualresponse := pickAvailabilityZone(req, region)
if !reflect.DeepEqual(expectedresponse, actualresponse) {
t.Errorf("actualresponse: (%v), expectedresponse: (%v)", actualresponse, expectedresponse)
}
},
},
{
name: "empty request ",
testFunc: func(t *testing.T) {
req := &csi.TopologyRequirement{}
expectedresponse := ""
region := "test"
actualresponse := pickAvailabilityZone(req, region)
if !reflect.DeepEqual(expectedresponse, actualresponse) {
t.Errorf("actualresponse: (%v), expectedresponse: (%v)", actualresponse, expectedresponse)
}
},
},
}
for _, tc := range testCases {
t.Run(tc.name, tc.testFunc)
}
}
26 changes: 23 additions & 3 deletions pkg/azuredisk/nodeserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"k8s.io/utils/mount"

"sigs.k8s.io/azuredisk-csi-driver/pkg/mounter"
volumehelper "sigs.k8s.io/azuredisk-csi-driver/pkg/util"
)

const (
Expand Down Expand Up @@ -232,9 +233,12 @@ func TestNodeGetVolumeStats(t *testing.T) {

func TestNodeStageVolume(t *testing.T) {
stdVolCap := &csi.VolumeCapability_Mount{
Mount: &csi.VolumeCapability_MountVolume{},
Mount: &csi.VolumeCapability_MountVolume{
FsType: defaultLinuxFsType,
},
}

mp := make(map[string]string)
mp["fstype"] = defaultLinuxFsType
stdVolCapBlock := &csi.VolumeCapability_Block{
Block: &csi.VolumeCapability_BlockVolume{},
}
Expand Down Expand Up @@ -287,7 +291,10 @@ func TestNodeStageVolume(t *testing.T) {
req: csi.NodeStageVolumeRequest{VolumeId: "vol_1", StagingTargetPath: sourceTest,
VolumeCapability: &csi.VolumeCapability{AccessMode: &volumeCap,
AccessType: stdVolCap},
PublishContext: publishContext},
PublishContext: publishContext,
VolumeContext: mp,
},

expectedErr: status.Error(codes.Internal, "Failed to find disk on lun /dev/01. cannot parse deviceInfo: /dev/01"),
},
}
Expand Down Expand Up @@ -545,6 +552,10 @@ func TestNodeUnpublishVolume(t *testing.T) {

func TestNodeExpandVolume(t *testing.T) {
d, _ := NewFakeDriver(t)
stdCapacityRange = &csi.CapacityRange{
RequiredBytes: volumehelper.GiBToBytes(15),
LimitBytes: volumehelper.GiBToBytes(10),
}
tests := []struct {
desc string
req csi.NodeExpandVolumeRequest
Expand All @@ -555,6 +566,15 @@ func TestNodeExpandVolume(t *testing.T) {
req: csi.NodeExpandVolumeRequest{},
expectedErr: status.Error(codes.InvalidArgument, "Volume ID not provided"),
},
{
desc: "cound not find path",
req: csi.NodeExpandVolumeRequest{
CapacityRange: stdCapacityRange,
VolumePath: "./test",
VolumeId: "test",
},
expectedErr: status.Error(codes.Internal, "Could not determine device path: exit status 1"),
},
}
for _, test := range tests {
_, err := d.NodeExpandVolume(context.Background(), &test.req)
Expand Down
23 changes: 23 additions & 0 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package util

import (
"os"
"reflect"
"testing"

Expand Down Expand Up @@ -122,3 +123,25 @@ func TestConvertTagsToMap(t *testing.T) {
}
}
}

func TestMakeDir(t *testing.T) {
targetTest := "./target_test"
//Successfully create directory
err := MakeDir(targetTest)
assert.NoError(t, err)

// Remove the directory created
err = os.RemoveAll(targetTest)
assert.NoError(t, err)
}

func TestMakeFile(t *testing.T) {
targetTest := "./target_test"
//Successfully create directory
err := MakeFile(targetTest)
assert.NoError(t, err)

// Remove the directory created
err = os.RemoveAll(targetTest)
assert.NoError(t, err)
}

0 comments on commit f6de053

Please sign in to comment.