blob: bb25643d673b91eae8ed743ace78aff2dfc1e351 [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
Joe Topjianf464c962016-09-12 08:02:43 -0600606// HandleServerCreationWithMetadata sets up the test server to respond to a server creation request
607// with a given response.
608func HandleServerCreationWithMetadata(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 "metadata": {
618 "abc": "def"
619 }
620 }
621 }`)
622
623 w.WriteHeader(http.StatusAccepted)
624 w.Header().Add("Content-Type", "application/json")
625 fmt.Fprintf(w, response)
626 })
627}
628
Ash Wilsona70510a2014-10-23 11:54:03 -0400629// HandleServerListSuccessfully sets up the test server to respond to a server List request.
630func HandleServerListSuccessfully(t *testing.T) {
631 th.Mux.HandleFunc("/servers/detail", func(w http.ResponseWriter, r *http.Request) {
632 th.TestMethod(t, r, "GET")
633 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
634
635 w.Header().Add("Content-Type", "application/json")
636 r.ParseForm()
637 marker := r.Form.Get("marker")
638 switch marker {
639 case "":
640 fmt.Fprintf(w, ServerListBody)
641 case "9e5476bd-a4ec-4653-93d6-72c93aa682ba":
642 fmt.Fprintf(w, `{ "servers": [] }`)
643 default:
644 t.Fatalf("/servers/detail invoked with unexpected marker=[%s]", marker)
645 }
646 })
647}
648
Ash Wilson664fe332014-10-21 17:47:49 -0400649// HandleServerDeletionSuccessfully sets up the test server to respond to a server deletion request.
650func HandleServerDeletionSuccessfully(t *testing.T) {
651 th.Mux.HandleFunc("/servers/asdfasdfasdf", func(w http.ResponseWriter, r *http.Request) {
652 th.TestMethod(t, r, "DELETE")
653 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
654
655 w.WriteHeader(http.StatusNoContent)
656 })
657}
Ash Wilson1c1eb882014-10-21 18:14:31 -0400658
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100659// HandleServerForceDeletionSuccessfully sets up the test server to respond to a server force deletion
660// request.
Ian Duffy370c4302016-01-21 10:44:56 +0000661func HandleServerForceDeletionSuccessfully(t *testing.T) {
662 th.Mux.HandleFunc("/servers/asdfasdfasdf/action", func(w http.ResponseWriter, r *http.Request) {
663 th.TestMethod(t, r, "POST")
664 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
665 th.TestJSONRequest(t, r, `{ "forceDelete": "" }`)
666
667 w.WriteHeader(http.StatusAccepted)
668 })
669}
670
Ash Wilson189c95c2014-10-23 11:41:35 -0400671// HandleServerGetSuccessfully sets up the test server to respond to a server Get request.
672func HandleServerGetSuccessfully(t *testing.T) {
673 th.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) {
674 th.TestMethod(t, r, "GET")
675 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
676 th.TestHeader(t, r, "Accept", "application/json")
677
678 fmt.Fprintf(w, SingleServerBody)
679 })
680}
681
682// HandleServerUpdateSuccessfully sets up the test server to respond to a server Update request.
683func HandleServerUpdateSuccessfully(t *testing.T) {
684 th.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) {
685 th.TestMethod(t, r, "PUT")
686 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
687 th.TestHeader(t, r, "Accept", "application/json")
688 th.TestHeader(t, r, "Content-Type", "application/json")
689 th.TestJSONRequest(t, r, `{ "server": { "name": "new-name" } }`)
690
691 fmt.Fprintf(w, SingleServerBody)
692 })
693}
694
Ash Wilson1c1eb882014-10-21 18:14:31 -0400695// HandleAdminPasswordChangeSuccessfully sets up the test server to respond to a server password
696// change request.
697func HandleAdminPasswordChangeSuccessfully(t *testing.T) {
698 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
699 th.TestMethod(t, r, "POST")
700 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
701 th.TestJSONRequest(t, r, `{ "changePassword": { "adminPass": "new-password" } }`)
702
703 w.WriteHeader(http.StatusAccepted)
704 })
705}
Ash Wilson2295ba52014-10-21 18:19:28 -0400706
707// HandleRebootSuccessfully sets up the test server to respond to a reboot request with success.
708func HandleRebootSuccessfully(t *testing.T) {
709 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
710 th.TestMethod(t, r, "POST")
711 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
712 th.TestJSONRequest(t, r, `{ "reboot": { "type": "SOFT" } }`)
713
714 w.WriteHeader(http.StatusAccepted)
715 })
716}
Ash Wilsonacf49c62014-10-21 18:25:11 -0400717
718// HandleRebuildSuccessfully sets up the test server to respond to a rebuild request with success.
719func HandleRebuildSuccessfully(t *testing.T, response string) {
720 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
721 th.TestMethod(t, r, "POST")
722 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
723 th.TestJSONRequest(t, r, `
724 {
725 "rebuild": {
726 "name": "new-name",
727 "adminPass": "swordfish",
728 "imageRef": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
729 "accessIPv4": "1.2.3.4"
730 }
731 }
732 `)
733
734 w.WriteHeader(http.StatusAccepted)
735 w.Header().Add("Content-Type", "application/json")
736 fmt.Fprintf(w, response)
737 })
738}
Alex Gaynorfa8fbeb2014-11-13 13:39:31 -0800739
Jon Perrittcc77da62014-11-16 13:14:21 -0700740// HandleServerRescueSuccessfully sets up the test server to respond to a server Rescue request.
Alex Gaynorfa8fbeb2014-11-13 13:39:31 -0800741func HandleServerRescueSuccessfully(t *testing.T) {
742 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
743 th.TestMethod(t, r, "POST")
744 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
745 th.TestJSONRequest(t, r, `{ "rescue": { "adminPass": "1234567890" } }`)
746
747 w.WriteHeader(http.StatusOK)
748 w.Write([]byte(`{ "adminPass": "1234567890" }`))
749 })
750}
Jon Perrittcc77da62014-11-16 13:14:21 -0700751
Jon Perritt78c57ce2014-11-20 11:07:18 -0700752// HandleMetadatumGetSuccessfully sets up the test server to respond to a metadatum Get request.
753func HandleMetadatumGetSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700754 th.Mux.HandleFunc("/servers/1234asdf/metadata/foo", func(w http.ResponseWriter, r *http.Request) {
755 th.TestMethod(t, r, "GET")
756 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
757 th.TestHeader(t, r, "Accept", "application/json")
758
759 w.WriteHeader(http.StatusOK)
760 w.Header().Add("Content-Type", "application/json")
761 w.Write([]byte(`{ "meta": {"foo":"bar"}}`))
762 })
763}
764
Jon Perritt78c57ce2014-11-20 11:07:18 -0700765// HandleMetadatumCreateSuccessfully sets up the test server to respond to a metadatum Create request.
766func HandleMetadatumCreateSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700767 th.Mux.HandleFunc("/servers/1234asdf/metadata/foo", func(w http.ResponseWriter, r *http.Request) {
768 th.TestMethod(t, r, "PUT")
769 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
770 th.TestJSONRequest(t, r, `{
771 "meta": {
772 "foo": "bar"
773 }
774 }`)
775
776 w.WriteHeader(http.StatusOK)
777 w.Header().Add("Content-Type", "application/json")
778 w.Write([]byte(`{ "meta": {"foo":"bar"}}`))
779 })
780}
781
Jon Perritt78c57ce2014-11-20 11:07:18 -0700782// HandleMetadatumDeleteSuccessfully sets up the test server to respond to a metadatum Delete request.
783func HandleMetadatumDeleteSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700784 th.Mux.HandleFunc("/servers/1234asdf/metadata/foo", func(w http.ResponseWriter, r *http.Request) {
785 th.TestMethod(t, r, "DELETE")
786 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
787
788 w.WriteHeader(http.StatusNoContent)
789 })
790}
791
Jon Perritt78c57ce2014-11-20 11:07:18 -0700792// HandleMetadataGetSuccessfully sets up the test server to respond to a metadata Get request.
793func HandleMetadataGetSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700794 th.Mux.HandleFunc("/servers/1234asdf/metadata", func(w http.ResponseWriter, r *http.Request) {
795 th.TestMethod(t, r, "GET")
796 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
797 th.TestHeader(t, r, "Accept", "application/json")
798
799 w.WriteHeader(http.StatusOK)
800 w.Write([]byte(`{ "metadata": {"foo":"bar", "this":"that"}}`))
801 })
802}
803
Jon Perritt789f8322014-11-21 08:20:04 -0700804// HandleMetadataResetSuccessfully sets up the test server to respond to a metadata Create request.
805func HandleMetadataResetSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700806 th.Mux.HandleFunc("/servers/1234asdf/metadata", func(w http.ResponseWriter, r *http.Request) {
807 th.TestMethod(t, r, "PUT")
808 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
809 th.TestJSONRequest(t, r, `{
810 "metadata": {
811 "foo": "bar",
812 "this": "that"
813 }
814 }`)
815
816 w.WriteHeader(http.StatusOK)
817 w.Header().Add("Content-Type", "application/json")
818 w.Write([]byte(`{ "metadata": {"foo":"bar", "this":"that"}}`))
819 })
820}
821
Jon Perritt78c57ce2014-11-20 11:07:18 -0700822// HandleMetadataUpdateSuccessfully sets up the test server to respond to a metadata Update request.
823func HandleMetadataUpdateSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700824 th.Mux.HandleFunc("/servers/1234asdf/metadata", func(w http.ResponseWriter, r *http.Request) {
825 th.TestMethod(t, r, "POST")
826 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
827 th.TestJSONRequest(t, r, `{
828 "metadata": {
829 "foo": "baz",
830 "this": "those"
831 }
832 }`)
833
834 w.WriteHeader(http.StatusOK)
835 w.Header().Add("Content-Type", "application/json")
836 w.Write([]byte(`{ "metadata": {"foo":"baz", "this":"those"}}`))
837 })
838}
Jon Perritt5cb49482015-02-19 12:19:58 -0700839
840// ListAddressesExpected represents an expected repsonse from a ListAddresses request.
jrperritt3d966162016-06-06 14:08:54 -0500841var ListAddressesExpected = map[string][]servers.Address{
842 "public": []servers.Address{
843 {
Jon Perritt5cb49482015-02-19 12:19:58 -0700844 Version: 4,
845 Address: "80.56.136.39",
846 },
jrperritt3d966162016-06-06 14:08:54 -0500847 {
Jon Perritt5cb49482015-02-19 12:19:58 -0700848 Version: 6,
849 Address: "2001:4800:790e:510:be76:4eff:fe04:82a8",
850 },
851 },
jrperritt3d966162016-06-06 14:08:54 -0500852 "private": []servers.Address{
853 {
Jon Perritt5cb49482015-02-19 12:19:58 -0700854 Version: 4,
855 Address: "10.880.3.154",
856 },
857 },
858}
859
860// HandleAddressListSuccessfully sets up the test server to respond to a ListAddresses request.
861func HandleAddressListSuccessfully(t *testing.T) {
862 th.Mux.HandleFunc("/servers/asdfasdfasdf/ips", func(w http.ResponseWriter, r *http.Request) {
863 th.TestMethod(t, r, "GET")
864 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
865
866 w.Header().Add("Content-Type", "application/json")
867 fmt.Fprintf(w, `{
868 "addresses": {
869 "public": [
870 {
871 "version": 4,
872 "addr": "50.56.176.35"
873 },
874 {
875 "version": 6,
876 "addr": "2001:4800:780e:510:be76:4eff:fe04:84a8"
877 }
878 ],
879 "private": [
880 {
881 "version": 4,
882 "addr": "10.180.3.155"
883 }
884 ]
885 }
886 }`)
887 })
888}
Jon Perritt04d073c2015-02-19 21:46:23 -0700889
890// ListNetworkAddressesExpected represents an expected repsonse from a ListAddressesByNetwork request.
jrperritt3d966162016-06-06 14:08:54 -0500891var ListNetworkAddressesExpected = []servers.Address{
892 {
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700893 Version: 4,
894 Address: "50.56.176.35",
895 },
jrperritt3d966162016-06-06 14:08:54 -0500896 {
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700897 Version: 6,
898 Address: "2001:4800:780e:510:be76:4eff:fe04:84a8",
Jon Perritt04d073c2015-02-19 21:46:23 -0700899 },
900}
901
902// HandleNetworkAddressListSuccessfully sets up the test server to respond to a ListAddressesByNetwork request.
903func HandleNetworkAddressListSuccessfully(t *testing.T) {
904 th.Mux.HandleFunc("/servers/asdfasdfasdf/ips/public", func(w http.ResponseWriter, r *http.Request) {
905 th.TestMethod(t, r, "GET")
906 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
907
908 w.Header().Add("Content-Type", "application/json")
909 fmt.Fprintf(w, `{
910 "public": [
911 {
912 "version": 4,
913 "addr": "50.56.176.35"
914 },
915 {
916 "version": 6,
917 "addr": "2001:4800:780e:510:be76:4eff:fe04:84a8"
918 }
919 ]
920 }`)
921 })
922}
einarf2fc665e2015-04-16 20:16:21 +0000923
924// HandleCreateServerImageSuccessfully sets up the test server to respond to a TestCreateServerImage request.
925func HandleCreateServerImageSuccessfully(t *testing.T) {
926 th.Mux.HandleFunc("/servers/serverimage/action", func(w http.ResponseWriter, r *http.Request) {
927 th.TestMethod(t, r, "POST")
928 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
929 w.Header().Add("Location", "https://0.0.0.0/images/xxxx-xxxxx-xxxxx-xxxx")
930 w.WriteHeader(http.StatusAccepted)
931 })
932}
933
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100934// HandlePasswordGetSuccessfully sets up the test server to respond to a password Get request.
935func HandlePasswordGetSuccessfully(t *testing.T) {
936 th.Mux.HandleFunc("/servers/1234asdf/os-server-password", func(w http.ResponseWriter, r *http.Request) {
937 th.TestMethod(t, r, "GET")
938 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
939 th.TestHeader(t, r, "Accept", "application/json")
940
941 fmt.Fprintf(w, ServerPasswordBody)
942 })
943}