Skip to content

Commit 87004e1

Browse files
add tests for volume snapshot endpoints
1 parent 1e6a79a commit 87004e1

File tree

2 files changed

+219
-0
lines changed

2 files changed

+219
-0
lines changed

volume_snapshot_test.go

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package civogo
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestListVolumeSnapshots(t *testing.T) {
9+
client, server, _ := NewClientForTesting(map[string]string{
10+
"/v2/snapshots?region=TEST&resource_type=volume": `[{
11+
"name": "test-snapshot",
12+
"snapshot_id": "12345",
13+
"snapshot_description": "snapshot for test",
14+
"volume_id": "12345",
15+
"restore_size": 20,
16+
"state": "available",
17+
"creation_time": "2020-01-01T00:00:00Z"
18+
}]`,
19+
})
20+
defer server.Close()
21+
22+
got, err := client.ListVolumeSnapshots()
23+
24+
if err != nil {
25+
t.Errorf("Request returned an error: %s", err)
26+
return
27+
}
28+
29+
expected := []VolumeSnapshot{
30+
{
31+
Name: "test-snapshot",
32+
SnapshotID: "12345",
33+
SnapshotDescription: "snapshot for test",
34+
VolumeID: "12345",
35+
RestoreSize: 20,
36+
State: "available",
37+
CreationTime: "2020-01-01T00:00:00Z",
38+
},
39+
}
40+
41+
if !reflect.DeepEqual(got, expected) {
42+
t.Errorf("Expected %+v, got %+v", expected, got)
43+
}
44+
}
45+
46+
func TestGetVolumeSnapshot(t *testing.T) {
47+
client, server, _ := NewClientForTesting(map[string]string{
48+
"/v2/snapshots/snapshot-uuid?region=TEST&resource_type=volume": `{
49+
"name": "test-snapshot",
50+
"snapshot_id": "snapshot-uuid",
51+
"snapshot_description": "snapshot for testing",
52+
"volume_id": "12345",
53+
"restore_size": 20,
54+
"state": "available",
55+
"creation_time": "2020-01-01T00:00:00Z"
56+
}`,
57+
})
58+
defer server.Close()
59+
got, err := client.GetVolumeSnapshot("snapshot-uuid")
60+
61+
if err != nil {
62+
t.Errorf("Request returned an error: %s", err)
63+
return
64+
}
65+
66+
expected := VolumeSnapshot{
67+
Name: "test-snapshot",
68+
SnapshotID: "snapshot-uuid",
69+
SnapshotDescription: "snapshot for testing",
70+
VolumeID: "12345",
71+
RestoreSize: 20,
72+
State: "available",
73+
CreationTime: "2020-01-01T00:00:00Z",
74+
}
75+
76+
if !reflect.DeepEqual(got, expected) {
77+
t.Errorf("Expected %+v, got %+v", expected, got)
78+
}
79+
}
80+
81+
func TestDeleteVolumeSnapshot(t *testing.T) {
82+
client, server, _ := NewClientForTesting(map[string]string{
83+
"/v2/snapshots/12346": `{"result": "success"}`,
84+
})
85+
defer server.Close()
86+
got, err := client.DeleteVolumeSnapshot("12346")
87+
88+
if err != nil {
89+
t.Errorf("Request returned an error: %s", err)
90+
return
91+
}
92+
93+
expected := &SimpleResponse{Result: "success"}
94+
95+
if !reflect.DeepEqual(got, expected) {
96+
t.Errorf("Expected %+v, got %+v", expected, got)
97+
}
98+
}

volume_test.go

