Skip to content

Commit

Permalink
Fill SnapshotId in Disk structure
Browse files Browse the repository at this point in the history
Signed-off-by: Maxime Dufour <[email protected]>
  • Loading branch information
outscale-mdr committed Jan 19, 2024
1 parent dbe706c commit 6ae506b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
2 changes: 2 additions & 0 deletions pkg/cloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,7 @@ func (c *cloud) GetDiskByName(ctx context.Context, name string, capacityBytes in
VolumeID: volume.GetVolumeId(),
CapacityGiB: int64(volSizeBytes),
AvailabilityZone: volume.GetSubregionName(),
SnapshotID: volume.GetSnapshotId(),
}, nil
}

Expand All @@ -697,6 +698,7 @@ func (c *cloud) GetDiskByID(ctx context.Context, volumeID string) (Disk, error)
VolumeID: volume.GetVolumeId(),
CapacityGiB: int64(volume.GetSize()),
AvailabilityZone: volume.GetSubregionName(),
SnapshotID: volume.GetSnapshotId(),
}, nil
}

Expand Down
44 changes: 37 additions & 7 deletions pkg/cloud/cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,19 +396,30 @@ func TestGetDiskByName(t *testing.T) {
volumeName string
volumeCapacity int64
availabilityZone string
snapshotId *string
expErr error
}{
{
name: "success: normal",
volumeName: "vol-test-1234",
volumeCapacity: util.GiBToBytes(1),
snapshotId: nil,
availabilityZone: expZone,
expErr: nil,
},
{
name: "success: normal with snapshotId",
volumeName: "vol-test-1234",
volumeCapacity: util.GiBToBytes(1),
snapshotId: osc.PtrString("snapshot-id123"),
availabilityZone: expZone,
expErr: nil,
},
{
name: "fail: DescribeVolumes returned generic error",
volumeName: "vol-test-1234",
volumeCapacity: util.GiBToBytes(1),
snapshotId: nil,
expErr: fmt.Errorf("DescribeVolumes generic error"),
},
}
Expand All @@ -422,6 +433,7 @@ func TestGetDiskByName(t *testing.T) {
vol := osc.Volume{
VolumeId: &tc.volumeName,
SubregionName: &tc.availabilityZone,
SnapshotId: tc.snapshotId,
}
vol.SetSize(int32(util.BytesToGiB(tc.volumeCapacity)))

Expand All @@ -443,6 +455,9 @@ func TestGetDiskByName(t *testing.T) {
if tc.availabilityZone != disk.AvailabilityZone {
t.Fatalf("GetDiskByName() failed: expected availabilityZone %q, got %q", tc.availabilityZone, disk.AvailabilityZone)
}
if tc.snapshotId != nil && *tc.snapshotId != disk.SnapshotID {
t.Fatalf("GetDiskByName() failed: expected snapshotId %q, got %q", *tc.snapshotId, disk.SnapshotID)
}
}

mockCtrl.Finish()
Expand All @@ -455,18 +470,29 @@ func TestGetDiskByID(t *testing.T) {
name string
volumeID string
availabilityZone string
snapshotId *string
expErr error
}{

{
name: "success: normal",
volumeID: "vol-test-1234",
snapshotId: nil,
availabilityZone: expZone,
expErr: nil,
},
{
name: "fail: DescribeVolumes returned generic error",
volumeID: "vol-test-1234",
expErr: fmt.Errorf("DescribeVolumes generic error"),
name: "success: normal with snapshotId",
volumeID: "vol-test-1234",
snapshotId: osc.PtrString("snapshot-id123"),
availabilityZone: expZone,
expErr: nil,
},
{
name: "fail: DescribeVolumes returned generic error",
volumeID: "vol-test-1234",
snapshotId: nil,
expErr: fmt.Errorf("DescribeVolumes generic error"),
},
}

Expand All @@ -483,6 +509,7 @@ func TestGetDiskByID(t *testing.T) {
{
VolumeId: &tc.volumeID,
SubregionName: &tc.availabilityZone,
SnapshotId: tc.snapshotId,
},
},
},
Expand All @@ -493,17 +520,20 @@ func TestGetDiskByID(t *testing.T) {
disk, err := c.GetDiskByID(ctx, tc.volumeID)
if err != nil {
if tc.expErr == nil {
t.Fatalf("GetDisk() failed: expected no error, got: %v", err)
t.Fatalf("GetDiskByID() failed: expected no error, got: %v", err)
}
} else {
if tc.expErr != nil {
t.Fatal("GetDisk() failed: expected error, got nothing")
t.Fatal("GetDiskByID() failed: expected error, got nothing")
}
if disk.VolumeID != tc.volumeID {
t.Fatalf("GetDisk() failed: expected ID %q, got %q", tc.volumeID, disk.VolumeID)
t.Fatalf("GetDiskByID() failed: expected ID %q, got %q", tc.volumeID, disk.VolumeID)
}
if tc.availabilityZone != disk.AvailabilityZone {
t.Fatalf("GetDiskByName() failed: expected availabilityZone %q, got %q", tc.availabilityZone, disk.AvailabilityZone)
t.Fatalf("GetDiskByID() failed: expected availabilityZone %q, got %q", tc.availabilityZone, disk.AvailabilityZone)
}
if tc.snapshotId != nil && *tc.snapshotId != disk.SnapshotID {
t.Fatalf("GetDiskByID() failed: expected snapshotId %q, got %q", *tc.snapshotId, disk.SnapshotID)
}
}

Expand Down

0 comments on commit 6ae506b

Please sign in to comment.