Skip to content

Commit

Permalink
Merge pull request #422 from Sakuralbj/master
Browse files Browse the repository at this point in the history
Add Unit tests in pkg/azuredisk/controllerserver_test.
  • Loading branch information
k8s-ci-robot committed Jun 8, 2020
2 parents 1b1b927 + 9070eaf commit d7c5e98
Showing 1 changed file with 217 additions and 0 deletions.
217 changes: 217 additions & 0 deletions pkg/azuredisk/controllerserver_test.go
Expand Up @@ -19,6 +19,8 @@ package azuredisk
import (
"context"
"fmt"
"github.com/stretchr/testify/assert"
"k8s.io/legacy-cloud-providers/azure"
"reflect"
"testing"

Expand Down Expand Up @@ -312,3 +314,218 @@ func TestDeleteVolume(t *testing.T) {
}
}
}

func TestIsCSISnapshotReady(t *testing.T) {
tests := []struct {
state string
expectedResp bool
}{
{
state: "Succeeded",
expectedResp: true,
},
{
state: "succeeded",
expectedResp: true,
},
{
state: "fail",
expectedResp: false,
},
}
for _, test := range tests {
flag, err := isCSISnapshotReady(test.state)

if flag != test.expectedResp {
t.Errorf("testdesc: %v \n expected result:%t \n actual result:%t", test.state, test.expectedResp, flag)
}
assert.Nil(t, err)
}
}

func TestExtractSnapshotInfo(t *testing.T) {
d, err := NewFakeDriver(t)
if err != nil {
t.Fatalf("Error getting driver: %v", err)
}
tests := []struct {
snapshotID string
expected1 string
expected2 string
expected3 error
}{
{
snapshotID: "testurl/subscriptions/12/resourceGroups/23/providers/Microsoft.Compute/snapshots/snapshot-name",
expected1: "snapshot-name",
expected2: "23",
expected3: nil,
},
{
// case insentive check
snapshotID: "testurl/subscriptions/12/resourcegroups/23/providers/Microsoft.Compute/snapshots/snapshot-name",
expected1: "snapshot-name",
expected2: "23",
expected3: nil,
},
{
snapshotID: "testurl/subscriptions/23/providers/Microsoft.Compute/snapshots/snapshot-name",
expected1: "",
expected2: "",
expected3: fmt.Errorf("could not get snapshot name from testurl/subscriptions/23/providers/Microsoft.Compute/snapshots/snapshot-name, correct format: (?i).*/subscriptions/(?:.*)/resourceGroups/(?:.*)/providers/Microsoft.Compute/snapshots/(.+)"),
},
{
// case snapshotID does not contain subscriptions
snapshotID: "testurl/subscription/12/resourcegroups/23/providers/Microsoft.Compute/snapshots/snapshot-name",
expected1: "testurl/subscription/12/resourcegroups/23/providers/Microsoft.Compute/snapshots/snapshot-name",
expected2: d.cloud.ResourceGroup,
expected3: nil,
},
}
for _, test := range tests {
snapshotName, resourceGroup, err := d.extractSnapshotInfo(test.snapshotID)
if !reflect.DeepEqual(snapshotName, test.expected1) || !reflect.DeepEqual(resourceGroup, test.expected2) || !reflect.DeepEqual(err, test.expected3) {
t.Errorf("input: %q, getSnapshotName result: %q, expected1: %q, getresourcegroup result: %q, expected2: %q\n", test.snapshotID, snapshotName, test.expected1,
resourceGroup, test.expected2)
if err != nil {
t.Errorf("err result %q\n", err)
}
}
}
}

