-
Notifications
You must be signed in to change notification settings - Fork 24
/
snapshot_test.go.disabled
180 lines (164 loc) · 5.05 KB
/
snapshot_test.go.disabled
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package civogo
import (
"reflect"
"testing"
)
func TestCreateSnapshot(t *testing.T) {
client, server, _ := NewClientForTesting(map[string]string{
"/v2/snapshots/my-backup": `{
"id": "0ca69adc-ff39-4fc1-8f08-d91434e86fac",
"instance_id": "44aab548-61ca-11e5-860e-5cf9389be614",
"hostname": "server1.prod.example.com",
"template_id": "0b213794-d795-4483-8982-9f249c0262b9",
"openstack_snapshot_id": null,
"region": "lon1",
"name": "my-backup",
"safe": 1,
"size_gb": 0,
"state": "new",
"cron_timing": null,
"requested_at": null,
"completed_at": null
}`,
})
defer server.Close()
cfg := &SnapshotConfig{
InstanceID: "44aab548-61ca-11e5-860e-5cf9389be614",
Safe: true,
Cron: "",
}
got, err := client.CreateSnapshot("my-backup", cfg)
if err != nil {
t.Errorf("Request returned an error: %s", err)
return
}
expected := &Snapshot{
ID: "0ca69adc-ff39-4fc1-8f08-d91434e86fac",
InstanceID: "44aab548-61ca-11e5-860e-5cf9389be614",
Hostname: "server1.prod.example.com",
Template: "0b213794-d795-4483-8982-9f249c0262b9",
Region: "lon1",
Name: "my-backup",
Safe: 1,
SizeGigabytes: 0,
State: "new",
}
if !reflect.DeepEqual(got, expected) {
t.Errorf("Expected %+v, got %+v", expected, got)
}
}
func TestListSnapshots(t *testing.T) {
client, server, _ := NewClientForTesting(map[string]string{
"/v2/snapshots": `[{
"id": "0ca69adc-ff39-4fc1-8f08-d91434e86fac",
"instance_id": "44aab548-61ca-11e5-860e-5cf9389be614",
"hostname": "server1.prod.example.com",
"template_id": "0b213794-d795-4483-8982-9f249c0262b9",
"openstack_snapshot_id": null,
"region": "lon1",
"name": "my-backup",
"safe": 1,
"size_gb": 0,
"state": "new",
"cron_timing": null,
"requested_at": null,
"completed_at": null
}]`,
})
defer server.Close()
got, err := client.ListSnapshots()
if err != nil {
t.Errorf("Request returned an error: %s", err)
return
}
expected := []Snapshot{{
ID: "0ca69adc-ff39-4fc1-8f08-d91434e86fac",
InstanceID: "44aab548-61ca-11e5-860e-5cf9389be614",
Hostname: "server1.prod.example.com",
Template: "0b213794-d795-4483-8982-9f249c0262b9",
Region: "lon1",
Name: "my-backup",
Safe: 1,
SizeGigabytes: 0,
State: "new",
}}
if !reflect.DeepEqual(got, expected) {
t.Errorf("Expected %+v, got %+v", expected, got)
}
}
func TestFindSnapshot(t *testing.T) {
client, server, _ := NewClientForTesting(map[string]string{
"/v2/snapshots": `[
{
"id": "0ca69adc-ff39-4fc1-8f08-d91434e86fac",
"instance_id": "44aab548-61ca-11e5-860e-5cf9389be614",
"hostname": "server1.prod.example.com",
"template_id": "0b213794-d795-4483-8982-9f249c0262b9",
"openstack_snapshot_id": null,
"region": "lon1",
"name": "my-backup",
"safe": 1,
"size_gb": 0,
"state": "new",
"cron_timing": null,
"requested_at": null,
"completed_at": null
},
{
"id": "aadec58e-26f4-43e7-8963-18739519ef76",
"instance_id": "44aab548-61ca-11e5-860e-5cf9389be614",
"hostname": "server1.prod.example.com",
"template_id": "0b213794-d795-4483-8982-9f249c0262b9",
"openstack_snapshot_id": null,
"region": "lon1",
"name": "other-backup",
"safe": 1,
"size_gb": 0,
"state": "new",
"cron_timing": null,
"requested_at": null,
"completed_at": null
}
]`,
})
defer server.Close()
got, _ := client.FindSnapshot("ff39")
if got.ID != "0ca69adc-ff39-4fc1-8f08-d91434e86fac" {
t.Errorf("Expected %s, got %s", "0ca69adc-ff39-4fc1-8f08-d91434e86fac", got.ID)
}
got, _ = client.FindSnapshot("26f4")
if got.ID != "aadec58e-26f4-43e7-8963-18739519ef76" {
t.Errorf("Expected %s, got %s", "aadec58e-26f4-43e7-8963-18739519ef76", got.ID)
}
got, _ = client.FindSnapshot("my")
if got.ID != "0ca69adc-ff39-4fc1-8f08-d91434e86fac" {
t.Errorf("Expected %s, got %s", "0ca69adc-ff39-4fc1-8f08-d91434e86fac", got.ID)
}
got, _ = client.FindSnapshot("other")
if got.ID != "aadec58e-26f4-43e7-8963-18739519ef76" {
t.Errorf("Expected %s, got %s", "aadec58e-26f4-43e7-8963-18739519ef76", got.ID)
}
_, err := client.FindSnapshot("backup")
if err.Error() != "MultipleMatchesError: unable to find backup because there were multiple matches" {
t.Errorf("Expected %s, got %s", "unable to find backup because there were multiple matches", err.Error())
}
_, err = client.FindSnapshot("missing")
if err.Error() != "ZeroMatchesError: unable to find missing, zero matches" {
t.Errorf("Expected %s, got %s", "unable to find missing, zero matches", err.Error())
}
}
func TestDeleteSnapshot(t *testing.T) {
client, server, _ := NewClientForTesting(map[string]string{
"/v2/snapshots/my-backup": `{"result": "success"}`,
})
defer server.Close()
got, err := client.DeleteSnapshot("my-backup")
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)
}
}