Skip to content

Commit

Permalink
Merge pull request #231 from civo/snapshot
Browse files Browse the repository at this point in the history
Update fakeclient interface for volumesnapshot
  • Loading branch information
TheRealSibasishBehera authored Jan 21, 2025
2 parents 3cef8ed + b15f803 commit a78861d
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 14 deletions.
97 changes: 97 additions & 0 deletions fake_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type FakeClient struct {
IP []IP
Networks []Network
Volumes []Volume
VolumeSnapshots []VolumeSnapshot
SSHKeys []SSHKey
Webhooks []Webhook
DiskImage []DiskImage
Expand Down Expand Up @@ -165,6 +166,15 @@ type Clienter interface {
DetachVolume(id string) (*SimpleResponse, error)
DeleteVolume(id string) (*SimpleResponse, error)

// VolumeSnapshot
GetVolumeSnapshotByVolumeID(volumeID, snapshotID string) (*VolumeSnapshot, error)
ListVolumeSnapshotsByVolumeID(volumeID string) ([]VolumeSnapshot, error)
CreateVolumeSnapshot(volumeID string, config *VolumeSnapshotConfig) (*VolumeSnapshot, error)
DeleteVolumeAndAllSnapshot(volumeID string) (*SimpleResponse, error)
ListVolumeSnapshots() ([]VolumeSnapshot, error)
GetVolumeSnapshot(id string) (*VolumeSnapshot, error)
DeleteVolumeSnapshot(id string) (*SimpleResponse, error)

// Webhooks
CreateWebhook(r *WebhookConfig) (*Webhook, error)
ListWebhooks() ([]Webhook, error)
Expand Down Expand Up @@ -1330,6 +1340,93 @@ func (c *FakeClient) DeleteVolume(id string) (*SimpleResponse, error) {
return &SimpleResponse{Result: "failed"}, nil
}

// GetVolumeSnapshotByVolumeID implemented in a fake way for automated tests
func (c *FakeClient) GetVolumeSnapshotByVolumeID(volumeID, snapshotID string) (*VolumeSnapshot, error) {
for _, snapshot := range c.VolumeSnapshots {
if snapshot.VolumeID == volumeID && snapshot.SnapshotID == snapshotID {
return &snapshot, nil
}
}

err := fmt.Errorf("unable to find volume snapshot %s, zero matches", snapshotID)
return nil, ZeroMatchesError.wrap(err)
}

// ListVolumeSnapshotsByVolumeID implemented in a fake way for automated tests
func (c *FakeClient) ListVolumeSnapshotsByVolumeID(volumeID string) ([]VolumeSnapshot, error) {
snapshots := make([]VolumeSnapshot, 0)
for _, snapshot := range c.VolumeSnapshots {
if snapshot.VolumeID == volumeID {
snapshots = append(snapshots, snapshot)
}
}

return snapshots, nil
}

// CreateVolumeSnapshot implemented in a fake way for automated tests
func (c *FakeClient) CreateVolumeSnapshot(volumeID string, config *VolumeSnapshotConfig) (*VolumeSnapshot, error) {
snapshot := VolumeSnapshot{
SnapshotID: c.generateID(),
Name: config.Name,
VolumeID: volumeID,
State: "Ready",
}
c.VolumeSnapshots = append(c.VolumeSnapshots, snapshot)

return &snapshot, nil
}

// DeleteVolumeAndAllSnapshot implemented in a fake way for automated tests
func (c *FakeClient) DeleteVolumeAndAllSnapshot(volumeID string) (*SimpleResponse, error) {
for i, volume := range c.Volumes {
if volume.ID == volumeID {
c.Volumes[len(c.Volumes)-1], c.Volumes[i] = c.Volumes[i], c.Volumes[len(c.Volumes)-1]
c.Volumes = c.Volumes[:len(c.Volumes)-1]
break
}
}

for i := 0; i < len(c.VolumeSnapshots); i++ {
if c.VolumeSnapshots[i].VolumeID == volumeID {
c.VolumeSnapshots = append(c.VolumeSnapshots[:i], c.VolumeSnapshots[i+1:]...)
i--
}
}

return &SimpleResponse{Result: "success"}, nil
}

// ListVolumeSnapshots implemented in a fake way for automated tests
func (c *FakeClient) ListVolumeSnapshots() ([]VolumeSnapshot, error) {
return c.VolumeSnapshots, nil
}

// GetVolumeSnapshot implemented in a fake way for automated tests
func (c *FakeClient) GetVolumeSnapshot(snapshotID string) (*VolumeSnapshot, error) {
for _, snapshot := range c.VolumeSnapshots {
if snapshot.SnapshotID == snapshotID {
return &snapshot, nil
}
}

err := fmt.Errorf("unable to find volume snapshot %s, zero matches", snapshotID)
return nil, ZeroMatchesError.wrap(err)
}

// DeleteVolumeSnapshot implemented in a fake way for automated tests
func (c *FakeClient) DeleteVolumeSnapshot(snapshotID string) (*SimpleResponse, error) {
for i, snapshot := range c.VolumeSnapshots {
if snapshot.SnapshotID == snapshotID {
c.VolumeSnapshots[len(c.VolumeSnapshots)-1], c.VolumeSnapshots[i] = c.VolumeSnapshots[i], c.VolumeSnapshots[len(c.VolumeSnapshots)-1]
c.VolumeSnapshots = c.VolumeSnapshots[:len(c.VolumeSnapshots)-1]
return &SimpleResponse{Result: "success"}, nil
}
}

return &SimpleResponse{Result: "failed"}, nil
}

// CreateWebhook implemented in a fake way for automated tests
func (c *FakeClient) CreateWebhook(r *WebhookConfig) (*Webhook, error) {
webhook := Webhook{
Expand Down
8 changes: 4 additions & 4 deletions volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,16 +245,16 @@ func (c *Client) DeleteVolume(id string) (*SimpleResponse, error) {
}

// GetVolumeSnapshotByVolumeID retrieves a specific volume snapshot by volume ID and snapshot ID
func (c *Client) GetVolumeSnapshotByVolumeID(volumeID, snapshotID string) (VolumeSnapshot, error) {
func (c *Client) GetVolumeSnapshotByVolumeID(volumeID, snapshotID string) (*VolumeSnapshot, error) {
resp, err := c.SendGetRequest(fmt.Sprintf("/v2/volumes/%s/snapshots/%s", volumeID, snapshotID))
if err != nil {
return VolumeSnapshot{}, decodeError(err)
return nil, decodeError(err)
}
var volumeSnapshot = VolumeSnapshot{}
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&volumeSnapshot); err != nil {
return VolumeSnapshot{}, err
return nil, err
}
return volumeSnapshot, nil
return &volumeSnapshot, nil
}

// ListVolumeSnapshotsByVolumeID returns all snapshots for a specific volume by volume ID
Expand Down
8 changes: 4 additions & 4 deletions volume_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ func (c *Client) ListVolumeSnapshots() ([]VolumeSnapshot, error) {
}

// GetVolumeSnapshot finds a volume by the full ID
func (c *Client) GetVolumeSnapshot(id string) (VolumeSnapshot, error) {
func (c *Client) GetVolumeSnapshot(id string) (*VolumeSnapshot, error) {
resp, err := c.SendGetRequest(fmt.Sprintf("/v2/snapshots/%s?resource_type=volume", id))
if err != nil {
return VolumeSnapshot{}, decodeError(err)
return nil, decodeError(err)
}
var volumeSnapshot = VolumeSnapshot{}
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&volumeSnapshot); err != nil {
return VolumeSnapshot{}, err
return nil, err
}
return volumeSnapshot, nil
return &volumeSnapshot, nil
}

// DeleteVolumeSnapshot deletes a volume snapshot
Expand Down
10 changes: 5 additions & 5 deletions volume_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestListVolumeSnapshots(t *testing.T) {
"source_volume_name": "test-volume",
"instance_id": "ins1234",
"restore_size": 20,
"state": "available",
"state": "Ready",
"creation_time": "2020-01-01T00:00:00Z"
}]`,
})
Expand All @@ -37,7 +37,7 @@ func TestListVolumeSnapshots(t *testing.T) {
SourceVolumeName: "test-volume",
InstanceID: "ins1234",
RestoreSize: 20,
State: "available",
State: "Ready",
CreationTime: "2020-01-01T00:00:00Z",
},
}
Expand All @@ -57,7 +57,7 @@ func TestGetVolumeSnapshot(t *testing.T) {
"source_volume_name": "test-volume",
"instance_id": "ins1234",
"restore_size": 20,
"state": "available",
"state": "Ready",
"creation_time": "2020-01-01T00:00:00Z"
}`,
})
Expand All @@ -69,15 +69,15 @@ func TestGetVolumeSnapshot(t *testing.T) {
return
}

expected := VolumeSnapshot{
expected := &VolumeSnapshot{
Name: "test-snapshot",
SnapshotID: "snapshot-uuid",
SnapshotDescription: "snapshot for testing",
VolumeID: "12345",
SourceVolumeName: "test-volume",
InstanceID: "ins1234",
RestoreSize: 20,
State: "available",
State: "Ready",
CreationTime: "2020-01-01T00:00:00Z",
}

Expand Down
2 changes: 1 addition & 1 deletion volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func TestGetVolumeSnapshotByVolumeID(t *testing.T) {
return
}

expected := VolumeSnapshot{
expected := &VolumeSnapshot{
SnapshotID: "12345",
Name: "test-snapshot",
SnapshotDescription: "snapshot for testing",
Expand Down

0 comments on commit a78861d

Please sign in to comment.