blob: 10a87b429fddd7a609f30e0329e41b45500403cf [file] [log] [blame]
jrperrittc5c590a2016-11-04 14:41:15 -05001package testing
2
3import (
4 "fmt"
5 "net/http"
6 "strconv"
7 "strings"
8 "testing"
9
10 th "github.com/gophercloud/gophercloud/testhelper"
11 fakeclient "github.com/gophercloud/gophercloud/testhelper/client"
12)
13
14type imageEntry struct {
15 ID string
16 JSON string
17}
18
19// HandleImageListSuccessfully test setup
20func HandleImageListSuccessfully(t *testing.T) {
21
22 images := make([]imageEntry, 3)
23
24 images[0] = imageEntry{"cirros-0.3.4-x86_64-uec",
25 `{
26 "status": "active",
27 "name": "cirros-0.3.4-x86_64-uec",
28 "tags": [],
29 "kernel_id": "e1b6edd4-bd9b-40ac-b010-8a6c16de4ba4",
30 "container_format": "ami",
31 "created_at": "2015-07-15T11:43:35Z",
32 "ramdisk_id": "8c64f48a-45a3-4eaa-adff-a8106b6c005b",
33 "disk_format": "ami",
34 "updated_at": "2015-07-15T11:43:35Z",
35 "visibility": "public",
36 "self": "/v2/images/07aa21a9-fa1a-430e-9a33-185be5982431",
37 "min_disk": 0,
38 "protected": false,
39 "id": "07aa21a9-fa1a-430e-9a33-185be5982431",
40 "size": 25165824,
41 "file": "/v2/images/07aa21a9-fa1a-430e-9a33-185be5982431/file",
42 "checksum": "eb9139e4942121f22bbc2afc0400b2a4",
43 "owner": "cba624273b8344e59dd1fd18685183b0",
44 "virtual_size": null,
45 "min_ram": 0,
46 "schema": "/v2/schemas/image"
47 }`}
48 images[1] = imageEntry{"cirros-0.3.4-x86_64-uec-ramdisk",
49 `{
50 "status": "active",
51 "name": "cirros-0.3.4-x86_64-uec-ramdisk",
52 "tags": [],
53 "container_format": "ari",
54 "created_at": "2015-07-15T11:43:32Z",
55 "size": 3740163,
56 "disk_format": "ari",
57 "updated_at": "2015-07-15T11:43:32Z",
58 "visibility": "public",
59 "self": "/v2/images/8c64f48a-45a3-4eaa-adff-a8106b6c005b",
60 "min_disk": 0,
61 "protected": false,
62 "id": "8c64f48a-45a3-4eaa-adff-a8106b6c005b",
63 "file": "/v2/images/8c64f48a-45a3-4eaa-adff-a8106b6c005b/file",
64 "checksum": "be575a2b939972276ef675752936977f",
65 "owner": "cba624273b8344e59dd1fd18685183b0",
66 "virtual_size": null,
67 "min_ram": 0,
68 "schema": "/v2/schemas/image"
69 }`}
70 images[2] = imageEntry{"cirros-0.3.4-x86_64-uec-kernel",
71 `{
72 "status": "active",
73 "name": "cirros-0.3.4-x86_64-uec-kernel",
74 "tags": [],
75 "container_format": "aki",
76 "created_at": "2015-07-15T11:43:29Z",
77 "size": 4979632,
78 "disk_format": "aki",
79 "updated_at": "2015-07-15T11:43:30Z",
80 "visibility": "public",
81 "self": "/v2/images/e1b6edd4-bd9b-40ac-b010-8a6c16de4ba4",
82 "min_disk": 0,
83 "protected": false,
84 "id": "e1b6edd4-bd9b-40ac-b010-8a6c16de4ba4",
85 "file": "/v2/images/e1b6edd4-bd9b-40ac-b010-8a6c16de4ba4/file",
86 "checksum": "8a40c862b5735975d82605c1dd395796",
87 "owner": "cba624273b8344e59dd1fd18685183b0",
88 "virtual_size": null,
89 "min_ram": 0,
90 "schema": "/v2/schemas/image"
91 }`}
92
93 th.Mux.HandleFunc("/images", func(w http.ResponseWriter, r *http.Request) {
94 th.TestMethod(t, r, "GET")
95 th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID)
96
97 w.Header().Add("Content-Type", "application/json")
98
99 w.WriteHeader(http.StatusOK)
100
101 limit := 10
102 var err error
103 if r.FormValue("limit") != "" {
104 limit, err = strconv.Atoi(r.FormValue("limit"))
105 if err != nil {
106 t.Errorf("Error value for 'limit' parameter %v (error: %v)", r.FormValue("limit"), err)
107 }
108
109 }
110
111 marker := ""
112 newMarker := ""
113
114 if r.Form["marker"] != nil {
115 marker = r.Form["marker"][0]
116 }
117
118 t.Logf("limit = %v marker = %v", limit, marker)
119
120 selected := 0
121 addNext := false
122 var imageJSON []string
123
124 fmt.Fprintf(w, `{"images": [`)
125
126 for _, i := range images {
127 if marker == "" || addNext {
128 t.Logf("Adding image %v to page", i.ID)
129 imageJSON = append(imageJSON, i.JSON)
130 newMarker = i.ID
131 selected++
132 } else {
133 if strings.Contains(i.JSON, marker) {
134 addNext = true
135 }
136 }
137
138 if selected == limit {
139 break
140 }
141 }
142 t.Logf("Writing out %v image(s)", len(imageJSON))
143 fmt.Fprintf(w, strings.Join(imageJSON, ","))
144
145 fmt.Fprintf(w, `],
146 "next": "/images?marker=%s&limit=%v",
147 "schema": "/schemas/images",
148 "first": "/images?limit=%v"}`, newMarker, limit, limit)
149
150 })
151}
152
153// HandleImageCreationSuccessfully test setup
154func HandleImageCreationSuccessfully(t *testing.T) {
155 th.Mux.HandleFunc("/images", func(w http.ResponseWriter, r *http.Request) {
156 th.TestMethod(t, r, "POST")
157 th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID)
158 th.TestJSONRequest(t, r, `{
159 "id": "e7db3b45-8db7-47ad-8109-3fb55c2c24fd",
160 "name": "Ubuntu 12.10",
Joe Topjian929e60b2017-02-20 15:31:15 -0700161 "architecture": "x86_64",
jrperrittc5c590a2016-11-04 14:41:15 -0500162 "tags": [
163 "ubuntu",
164 "quantal"
165 ]
166 }`)
167
168 w.WriteHeader(http.StatusCreated)
169 w.Header().Add("Content-Type", "application/json")
170 fmt.Fprintf(w, `{
171 "status": "queued",
172 "name": "Ubuntu 12.10",
173 "protected": false,
174 "tags": ["ubuntu","quantal"],
175 "container_format": "bare",
176 "created_at": "2014-11-11T20:47:55Z",
177 "disk_format": "qcow2",
178 "updated_at": "2014-11-11T20:47:55Z",
179 "visibility": "private",
180 "self": "/v2/images/e7db3b45-8db7-47ad-8109-3fb55c2c24fd",
181 "min_disk": 0,
182 "protected": false,
183 "id": "e7db3b45-8db7-47ad-8109-3fb55c2c24fd",
184 "file": "/v2/images/e7db3b45-8db7-47ad-8109-3fb55c2c24fd/file",
185 "owner": "b4eedccc6fb74fa8a7ad6b08382b852b",
186 "min_ram": 0,
187 "schema": "/v2/schemas/image",
188 "size": 0,
189 "checksum": "",
190 "virtual_size": 0
191 }`)
192 })
193}
194
195// HandleImageCreationSuccessfullyNulls test setup
196// JSON null values could be also returned according to behaviour https://bugs.launchpad.net/glance/+bug/1481512
197func HandleImageCreationSuccessfullyNulls(t *testing.T) {
198 th.Mux.HandleFunc("/images", func(w http.ResponseWriter, r *http.Request) {
199 th.TestMethod(t, r, "POST")
200 th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID)
201 th.TestJSONRequest(t, r, `{
202 "id": "e7db3b45-8db7-47ad-8109-3fb55c2c24fd",
203 "name": "Ubuntu 12.10",
204 "tags": [
205 "ubuntu",
206 "quantal"
207 ]
208 }`)
209
210 w.WriteHeader(http.StatusCreated)
211 w.Header().Add("Content-Type", "application/json")
212 fmt.Fprintf(w, `{
213 "status": "queued",
214 "name": "Ubuntu 12.10",
215 "protected": false,
216 "tags": ["ubuntu","quantal"],
217 "container_format": "bare",
218 "created_at": "2014-11-11T20:47:55Z",
219 "disk_format": "qcow2",
220 "updated_at": "2014-11-11T20:47:55Z",
221 "visibility": "private",
222 "self": "/v2/images/e7db3b45-8db7-47ad-8109-3fb55c2c24fd",
223 "min_disk": 0,
224 "protected": false,
225 "id": "e7db3b45-8db7-47ad-8109-3fb55c2c24fd",
226 "file": "/v2/images/e7db3b45-8db7-47ad-8109-3fb55c2c24fd/file",
227 "owner": "b4eedccc6fb74fa8a7ad6b08382b852b",
228 "min_ram": 0,
229 "schema": "/v2/schemas/image",
230 "size": null,
231 "checksum": null,
232 "virtual_size": null
233 }`)
234 })
235}
236
237// HandleImageGetSuccessfully test setup
238func HandleImageGetSuccessfully(t *testing.T) {
239 th.Mux.HandleFunc("/images/1bea47ed-f6a9-463b-b423-14b9cca9ad27", func(w http.ResponseWriter, r *http.Request) {
240 th.TestMethod(t, r, "GET")
241 th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID)
242
243 w.WriteHeader(http.StatusOK)
244 w.Header().Add("Content-Type", "application/json")
245 fmt.Fprintf(w, `{
246 "status": "active",
247 "name": "cirros-0.3.2-x86_64-disk",
248 "tags": [],
249 "container_format": "bare",
250 "created_at": "2014-05-05T17:15:10Z",
251 "disk_format": "qcow2",
252 "updated_at": "2014-05-05T17:15:11Z",
253 "visibility": "public",
254 "self": "/v2/images/1bea47ed-f6a9-463b-b423-14b9cca9ad27",
255 "min_disk": 0,
256 "protected": false,
257 "id": "1bea47ed-f6a9-463b-b423-14b9cca9ad27",
258 "file": "/v2/images/1bea47ed-f6a9-463b-b423-14b9cca9ad27/file",
259 "checksum": "64d7c1cd2b6f60c92c14662941cb7913",
260 "owner": "5ef70662f8b34079a6eddb8da9d75fe8",
261 "size": 13167616,
262 "min_ram": 0,
263 "schema": "/v2/schemas/image",
264 "virtual_size": "None"
265 }`)
266 })
267}
268
269// HandleImageDeleteSuccessfully test setup
270func HandleImageDeleteSuccessfully(t *testing.T) {
271 th.Mux.HandleFunc("/images/1bea47ed-f6a9-463b-b423-14b9cca9ad27", func(w http.ResponseWriter, r *http.Request) {
272 th.TestMethod(t, r, "DELETE")
273 th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID)
274
275 w.WriteHeader(http.StatusNoContent)
276 })
277}
278
279// HandleImageUpdateSuccessfully setup
280func HandleImageUpdateSuccessfully(t *testing.T) {
281 th.Mux.HandleFunc("/images/da3b75d9-3f4a-40e7-8a2c-bfab23927dea", func(w http.ResponseWriter, r *http.Request) {
282 th.TestMethod(t, r, "PATCH")
283 th.TestHeader(t, r, "X-Auth-Token", fakeclient.TokenID)
284
285 th.TestJSONRequest(t, r, `[
286 {
287 "op": "replace",
288 "path": "/name",
289 "value": "Fedora 17"
290 },
291 {
292 "op": "replace",
293 "path": "/tags",
294 "value": [
295 "fedora",
296 "beefy"
297 ]
298 }
299 ]`)
300
301 th.AssertEquals(t, "application/openstack-images-v2.1-json-patch", r.Header.Get("Content-Type"))
302
303 w.WriteHeader(http.StatusOK)
304 w.Header().Add("Content-Type", "application/json")
305 fmt.Fprintf(w, `{
306 "id": "da3b75d9-3f4a-40e7-8a2c-bfab23927dea",
307 "name": "Fedora 17",
308 "status": "active",
309 "visibility": "public",
310 "size": 2254249,
311 "checksum": "2cec138d7dae2aa59038ef8c9aec2390",
312 "tags": [
313 "fedora",
314 "beefy"
315 ],
316 "created_at": "2012-08-10T19:23:50Z",
317 "updated_at": "2012-08-12T11:11:33Z",
318 "self": "/v2/images/da3b75d9-3f4a-40e7-8a2c-bfab23927dea",
319 "file": "/v2/images/da3b75d9-3f4a-40e7-8a2c-bfab23927dea/file",
320 "schema": "/v2/schemas/image",
321 "owner": "",
322 "min_ram": 0,
323 "min_disk": 0,
324 "disk_format": "",
325 "virtual_size": 0,
326 "container_format": ""
327 }`)
328 })
329}