Skip to content

Commit 58737f0

Browse files
committed
add regional key testcases
1 parent a4b2fc8 commit 58737f0

File tree

2 files changed

+87
-39
lines changed

2 files changed

+87
-39
lines changed

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,13 @@ func (gceCS *GCEControllerServer) CreateVolume(ctx context.Context, req *csi.Cre
207207
if content.GetVolume() != nil {
208208
volumeContentSourceVolumeID = content.GetVolume().GetVolumeId()
209209
// Verify that the source VolumeID is in the correct format.
210-
project, volKey, err := common.VolumeIDToKey(volumeContentSourceVolumeID)
210+
project, sourceVolKey, err := common.VolumeIDToKey(volumeContentSourceVolumeID)
211211
if err != nil {
212-
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("CreateVolume source volume ID is invalid: %v", err))
212+
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("CreateVolume source volume id is invalid: %v", err))
213213
}
214214

215215
// Verify that the volume in VolumeContentSource exists.
216-
diskFromSourceVolume, err := gceCS.CloudProvider.GetDisk(ctx, project, volKey, gceAPIVersion)
216+
diskFromSourceVolume, err := gceCS.CloudProvider.GetDisk(ctx, project, sourceVolKey, gceAPIVersion)
217217
if err != nil {
218218
if gce.IsGCEError(err, "notFound") {
219219
return nil, status.Errorf(codes.NotFound, "CreateVolume source volume %s does not exist", volumeContentSourceVolumeID)
@@ -229,10 +229,10 @@ func (gceCS *GCEControllerServer) CreateVolume(ctx context.Context, req *csi.Cre
229229
if err == nil {
230230
ready, err := isDiskReady(diskFromSourceVolume)
231231
if err != nil {
232-
return nil, status.Error(codes.Internal, fmt.Sprintf("CreateVolume disk from source volume %v had error checking ready status: %v", volKey, err))
232+
return nil, status.Error(codes.Internal, fmt.Sprintf("CreateVolume disk from source volume %v had error checking ready status: %v", sourceVolKey, err))
233233
}
234234
if !ready {
235-
return nil, status.Error(codes.Internal, fmt.Sprintf("CreateVolume disk from source volume %v is not ready", volKey))
235+
return nil, status.Error(codes.Internal, fmt.Sprintf("CreateVolume disk from source volume %v is not ready", sourceVolKey))
236236
}
237237
}
238238
}

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

+82-34
Original file line numberDiff line numberDiff line change
@@ -902,52 +902,90 @@ func TestCreateVolumeWithVolumeSourceFromSnapshot(t *testing.T) {
902902

903903
func TestCreateVolumeWithVolumeSourceFromVolume(t *testing.T) {
904904
testSourceVolumeName := "test-volume-source-name"
905-
testVolumeSourceID := fmt.Sprintf("projects/%s/zones/%s/disks/%s", project, zone, testSourceVolumeName)
905+
testZonalVolumeSourceID := fmt.Sprintf("projects/%s/zones/%s/disks/%s", project, zone, testSourceVolumeName)
906+
testRegionalVolumeSourceID := fmt.Sprintf("projects/%s/regions/%s/disks/%s", project, region, testSourceVolumeName)
906907
testVolumeSourceIDDifferentZone := fmt.Sprintf("projects/%s/zones/%s/disks/%s", project, "different-zone", testSourceVolumeName)
908+
topology := &csi.TopologyRequirement{
909+
Requisite: []*csi.Topology{
910+
{
911+
Segments: map[string]string{common.TopologyKeyZone: region + "-b"},
912+
},
913+
{
914+
Segments: map[string]string{common.TopologyKeyZone: region + "-c"},
915+
},
916+
},
917+
}
918+
regionalParams := map[string]string{
919+
common.ParameterKeyType: "test-type", common.ParameterKeyReplicationType: "regional-pd",
920+
}
907921
// Define test cases
908922
testCases := []struct {
909-
name string
910-
project string
911-
volKey *meta.Key
912-
volumeOnCloud bool
913-
expErrCode codes.Code
914-
sourceVolumeID string
915-
parameters map[string]string
923+
name string
924+
volumeOnCloud bool
925+
expErrCode codes.Code
926+
sourceVolumeID string
927+
reqParameters map[string]string
928+
sourceReqParameters map[string]string
929+
topology *csi.TopologyRequirement
916930
}{
917931
{
918-
name: "success with data source of volume type",
919-
project: "test-project",
920-
volKey: meta.ZonalKey("my-disk", zone),
921-
volumeOnCloud: true,
922-
sourceVolumeID: testVolumeSourceID,
932+
name: "success with data source of zonal volume type",
933+
volumeOnCloud: true,
934+
sourceVolumeID: testZonalVolumeSourceID,
935+
reqParameters: stdParams,
936+
sourceReqParameters: stdParams,
923937
},
924938
{
925-
name: "fail with data source of volume type that doesn't exist",
926-
project: "test-project",
927-
volKey: meta.ZonalKey("my-disk", zone),
928-
volumeOnCloud: false,
929-
expErrCode: codes.NotFound,
930-
sourceVolumeID: testVolumeSourceID,
939+
name: "success with data source of regional volume type",
940+
volumeOnCloud: true,
941+
sourceVolumeID: testRegionalVolumeSourceID,
942+
reqParameters: regionalParams,
943+
sourceReqParameters: regionalParams,
944+
topology: topology,
931945
},
932946
{
933-
name: "fail with data source of volume type with invalid volume id format",
934-
project: "test-project",
935-
volKey: meta.ZonalKey("my-disk", zone),
936-
volumeOnCloud: false,
937-
expErrCode: codes.InvalidArgument,
938-
sourceVolumeID: testVolumeSourceID + "invalid/format",
947+
name: "fail with with data source of replication-type different from CreateVolumeRequest",
948+
volumeOnCloud: true,
949+
expErrCode: codes.InvalidArgument,
950+
sourceVolumeID: testZonalVolumeSourceID,
951+
reqParameters: stdParams,
952+
sourceReqParameters: regionalParams,
953+
topology: topology,
954+
},
955+
{
956+
name: "fail with data source of zonal volume type that doesn't exist",
957+
volumeOnCloud: false,
958+
expErrCode: codes.NotFound,
959+
sourceVolumeID: testZonalVolumeSourceID,
960+
reqParameters: stdParams,
961+
sourceReqParameters: stdParams,
962+
},
963+
{
964+
name: "fail with data source of zonal volume type with invalid volume id format",
965+
volumeOnCloud: false,
966+
expErrCode: codes.InvalidArgument,
967+
sourceVolumeID: testZonalVolumeSourceID + "invalid/format",
968+
reqParameters: stdParams,
969+
sourceReqParameters: stdParams,
939970
},
940971
{
941-
name: "fail with data source of volume type with invalid disk parameters",
942-
project: "test-project",
943-
volKey: meta.ZonalKey("my-disk", zone),
972+
name: "fail with data source of zonal volume type with invalid disk parameters",
944973
volumeOnCloud: true,
945974
expErrCode: codes.InvalidArgument,
946975
sourceVolumeID: testVolumeSourceIDDifferentZone,
947-
parameters: map[string]string{
976+
reqParameters: stdParams,
977+
sourceReqParameters: map[string]string{
948978
common.ParameterKeyType: "different-type",
949979
},
950980
},
981+
{
982+
name: "fail with data source of zonal volume type with invalid replication type",
983+
volumeOnCloud: true,
984+
expErrCode: codes.InvalidArgument,
985+
sourceVolumeID: testZonalVolumeSourceID,
986+
reqParameters: regionalParams,
987+
sourceReqParameters: stdParams,
988+
},
951989
}
952990

953991
for _, tc := range testCases {
@@ -958,7 +996,7 @@ func TestCreateVolumeWithVolumeSourceFromVolume(t *testing.T) {
958996
Name: name,
959997
CapacityRange: stdCapRange,
960998
VolumeCapabilities: stdVolCaps,
961-
Parameters: stdParams,
999+
Parameters: tc.reqParameters,
9621000
VolumeContentSource: &csi.VolumeContentSource{
9631001
Type: &csi.VolumeContentSource_Volume{
9641002
Volume: &csi.VolumeContentSource_VolumeSource{
@@ -972,16 +1010,26 @@ func TestCreateVolumeWithVolumeSourceFromVolume(t *testing.T) {
9721010
Name: testSourceVolumeName,
9731011
CapacityRange: stdCapRange,
9741012
VolumeCapabilities: stdVolCaps,
975-
Parameters: stdParams,
1013+
Parameters: tc.sourceReqParameters,
9761014
}
977-
if tc.parameters != nil {
978-
sourceVolumeRequest.Parameters = tc.parameters
1015+
1016+
if tc.topology != nil {
1017+
// req.AccessibilityRequirements = tc.topology
1018+
sourceVolumeRequest.AccessibilityRequirements = tc.topology
9791019
}
9801020

9811021
if tc.volumeOnCloud {
9821022
// Create the source volume.
983-
gceDriver.cs.CreateVolume(context.Background(), sourceVolumeRequest)
1023+
sourceVolume, _ := gceDriver.cs.CreateVolume(context.Background(), sourceVolumeRequest)
1024+
req.VolumeContentSource = &csi.VolumeContentSource{
1025+
Type: &csi.VolumeContentSource_Volume{
1026+
Volume: &csi.VolumeContentSource_VolumeSource{
1027+
VolumeId: sourceVolume.GetVolume().VolumeId,
1028+
},
1029+
},
1030+
}
9841031
}
1032+
9851033
resp, err := gceDriver.cs.CreateVolume(context.Background(), req)
9861034
t.Logf("response: %v err: %v", resp, err)
9871035
if err != nil {

0 commit comments

Comments
 (0)