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