blob: adedb46578f0e475ed644bcb379f5b0b84f13ed1 [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",
Joe Topjianf464c962016-09-12 08:02:43 -0600355 Metadata: map[string]string{},
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",
Joe Topjianf464c962016-09-12 08:02:43 -0600411 Metadata: map[string]string{},
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",
Joe Topjianf464c962016-09-12 08:02:43 -0600459 Metadata: map[string]string{},
jrperritt1fa92502016-07-21 19:22:59 -0500460 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
Gavin Williamscd65a062016-11-08 19:05:47 +0000606// HandleServerCreationWithUserdata sets up the test server to respond to a server creation request
607// with a given response.
608func HandleServerCreationWithUserdata(t *testing.T, response string) {
609 th.Mux.HandleFunc("/servers", func(w http.ResponseWriter, r *http.Request) {
610 th.TestMethod(t, r, "POST")
611 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
612 th.TestJSONRequest(t, r, `{
613 "server": {
614 "name": "derp",
615 "imageRef": "f90f6034-2570-4974-8351-6b49732ef2eb",
616 "flavorRef": "1",
617 "user_data": "dXNlcmRhdGEgc3RyaW5n"
618 }
619 }`)
620
621 w.WriteHeader(http.StatusAccepted)
622 w.Header().Add("Content-Type", "application/json")
623 fmt.Fprintf(w, response)
624 })
625}
626
Joe Topjianf464c962016-09-12 08:02:43 -0600627// HandleServerCreationWithMetadata sets up the test server to respond to a server creation request
628// with a given response.
629func HandleServerCreationWithMetadata(t *testing.T, response string) {
630 th.Mux.HandleFunc("/servers", func(w http.ResponseWriter, r *http.Request) {
631 th.TestMethod(t, r, "POST")
632 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
633 th.TestJSONRequest(t, r, `{
634 "server": {
635 "name": "derp",
636 "imageRef": "f90f6034-2570-4974-8351-6b49732ef2eb",
637 "flavorRef": "1",
638 "metadata": {
639 "abc": "def"
640 }
641 }
642 }`)
643
644 w.WriteHeader(http.StatusAccepted)
645 w.Header().Add("Content-Type", "application/json")
646 fmt.Fprintf(w, response)
647 })
648}
649
Ash Wilsona70510a2014-10-23 11:54:03 -0400650// HandleServerListSuccessfully sets up the test server to respond to a server List request.
651func HandleServerListSuccessfully(t *testing.T) {
652 th.Mux.HandleFunc("/servers/detail", func(w http.ResponseWriter, r *http.Request) {
653 th.TestMethod(t, r, "GET")
654 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
655
656 w.Header().Add("Content-Type", "application/json")
657 r.ParseForm()
658 marker := r.Form.Get("marker")
659 switch marker {
660 case "":
661 fmt.Fprintf(w, ServerListBody)
662 case "9e5476bd-a4ec-4653-93d6-72c93aa682ba":
663 fmt.Fprintf(w, `{ "servers": [] }`)
664 default:
665 t.Fatalf("/servers/detail invoked with unexpected marker=[%s]", marker)
666 }
667 })
668}
669
Ash Wilson664fe332014-10-21 17:47:49 -0400670// HandleServerDeletionSuccessfully sets up the test server to respond to a server deletion request.
671func HandleServerDeletionSuccessfully(t *testing.T) {
672 th.Mux.HandleFunc("/servers/asdfasdfasdf", func(w http.ResponseWriter, r *http.Request) {
673 th.TestMethod(t, r, "DELETE")
674 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
675
676 w.WriteHeader(http.StatusNoContent)
677 })
678}
Ash Wilson1c1eb882014-10-21 18:14:31 -0400679
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100680// HandleServerForceDeletionSuccessfully sets up the test server to respond to a server force deletion
681// request.
Ian Duffy370c4302016-01-21 10:44:56 +0000682func HandleServerForceDeletionSuccessfully(t *testing.T) {
683 th.Mux.HandleFunc("/servers/asdfasdfasdf/action", func(w http.ResponseWriter, r *http.Request) {
684 th.TestMethod(t, r, "POST")
685 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
686 th.TestJSONRequest(t, r, `{ "forceDelete": "" }`)
687
688 w.WriteHeader(http.StatusAccepted)
689 })
690}
691
Ash Wilson189c95c2014-10-23 11:41:35 -0400692// HandleServerGetSuccessfully sets up the test server to respond to a server Get request.
693func HandleServerGetSuccessfully(t *testing.T) {
694 th.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) {
695 th.TestMethod(t, r, "GET")
696 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
697 th.TestHeader(t, r, "Accept", "application/json")
698
699 fmt.Fprintf(w, SingleServerBody)
700 })
701}
702
703// HandleServerUpdateSuccessfully sets up the test server to respond to a server Update request.
704func HandleServerUpdateSuccessfully(t *testing.T) {
705 th.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) {
706 th.TestMethod(t, r, "PUT")
707 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
708 th.TestHeader(t, r, "Accept", "application/json")
709 th.TestHeader(t, r, "Content-Type", "application/json")
710 th.TestJSONRequest(t, r, `{ "server": { "name": "new-name" } }`)
711
712 fmt.Fprintf(w, SingleServerBody)
713 })
714}
715
Ash Wilson1c1eb882014-10-21 18:14:31 -0400716// HandleAdminPasswordChangeSuccessfully sets up the test server to respond to a server password
717// change request.
718func HandleAdminPasswordChangeSuccessfully(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, `{ "changePassword": { "adminPass": "new-password" } }`)
723
724 w.WriteHeader(http.StatusAccepted)
725 })
726}
Ash Wilson2295ba52014-10-21 18:19:28 -0400727
728// HandleRebootSuccessfully sets up the test server to respond to a reboot request with success.
729func HandleRebootSuccessfully(t *testing.T) {
730 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
731 th.TestMethod(t, r, "POST")
732 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
733 th.TestJSONRequest(t, r, `{ "reboot": { "type": "SOFT" } }`)
734
735 w.WriteHeader(http.StatusAccepted)
736 })
737}
Ash Wilsonacf49c62014-10-21 18:25:11 -0400738
739// HandleRebuildSuccessfully sets up the test server to respond to a rebuild request with success.
740func HandleRebuildSuccessfully(t *testing.T, response string) {
741 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
742 th.TestMethod(t, r, "POST")
743 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
744 th.TestJSONRequest(t, r, `
745 {
746 "rebuild": {
747 "name": "new-name",
748 "adminPass": "swordfish",
749 "imageRef": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
750 "accessIPv4": "1.2.3.4"
751 }
752 }
753 `)
754
755 w.WriteHeader(http.StatusAccepted)
756 w.Header().Add("Content-Type", "application/json")
757 fmt.Fprintf(w, response)
758 })
759}
Alex Gaynorfa8fbeb2014-11-13 13:39:31 -0800760
Jon Perrittcc77da62014-11-16 13:14:21 -0700761// HandleServerRescueSuccessfully sets up the test server to respond to a server Rescue request.
Alex Gaynorfa8fbeb2014-11-13 13:39:31 -0800762func HandleServerRescueSuccessfully(t *testing.T) {
763 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
764 th.TestMethod(t, r, "POST")
765 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
766 th.TestJSONRequest(t, r, `{ "rescue": { "adminPass": "1234567890" } }`)
767
768 w.WriteHeader(http.StatusOK)
769 w.Write([]byte(`{ "adminPass": "1234567890" }`))
770 })
771}
Jon Perrittcc77da62014-11-16 13:14:21 -0700772
Jon Perritt78c57ce2014-11-20 11:07:18 -0700773// HandleMetadatumGetSuccessfully sets up the test server to respond to a metadatum Get request.
774func HandleMetadatumGetSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700775 th.Mux.HandleFunc("/servers/1234asdf/metadata/foo", func(w http.ResponseWriter, r *http.Request) {
776 th.TestMethod(t, r, "GET")
777 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
778 th.TestHeader(t, r, "Accept", "application/json")
779
780 w.WriteHeader(http.StatusOK)
781 w.Header().Add("Content-Type", "application/json")
782 w.Write([]byte(`{ "meta": {"foo":"bar"}}`))
783 })
784}
785
Jon Perritt78c57ce2014-11-20 11:07:18 -0700786// HandleMetadatumCreateSuccessfully sets up the test server to respond to a metadatum Create request.
787func HandleMetadatumCreateSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700788 th.Mux.HandleFunc("/servers/1234asdf/metadata/foo", func(w http.ResponseWriter, r *http.Request) {
789 th.TestMethod(t, r, "PUT")
790 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
791 th.TestJSONRequest(t, r, `{
792 "meta": {
793 "foo": "bar"
794 }
795 }`)
796
797 w.WriteHeader(http.StatusOK)
798 w.Header().Add("Content-Type", "application/json")
799 w.Write([]byte(`{ "meta": {"foo":"bar"}}`))
800 })
801}
802
Jon Perritt78c57ce2014-11-20 11:07:18 -0700803// HandleMetadatumDeleteSuccessfully sets up the test server to respond to a metadatum Delete request.
804func HandleMetadatumDeleteSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700805 th.Mux.HandleFunc("/servers/1234asdf/metadata/foo", func(w http.ResponseWriter, r *http.Request) {
806 th.TestMethod(t, r, "DELETE")
807 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
808
809 w.WriteHeader(http.StatusNoContent)
810 })
811}
812
Jon Perritt78c57ce2014-11-20 11:07:18 -0700813// HandleMetadataGetSuccessfully sets up the test server to respond to a metadata Get request.
814func HandleMetadataGetSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700815 th.Mux.HandleFunc("/servers/1234asdf/metadata", func(w http.ResponseWriter, r *http.Request) {
816 th.TestMethod(t, r, "GET")
817 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
818 th.TestHeader(t, r, "Accept", "application/json")
819
820 w.WriteHeader(http.StatusOK)
821 w.Write([]byte(`{ "metadata": {"foo":"bar", "this":"that"}}`))
822 })
823}
824
Jon Perritt789f8322014-11-21 08:20:04 -0700825// HandleMetadataResetSuccessfully sets up the test server to respond to a metadata Create request.
826func HandleMetadataResetSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700827 th.Mux.HandleFunc("/servers/1234asdf/metadata", func(w http.ResponseWriter, r *http.Request) {
828 th.TestMethod(t, r, "PUT")
829 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
830 th.TestJSONRequest(t, r, `{
831 "metadata": {
832 "foo": "bar",
833 "this": "that"
834 }
835 }`)
836
837 w.WriteHeader(http.StatusOK)
838 w.Header().Add("Content-Type", "application/json")
839 w.Write([]byte(`{ "metadata": {"foo":"bar", "this":"that"}}`))
840 })
841}
842
Jon Perritt78c57ce2014-11-20 11:07:18 -0700843// HandleMetadataUpdateSuccessfully sets up the test server to respond to a metadata Update request.
844func HandleMetadataUpdateSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700845 th.Mux.HandleFunc("/servers/1234asdf/metadata", func(w http.ResponseWriter, r *http.Request) {
846 th.TestMethod(t, r, "POST")
847 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
848 th.TestJSONRequest(t, r, `{
849 "metadata": {
850 "foo": "baz",
851 "this": "those"
852 }
853 }`)
854
855 w.WriteHeader(http.StatusOK)
856 w.Header().Add("Content-Type", "application/json")
857 w.Write([]byte(`{ "metadata": {"foo":"baz", "this":"those"}}`))
858 })
859}
Jon Perritt5cb49482015-02-19 12:19:58 -0700860
861// ListAddressesExpected represents an expected repsonse from a ListAddresses request.
jrperritt3d966162016-06-06 14:08:54 -0500862var ListAddressesExpected = map[string][]servers.Address{
863 "public": []servers.Address{
864 {
Jon Perritt5cb49482015-02-19 12:19:58 -0700865 Version: 4,
866 Address: "80.56.136.39",
867 },
jrperritt3d966162016-06-06 14:08:54 -0500868 {
Jon Perritt5cb49482015-02-19 12:19:58 -0700869 Version: 6,
870 Address: "2001:4800:790e:510:be76:4eff:fe04:82a8",
871 },
872 },
jrperritt3d966162016-06-06 14:08:54 -0500873 "private": []servers.Address{
874 {
Jon Perritt5cb49482015-02-19 12:19:58 -0700875 Version: 4,
876 Address: "10.880.3.154",
877 },
878 },
879}
880
881// HandleAddressListSuccessfully sets up the test server to respond to a ListAddresses request.
882func HandleAddressListSuccessfully(t *testing.T) {
883 th.Mux.HandleFunc("/servers/asdfasdfasdf/ips", func(w http.ResponseWriter, r *http.Request) {
884 th.TestMethod(t, r, "GET")
885 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
886
887 w.Header().Add("Content-Type", "application/json")
888 fmt.Fprintf(w, `{
889 "addresses": {
890 "public": [
891 {
892 "version": 4,
893 "addr": "50.56.176.35"
894 },
895 {
896 "version": 6,
897 "addr": "2001:4800:780e:510:be76:4eff:fe04:84a8"
898 }
899 ],
900 "private": [
901 {
902 "version": 4,
903 "addr": "10.180.3.155"
904 }
905 ]
906 }
907 }`)
908 })
909}
Jon Perritt04d073c2015-02-19 21:46:23 -0700910
911// ListNetworkAddressesExpected represents an expected repsonse from a ListAddressesByNetwork request.
jrperritt3d966162016-06-06 14:08:54 -0500912var ListNetworkAddressesExpected = []servers.Address{
913 {
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700914 Version: 4,
915 Address: "50.56.176.35",
916 },
jrperritt3d966162016-06-06 14:08:54 -0500917 {
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700918 Version: 6,
919 Address: "2001:4800:780e:510:be76:4eff:fe04:84a8",
Jon Perritt04d073c2015-02-19 21:46:23 -0700920 },
921}
922
923// HandleNetworkAddressListSuccessfully sets up the test server to respond to a ListAddressesByNetwork request.
924func HandleNetworkAddressListSuccessfully(t *testing.T) {
925 th.Mux.HandleFunc("/servers/asdfasdfasdf/ips/public", func(w http.ResponseWriter, r *http.Request) {
926 th.TestMethod(t, r, "GET")
927 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
928
929 w.Header().Add("Content-Type", "application/json")
930 fmt.Fprintf(w, `{
931 "public": [
932 {
933 "version": 4,
934 "addr": "50.56.176.35"
935 },
936 {
937 "version": 6,
938 "addr": "2001:4800:780e:510:be76:4eff:fe04:84a8"
939 }
940 ]
941 }`)
942 })
943}
einarf2fc665e2015-04-16 20:16:21 +0000944
945// HandleCreateServerImageSuccessfully sets up the test server to respond to a TestCreateServerImage request.
946func HandleCreateServerImageSuccessfully(t *testing.T) {
947 th.Mux.HandleFunc("/servers/serverimage/action", func(w http.ResponseWriter, r *http.Request) {
948 th.TestMethod(t, r, "POST")
949 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
950 w.Header().Add("Location", "https://0.0.0.0/images/xxxx-xxxxx-xxxxx-xxxx")
951 w.WriteHeader(http.StatusAccepted)
952 })
953}
954
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100955// HandlePasswordGetSuccessfully sets up the test server to respond to a password Get request.
956func HandlePasswordGetSuccessfully(t *testing.T) {
957 th.Mux.HandleFunc("/servers/1234asdf/os-server-password", func(w http.ResponseWriter, r *http.Request) {
958 th.TestMethod(t, r, "GET")
959 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
960 th.TestHeader(t, r, "Accept", "application/json")
961
962 fmt.Fprintf(w, ServerPasswordBody)
963 })
964}