Skip to content

Commit 5e9a5d5

Browse files
committed
Fix unit tests and add a check for volume existence for CreateSnapshot for errors conforming to spec
1 parent db213e5 commit 5e9a5d5

File tree

2 files changed

+52
-32
lines changed

2 files changed

+52
-32
lines changed

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func (gceCS *GCEControllerServer) DeleteVolume(ctx context.Context, req *csi.Del
232232

233233
volKey, err = gceCS.CloudProvider.RepairUnderspecifiedVolumeKey(ctx, volKey)
234234
if err != nil {
235-
klog.Warningf("Treating volume as deleted because cannot find volume %v: %v", volKey.String(), err)
235+
klog.Warningf("Treating volume as deleted because cannot find volume %v: %v", volumeID, err)
236236
return &csi.DeleteVolumeResponse{}, nil
237237
}
238238

@@ -547,6 +547,15 @@ func (gceCS *GCEControllerServer) CreateSnapshot(ctx context.Context, req *csi.C
547547
}
548548
defer gceCS.volumeLocks.Release(volumeID)
549549

550+
// Check if volume exists
551+
_, err = gceCS.CloudProvider.GetDisk(ctx, volKey)
552+
if err != nil {
553+
if gce.IsGCENotFoundError(err) {
554+
return nil, status.Error(codes.NotFound, fmt.Sprintf("CreateSnapshot could not find disk %v: %v", volKey.String(), err))
555+
}
556+
return nil, status.Error(codes.Internal, fmt.Sprintf("CreateSnapshot unknown get disk error: %v", err))
557+
}
558+
550559
// Check if snapshot already exists
551560
var snapshot *compute.Snapshot
552561
snapshot, err = gceCS.CloudProvider.GetSnapshot(ctx, req.Name)

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

+42-31
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,10 @@ var (
5959
Segments: map[string]string{common.TopologyKeyZone: metadataservice.FakeZone},
6060
},
6161
}
62-
testVolumeID = fmt.Sprintf("projects/%s/zones/%s/disks/%s", project, zone, name)
63-
region, _ = common.GetRegionFromZones([]string{zone})
64-
testRegionalID = fmt.Sprintf("projects/%s/regions/%s/disks/%s", project, region, name)
65-
testSnapshotID = fmt.Sprintf("projects/%s/global/snapshots/%s", project, name)
66-
totalSnapshotsNumber = 5
62+
testVolumeID = fmt.Sprintf("projects/%s/zones/%s/disks/%s", project, zone, name)
63+
region, _ = common.GetRegionFromZones([]string{zone})
64+
testRegionalID = fmt.Sprintf("projects/%s/regions/%s/disks/%s", project, region, name)
65+
testSnapshotID = fmt.Sprintf("projects/%s/global/snapshots/%s", project, name)
6766
)
6867

