Skip to content

Commit

Permalink
add tests for volume snapshot endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRealSibasishBehera committed Dec 3, 2024
1 parent 1e6a79a commit 2ffdc71
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 2 deletions.
2 changes: 1 addition & 1 deletion volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func (c *Client) CreateVolumeSnapshot(volumeID string, config *VolumeSnapshotCon
return result, nil
}

// Deletes a volume and all its snapshots
// DeleteVolumeAndAllSnapshot deletes a volume and all its snapshots
func (c *Client) DeleteVolumeAndAllSnapshot(volumeID string) (*SimpleResponse, error) {
resp, err := c.SendDeleteRequest(fmt.Sprintf("/v2/volumes/%s?delete_snapshot=true", volumeID))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion volume_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type VolumeSnapshotConfig struct {
Description string `json:"description"`
}

// ListSnapshots returns all snapshots owned by the calling API account
// ListVolumeSnapshots returns all snapshots owned by the calling API account
func (c *Client) ListVolumeSnapshots() ([]VolumeSnapshot, error) {
resp, err := c.SendGetRequest("/v2/snapshots?resource_type=volume")
if err != nil {
Expand Down
98 changes: 98 additions & 0 deletions volume_snapshot_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package civogo

import (
"reflect"
"testing"
)

func TestListVolumeSnapshots(t *testing.T) {
client, server, _ := NewClientForTesting(map[string]string{
"/v2/snapshots?region=TEST&resource_type=volume": `[{
"name": "test-snapshot",
"snapshot_id": "12345",
"snapshot_description": "snapshot for test",
"volume_id": "12345",
"restore_size": 20,
"state": "available",
"creation_time": "2020-01-01T00:00:00Z"
}]`,
})
defer server.Close()

got, err := client.ListVolumeSnapshots()

if err != nil {
t.Errorf("Request returned an error: %s", err)
return
}

expected := []VolumeSnapshot{
{
Name: "test-snapshot",
SnapshotID: "12345",
SnapshotDescription: "snapshot for test",
VolumeID: "12345",
RestoreSize: 20,
State: "available",
CreationTime: "2020-01-01T00:00:00Z",
},
}

if !reflect.DeepEqual(got, expected) {
t.Errorf("Expected %+v, got %+v", expected, got)
}
}

func TestGetVolumeSnapshot(t *testing.T) {
client, server, _ := NewClientForTesting(map[string]string{
"/v2/snapshots/snapshot-uuid?region=TEST&resource_type=volume": `{
"name": "test-snapshot",
"snapshot_id": "snapshot-uuid",
"snapshot_description": "snapshot for testing",
"volume_id": "12345",
"restore_size": 20,
"state": "available",
"creation_time": "2020-01-01T00:00:00Z"
}`,
})
defer server.Close()
got, err := client.GetVolumeSnapshot("snapshot-uuid")

if err != nil {
t.Errorf("Request returned an error: %s", err)
return
}

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

if !reflect.DeepEqual(got, expected) {
t.Errorf("Expected %+v, got %+v", expected, got)
}
}

func TestDeleteVolumeSnapshot(t *testing.T) {
client, server, _ := NewClientForTesting(map[string]string{
"/v2/snapshots/12346": `{"result": "success"}`,
})
defer server.Close()
got, err := client.DeleteVolumeSnapshot("12346")

if err != nil {
t.Errorf("Request returned an error: %s", err)
return
}

expected := &SimpleResponse{Result: "success"}

if !reflect.DeepEqual(got, expected) {
t.Errorf("Expected %+v, got %+v", expected, got)
}
}
121 changes: 121 additions & 0 deletions volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,124 @@ func TestResizeVolume(t *testing.T) {
t.Errorf("Expected %+v, got %+v", expected, got)
}
}

func TestCreateVolumeSnapshot(t *testing.T) {
client, server, _ := NewClientForTesting(map[string]string{
"/v2/volumes/12346/snapshot": `{
"snapshot_id": "12345",
"name": "test-snapshot",
"snapshot_description": "snapshot for testing",
"volume_id": "12346",
"state": "available",
"creation_time": "2020-01-01T00:00:00Z"
}`,
})
defer server.Close()
cfg := &VolumeSnapshotConfig{Name: "my-snapshot"}
got, err := client.CreateVolumeSnapshot("12346", cfg)
if err != nil {
t.Errorf("Request returned an error: %s", err)
return
}

expected := &VolumeSnapshot{
SnapshotID: "12345",
Name: "test-snapshot",
SnapshotDescription: "snapshot for testing",
VolumeID: "12346",
State: "available",
CreationTime: "2020-01-01T00:00:00Z",
}
if !reflect.DeepEqual(got, expected) {
t.Errorf("Expected %+v, got %+v", expected, got)
}
}

func TestGetVolumeSnapshotByVolumeID(t *testing.T) {
client, server, _ := NewClientForTesting(map[string]string{
"/v2/volumes/12346/snapshot/12345": `{
"snapshot_id": "12345",
"name": "test-snapshot",
"snapshot_description": "snapshot for testing",
"volume_id": "12346",
"state": "available",
"creation_time": "2020-01-01T00:00:00Z"
}`,
})
defer server.Close()

got, err := client.GetVolumeSnapshotByVolumeID("12346", "12345")
if err != nil {
t.Errorf("Request returned an error: %s", err)
return
}

expected := VolumeSnapshot{
SnapshotID: "12345",
Name: "test-snapshot",
SnapshotDescription: "snapshot for testing",
VolumeID: "12346",
State: "available",
CreationTime: "2020-01-01T00:00:00Z",
}

if !reflect.DeepEqual(got, expected) {
t.Errorf("Expected %+v, got %+v", expected, got)
}
}

func TestListVolumeSnapshotsByVolumeID(t *testing.T) {
client, server, _ := NewClientForTesting(map[string]string{
"/v2/volumes/12346/snapshots": `[{
"snapshot_id": "12345",
"name": "test-snapshot",
"snapshot_description": "snapshot for testing",
"volume_id": "12346",
"state": "available",
"creation_time": "2020-01-01T00:00:00Z"
}]`,
})
defer server.Close()

got, err := client.ListVolumeSnapshotsByVolumeID("12346")
if err != nil {
t.Errorf("Request returned an error: %s", err)
return
}

expected := []VolumeSnapshot{
{
SnapshotID: "12345",
Name: "test-snapshot",
SnapshotDescription: "snapshot for testing",
VolumeID: "12346",
State: "available",
CreationTime: "2020-01-01T00:00:00Z",
},
}

if !reflect.DeepEqual(got, expected) {
t.Errorf("Expected %+v, got %+v", expected, got)
}
}

func TestDeleteVolumeAndAllSnapshot(t *testing.T) {
client, server, _ := NewClientForTesting(map[string]string{
"/v2/volumes/12346?delete_snapshot=true": `{"result": "success"}`,
})
defer server.Close()

got, err := client.DeleteVolumeAndAllSnapshot("12346")
if err != nil {
t.Errorf("Request returned an error: %s", err)
return
}

expected := &SimpleResponse{
Result: "success",
}

if !reflect.DeepEqual(got, expected) {
t.Errorf("Expected %+v, got %+v", expected, got)
}
}

0 comments on commit 2ffdc71

Please sign in to comment.