+121
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,124 @@ func TestResizeVolume(t *testing.T) {
260260
t.Errorf("Expected %+v, got %+v", expected, got)
261261
}
262262
}
263+
264+
func TestCreateVolumeSnapshot(t *testing.T) {
265+
client, server, _ := NewClientForTesting(map[string]string{
266+
"/v2/volumes/12346/snapshot": `{
267+
"snapshot_id": "12345",
268+
"name": "test-snapshot",
269+
"snapshot_description": "snapshot for testing",
270+
"volume_id": "12346",
271+
"state": "available",
272+
"creation_time": "2020-01-01T00:00:00Z"
273+
}`,
274+
})
275+
defer server.Close()
276+
cfg := &VolumeSnapshotConfig{Name: "my-snapshot"}
277+
got, err := client.CreateVolumeSnapshot("12346", cfg)
278+
if err != nil {
279+
t.Errorf("Request returned an error: %s", err)
280+
return
281+
}
282+
283+
expected := &VolumeSnapshot{
284+
SnapshotID: "12345",
285+
Name: "test-snapshot",
286+
SnapshotDescription: "snapshot for testing",
287+
VolumeID: "12346",
288+
State: "available",
289+
CreationTime: "2020-01-01T00:00:00Z",
290+
}
291+
if !reflect.DeepEqual(got, expected) {
292+
t.Errorf("Expected %+v, got %+v", expected, got)
293+
}
294+
}
295+
296+
func TestGetVolumeSnapshotByVolumeID(t *testing.T) {
297+
client, server, _ := NewClientForTesting(map[string]string{
298+
"/v2/volumes/12346/snapshot/12345": `{
299+
"snapshot_id": "12345",
300+
"name": "test-snapshot",
301+
"snapshot_description": "snapshot for testing",
302+
"volume_id": "12346",
303+
"state": "available",
304+
"creation_time": "2020-01-01T00:00:00Z"
305+
}`,
306+
})
307+
defer server.Close()
308+
309+
got, err := client.GetVolumeSnapshotByVolumeID("12346", "12345")
310+
if err != nil {
311+
t.Errorf("Request returned an error: %s", err)
312+
return
313+
}
314+
315+
expected := VolumeSnapshot{
316+
SnapshotID: "12345",
317+
Name: "test-snapshot",
318+
SnapshotDescription: "snapshot for testing",
319+
VolumeID: "12346",
320+
State: "available",
321+
CreationTime: "2020-01-01T00:00:00Z",
322+
}
323+
324+
if !reflect.DeepEqual(got, expected) {
325+
t.Errorf("Expected %+v, got %+v", expected, got)
326+
}
327+
}
328+
329+
func TestListVolumeSnapshotsByVolumeID(t *testing.T) {
330+
client, server, _ := NewClientForTesting(map[string]string{
331+
"/v2/volumes/12346/snapshots": `[{
332+
"snapshot_id": "12345",
333+
"name": "test-snapshot",
334+
"snapshot_description": "snapshot for testing",
335+
"volume_id": "12346",
336+
"state": "available",
337+
"creation_time": "2020-01-01T00:00:00Z"
338+
}]`,
339+
})
340+
defer server.Close()
341+
342+
got, err := client.ListVolumeSnapshotsByVolumeID("12346")
343+
if err != nil {
344+
t.Errorf("Request returned an error: %s", err)
345+
return
346+
}
347+
348+
expected := []VolumeSnapshot{
349+
{
350+
SnapshotID: "12345",
351+
Name: "test-snapshot",
352+
SnapshotDescription: "snapshot for testing",
353+
VolumeID: "12346",
354+
State: "available",
355+
CreationTime: "2020-01-01T00:00:00Z",
356+
},
357+
}
358+
359+
if !reflect.DeepEqual(got, expected) {
360+
t.Errorf("Expected %+v, got %+v", expected, got)
361+
}
362+
}
363+
364+
func TestDeleteVolumeAndAllSnapshot(t *testing.T) {
365+
client, server, _ := NewClientForTesting(map[string]string{
366+
"/v2/volumes/12346?delete_snapshot=true": `{"result": "success"}`,
367+
})
368+
defer server.Close()
369+
370+
got, err := client.DeleteVolumeAndAllSnapshot("12346")
371+
if err != nil {
372+
t.Errorf("Request returned an error: %s", err)
373+
return
374+
}
375+
376+
expected := &SimpleResponse{
377+
Result: "success",
378+
}
379+
380+
if !reflect.DeepEqual(got, expected) {
381+
t.Errorf("Expected %+v, got %+v", expected, got)
382+
}
383+
}

0 commit comments

Comments
 (0)