Skip to content

Commit b15f803

Browse files
Update fakeclient interface for volumesnapshot
Signed-off-by: Sibasish Behera <fangedhamster3114@gmail.com>
1 parent 3cef8ed commit b15f803

File tree

5 files changed

+111
-14
lines changed

5 files changed

+111
-14
lines changed

fake_client.go

+97
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type FakeClient struct {
2222
IP []IP
2323
Networks []Network
2424
Volumes []Volume
25+
VolumeSnapshots []VolumeSnapshot
2526
SSHKeys []SSHKey
2627
Webhooks []Webhook
2728
DiskImage []DiskImage
@@ -165,6 +166,15 @@ type Clienter interface {
165166
DetachVolume(id string) (*SimpleResponse, error)
166167
DeleteVolume(id string) (*SimpleResponse, error)
167168

169+
// VolumeSnapshot
170+
GetVolumeSnapshotByVolumeID(volumeID, snapshotID string) (*VolumeSnapshot, error)
171+
ListVolumeSnapshotsByVolumeID(volumeID string) ([]VolumeSnapshot, error)
172+
CreateVolumeSnapshot(volumeID string, config *VolumeSnapshotConfig) (*VolumeSnapshot, error)
173+
DeleteVolumeAndAllSnapshot(volumeID string) (*SimpleResponse, error)
174+
ListVolumeSnapshots() ([]VolumeSnapshot, error)
175+
GetVolumeSnapshot(id string) (*VolumeSnapshot, error)
176+
DeleteVolumeSnapshot(id string) (*SimpleResponse, error)
177+
168178
// Webhooks
169179
CreateWebhook(r *WebhookConfig) (*Webhook, error)
170180
ListWebhooks() ([]Webhook, error)
@@ -1330,6 +1340,93 @@ func (c *FakeClient) DeleteVolume(id string) (*SimpleResponse, error) {
13301340
return &SimpleResponse{Result: "failed"}, nil
13311341
}
13321342

1343+
// GetVolumeSnapshotByVolumeID implemented in a fake way for automated tests
1344+
func (c *FakeClient) GetVolumeSnapshotByVolumeID(volumeID, snapshotID string) (*VolumeSnapshot, error) {
1345+
for _, snapshot := range c.VolumeSnapshots {
1346+
if snapshot.VolumeID == volumeID && snapshot.SnapshotID == snapshotID {
1347+
return &snapshot, nil
1348+
}
1349+
}
1350+
1351+
err := fmt.Errorf("unable to find volume snapshot %s, zero matches", snapshotID)
1352+
return nil, ZeroMatchesError.wrap(err)
1353+
}
1354+
1355+
// ListVolumeSnapshotsByVolumeID implemented in a fake way for automated tests
1356+
func (c *FakeClient) ListVolumeSnapshotsByVolumeID(volumeID string) ([]VolumeSnapshot, error) {
1357+
snapshots := make([]VolumeSnapshot, 0)
1358+
for _, snapshot := range c.VolumeSnapshots {
1359+
if snapshot.VolumeID == volumeID {
1360+
snapshots = append(snapshots, snapshot)
1361+
}
1362+
}
1363+
1364+
return snapshots, nil
1365+
}
1366+
1367+
// CreateVolumeSnapshot implemented in a fake way for automated tests
1368+
func (c *FakeClient) CreateVolumeSnapshot(volumeID string, config *VolumeSnapshotConfig) (*VolumeSnapshot, error) {
1369+
snapshot := VolumeSnapshot{
1370+
SnapshotID: c.generateID(),
1371+
Name: config.Name,
1372+
VolumeID: volumeID,
1373+
State: "Ready",
1374+
}
1375+
c.VolumeSnapshots = append(c.VolumeSnapshots, snapshot)
1376+
1377+
return &snapshot, nil
1378+
}
1379+
1380+
// DeleteVolumeAndAllSnapshot implemented in a fake way for automated tests
1381+
func (c *FakeClient) DeleteVolumeAndAllSnapshot(volumeID string) (*SimpleResponse, error) {
1382+
for i, volume := range c.Volumes {
1383+
if volume.ID == volumeID {
1384+
c.Volumes[len(c.Volumes)-1], c.Volumes[i] = c.Volumes[i], c.Volumes[len(c.Volumes)-1]
1385+
c.Volumes = c.Volumes[:len(c.Volumes)-1]
1386+
break
1387+
}
1388+
}
1389+
1390+
for i := 0; i < len(c.VolumeSnapshots); i++ {
1391+
if c.VolumeSnapshots[i].VolumeID == volumeID {
1392+
c.VolumeSnapshots = append(c.VolumeSnapshots[:i], c.VolumeSnapshots[i+1:]...)
1393+
i--
1394+
}
1395+
}
1396+
1397+
return &SimpleResponse{Result: "success"}, nil
1398+
}
1399+
1400+
// ListVolumeSnapshots implemented in a fake way for automated tests
1401+
func (c *FakeClient) ListVolumeSnapshots() ([]VolumeSnapshot, error) {
1402+
return c.VolumeSnapshots, nil
1403+
}
1404+
1405+
// GetVolumeSnapshot implemented in a fake way for automated tests
1406+
func (c *FakeClient) GetVolumeSnapshot(snapshotID string) (*VolumeSnapshot, error) {
1407+
for _, snapshot := range c.VolumeSnapshots {
1408+
if snapshot.SnapshotID == snapshotID {
1409+
return &snapshot, nil
1410+
}
1411+
}
1412+
1413+
err := fmt.Errorf("unable to find volume snapshot %s, zero matches", snapshotID)
1414+
return nil, ZeroMatchesError.wrap(err)
1415+
}
1416+
1417+
// DeleteVolumeSnapshot implemented in a fake way for automated tests
1418+
func (c *FakeClient) DeleteVolumeSnapshot(snapshotID string) (*SimpleResponse, error) {
1419+
for i, snapshot := range c.VolumeSnapshots {
1420+
if snapshot.SnapshotID == snapshotID {
1421+
c.VolumeSnapshots[len(c.VolumeSnapshots)-1], c.VolumeSnapshots[i] = c.VolumeSnapshots[i], c.VolumeSnapshots[len(c.VolumeSnapshots)-1]
1422+
c.VolumeSnapshots = c.VolumeSnapshots[:len(c.VolumeSnapshots)-1]
1423+
return &SimpleResponse{Result: "success"}, nil
1424+
}
1425+
}
1426+
1427+
return &SimpleResponse{Result: "failed"}, nil
1428+
}
1429+
13331430
// CreateWebhook implemented in a fake way for automated tests
13341431
func (c *FakeClient) CreateWebhook(r *WebhookConfig) (*Webhook, error) {
13351432
webhook := Webhook{

volume.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -245,16 +245,16 @@ func (c *Client) DeleteVolume(id string) (*SimpleResponse, error) {
245245
}
246246

247247
// GetVolumeSnapshotByVolumeID retrieves a specific volume snapshot by volume ID and snapshot ID
248-
func (c *Client) GetVolumeSnapshotByVolumeID(volumeID, snapshotID string) (VolumeSnapshot, error) {
248+
func (c *Client) GetVolumeSnapshotByVolumeID(volumeID, snapshotID string) (*VolumeSnapshot, error) {
249249
resp, err := c.SendGetRequest(fmt.Sprintf("/v2/volumes/%s/snapshots/%s", volumeID, snapshotID))
250250
if err != nil {
251-
return VolumeSnapshot{}, decodeError(err)
251+
return nil, decodeError(err)
252252
}
253253
var volumeSnapshot = VolumeSnapshot{}
254254
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&volumeSnapshot); err != nil {
255-
return VolumeSnapshot{}, err
255+
return nil, err
256256
}
257-
return volumeSnapshot, nil
257+
return &volumeSnapshot, nil
258258
}
259259

260260
// ListVolumeSnapshotsByVolumeID returns all snapshots for a specific volume by volume ID

volume_snapshot.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ func (c *Client) ListVolumeSnapshots() ([]VolumeSnapshot, error) {
4242
}
4343

4444
// GetVolumeSnapshot finds a volume by the full ID
45-
func (c *Client) GetVolumeSnapshot(id string) (VolumeSnapshot, error) {
45+
func (c *Client) GetVolumeSnapshot(id string) (*VolumeSnapshot, error) {
4646
resp, err := c.SendGetRequest(fmt.Sprintf("/v2/snapshots/%s?resource_type=volume", id))
4747
if err != nil {
48-
return VolumeSnapshot{}, decodeError(err)
48+
return nil, decodeError(err)
4949
}
5050
var volumeSnapshot = VolumeSnapshot{}
5151
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&volumeSnapshot); err != nil {
52-
return VolumeSnapshot{}, err
52+
return nil, err
5353
}
54-
return volumeSnapshot, nil
54+
return &volumeSnapshot, nil
5555
}
5656

5757
// DeleteVolumeSnapshot deletes a volume snapshot

volume_snapshot_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestListVolumeSnapshots(t *testing.T) {
1515
"source_volume_name": "test-volume",
1616
"instance_id": "ins1234",
1717
"restore_size": 20,
18-
"state": "available",
18+
"state": "Ready",
1919
"creation_time": "2020-01-01T00:00:00Z"
2020
}]`,
2121
})
@@ -37,7 +37,7 @@ func TestListVolumeSnapshots(t *testing.T) {
3737
SourceVolumeName: "test-volume",
3838
InstanceID: "ins1234",
3939
RestoreSize: 20,
40-
State: "available",
40+
State: "Ready",
4141
CreationTime: "2020-01-01T00:00:00Z",
4242
},
4343
}
@@ -57,7 +57,7 @@ func TestGetVolumeSnapshot(t *testing.T) {
5757
"source_volume_name": "test-volume",
5858
"instance_id": "ins1234",
5959
"restore_size": 20,
60-
"state": "available",
60+
"state": "Ready",
6161
"creation_time": "2020-01-01T00:00:00Z"
6262
}`,
6363
})
@@ -69,15 +69,15 @@ func TestGetVolumeSnapshot(t *testing.T) {
6969
return
7070
}
7171

72-
expected := VolumeSnapshot{
72+
expected := &VolumeSnapshot{
7373
Name: "test-snapshot",
7474
SnapshotID: "snapshot-uuid",
7575
SnapshotDescription: "snapshot for testing",
7676
VolumeID: "12345",
7777
SourceVolumeName: "test-volume",
7878
InstanceID: "ins1234",
7979
RestoreSize: 20,
80-
State: "available",
80+
State: "Ready",
8181
CreationTime: "2020-01-01T00:00:00Z",
8282
}
8383

volume_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ func TestGetVolumeSnapshotByVolumeID(t *testing.T) {
318318
return
319319
}
320320

321-
expected := VolumeSnapshot{
321+
expected := &VolumeSnapshot{
322322
SnapshotID: "12345",
323323
Name: "test-snapshot",
324324
SnapshotDescription: "snapshot for testing",

0 commit comments

Comments
 (0)