6968
func TestCreateSnapshotArguments(t *testing.T) {
@@ -76,6 +75,7 @@ func TestCreateSnapshotArguments(t *testing.T) {
7675
testCases := []struct {
7776
name string
7877
req *csi.CreateSnapshotRequest
78+
seedDisks []*gce.CloudDisk
7979
expSnapshot *csi.Snapshot
8080
expErrCode codes.Code
8181
}{
@@ -85,6 +85,9 @@ func TestCreateSnapshotArguments(t *testing.T) {
8585
Name: name,
8686
SourceVolumeId: testVolumeID,
8787
},
88+
seedDisks: []*gce.CloudDisk{
89+
createZonalCloudDisk(name),
90+
},
8891
expSnapshot: &csi.Snapshot{
8992
SnapshotId: testSnapshotID,
9093
SourceVolumeId: testVolumeID,
@@ -110,19 +113,27 @@ func TestCreateSnapshotArguments(t *testing.T) {
110113
expErrCode: codes.InvalidArgument,
111114
},
112115
{
113-
name: "fail wrong source volume",
116+
name: "fail not found source volume",
114117
req: &csi.CreateSnapshotRequest{
115118
Name: name,
116-
SourceVolumeId: "/test/wrongname",
119+
SourceVolumeId: common.CreateZonalVolumeID(project, zone, "non-exist-vol-name"),
117120
},
118121
expErrCode: codes.NotFound,
119122
},
123+
{
124+
name: "fail invalid source volume",
125+
req: &csi.CreateSnapshotRequest{
126+
Name: name,
127+
SourceVolumeId: "/test/wrongname",
128+
},
129+
expErrCode: codes.InvalidArgument,
130+
},
120131
}
121132

122133
for _, tc := range testCases {
123134
t.Logf("test case: %s", tc.name)
124135
// Setup new driver each time so no interference
125-
gceDriver := initGCEDriver(t, nil)
136+
gceDriver := initGCEDriver(t, tc.seedDisks)
126137

127138
// Start Test
128139
resp, err := gceDriver.cs.CreateSnapshot(context.Background(), tc.req)
@@ -210,44 +221,45 @@ func TestListSnapshotsArguments(t *testing.T) {
210221
testCases := []struct {
211222
name string
212223
req *csi.ListSnapshotsRequest
213-
expSnapshots int
224+
numSnapshots int
214225
expErrCode codes.Code
215226
}{
216227
{
217228
name: "valid",
218229
req: &csi.ListSnapshotsRequest{
219-
SnapshotId: testSnapshotID,
230+
SnapshotId: testSnapshotID + "0",
220231
},
221-
expSnapshots: 1,
232+
numSnapshots: 1,
222233
},
223234
{
224235
name: "invalid id",
225236
req: &csi.ListSnapshotsRequest{
226237
SnapshotId: testSnapshotID + "/foo",
227238
},
228-
expSnapshots: 0,
239+
numSnapshots: 0,
229240
},
230241
{
231242
name: "no id",
232243
req: &csi.ListSnapshotsRequest{
233244
SnapshotId: "",
234245
},
235-
expSnapshots: totalSnapshotsNumber + 1,
246+
numSnapshots: 5,
236247
},
237248
}
238249

239250
for _, tc := range testCases {
240251
t.Logf("test case: %s", tc.name)
241-
// Setup new driver each time so no interference
242-
gceDriver := initGCEDriver(t, nil)
243252

244-
createReq := &csi.CreateSnapshotRequest{
245-
Name: name,
246-
SourceVolumeId: testVolumeID,
253+
disks := []*gce.CloudDisk{}
254+
for i := 0; i < tc.numSnapshots; i++ {
255+
sname := fmt.Sprintf("%s%d", name, i)
256+
disks = append(disks, createZonalCloudDisk(sname))
247257
}
248-
gceDriver.cs.CreateSnapshot(context.Background(), createReq)
249258

250-
for i := 0; i < totalSnapshotsNumber; i++ {
259+
// Setup new driver each time so no interference
260+
gceDriver := initGCEDriver(t, disks)
261+
262+
for i := 0; i < tc.numSnapshots; i++ {
251263
volumeID := fmt.Sprintf("%s%d", testVolumeID, i)
252264
nameID := fmt.Sprintf("%s%d", name, i)
253265
createReq := &csi.CreateSnapshotRequest{
@@ -280,16 +292,16 @@ func TestListSnapshotsArguments(t *testing.T) {
280292
// Make sure responses match
281293
snapshots := resp.GetEntries()
282294
//expectsnapshots := expSnapshot.GetEntries()
283-
if (snapshots == nil || len(snapshots) == 0) && tc.expSnapshots == 0 {
295+
if (snapshots == nil || len(snapshots) == 0) && tc.numSnapshots == 0 {
284296
continue
285297
}
286298

287299
if snapshots == nil || len(snapshots) == 0 {
288300
// If one is nil or empty but not both
289-
t.Fatalf("Expected snapshots number %v, got no snapshot", tc.expSnapshots)
301+
t.Fatalf("Expected snapshots number %v, got no snapshot", tc.numSnapshots)
290302
}
291-
if len(snapshots) != tc.expSnapshots {
292-
errStr := fmt.Sprintf("Expected snapshot: %#v\n to equal snapshot: %#v\n", snapshots[0].Snapshot, tc.expSnapshots)
303+
if len(snapshots) != tc.numSnapshots {
304+
errStr := fmt.Sprintf("Expected snapshot: %#v\n to equal snapshot: %#v\n", snapshots[0].Snapshot, tc.numSnapshots)
293305
t.Errorf(errStr)
294306
}
295307
}
@@ -664,8 +676,6 @@ func TestCreateVolumeArguments(t *testing.T) {
664676
// Setup new driver each time so no interference
665677
gceDriver := initGCEDriver(t, nil)
666678

667-
//gceDriver.cs.CloudProvider.CreateSnapshot(context.Background, )
668-
669679
// Start Test
670680
resp, err := gceDriver.cs.CreateVolume(context.Background(), tc.req)
671681
//check response
@@ -731,8 +741,6 @@ func TestCreateVolumeWithVolumeSource(t *testing.T) {
731741
// Setup new driver each time so no interference
732742
gceDriver := initGCEDriver(t, nil)
733743

734-
//gceDriver.cs.CloudProvider.CreateSnapshot(context.Background, )
735-
736744
// Start Test
737745
req := &csi.CreateVolumeRequest{
738746
Name: "test-name",
@@ -857,14 +865,14 @@ func TestDeleteVolume(t *testing.T) {
857865
expErr: false,
858866
},
859867
{
860-
name: "non-repairable ID",
868+
name: "non-repairable ID (invalid)",
861869
seedDisks: []*gce.CloudDisk{
862870
createZonalCloudDisk("nottherightname"),
863871
},
864872
req: &csi.DeleteVolumeRequest{
865873
VolumeId: common.GenerateUnderspecifiedVolumeID(name, true /* isZonal */),
866874
},
867-
expErr: true,
875+
expErr: false,
868876
},
869877
}
870878
for _, tc := range testCases {
@@ -1416,7 +1424,10 @@ func TestPickRandAndConsecutive(t *testing.T) {
14161424

14171425
func TestVolumeOperationConcurrency(t *testing.T) {
14181426
readyToExecute := make(chan chan struct{}, 1)
1419-
gceDriver := initBlockingGCEDriver(t, nil, readyToExecute)
1427+
gceDriver := initBlockingGCEDriver(t, []*gce.CloudDisk{
1428+
createZonalCloudDisk(name + "1"),
1429+
createZonalCloudDisk(name + "2"),
1430+
}, readyToExecute)
14201431
cs := gceDriver.cs
14211432

14221433
vol1CreateSnapshotAReq := &csi.CreateSnapshotRequest{

0 commit comments

Comments
 (0)