Skip to content

Commit 7b74b9d

Browse files
committed
Fix unit tests around create volume to deal with non-existant snapshots
1 parent 79ade28 commit 7b74b9d

File tree

1 file changed

+75
-28
lines changed

1 file changed

+75
-28
lines changed

pkg/gce-pd-csi-driver/controller_test.go

+75-28
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ import (
2222
"testing"
2323
"time"
2424

25+
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
2526
"github.com/golang/protobuf/ptypes"
2627

2728
"context"
29+
2830
compute "google.golang.org/api/compute/v1"
2931
"google.golang.org/grpc/codes"
3032
"google.golang.org/grpc/status"
@@ -588,34 +590,6 @@ func TestCreateVolumeArguments(t *testing.T) {
588590
},
589591
},
590592
},
591-
{
592-
name: "success with data source of snapshot type",
593-
req: &csi.CreateVolumeRequest{
594-
Name: "test-name",
595-
CapacityRange: stdCapRange,
596-
VolumeCapabilities: stdVolCaps,
597-
VolumeContentSource: &csi.VolumeContentSource{
598-
Type: &csi.VolumeContentSource_Snapshot{
599-
Snapshot: &csi.VolumeContentSource_SnapshotSource{
600-
SnapshotId: "snapshot-source",
601-
},
602-
},
603-
},
604-
},
605-
expVol: &csi.Volume{
606-
CapacityBytes: common.GbToBytes(20),
607-
VolumeId: testVolumeId,
608-
VolumeContext: nil,
609-
AccessibleTopology: stdTopology,
610-
ContentSource: &csi.VolumeContentSource{
611-
Type: &csi.VolumeContentSource_Snapshot{
612-
Snapshot: &csi.VolumeContentSource_SnapshotSource{
613-
SnapshotId: "snapshot-source",
614-
},
615-
},
616-
},
617-
},
618-
},
619593
{
620594
name: "success with block volume capability",
621595
req: &csi.CreateVolumeRequest{
@@ -690,6 +664,8 @@ func TestCreateVolumeArguments(t *testing.T) {
690664
// Setup new driver each time so no interference
691665
gceDriver := initGCEDriver(t, nil)
692666

667+
//gceDriver.cs.CloudProvider.CreateSnapshot(context.Background, )
668+
693669
// Start Test
694670
resp, err := gceDriver.cs.CreateVolume(context.Background(), tc.req)
695671
//check response
@@ -728,6 +704,77 @@ func TestCreateVolumeArguments(t *testing.T) {
728704
}
729705
}
730706

707+
func TestCreateVolumeWithVolumeSource(t *testing.T) {
708+
// Define test cases
709+
testCases := []struct {
710+
name string
711+
volKey *meta.Key
712+
snapshotOnCloud bool
713+
expErrCode codes.Code
714+
}{
715+
{
716+
name: "success with data source of snapshot type",
717+
volKey: meta.ZonalKey("my-disk", zone),
718+
snapshotOnCloud: true,
719+
},
720+
{
721+
name: "fail with data source of snapshot type that doesn't exist",
722+
volKey: meta.ZonalKey("my-disk", zone),
723+
snapshotOnCloud: false,
724+
expErrCode: codes.NotFound,
725+
},
726+
}
727+
728+
// Run test cases
729+
for _, tc := range testCases {
730+
t.Logf("test case: %s", tc.name)
731+
// Setup new driver each time so no interference
732+
gceDriver := initGCEDriver(t, nil)
733+
734+
//gceDriver.cs.CloudProvider.CreateSnapshot(context.Background, )
735+
736+
// Start Test
737+
req := &csi.CreateVolumeRequest{
738+
Name: "test-name",
739+
CapacityRange: stdCapRange,
740+
VolumeCapabilities: stdVolCaps,
741+
VolumeContentSource: &csi.VolumeContentSource{
742+
Type: &csi.VolumeContentSource_Snapshot{
743+
Snapshot: &csi.VolumeContentSource_SnapshotSource{
744+
SnapshotId: testSnapshotId,
745+
},
746+
},
747+
},
748+
}
749+
750+
if tc.snapshotOnCloud {
751+
gceDriver.cs.CloudProvider.CreateSnapshot(context.Background(), tc.volKey, name)
752+
}
753+
resp, err := gceDriver.cs.CreateVolume(context.Background(), req)
754+
//check response
755+
if err != nil {
756+
serverError, ok := status.FromError(err)
757+
if !ok {
758+
t.Fatalf("Could not get error status code from err: %v", serverError)
759+
}
760+
if serverError.Code() != tc.expErrCode {
761+
t.Fatalf("Expected error code: %v, got: %v. err : %v", tc.expErrCode, serverError.Code(), err)
762+
}
763+
continue
764+
}
765+
if tc.expErrCode != codes.OK {
766+
t.Fatalf("Expected error: %v, got no error", tc.expErrCode)
767+
}
768+
769+
// Make sure response has snapshot
770+
vol := resp.GetVolume()
771+
if vol.ContentSource == nil || vol.ContentSource.Type == nil || vol.ContentSource.GetSnapshot() == nil || vol.ContentSource.GetSnapshot().SnapshotId == "" {
772+
t.Fatalf("Expected volume content source to have snapshot ID, got none")
773+
}
774+
775+
}
776+
}
777+
731778
func TestCreateVolumeRandomRequisiteTopology(t *testing.T) {
732779
req := &csi.CreateVolumeRequest{
733780
Name: "test-name",

0 commit comments

Comments
 (0)