Skip to content

Commit 692944b

Browse files
committed
chore: add unit test for convertSnapshot method
- Handle empty snapshot creationTime. - Export private methods IsSnapshotReady, ConvertSnapshot, ToCSISnapshot
1 parent 308018b commit 692944b

File tree

2 files changed

+86
-21
lines changed

2 files changed

+86
-21
lines changed

Diff for: pkg/driver/controller_server.go

+27-21
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ func (d *Driver) CreateSnapshot(ctx context.Context, req *csi.CreateSnapshotRequ
647647
continue
648648
}
649649
if snapshot.VolumeID == sourceVolID {
650-
snap, err := toCSISnapshot(&snapshot)
650+
snap, err := ToCSISnapshot(&snapshot)
651651
if err != nil{
652652
log.Error().
653653
Str("snapshot_name", snapshotName).
@@ -704,7 +704,7 @@ func (d *Driver) CreateSnapshot(ctx context.Context, req *csi.CreateSnapshotRequ
704704
return nil, status.Error(codes.Internal, fmt.Sprintf("failed to parse creation time: %v", err))
705705
}
706706

707-
isReady := isSnapshotReady(snapshot.State)
707+
isReady := IsSnapshotReady(snapshot.State)
708708

709709
return &csi.CreateSnapshotResponse{
710710
Snapshot: &csi.Snapshot{
@@ -783,7 +783,7 @@ func (d *Driver) ListSnapshots(ctx context.Context, req *csi.ListSnapshotsReques
783783
Msg("Failed to list snapshot from Civo API")
784784
return nil, status.Errorf(codes.Internal, "failed to list snapshot %q: %v", snapshotID, err)
785785
}
786-
entry, err := convertSnapshot(snapshot)
786+
entry, err := ConvertSnapshot(snapshot)
787787
if err != nil {
788788
log.Error().
789789
Err(err).
@@ -820,7 +820,7 @@ func (d *Driver) ListSnapshots(ctx context.Context, req *csi.ListSnapshotsReques
820820
return nil, status.Errorf(codes.Internal, "failed to list snapshot %q: %v", snapshotID, err)
821821
}
822822

823-
entry, err := convertSnapshot(snapshot)
823+
entry, err := ConvertSnapshot(snapshot)
824824
if err != nil {
825825
log.Error().
826826
Err(err).
@@ -854,7 +854,7 @@ func (d *Driver) ListSnapshots(ctx context.Context, req *csi.ListSnapshotsReques
854854
entries := []*csi.ListSnapshotsResponse_Entry{}
855855
for _, snapshot := range snapshots {
856856
if snapshot.VolumeID == sourceVolumeID {
857-
entry, err := convertSnapshot(&snapshot)
857+
entry, err := ConvertSnapshot(&snapshot)
858858
if err != nil {
859859
log.Error().
860860
Err(err).
@@ -890,7 +890,7 @@ func (d *Driver) ListSnapshots(ctx context.Context, req *csi.ListSnapshotsReques
890890
entries := []*csi.ListSnapshotsResponse_Entry{}
891891

892892
for _, snap := range snapshots {
893-
entry, err := convertSnapshot(&snap)
893+
entry, err := ConvertSnapshot(&snap)
894894
if err != nil {
895895
log.Error().
896896
Err(err).
@@ -926,9 +926,9 @@ func getVolSizeInBytes(capRange *csi.CapacityRange) (int64, error) {
926926
return bytes, nil
927927
}
928928

929-
// convertSnapshot function converts a civogo.Snapshot object(API response) into a CSI ListSnapshotsResponse_Entry
930-
func convertSnapshot(in *civogo.VolumeSnapshot) (*csi.ListSnapshotsResponse_Entry, error) {
931-
snap, err := toCSISnapshot(in)
929+
// ConvertSnapshot function converts a civogo.Snapshot object(API response) into a CSI ListSnapshotsResponse_Entry
930+
func ConvertSnapshot(in *civogo.VolumeSnapshot) (*csi.ListSnapshotsResponse_Entry, error) {
931+
snap, err := ToCSISnapshot(in)
932932
if err != nil{
933933
return nil, fmt.Errorf("filed to convert civo snapshot %s to csi snapshot: %v", in.SnapshotID, err)
934934
}
@@ -947,31 +947,37 @@ func ParseTimeToProtoTimestamp(timeStr string) (*timestamppb.Timestamp, error) {
947947
return timestamppb.New(t), nil
948948
}
949949

950-
// isSnapshotReady determines if a snapshot is ready for use
951-
func isSnapshotReady(state string) bool {
950+
// IsSnapshotReady determines if a snapshot is ready for use
951+
func IsSnapshotReady(state string) bool {
952952
// Define the states that indicate the snapshot is ready
953953
readyStates := map[string]bool{
954954
"Ready": true,
955955
"Available": true, // Add other states if applicable
956956
}
957-
return readyStates[state]
957+
// Check if the state exists in the map, return false if not found
958+
_, exists := readyStates[state]
959+
return exists
958960
}
959961

960962

961-
func toCSISnapshot(snap *civogo.VolumeSnapshot)(*csi.Snapshot, error){
962-
creationTime, err := ParseTimeToProtoTimestamp(snap.CreationTime)
963-
if err != nil {
964-
return nil, fmt.Errorf("failed to parse creation time for snapshot %s: %w", snap.SnapshotID, err)
963+
func ToCSISnapshot(snap *civogo.VolumeSnapshot)(*csi.Snapshot, error){
964+
var creationTime *timestamppb.Timestamp
965+
var err error
966+
if snap.CreationTime != ""{
967+
creationTime, err = ParseTimeToProtoTimestamp(snap.CreationTime)
968+
if err != nil {
969+
return nil, fmt.Errorf("failed to parse creation time for snapshot %s: %w", snap.SnapshotID, err)
970+
}
965971
}
966972

967973
// Explicitly define which state indicates the snapshot is ready for use
968-
isReady := isSnapshotReady(snap.State)
974+
isReady := IsSnapshotReady(snap.State)
969975

970976
return &csi.Snapshot{
971977
SnapshotId: snap.SnapshotID,
972-
SourceVolumeId: snap.VolumeID,
973-
CreationTime: creationTime,
974-
SizeBytes: int64(snap.RestoreSize),
975-
ReadyToUse: isReady,
978+
SourceVolumeId: snap.VolumeID,
979+
CreationTime: creationTime,
980+
SizeBytes: int64(snap.RestoreSize),
981+
ReadyToUse: isReady,
976982
}, nil
977983
}

Diff for: pkg/driver/controller_server_test.go

+59
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,62 @@ func TestGetCapacity(t *testing.T) {
290290
})
291291

292292
}
293+
294+
func TestConvertSnapshot(t *testing.T) {
295+
creationTime := "2024-02-12T10:00:00Z"
296+
expectedTime, _ := driver.ParseTimeToProtoTimestamp(creationTime)
297+
tests := []struct {
298+
name string
299+
input *civogo.VolumeSnapshot
300+
expected *csi.ListSnapshotsResponse_Entry
301+
expectedError bool
302+
}{
303+
{
304+
name: "Valid Snapshot Conversion",
305+
input: &civogo.VolumeSnapshot{
306+
SnapshotID: "snap-123",
307+
VolumeID: "vol-123",
308+
SourceVolumeName: "vol1",
309+
RestoreSize: 1024,
310+
State: "Available",
311+
CreationTime: creationTime,
312+
},
313+
expected: &csi.ListSnapshotsResponse_Entry{
314+
Snapshot: &csi.Snapshot{
315+
SnapshotId: "snap-123",
316+
SourceVolumeId: "vol-123",
317+
SizeBytes: 1024,
318+
ReadyToUse: true,
319+
CreationTime: expectedTime,
320+
},
321+
},
322+
expectedError: false,
323+
},
324+
{
325+
name: "Invalid Creation Time",
326+
input: &civogo.VolumeSnapshot{
327+
SnapshotID: "snap-456",
328+
VolumeID: "vol-456",
329+
RestoreSize: 2048,
330+
State: "creating",
331+
CreationTime: "invalid-time",
332+
},
333+
expected: nil,
334+
expectedError: true,
335+
},
336+
}
337+
338+
for _, tt := range tests {
339+
t.Run(tt.name, func(t *testing.T) {
340+
result, err := driver.ConvertSnapshot(tt.input)
341+
342+
if (err != nil) != tt.expectedError {
343+
t.Errorf("convertSnapshot() error = %v, expectedError %v", err, tt.expectedError)
344+
}
345+
346+
if err == nil && !assert.Equal(t, result, tt.expected) {
347+
t.Errorf("Got:\n%v\n\n, expected:\n%v\n\n", result, tt.expected)
348+
}
349+
})
350+
}
351+
}

0 commit comments

Comments
 (0)