Skip to content

Commit bf680f1

Browse files
committed
Add unit tests
1 parent 63b7a71 commit bf680f1

File tree

2 files changed

+198
-1
lines changed

2 files changed

+198
-1
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ require (
1515
golang.org/x/sync v0.1.0
1616
golang.org/x/sys v0.18.0
1717
google.golang.org/grpc v1.56.3
18+
google.golang.org/protobuf v1.33.0
1819
k8s.io/api v0.29.0
1920
k8s.io/apimachinery v0.29.0
2021
k8s.io/client-go v0.29.0
@@ -57,7 +58,6 @@ require (
5758
golang.org/x/time v0.3.0 // indirect
5859
google.golang.org/appengine v1.6.7 // indirect
5960
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
60-
google.golang.org/protobuf v1.33.0 // indirect
6161
gopkg.in/inf.v0 v0.9.1 // indirect
6262
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
6363
gopkg.in/yaml.v2 v2.4.0 // indirect

pkg/driver/controller_server_test.go

+197
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"github.com/civo/civogo"
99
"github.com/container-storage-interface/spec/lib/go/csi"
1010
"github.com/stretchr/testify/assert"
11+
"google.golang.org/grpc/codes"
12+
"google.golang.org/grpc/status"
1113
)
1214

1315
func TestCreateVolume(t *testing.T) {
@@ -291,6 +293,201 @@ func TestGetCapacity(t *testing.T) {
291293

292294
}
293295

296+
func TestCreateSnapshot(t *testing.T){
297+
tests := []struct {
298+
name string
299+
req *csi.CreateSnapshotRequest
300+
sourceVolume *civogo.Volume
301+
volSnapshots []civogo.VolumeSnapshot
302+
expectedError error
303+
expectedResp *csi.CreateSnapshotResponse
304+
}{
305+
{
306+
name: "snapshot name missing",
307+
req: &csi.CreateSnapshotRequest{
308+
Name: "snapshot-vol-1",
309+
SourceVolumeId: "vol-1",
310+
},
311+
sourceVolume: &civogo.Volume{
312+
ID: "vol-1",
313+
},
314+
volSnapshots: []civogo.VolumeSnapshot{
315+
{
316+
Name: "snapshot-vol-1",
317+
VolumeID: "vol-x",
318+
},
319+
},
320+
expectedError: status.Error(codes.AlreadyExists, "snapshot with the same name \"snapshot-vol-1\" but with different SourceVolumeId already exist"),
321+
expectedResp: nil,
322+
},
323+
}
324+
325+
for _, tt := range tests {
326+
t.Run(tt.name, func(t *testing.T) {
327+
fc, _ := civogo.NewFakeClient()
328+
fc.Volumes = []civogo.Volume{*tt.sourceVolume}
329+
fc.VolumeSnapshots = tt.volSnapshots
330+
331+
332+
d, _ := driver.NewTestDriver(fc)
333+
334+
resp, err := d.CreateSnapshot(context.Background(), tt.req)
335+
336+
if tt.expectedError != nil {
337+
assert.Nil(t, resp)
338+
assert.Equal(t, tt.expectedError, err)
339+
} else {
340+
assert.Nil(t, err)
341+
assert.Equal(t, tt.expectedResp, resp)
342+
}
343+
})
344+
}
345+
346+
}
347+
348+
func TestListSnapshots(t *testing.T) {
349+
tests := []struct {
350+
name string
351+
req *csi.ListSnapshotsRequest
352+
snapshots []civogo.VolumeSnapshot
353+
expectedError error
354+
expectedResp *csi.ListSnapshotsResponse
355+
}{
356+
{
357+
name: "No snapshots found",
358+
req: &csi.ListSnapshotsRequest{},
359+
snapshots: []civogo.VolumeSnapshot{},
360+
expectedResp: &csi.ListSnapshotsResponse{
361+
Entries: []*csi.ListSnapshotsResponse_Entry{},
362+
},
363+
},
364+
{
365+
name: "starting token provided",
366+
req: &csi.ListSnapshotsRequest{
367+
StartingToken: "12",
368+
},
369+
snapshots: []civogo.VolumeSnapshot{},
370+
expectedError: status.Error(codes.Aborted, "starting-token not supported"),
371+
expectedResp: nil,
372+
},
373+
{
374+
name: "Both snapshotID and sourceVolumeID given",
375+
req: &csi.ListSnapshotsRequest{
376+
SnapshotId: "snap-1",
377+
SourceVolumeId: "vol-1",
378+
},
379+
snapshots: []civogo.VolumeSnapshot{
380+
{SnapshotID: "snap-1", VolumeID: "vol-1"},
381+
},
382+
expectedResp: &csi.ListSnapshotsResponse{
383+
Entries: []*csi.ListSnapshotsResponse_Entry{
384+
{Snapshot: &csi.Snapshot{SnapshotId: "snap-1", SourceVolumeId: "vol-1"}},
385+
},
386+
},
387+
},
388+
{
389+
name: "Only snapshotID given",
390+
req: &csi.ListSnapshotsRequest{
391+
SnapshotId: "snap-1",
392+
},
393+
snapshots: []civogo.VolumeSnapshot{
394+
{SnapshotID: "snap-1", VolumeID: "vol-1"},
395+
},
396+
expectedResp: &csi.ListSnapshotsResponse{
397+
Entries: []*csi.ListSnapshotsResponse_Entry{
398+
{Snapshot: &csi.Snapshot{SnapshotId: "snap-1", SourceVolumeId: "vol-1"}},
399+
},
400+
},
401+
},
402+
{
403+
name: "non-existing snapshotID given",
404+
req: &csi.ListSnapshotsRequest{
405+
SnapshotId: "snap-2",
406+
},
407+
snapshots: []civogo.VolumeSnapshot{
408+
{SnapshotID: "snap-1", VolumeID: "vol-1"},
409+
},
410+
expectedResp: &csi.ListSnapshotsResponse{},
411+
},
412+
{
413+
name: "Only sourceVolumeID having single snapshot given",
414+
req: &csi.ListSnapshotsRequest{
415+
SourceVolumeId: "vol-1",
416+
},
417+
snapshots: []civogo.VolumeSnapshot{
418+
{SnapshotID: "snap-1", VolumeID: "vol-1"},
419+
},
420+
expectedResp: &csi.ListSnapshotsResponse{
421+
Entries: []*csi.ListSnapshotsResponse_Entry{
422+
{Snapshot: &csi.Snapshot{SnapshotId: "snap-1", SourceVolumeId: "vol-1"}},
423+
},
424+
},
425+
},
426+
{
427+
name: "Only sourceVolumeID having multiple snapshots given",
428+
req: &csi.ListSnapshotsRequest{
429+
SourceVolumeId: "vol-1",
430+
},
431+
snapshots: []civogo.VolumeSnapshot{
432+
{SnapshotID: "snap-1", VolumeID: "vol-1"},
433+
{SnapshotID: "snap-2", VolumeID: "vol-1"},
434+
{SnapshotID: "snap-3", VolumeID: "vol-1"},
435+
},
436+
expectedResp: &csi.ListSnapshotsResponse{
437+
Entries: []*csi.ListSnapshotsResponse_Entry{
438+
{Snapshot: &csi.Snapshot{SnapshotId: "snap-1", SourceVolumeId: "vol-1"}},
439+
{Snapshot: &csi.Snapshot{SnapshotId: "snap-2", SourceVolumeId: "vol-1"}},
440+
{Snapshot: &csi.Snapshot{SnapshotId: "snap-3", SourceVolumeId: "vol-1"}},
441+
},
442+
},
443+
},
444+
{
445+
name: "Multiple snapshots found",
446+
req: &csi.ListSnapshotsRequest{},
447+
snapshots: []civogo.VolumeSnapshot{
448+
{SnapshotID: "snap-2", VolumeID: "vol-2"},
449+
{SnapshotID: "snap-1", VolumeID: "vol-1"},
450+
},
451+
expectedResp: &csi.ListSnapshotsResponse{
452+
Entries: []*csi.ListSnapshotsResponse_Entry{
453+
{Snapshot: &csi.Snapshot{SnapshotId: "snap-1", SourceVolumeId: "vol-1"}},
454+
{Snapshot: &csi.Snapshot{SnapshotId: "snap-2", SourceVolumeId: "vol-2"}},
455+
},
456+
},
457+
},
458+
{
459+
name: "Empty snapshot creationTime",
460+
req: &csi.ListSnapshotsRequest{},
461+
snapshots: []civogo.VolumeSnapshot{
462+
{SnapshotID: "snap-1", VolumeID: "vol-1", CreationTime: ""},
463+
},
464+
expectedResp: &csi.ListSnapshotsResponse{
465+
Entries: []*csi.ListSnapshotsResponse_Entry{
466+
{Snapshot: &csi.Snapshot{SnapshotId: "snap-1", SourceVolumeId: "vol-1"}},
467+
},
468+
},
469+
},
470+
}
471+
472+
for _, tt := range tests {
473+
t.Run(tt.name, func(t *testing.T) {
474+
fc, _ := civogo.NewFakeClient()
475+
fc.VolumeSnapshots = tt.snapshots
476+
477+
d, _ := driver.NewTestDriver(fc)
478+
479+
resp, err := d.ListSnapshots(context.Background(), tt.req)
480+
481+
if tt.expectedError != nil {
482+
assert.Equal(t, tt.expectedError, err)
483+
} else {
484+
assert.Nil(t, err)
485+
assert.Equal(t, tt.expectedResp, resp)
486+
}
487+
})
488+
}
489+
}
490+
294491
func TestConvertSnapshot(t *testing.T) {
295492
creationTime := "2024-02-12T10:00:00Z"
296493
expectedTime, _ := driver.ParseTimeToProtoTimestamp(creationTime)

0 commit comments

Comments
 (0)