blob: 1e56a005e5b5ff7df44f5d141c9d0104e3daa47c [file] [log] [blame]
jrperritt3d966162016-06-06 14:08:54 -05001package testing
Ash Wilsone77ffb02014-10-20 13:10:26 -04002
Ash Wilson664fe332014-10-21 17:47:49 -04003import (
4 "fmt"
5 "net/http"
6 "testing"
7
Jon Perrittdb0ae142016-03-13 00:33:41 -06008 "github.com/gophercloud/gophercloud"
jrperritt3d966162016-06-06 14:08:54 -05009 "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
Jon Perritt27249f42016-02-18 10:35:59 -060010 th "github.com/gophercloud/gophercloud/testhelper"
11 "github.com/gophercloud/gophercloud/testhelper/client"
Ash Wilson664fe332014-10-21 17:47:49 -040012)
13
Ash Wilsone77ffb02014-10-20 13:10:26 -040014// ServerListBody contains the canned body of a servers.List response.
15const ServerListBody = `
16{
17 "servers": [
18 {
19 "status": "ACTIVE",
20 "updated": "2014-09-25T13:10:10Z",
21 "hostId": "29d3c8c896a45aa4c34e52247875d7fefc3d94bbcc9f622b5d204362",
22 "OS-EXT-SRV-ATTR:host": "devstack",
23 "addresses": {
24 "private": [
25 {
26 "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:7c:1b:2b",
27 "version": 4,
28 "addr": "10.0.0.32",
29 "OS-EXT-IPS:type": "fixed"
30 }
31 ]
32 },
33 "links": [
34 {
35 "href": "http://104.130.131.164:8774/v2/fcad67a6189847c4aecfa3c81a05783b/servers/ef079b0c-e610-4dfb-b1aa-b49f07ac48e5",
36 "rel": "self"
37 },
38 {
39 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/servers/ef079b0c-e610-4dfb-b1aa-b49f07ac48e5",
40 "rel": "bookmark"
41 }
42 ],
43 "key_name": null,
44 "image": {
45 "id": "f90f6034-2570-4974-8351-6b49732ef2eb",
46 "links": [
47 {
48 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
49 "rel": "bookmark"
50 }
51 ]
52 },
53 "OS-EXT-STS:task_state": null,
54 "OS-EXT-STS:vm_state": "active",
55 "OS-EXT-SRV-ATTR:instance_name": "instance-0000001e",
56 "OS-SRV-USG:launched_at": "2014-09-25T13:10:10.000000",
57 "OS-EXT-SRV-ATTR:hypervisor_hostname": "devstack",
58 "flavor": {
59 "id": "1",
60 "links": [
61 {
62 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/flavors/1",
63 "rel": "bookmark"
64 }
65 ]
66 },
67 "id": "ef079b0c-e610-4dfb-b1aa-b49f07ac48e5",
68 "security_groups": [
69 {
70 "name": "default"
71 }
72 ],
73 "OS-SRV-USG:terminated_at": null,
74 "OS-EXT-AZ:availability_zone": "nova",
75 "user_id": "9349aff8be7545ac9d2f1d00999a23cd",
76 "name": "herp",
77 "created": "2014-09-25T13:10:02Z",
78 "tenant_id": "fcad67a6189847c4aecfa3c81a05783b",
79 "OS-DCF:diskConfig": "MANUAL",
80 "os-extended-volumes:volumes_attached": [],
81 "accessIPv4": "",
82 "accessIPv6": "",
83 "progress": 0,
84 "OS-EXT-STS:power_state": 1,
85 "config_drive": "",
86 "metadata": {}
87 },
88 {
89 "status": "ACTIVE",
90 "updated": "2014-09-25T13:04:49Z",
91 "hostId": "29d3c8c896a45aa4c34e52247875d7fefc3d94bbcc9f622b5d204362",
92 "OS-EXT-SRV-ATTR:host": "devstack",
93 "addresses": {
94 "private": [
95 {
96 "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:9e:89:be",
97 "version": 4,
98 "addr": "10.0.0.31",
99 "OS-EXT-IPS:type": "fixed"
100 }
101 ]
102 },
103 "links": [
104 {
105 "href": "http://104.130.131.164:8774/v2/fcad67a6189847c4aecfa3c81a05783b/servers/9e5476bd-a4ec-4653-93d6-72c93aa682ba",
106 "rel": "self"
107 },
108 {
109 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/servers/9e5476bd-a4ec-4653-93d6-72c93aa682ba",
110 "rel": "bookmark"
111 }
112 ],
113 "key_name": null,
114 "image": {
115 "id": "f90f6034-2570-4974-8351-6b49732ef2eb",
116 "links": [
117 {
118 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
119 "rel": "bookmark"
120 }
121 ]
122 },
123 "OS-EXT-STS:task_state": null,
124 "OS-EXT-STS:vm_state": "active",
125 "OS-EXT-SRV-ATTR:instance_name": "instance-0000001d",
126 "OS-SRV-USG:launched_at": "2014-09-25T13:04:49.000000",
127 "OS-EXT-SRV-ATTR:hypervisor_hostname": "devstack",
128 "flavor": {
129 "id": "1",
130 "links": [
131 {
132 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/flavors/1",
133 "rel": "bookmark"
134 }
135 ]
136 },
137 "id": "9e5476bd-a4ec-4653-93d6-72c93aa682ba",
138 "security_groups": [
139 {
140 "name": "default"
141 }
142 ],
143 "OS-SRV-USG:terminated_at": null,
144 "OS-EXT-AZ:availability_zone": "nova",
145 "user_id": "9349aff8be7545ac9d2f1d00999a23cd",
146 "name": "derp",
147 "created": "2014-09-25T13:04:41Z",
148 "tenant_id": "fcad67a6189847c4aecfa3c81a05783b",
149 "OS-DCF:diskConfig": "MANUAL",
150 "os-extended-volumes:volumes_attached": [],
151 "accessIPv4": "",
152 "accessIPv6": "",
153 "progress": 0,
154 "OS-EXT-STS:power_state": 1,
155 "config_drive": "",
156 "metadata": {}
jrperritt1fa92502016-07-21 19:22:59 -0500157 },
158 {
159 "status": "ACTIVE",
160 "updated": "2014-09-25T13:04:49Z",
161 "hostId": "29d3c8c896a45aa4c34e52247875d7fefc3d94bbcc9f622b5d204362",
162 "OS-EXT-SRV-ATTR:host": "devstack",
163 "addresses": {
164 "private": [
165 {
166 "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:9e:89:be",
167 "version": 4,
168 "addr": "10.0.0.31",
169 "OS-EXT-IPS:type": "fixed"
170 }
171 ]
172 },
173 "links": [
174 {
175 "href": "http://104.130.131.164:8774/v2/fcad67a6189847c4aecfa3c81a05783b/servers/9e5476bd-a4ec-4653-93d6-72c93aa682ba",
176 "rel": "self"
177 },
178 {
179 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/servers/9e5476bd-a4ec-4653-93d6-72c93aa682ba",
180 "rel": "bookmark"
181 }
182 ],
183 "key_name": null,
184 "image": "",
185 "OS-EXT-STS:task_state": null,
186 "OS-EXT-STS:vm_state": "active",
187 "OS-EXT-SRV-ATTR:instance_name": "instance-0000001d",
188 "OS-SRV-USG:launched_at": "2014-09-25T13:04:49.000000",
189 "OS-EXT-SRV-ATTR:hypervisor_hostname": "devstack",
190 "flavor": {
191 "id": "1",
192 "links": [
193 {
194 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/flavors/1",
195 "rel": "bookmark"
196 }
197 ]
198 },
199 "id": "9e5476bd-a4ec-4653-93d6-72c93aa682bb",
200 "security_groups": [
201 {
202 "name": "default"
203 }
204 ],
205 "OS-SRV-USG:terminated_at": null,
206 "OS-EXT-AZ:availability_zone": "nova",
207 "user_id": "9349aff8be7545ac9d2f1d00999a23cd",
208 "name": "merp",
209 "created": "2014-09-25T13:04:41Z",
210 "tenant_id": "fcad67a6189847c4aecfa3c81a05783b",
211 "OS-DCF:diskConfig": "MANUAL",
212 "os-extended-volumes:volumes_attached": [],
213 "accessIPv4": "",
214 "accessIPv6": "",
215 "progress": 0,
216 "OS-EXT-STS:power_state": 1,
217 "config_drive": "",
218 "metadata": {}
219 }
Ash Wilsone77ffb02014-10-20 13:10:26 -0400220 ]
221}
222`
223
224// SingleServerBody is the canned body of a Get request on an existing server.
225const SingleServerBody = `
226{
227 "server": {
228 "status": "ACTIVE",
229 "updated": "2014-09-25T13:04:49Z",
230 "hostId": "29d3c8c896a45aa4c34e52247875d7fefc3d94bbcc9f622b5d204362",
231 "OS-EXT-SRV-ATTR:host": "devstack",
232 "addresses": {
233 "private": [
234 {
235 "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:9e:89:be",
236 "version": 4,
237 "addr": "10.0.0.31",
238 "OS-EXT-IPS:type": "fixed"
239 }
240 ]
241 },
242 "links": [
243 {
244 "href": "http://104.130.131.164:8774/v2/fcad67a6189847c4aecfa3c81a05783b/servers/9e5476bd-a4ec-4653-93d6-72c93aa682ba",
245 "rel": "self"
246 },
247 {
248 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/servers/9e5476bd-a4ec-4653-93d6-72c93aa682ba",
249 "rel": "bookmark"
250 }
251 ],
252 "key_name": null,
253 "image": {
254 "id": "f90f6034-2570-4974-8351-6b49732ef2eb",
255 "links": [
256 {
257 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
258 "rel": "bookmark"
259 }
260 ]
261 },
262 "OS-EXT-STS:task_state": null,
263 "OS-EXT-STS:vm_state": "active",
264 "OS-EXT-SRV-ATTR:instance_name": "instance-0000001d",
265 "OS-SRV-USG:launched_at": "2014-09-25T13:04:49.000000",
266 "OS-EXT-SRV-ATTR:hypervisor_hostname": "devstack",
267 "flavor": {
268 "id": "1",
269 "links": [
270 {
271 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/flavors/1",
272 "rel": "bookmark"
273 }
274 ]
275 },
276 "id": "9e5476bd-a4ec-4653-93d6-72c93aa682ba",
277 "security_groups": [
278 {
279 "name": "default"
280 }
281 ],
282 "OS-SRV-USG:terminated_at": null,
283 "OS-EXT-AZ:availability_zone": "nova",
284 "user_id": "9349aff8be7545ac9d2f1d00999a23cd",
285 "name": "derp",
286 "created": "2014-09-25T13:04:41Z",
287 "tenant_id": "fcad67a6189847c4aecfa3c81a05783b",
288 "OS-DCF:diskConfig": "MANUAL",
289 "os-extended-volumes:volumes_attached": [],
290 "accessIPv4": "",
291 "accessIPv6": "",
292 "progress": 0,
293 "OS-EXT-STS:power_state": 1,
294 "config_drive": "",
295 "metadata": {}
296 }
297}
298`
299
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100300const ServerPasswordBody = `
301{
302 "password": "xlozO3wLCBRWAa2yDjCCVx8vwNPypxnypmRYDa/zErlQ+EzPe1S/Gz6nfmC52mOlOSCRuUOmG7kqqgejPof6M7bOezS387zjq4LSvvwp28zUknzy4YzfFGhnHAdai3TxUJ26pfQCYrq8UTzmKF2Bq8ioSEtVVzM0A96pDh8W2i7BOz6MdoiVyiev/I1K2LsuipfxSJR7Wdke4zNXJjHHP2RfYsVbZ/k9ANu+Nz4iIH8/7Cacud/pphH7EjrY6a4RZNrjQskrhKYed0YERpotyjYk1eDtRe72GrSiXteqCM4biaQ5w3ruS+AcX//PXk3uJ5kC7d67fPXaVz4WaQRYMg=="
303}
304`
305
Ash Wilsone77ffb02014-10-20 13:10:26 -0400306var (
307 // ServerHerp is a Server struct that should correspond to the first result in ServerListBody.
jrperritt3d966162016-06-06 14:08:54 -0500308 ServerHerp = servers.Server{
Ash Wilsone77ffb02014-10-20 13:10:26 -0400309 Status: "ACTIVE",
310 Updated: "2014-09-25T13:10:10Z",
311 HostID: "29d3c8c896a45aa4c34e52247875d7fefc3d94bbcc9f622b5d204362",
312 Addresses: map[string]interface{}{
313 "private": []interface{}{
314 map[string]interface{}{
315 "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:7c:1b:2b",
316 "version": float64(4),
317 "addr": "10.0.0.32",
318 "OS-EXT-IPS:type": "fixed",
319 },
320 },
321 },
322 Links: []interface{}{
323 map[string]interface{}{
324 "href": "http://104.130.131.164:8774/v2/fcad67a6189847c4aecfa3c81a05783b/servers/ef079b0c-e610-4dfb-b1aa-b49f07ac48e5",
325 "rel": "self",
326 },
327 map[string]interface{}{
328 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/servers/ef079b0c-e610-4dfb-b1aa-b49f07ac48e5",
329 "rel": "bookmark",
330 },
331 },
332 Image: map[string]interface{}{
333 "id": "f90f6034-2570-4974-8351-6b49732ef2eb",
334 "links": []interface{}{
335 map[string]interface{}{
336 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
337 "rel": "bookmark",
338 },
339 },
340 },
341 Flavor: map[string]interface{}{
342 "id": "1",
343 "links": []interface{}{
344 map[string]interface{}{
345 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/flavors/1",
346 "rel": "bookmark",
347 },
348 },
349 },
350 ID: "ef079b0c-e610-4dfb-b1aa-b49f07ac48e5",
351 UserID: "9349aff8be7545ac9d2f1d00999a23cd",
352 Name: "herp",
353 Created: "2014-09-25T13:10:02Z",
354 TenantID: "fcad67a6189847c4aecfa3c81a05783b",
355 Metadata: map[string]interface{}{},
Joe Topjian978bb502015-02-12 20:55:31 +0000356 SecurityGroups: []map[string]interface{}{
357 map[string]interface{}{
358 "name": "default",
359 },
360 },
Ash Wilsone77ffb02014-10-20 13:10:26 -0400361 }
362
363 // ServerDerp is a Server struct that should correspond to the second server in ServerListBody.
jrperritt3d966162016-06-06 14:08:54 -0500364 ServerDerp = servers.Server{
Ash Wilsone77ffb02014-10-20 13:10:26 -0400365 Status: "ACTIVE",
366 Updated: "2014-09-25T13:04:49Z",
367 HostID: "29d3c8c896a45aa4c34e52247875d7fefc3d94bbcc9f622b5d204362",
368 Addresses: map[string]interface{}{
369 "private": []interface{}{
370 map[string]interface{}{
371 "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:9e:89:be",
372 "version": float64(4),
373 "addr": "10.0.0.31",
374 "OS-EXT-IPS:type": "fixed",
375 },
376 },
377 },
378 Links: []interface{}{
379 map[string]interface{}{
380 "href": "http://104.130.131.164:8774/v2/fcad67a6189847c4aecfa3c81a05783b/servers/9e5476bd-a4ec-4653-93d6-72c93aa682ba",
381 "rel": "self",
382 },
383 map[string]interface{}{
384 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/servers/9e5476bd-a4ec-4653-93d6-72c93aa682ba",
385 "rel": "bookmark",
386 },
387 },
388 Image: map[string]interface{}{
389 "id": "f90f6034-2570-4974-8351-6b49732ef2eb",
390 "links": []interface{}{
391 map[string]interface{}{
392 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
393 "rel": "bookmark",
394 },
395 },
396 },
397 Flavor: map[string]interface{}{
398 "id": "1",
399 "links": []interface{}{
400 map[string]interface{}{
401 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/flavors/1",
402 "rel": "bookmark",
403 },
404 },
405 },
406 ID: "9e5476bd-a4ec-4653-93d6-72c93aa682ba",
407 UserID: "9349aff8be7545ac9d2f1d00999a23cd",
408 Name: "derp",
409 Created: "2014-09-25T13:04:41Z",
410 TenantID: "fcad67a6189847c4aecfa3c81a05783b",
411 Metadata: map[string]interface{}{},
Joe Topjian978bb502015-02-12 20:55:31 +0000412 SecurityGroups: []map[string]interface{}{
413 map[string]interface{}{
414 "name": "default",
415 },
416 },
Ash Wilsone77ffb02014-10-20 13:10:26 -0400417 }
jrperritt1fa92502016-07-21 19:22:59 -0500418
419 // ServerMerp is a Server struct that should correspond to the second server in ServerListBody.
420 ServerMerp = servers.Server{
421 Status: "ACTIVE",
422 Updated: "2014-09-25T13:04:49Z",
423 HostID: "29d3c8c896a45aa4c34e52247875d7fefc3d94bbcc9f622b5d204362",
424 Addresses: map[string]interface{}{
425 "private": []interface{}{
426 map[string]interface{}{
427 "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:9e:89:be",
428 "version": float64(4),
429 "addr": "10.0.0.31",
430 "OS-EXT-IPS:type": "fixed",
431 },
432 },
433 },
434 Links: []interface{}{
435 map[string]interface{}{
436 "href": "http://104.130.131.164:8774/v2/fcad67a6189847c4aecfa3c81a05783b/servers/9e5476bd-a4ec-4653-93d6-72c93aa682ba",
437 "rel": "self",
438 },
439 map[string]interface{}{
440 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/servers/9e5476bd-a4ec-4653-93d6-72c93aa682ba",
441 "rel": "bookmark",
442 },
443 },
444 Image: nil,
445 Flavor: map[string]interface{}{
446 "id": "1",
447 "links": []interface{}{
448 map[string]interface{}{
449 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/flavors/1",
450 "rel": "bookmark",
451 },
452 },
453 },
454 ID: "9e5476bd-a4ec-4653-93d6-72c93aa682bb",
455 UserID: "9349aff8be7545ac9d2f1d00999a23cd",
456 Name: "merp",
457 Created: "2014-09-25T13:04:41Z",
458 TenantID: "fcad67a6189847c4aecfa3c81a05783b",
459 Metadata: map[string]interface{}{},
460 SecurityGroups: []map[string]interface{}{
461 map[string]interface{}{
462 "name": "default",
463 },
464 },
465 }
Ash Wilsone77ffb02014-10-20 13:10:26 -0400466)
Ash Wilson664fe332014-10-21 17:47:49 -0400467
Jon Perrittdb0ae142016-03-13 00:33:41 -0600468type CreateOptsWithCustomField struct {
jrperritt3d966162016-06-06 14:08:54 -0500469 servers.CreateOpts
Jon Perrittdb0ae142016-03-13 00:33:41 -0600470 Foo string `json:"foo,omitempty"`
471}
472
473func (opts CreateOptsWithCustomField) ToServerCreateMap() (map[string]interface{}, error) {
474 return gophercloud.BuildRequestBody(opts, "server")
475}
476
Ash Wilson664fe332014-10-21 17:47:49 -0400477// HandleServerCreationSuccessfully sets up the test server to respond to a server creation request
478// with a given response.
479func HandleServerCreationSuccessfully(t *testing.T, response string) {
480 th.Mux.HandleFunc("/servers", func(w http.ResponseWriter, r *http.Request) {
481 th.TestMethod(t, r, "POST")
482 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
483 th.TestJSONRequest(t, r, `{
484 "server": {
485 "name": "derp",
486 "imageRef": "f90f6034-2570-4974-8351-6b49732ef2eb",
487 "flavorRef": "1"
488 }
489 }`)
490
491 w.WriteHeader(http.StatusAccepted)
492 w.Header().Add("Content-Type", "application/json")
493 fmt.Fprintf(w, response)
494 })
jrperritt0d7ed5d2016-08-16 11:23:26 -0500495
496 th.Mux.HandleFunc("/images/detail", func(w http.ResponseWriter, r *http.Request) {
497 th.TestMethod(t, r, "GET")
498 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
499
500 w.Header().Add("Content-Type", "application/json")
501 r.ParseForm()
502 marker := r.Form.Get("marker")
503 switch marker {
504 case "":
505 fmt.Fprintf(w, `
506 {
507 "images": [
508 {
509 "status": "ACTIVE",
510 "updated": "2014-09-23T12:54:56Z",
511 "id": "f3e4a95d-1f4f-4989-97ce-f3a1fb8c04d7",
512 "OS-EXT-IMG-SIZE:size": 476704768,
513 "name": "F17-x86_64-cfntools",
514 "created": "2014-09-23T12:54:52Z",
515 "minDisk": 0,
516 "progress": 100,
517 "minRam": 0
518 },
519 {
520 "status": "ACTIVE",
521 "updated": "2014-09-23T12:51:43Z",
522 "id": "f90f6034-2570-4974-8351-6b49732ef2eb",
523 "OS-EXT-IMG-SIZE:size": 13167616,
524 "name": "cirros-0.3.2-x86_64-disk",
525 "created": "2014-09-23T12:51:42Z",
526 "minDisk": 0,
527 "progress": 100,
528 "minRam": 0
529 }
530 ]
531 }
532 `)
533 case "2":
534 fmt.Fprintf(w, `{ "images": [] }`)
535 default:
536 t.Fatalf("Unexpected marker: [%s]", marker)
537 }
538 })
539
540 th.Mux.HandleFunc("/flavors/detail", func(w http.ResponseWriter, r *http.Request) {
541 th.TestMethod(t, r, "GET")
542 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
543
544 w.Header().Add("Content-Type", "application/json")
545 r.ParseForm()
546 marker := r.Form.Get("marker")
547 switch marker {
548 case "":
549 fmt.Fprintf(w, `
550 {
551 "flavors": [
552 {
553 "id": "1",
554 "name": "m1.tiny",
555 "disk": 1,
556 "ram": 512,
557 "vcpus": 1,
558 "swap":""
559 },
560 {
561 "id": "2",
562 "name": "m2.small",
563 "disk": 10,
564 "ram": 1024,
565 "vcpus": 2,
566 "swap": 1000
567 }
568 ],
569 "flavors_links": [
570 {
571 "href": "%s/flavors/detail?marker=2",
572 "rel": "next"
573 }
574 ]
575 }
576 `, th.Server.URL)
577 case "2":
578 fmt.Fprintf(w, `{ "flavors": [] }`)
579 default:
580 t.Fatalf("Unexpected marker: [%s]", marker)
581 }
582 })
Ash Wilson664fe332014-10-21 17:47:49 -0400583}
584
Jon Perrittdb0ae142016-03-13 00:33:41 -0600585// HandleServerCreationWithCustomFieldSuccessfully sets up the test server to respond to a server creation request
586// with a given response.
587func HandleServerCreationWithCustomFieldSuccessfully(t *testing.T, response string) {
588 th.Mux.HandleFunc("/servers", func(w http.ResponseWriter, r *http.Request) {
589 th.TestMethod(t, r, "POST")
590 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
591 th.TestJSONRequest(t, r, `{
592 "server": {
593 "name": "derp",
594 "imageRef": "f90f6034-2570-4974-8351-6b49732ef2eb",
595 "flavorRef": "1",
596 "foo": "bar"
597 }
598 }`)
599
600 w.WriteHeader(http.StatusAccepted)
601 w.Header().Add("Content-Type", "application/json")
602 fmt.Fprintf(w, response)
603 })
604}
605
Ash Wilsona70510a2014-10-23 11:54:03 -0400606// HandleServerListSuccessfully sets up the test server to respond to a server List request.
607func HandleServerListSuccessfully(t *testing.T) {
608 th.Mux.HandleFunc("/servers/detail", func(w http.ResponseWriter, r *http.Request) {
609 th.TestMethod(t, r, "GET")
610 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
611
612 w.Header().Add("Content-Type", "application/json")
613 r.ParseForm()
614 marker := r.Form.Get("marker")
615 switch marker {
616 case "":
617 fmt.Fprintf(w, ServerListBody)
618 case "9e5476bd-a4ec-4653-93d6-72c93aa682ba":
619 fmt.Fprintf(w, `{ "servers": [] }`)
620 default:
621 t.Fatalf("/servers/detail invoked with unexpected marker=[%s]", marker)
622 }
623 })
624}
625
Ash Wilson664fe332014-10-21 17:47:49 -0400626// HandleServerDeletionSuccessfully sets up the test server to respond to a server deletion request.
627func HandleServerDeletionSuccessfully(t *testing.T) {
628 th.Mux.HandleFunc("/servers/asdfasdfasdf", func(w http.ResponseWriter, r *http.Request) {
629 th.TestMethod(t, r, "DELETE")
630 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
631
632 w.WriteHeader(http.StatusNoContent)
633 })
634}
Ash Wilson1c1eb882014-10-21 18:14:31 -0400635
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100636// HandleServerForceDeletionSuccessfully sets up the test server to respond to a server force deletion
637// request.
Ian Duffy370c4302016-01-21 10:44:56 +0000638func HandleServerForceDeletionSuccessfully(t *testing.T) {
639 th.Mux.HandleFunc("/servers/asdfasdfasdf/action", func(w http.ResponseWriter, r *http.Request) {
640 th.TestMethod(t, r, "POST")
641 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
642 th.TestJSONRequest(t, r, `{ "forceDelete": "" }`)
643
644 w.WriteHeader(http.StatusAccepted)
645 })
646}
647
Ash Wilson189c95c2014-10-23 11:41:35 -0400648// HandleServerGetSuccessfully sets up the test server to respond to a server Get request.
649func HandleServerGetSuccessfully(t *testing.T) {
650 th.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) {
651 th.TestMethod(t, r, "GET")
652 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
653 th.TestHeader(t, r, "Accept", "application/json")
654
655 fmt.Fprintf(w, SingleServerBody)
656 })
657}
658
659// HandleServerUpdateSuccessfully sets up the test server to respond to a server Update request.
660func HandleServerUpdateSuccessfully(t *testing.T) {
661 th.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) {
662 th.TestMethod(t, r, "PUT")
663 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
664 th.TestHeader(t, r, "Accept", "application/json")
665 th.TestHeader(t, r, "Content-Type", "application/json")
666 th.TestJSONRequest(t, r, `{ "server": { "name": "new-name" } }`)
667
668 fmt.Fprintf(w, SingleServerBody)
669 })
670}
671
Ash Wilson1c1eb882014-10-21 18:14:31 -0400672// HandleAdminPasswordChangeSuccessfully sets up the test server to respond to a server password
673// change request.
674func HandleAdminPasswordChangeSuccessfully(t *testing.T) {
675 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
676 th.TestMethod(t, r, "POST")
677 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
678 th.TestJSONRequest(t, r, `{ "changePassword": { "adminPass": "new-password" } }`)
679
680 w.WriteHeader(http.StatusAccepted)
681 })
682}
Ash Wilson2295ba52014-10-21 18:19:28 -0400683
684// HandleRebootSuccessfully sets up the test server to respond to a reboot request with success.
685func HandleRebootSuccessfully(t *testing.T) {
686 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
687 th.TestMethod(t, r, "POST")
688 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
689 th.TestJSONRequest(t, r, `{ "reboot": { "type": "SOFT" } }`)
690
691 w.WriteHeader(http.StatusAccepted)
692 })
693}
Ash Wilsonacf49c62014-10-21 18:25:11 -0400694
695// HandleRebuildSuccessfully sets up the test server to respond to a rebuild request with success.
696func HandleRebuildSuccessfully(t *testing.T, response string) {
697 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
698 th.TestMethod(t, r, "POST")
699 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
700 th.TestJSONRequest(t, r, `
701 {
702 "rebuild": {
703 "name": "new-name",
704 "adminPass": "swordfish",
705 "imageRef": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
706 "accessIPv4": "1.2.3.4"
707 }
708 }
709 `)
710
711 w.WriteHeader(http.StatusAccepted)
712 w.Header().Add("Content-Type", "application/json")
713 fmt.Fprintf(w, response)
714 })
715}
Alex Gaynorfa8fbeb2014-11-13 13:39:31 -0800716
Jon Perrittcc77da62014-11-16 13:14:21 -0700717// HandleServerRescueSuccessfully sets up the test server to respond to a server Rescue request.
Alex Gaynorfa8fbeb2014-11-13 13:39:31 -0800718func HandleServerRescueSuccessfully(t *testing.T) {
719 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
720 th.TestMethod(t, r, "POST")
721 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
722 th.TestJSONRequest(t, r, `{ "rescue": { "adminPass": "1234567890" } }`)
723
724 w.WriteHeader(http.StatusOK)
725 w.Write([]byte(`{ "adminPass": "1234567890" }`))
726 })
727}
Jon Perrittcc77da62014-11-16 13:14:21 -0700728
Jon Perritt78c57ce2014-11-20 11:07:18 -0700729// HandleMetadatumGetSuccessfully sets up the test server to respond to a metadatum Get request.
730func HandleMetadatumGetSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700731 th.Mux.HandleFunc("/servers/1234asdf/metadata/foo", func(w http.ResponseWriter, r *http.Request) {
732 th.TestMethod(t, r, "GET")
733 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
734 th.TestHeader(t, r, "Accept", "application/json")
735
736 w.WriteHeader(http.StatusOK)
737 w.Header().Add("Content-Type", "application/json")
738 w.Write([]byte(`{ "meta": {"foo":"bar"}}`))
739 })
740}
741
Jon Perritt78c57ce2014-11-20 11:07:18 -0700742// HandleMetadatumCreateSuccessfully sets up the test server to respond to a metadatum Create request.
743func HandleMetadatumCreateSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700744 th.Mux.HandleFunc("/servers/1234asdf/metadata/foo", func(w http.ResponseWriter, r *http.Request) {
745 th.TestMethod(t, r, "PUT")
746 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
747 th.TestJSONRequest(t, r, `{
748 "meta": {
749 "foo": "bar"
750 }
751 }`)
752
753 w.WriteHeader(http.StatusOK)
754 w.Header().Add("Content-Type", "application/json")
755 w.Write([]byte(`{ "meta": {"foo":"bar"}}`))
756 })
757}
758
Jon Perritt78c57ce2014-11-20 11:07:18 -0700759// HandleMetadatumDeleteSuccessfully sets up the test server to respond to a metadatum Delete request.
760func HandleMetadatumDeleteSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700761 th.Mux.HandleFunc("/servers/1234asdf/metadata/foo", func(w http.ResponseWriter, r *http.Request) {
762 th.TestMethod(t, r, "DELETE")
763 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
764
765 w.WriteHeader(http.StatusNoContent)
766 })
767}
768
Jon Perritt78c57ce2014-11-20 11:07:18 -0700769// HandleMetadataGetSuccessfully sets up the test server to respond to a metadata Get request.
770func HandleMetadataGetSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700771 th.Mux.HandleFunc("/servers/1234asdf/metadata", func(w http.ResponseWriter, r *http.Request) {
772 th.TestMethod(t, r, "GET")
773 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
774 th.TestHeader(t, r, "Accept", "application/json")
775
776 w.WriteHeader(http.StatusOK)
777 w.Write([]byte(`{ "metadata": {"foo":"bar", "this":"that"}}`))
778 })
779}
780
Jon Perritt789f8322014-11-21 08:20:04 -0700781// HandleMetadataResetSuccessfully sets up the test server to respond to a metadata Create request.
782func HandleMetadataResetSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700783 th.Mux.HandleFunc("/servers/1234asdf/metadata", func(w http.ResponseWriter, r *http.Request) {
784 th.TestMethod(t, r, "PUT")
785 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
786 th.TestJSONRequest(t, r, `{
787 "metadata": {
788 "foo": "bar",
789 "this": "that"
790 }
791 }`)
792
793 w.WriteHeader(http.StatusOK)
794 w.Header().Add("Content-Type", "application/json")
795 w.Write([]byte(`{ "metadata": {"foo":"bar", "this":"that"}}`))
796 })
797}
798
Jon Perritt78c57ce2014-11-20 11:07:18 -0700799// HandleMetadataUpdateSuccessfully sets up the test server to respond to a metadata Update request.
800func HandleMetadataUpdateSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700801 th.Mux.HandleFunc("/servers/1234asdf/metadata", func(w http.ResponseWriter, r *http.Request) {
802 th.TestMethod(t, r, "POST")
803 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
804 th.TestJSONRequest(t, r, `{
805 "metadata": {
806 "foo": "baz",
807 "this": "those"
808 }
809 }`)
810
811 w.WriteHeader(http.StatusOK)
812 w.Header().Add("Content-Type", "application/json")
813 w.Write([]byte(`{ "metadata": {"foo":"baz", "this":"those"}}`))
814 })
815}
Jon Perritt5cb49482015-02-19 12:19:58 -0700816
817// ListAddressesExpected represents an expected repsonse from a ListAddresses request.
jrperritt3d966162016-06-06 14:08:54 -0500818var ListAddressesExpected = map[string][]servers.Address{
819 "public": []servers.Address{
820 {
Jon Perritt5cb49482015-02-19 12:19:58 -0700821 Version: 4,
822 Address: "80.56.136.39",
823 },
jrperritt3d966162016-06-06 14:08:54 -0500824 {
Jon Perritt5cb49482015-02-19 12:19:58 -0700825 Version: 6,
826 Address: "2001:4800:790e:510:be76:4eff:fe04:82a8",
827 },
828 },
jrperritt3d966162016-06-06 14:08:54 -0500829 "private": []servers.Address{
830 {
Jon Perritt5cb49482015-02-19 12:19:58 -0700831 Version: 4,
832 Address: "10.880.3.154",
833 },
834 },
835}
836
837// HandleAddressListSuccessfully sets up the test server to respond to a ListAddresses request.
838func HandleAddressListSuccessfully(t *testing.T) {
839 th.Mux.HandleFunc("/servers/asdfasdfasdf/ips", func(w http.ResponseWriter, r *http.Request) {
840 th.TestMethod(t, r, "GET")
841 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
842
843 w.Header().Add("Content-Type", "application/json")
844 fmt.Fprintf(w, `{
845 "addresses": {
846 "public": [
847 {
848 "version": 4,
849 "addr": "50.56.176.35"
850 },
851 {
852 "version": 6,
853 "addr": "2001:4800:780e:510:be76:4eff:fe04:84a8"
854 }
855 ],
856 "private": [
857 {
858 "version": 4,
859 "addr": "10.180.3.155"
860 }
861 ]
862 }
863 }`)
864 })
865}
Jon Perritt04d073c2015-02-19 21:46:23 -0700866
867// ListNetworkAddressesExpected represents an expected repsonse from a ListAddressesByNetwork request.
jrperritt3d966162016-06-06 14:08:54 -0500868var ListNetworkAddressesExpected = []servers.Address{
869 {
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700870 Version: 4,
871 Address: "50.56.176.35",
872 },
jrperritt3d966162016-06-06 14:08:54 -0500873 {
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700874 Version: 6,
875 Address: "2001:4800:780e:510:be76:4eff:fe04:84a8",
Jon Perritt04d073c2015-02-19 21:46:23 -0700876 },
877}
878
879// HandleNetworkAddressListSuccessfully sets up the test server to respond to a ListAddressesByNetwork request.
880func HandleNetworkAddressListSuccessfully(t *testing.T) {
881 th.Mux.HandleFunc("/servers/asdfasdfasdf/ips/public", func(w http.ResponseWriter, r *http.Request) {
882 th.TestMethod(t, r, "GET")
883 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
884
885 w.Header().Add("Content-Type", "application/json")
886 fmt.Fprintf(w, `{
887 "public": [
888 {
889 "version": 4,
890 "addr": "50.56.176.35"
891 },
892 {
893 "version": 6,
894 "addr": "2001:4800:780e:510:be76:4eff:fe04:84a8"
895 }
896 ]
897 }`)
898 })
899}
einarf2fc665e2015-04-16 20:16:21 +0000900
901// HandleCreateServerImageSuccessfully sets up the test server to respond to a TestCreateServerImage request.
902func HandleCreateServerImageSuccessfully(t *testing.T) {
903 th.Mux.HandleFunc("/servers/serverimage/action", func(w http.ResponseWriter, r *http.Request) {
904 th.TestMethod(t, r, "POST")
905 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
906 w.Header().Add("Location", "https://0.0.0.0/images/xxxx-xxxxx-xxxxx-xxxx")
907 w.WriteHeader(http.StatusAccepted)
908 })
909}
910
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100911// HandlePasswordGetSuccessfully sets up the test server to respond to a password Get request.
912func HandlePasswordGetSuccessfully(t *testing.T) {
913 th.Mux.HandleFunc("/servers/1234asdf/os-server-password", func(w http.ResponseWriter, r *http.Request) {
914 th.TestMethod(t, r, "GET")
915 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
916 th.TestHeader(t, r, "Accept", "application/json")
917
918 fmt.Fprintf(w, ServerPasswordBody)
919 })
920}