func TestControllerPublishVolume(t *testing.T) {
d, err := NewFakeDriver(t)
d.cloud = &azure.Cloud{}
if err != nil {
t.Fatalf("Error getting driver: %v", err)
}
tests := []struct {
desc string
req *csi.ControllerPublishVolumeRequest
expectedErr error
}{
{
desc: "Volume ID missing",
req: &csi.ControllerPublishVolumeRequest{},
expectedErr: status.Error(codes.InvalidArgument, "Volume ID not provided"),
},
{
desc: "Volume capability missing",
req: &csi.ControllerPublishVolumeRequest{
VolumeId: "vol_1",
},
expectedErr: status.Error(codes.InvalidArgument, "Volume capability not provided"),
},
}
for _, test := range tests {
_, err := d.ControllerPublishVolume(context.Background(), test.req)
if !reflect.DeepEqual(err, test.expectedErr) {
t.Errorf("Unexpected error: %v", err)
}
}
}

func TestIsValidVolumeCapabilities(t *testing.T) {
var caps []*csi.VolumeCapability
stdVolCap := csi.VolumeCapability{
AccessMode: &csi.VolumeCapability_AccessMode{
Mode: csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
},
}
caps = append(caps, &stdVolCap)
if !isValidVolumeCapabilities(caps) {
t.Errorf("Unexpected error")
}
stdVolCap1 := csi.VolumeCapability{
AccessMode: &csi.VolumeCapability_AccessMode{
Mode: 10,
},
}
caps = append(caps, &stdVolCap1)
if isValidVolumeCapabilities(caps) {
t.Errorf("Unexpected error")
}
}

func TestControllerExpandVolume(t *testing.T) {
stdVolSize := int64(5 * 1024 * 1024 * 1024)
stdCapRange := &csi.CapacityRange{RequiredBytes: stdVolSize}

testCases := []struct {
name string
testFunc func(t *testing.T)
}{
{
name: "Volume ID missing",
testFunc: func(t *testing.T) {
req := &csi.ControllerExpandVolumeRequest{}

ctx := context.Background()
d, _ := NewFakeDriver(t)

expectedErr := status.Error(codes.InvalidArgument, "Volume ID missing in request")
_, err := d.ControllerExpandVolume(ctx, req)
if !reflect.DeepEqual(err, expectedErr) {
t.Errorf("Unexpected error: %v", err)
}
},
},
{
name: "Volume capabilities missing",
testFunc: func(t *testing.T) {
req := &csi.ControllerExpandVolumeRequest{
VolumeId: "vol_1",
}

ctx := context.Background()
d, _ := NewFakeDriver(t)
var csc []*csi.ControllerServiceCapability
d.Cap = csc
expectedErr := status.Error(codes.InvalidArgument, "invalid expand volume request: volume_id:\"vol_1\" ")
_, err := d.ControllerExpandVolume(ctx, req)
if !reflect.DeepEqual(err, expectedErr) {
t.Errorf("Unexpected error: %v", err)
}
},
},
{
name: "Volume Capacity range missing",
testFunc: func(t *testing.T) {
req := &csi.ControllerExpandVolumeRequest{
VolumeId: "vol_1",
}

ctx := context.Background()
d, _ := NewFakeDriver(t)

expectedErr := status.Error(codes.InvalidArgument, "volume capacity range missing in request")
_, 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) {
req := &csi.ControllerExpandVolumeRequest{
VolumeId: "vol_1",
CapacityRange: stdCapRange,
}

ctx := context.Background()
d, _ := NewFakeDriver(t)

expectedErr := status.Errorf(codes.InvalidArgument, "disk URI(vol_1) is not valid: Inavlid DiskURI: vol_1, correct format: [/subscriptions/{sub-id}/resourcegroups/{group-name}/providers/microsoft.compute/disks/{disk-id}]")
_, err := d.ControllerExpandVolume(ctx, req)
if !reflect.DeepEqual(err, expectedErr) {
t.Errorf("Unexpected error: %v", err)
}
},
},
}

for _, tc := range testCases {
t.Run(tc.name, tc.testFunc)
}
}

0 comments on commit d7c5e98

Please sign in to comment.