forked from digitalocean/godo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
images_test.go
262 lines (216 loc) · 6.45 KB
/
images_test.go
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package godo
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
)
func TestImages_List(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/images", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"images":[{"id":1},{"id":2}]}`)
})
images, _, err := client.Images.List(ctx, nil)
if err != nil {
t.Errorf("Images.List returned error: %v", err)
}
expected := []Image{{ID: 1}, {ID: 2}}
if !reflect.DeepEqual(images, expected) {
t.Errorf("Images.List returned %+v, expected %+v", images, expected)
}
}
func TestImages_ListDistribution(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/images", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
expected := "distribution"
actual := r.URL.Query().Get("type")
if actual != expected {
t.Errorf("'type' query = %v, expected %v", actual, expected)
}
fmt.Fprint(w, `{"images":[{"id":1},{"id":2}]}`)
})
images, _, err := client.Images.ListDistribution(ctx, nil)
if err != nil {
t.Errorf("Images.ListDistribution returned error: %v", err)
}
expected := []Image{{ID: 1}, {ID: 2}}
if !reflect.DeepEqual(images, expected) {
t.Errorf("Images.ListDistribution returned %+v, expected %+v", images, expected)
}
}
func TestImages_ListApplication(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/images", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
expected := "application"
actual := r.URL.Query().Get("type")
if actual != expected {
t.Errorf("'type' query = %v, expected %v", actual, expected)
}
fmt.Fprint(w, `{"images":[{"id":1},{"id":2}]}`)
})
images, _, err := client.Images.ListApplication(ctx, nil)
if err != nil {
t.Errorf("Images.ListApplication returned error: %v", err)
}
expected := []Image{{ID: 1}, {ID: 2}}
if !reflect.DeepEqual(images, expected) {
t.Errorf("Images.ListApplication returned %+v, expected %+v", images, expected)
}
}
func TestImages_ListUser(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/images", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
expected := "true"
actual := r.URL.Query().Get("private")
if actual != expected {
t.Errorf("'private' query = %v, expected %v", actual, expected)
}
fmt.Fprint(w, `{"images":[{"id":1},{"id":2}]}`)
})
images, _, err := client.Images.ListUser(ctx, nil)
if err != nil {
t.Errorf("Images.ListUser returned error: %v", err)
}
expected := []Image{{ID: 1}, {ID: 2}}
if !reflect.DeepEqual(images, expected) {
t.Errorf("Images.ListUser returned %+v, expected %+v", images, expected)
}
}
func TestImages_ListImagesMultiplePages(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/images", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"images": [{"id":1},{"id":2}], "links":{"pages":{"next":"http://example.com/v2/images/?page=2"}}}`)
})
_, resp, err := client.Images.List(ctx, &ListOptions{Page: 2})
if err != nil {
t.Fatal(err)
}
checkCurrentPage(t, resp, 1)
}
func TestImages_RetrievePageByNumber(t *testing.T) {
setup()
defer teardown()
jBlob := `
{
"images": [{"id":1},{"id":2}],
"links":{
"pages":{
"next":"http://example.com/v2/images/?page=3",
"prev":"http://example.com/v2/images/?page=1",
"last":"http://example.com/v2/images/?page=3",
"first":"http://example.com/v2/images/?page=1"
}
}
}`
mux.HandleFunc("/v2/images", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, jBlob)
})
opt := &ListOptions{Page: 2}
_, resp, err := client.Images.List(ctx, opt)
if err != nil {
t.Fatal(err)
}
checkCurrentPage(t, resp, 2)
}
func TestImages_GetImageByID(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/images/12345", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"image":{"id":12345}}`)
})
images, _, err := client.Images.GetByID(ctx, 12345)
if err != nil {
t.Errorf("Image.GetByID returned error: %v", err)
}
expected := &Image{ID: 12345}
if !reflect.DeepEqual(images, expected) {
t.Errorf("Images.GetByID returned %+v, expected %+v", images, expected)
}
}
func TestImages_GetImageBySlug(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/images/ubuntu", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"image":{"id":12345}}`)
})
images, _, err := client.Images.GetBySlug(ctx, "ubuntu")
if err != nil {
t.Errorf("Image.GetBySlug returned error: %v", err)
}
expected := &Image{ID: 12345}
if !reflect.DeepEqual(images, expected) {
t.Errorf("Images.Get returned %+v, expected %+v", images, expected)
}
}
func TestImages_Update(t *testing.T) {
setup()
defer teardown()
updateRequest := &ImageUpdateRequest{
Name: "name",
}
mux.HandleFunc("/v2/images/12345", func(w http.ResponseWriter, r *http.Request) {
expected := map[string]interface{}{
"name": "name",
}
var v map[string]interface{}
err := json.NewDecoder(r.Body).Decode(&v)
if err != nil {
t.Fatalf("decode json: %v", err)
}
if !reflect.DeepEqual(v, expected) {
t.Errorf("Request body = %#v, expected %#v", v, expected)
}
fmt.Fprintf(w, `{"image":{"id":1}}`)
})
image, _, err := client.Images.Update(ctx, 12345, updateRequest)
if err != nil {
t.Errorf("Images.Update returned error: %v", err)
} else {
if id := image.ID; id != 1 {
t.Errorf("expected id '%d', received '%d'", 1, id)
}
}
}
func TestImages_Destroy(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/images/12345", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
})
_, err := client.Images.Delete(ctx, 12345)
if err != nil {
t.Errorf("Image.Delete returned error: %v", err)
}
}
func TestImage_String(t *testing.T) {
image := &Image{
ID: 1,
Name: "Image",
Type: "snapshot",
Distribution: "Ubuntu",
Slug: "image",
Public: true,
Regions: []string{"one", "two"},
MinDiskSize: 20,
Created: "2013-11-27T09:24:55Z",
}
stringified := image.String()
expected := `godo.Image{ID:1, Name:"Image", Type:"snapshot", Distribution:"Ubuntu", Slug:"image", Public:true, Regions:["one" "two"], MinDiskSize:20, Created:"2013-11-27T09:24:55Z"}`
if expected != stringified {
t.Errorf("Image.String returned %+v, expected %+v", stringified, expected)
}
}