blob: 613dc1ed4860f5978b553442fd0aadd17d4ce3dc [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 })
495}
496
Jon Perrittdb0ae142016-03-13 00:33:41 -0600497// HandleServerCreationWithCustomFieldSuccessfully sets up the test server to respond to a server creation request
498// with a given response.
499func HandleServerCreationWithCustomFieldSuccessfully(t *testing.T, response string) {
500 th.Mux.HandleFunc("/servers", func(w http.ResponseWriter, r *http.Request) {
501 th.TestMethod(t, r, "POST")
502 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
503 th.TestJSONRequest(t, r, `{
504 "server": {
505 "name": "derp",
506 "imageRef": "f90f6034-2570-4974-8351-6b49732ef2eb",
507 "flavorRef": "1",
508 "foo": "bar"
509 }
510 }`)
511
512 w.WriteHeader(http.StatusAccepted)
513 w.Header().Add("Content-Type", "application/json")
514 fmt.Fprintf(w, response)
515 })
516}
517
Ash Wilsona70510a2014-10-23 11:54:03 -0400518// HandleServerListSuccessfully sets up the test server to respond to a server List request.
519func HandleServerListSuccessfully(t *testing.T) {
520 th.Mux.HandleFunc("/servers/detail", func(w http.ResponseWriter, r *http.Request) {
521 th.TestMethod(t, r, "GET")
522 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
523
524 w.Header().Add("Content-Type", "application/json")
525 r.ParseForm()
526 marker := r.Form.Get("marker")
527 switch marker {
528 case "":
529 fmt.Fprintf(w, ServerListBody)
530 case "9e5476bd-a4ec-4653-93d6-72c93aa682ba":
531 fmt.Fprintf(w, `{ "servers": [] }`)
532 default:
533 t.Fatalf("/servers/detail invoked with unexpected marker=[%s]", marker)
534 }
535 })
536}
537
Ash Wilson664fe332014-10-21 17:47:49 -0400538// HandleServerDeletionSuccessfully sets up the test server to respond to a server deletion request.
539func HandleServerDeletionSuccessfully(t *testing.T) {
540 th.Mux.HandleFunc("/servers/asdfasdfasdf", func(w http.ResponseWriter, r *http.Request) {
541 th.TestMethod(t, r, "DELETE")
542 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
543
544 w.WriteHeader(http.StatusNoContent)
545 })
546}
Ash Wilson1c1eb882014-10-21 18:14:31 -0400547
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100548// HandleServerForceDeletionSuccessfully sets up the test server to respond to a server force deletion
549// request.
Ian Duffy370c4302016-01-21 10:44:56 +0000550func HandleServerForceDeletionSuccessfully(t *testing.T) {
551 th.Mux.HandleFunc("/servers/asdfasdfasdf/action", func(w http.ResponseWriter, r *http.Request) {
552 th.TestMethod(t, r, "POST")
553 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
554 th.TestJSONRequest(t, r, `{ "forceDelete": "" }`)
555
556 w.WriteHeader(http.StatusAccepted)
557 })
558}
559
Ash Wilson189c95c2014-10-23 11:41:35 -0400560// HandleServerGetSuccessfully sets up the test server to respond to a server Get request.
561func HandleServerGetSuccessfully(t *testing.T) {
562 th.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) {
563 th.TestMethod(t, r, "GET")
564 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
565 th.TestHeader(t, r, "Accept", "application/json")
566
567 fmt.Fprintf(w, SingleServerBody)
568 })
569}
570
571// HandleServerUpdateSuccessfully sets up the test server to respond to a server Update request.
572func HandleServerUpdateSuccessfully(t *testing.T) {
573 th.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) {
574 th.TestMethod(t, r, "PUT")
575 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
576 th.TestHeader(t, r, "Accept", "application/json")
577 th.TestHeader(t, r, "Content-Type", "application/json")
578 th.TestJSONRequest(t, r, `{ "server": { "name": "new-name" } }`)
579
580 fmt.Fprintf(w, SingleServerBody)
581 })
582}
583
Ash Wilson1c1eb882014-10-21 18:14:31 -0400584// HandleAdminPasswordChangeSuccessfully sets up the test server to respond to a server password
585// change request.
586func HandleAdminPasswordChangeSuccessfully(t *testing.T) {
587 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
588 th.TestMethod(t, r, "POST")
589 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
590 th.TestJSONRequest(t, r, `{ "changePassword": { "adminPass": "new-password" } }`)
591
592 w.WriteHeader(http.StatusAccepted)
593 })
594}
Ash Wilson2295ba52014-10-21 18:19:28 -0400595
596// HandleRebootSuccessfully sets up the test server to respond to a reboot request with success.
597func HandleRebootSuccessfully(t *testing.T) {
598 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
599 th.TestMethod(t, r, "POST")
600 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
601 th.TestJSONRequest(t, r, `{ "reboot": { "type": "SOFT" } }`)
602
603 w.WriteHeader(http.StatusAccepted)
604 })
605}
Ash Wilsonacf49c62014-10-21 18:25:11 -0400606
607// HandleRebuildSuccessfully sets up the test server to respond to a rebuild request with success.
608func HandleRebuildSuccessfully(t *testing.T, response string) {
609 th.Mux.HandleFunc("/servers/1234asdf/action", 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 {
614 "rebuild": {
615 "name": "new-name",
616 "adminPass": "swordfish",
617 "imageRef": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
618 "accessIPv4": "1.2.3.4"
619 }
620 }
621 `)
622
623 w.WriteHeader(http.StatusAccepted)
624 w.Header().Add("Content-Type", "application/json")
625 fmt.Fprintf(w, response)
626 })
627}
Alex Gaynorfa8fbeb2014-11-13 13:39:31 -0800628
Jon Perrittcc77da62014-11-16 13:14:21 -0700629// HandleServerRescueSuccessfully sets up the test server to respond to a server Rescue request.
Alex Gaynorfa8fbeb2014-11-13 13:39:31 -0800630func HandleServerRescueSuccessfully(t *testing.T) {
631 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
632 th.TestMethod(t, r, "POST")
633 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
634 th.TestJSONRequest(t, r, `{ "rescue": { "adminPass": "1234567890" } }`)
635
636 w.WriteHeader(http.StatusOK)
637 w.Write([]byte(`{ "adminPass": "1234567890" }`))
638 })
639}
Jon Perrittcc77da62014-11-16 13:14:21 -0700640
Jon Perritt78c57ce2014-11-20 11:07:18 -0700641// HandleMetadatumGetSuccessfully sets up the test server to respond to a metadatum Get request.
642func HandleMetadatumGetSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700643 th.Mux.HandleFunc("/servers/1234asdf/metadata/foo", func(w http.ResponseWriter, r *http.Request) {
644 th.TestMethod(t, r, "GET")
645 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
646 th.TestHeader(t, r, "Accept", "application/json")
647
648 w.WriteHeader(http.StatusOK)
649 w.Header().Add("Content-Type", "application/json")
650 w.Write([]byte(`{ "meta": {"foo":"bar"}}`))
651 })
652}
653
Jon Perritt78c57ce2014-11-20 11:07:18 -0700654// HandleMetadatumCreateSuccessfully sets up the test server to respond to a metadatum Create request.
655func HandleMetadatumCreateSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700656 th.Mux.HandleFunc("/servers/1234asdf/metadata/foo", func(w http.ResponseWriter, r *http.Request) {
657 th.TestMethod(t, r, "PUT")
658 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
659 th.TestJSONRequest(t, r, `{
660 "meta": {
661 "foo": "bar"
662 }
663 }`)
664
665 w.WriteHeader(http.StatusOK)
666 w.Header().Add("Content-Type", "application/json")
667 w.Write([]byte(`{ "meta": {"foo":"bar"}}`))
668 })
669}
670
Jon Perritt78c57ce2014-11-20 11:07:18 -0700671// HandleMetadatumDeleteSuccessfully sets up the test server to respond to a metadatum Delete request.
672func HandleMetadatumDeleteSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700673 th.Mux.HandleFunc("/servers/1234asdf/metadata/foo", func(w http.ResponseWriter, r *http.Request) {
674 th.TestMethod(t, r, "DELETE")
675 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
676
677 w.WriteHeader(http.StatusNoContent)
678 })
679}
680
Jon Perritt78c57ce2014-11-20 11:07:18 -0700681// HandleMetadataGetSuccessfully sets up the test server to respond to a metadata Get request.
682func HandleMetadataGetSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700683 th.Mux.HandleFunc("/servers/1234asdf/metadata", func(w http.ResponseWriter, r *http.Request) {
684 th.TestMethod(t, r, "GET")
685 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
686 th.TestHeader(t, r, "Accept", "application/json")
687
688 w.WriteHeader(http.StatusOK)
689 w.Write([]byte(`{ "metadata": {"foo":"bar", "this":"that"}}`))
690 })
691}
692
Jon Perritt789f8322014-11-21 08:20:04 -0700693// HandleMetadataResetSuccessfully sets up the test server to respond to a metadata Create request.
694func HandleMetadataResetSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700695 th.Mux.HandleFunc("/servers/1234asdf/metadata", func(w http.ResponseWriter, r *http.Request) {
696 th.TestMethod(t, r, "PUT")
697 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
698 th.TestJSONRequest(t, r, `{
699 "metadata": {
700 "foo": "bar",
701 "this": "that"
702 }
703 }`)
704
705 w.WriteHeader(http.StatusOK)
706 w.Header().Add("Content-Type", "application/json")
707 w.Write([]byte(`{ "metadata": {"foo":"bar", "this":"that"}}`))
708 })
709}
710
Jon Perritt78c57ce2014-11-20 11:07:18 -0700711// HandleMetadataUpdateSuccessfully sets up the test server to respond to a metadata Update request.
712func HandleMetadataUpdateSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700713 th.Mux.HandleFunc("/servers/1234asdf/metadata", func(w http.ResponseWriter, r *http.Request) {
714 th.TestMethod(t, r, "POST")
715 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
716 th.TestJSONRequest(t, r, `{
717 "metadata": {
718 "foo": "baz",
719 "this": "those"
720 }
721 }`)
722
723 w.WriteHeader(http.StatusOK)
724 w.Header().Add("Content-Type", "application/json")
725 w.Write([]byte(`{ "metadata": {"foo":"baz", "this":"those"}}`))
726 })
727}
Jon Perritt5cb49482015-02-19 12:19:58 -0700728
729// ListAddressesExpected represents an expected repsonse from a ListAddresses request.
jrperritt3d966162016-06-06 14:08:54 -0500730var ListAddressesExpected = map[string][]servers.Address{
731 "public": []servers.Address{
732 {
Jon Perritt5cb49482015-02-19 12:19:58 -0700733 Version: 4,
734 Address: "80.56.136.39",
735 },
jrperritt3d966162016-06-06 14:08:54 -0500736 {
Jon Perritt5cb49482015-02-19 12:19:58 -0700737 Version: 6,
738 Address: "2001:4800:790e:510:be76:4eff:fe04:82a8",
739 },
740 },
jrperritt3d966162016-06-06 14:08:54 -0500741 "private": []servers.Address{
742 {
Jon Perritt5cb49482015-02-19 12:19:58 -0700743 Version: 4,
744 Address: "10.880.3.154",
745 },
746 },
747}
748
749// HandleAddressListSuccessfully sets up the test server to respond to a ListAddresses request.
750func HandleAddressListSuccessfully(t *testing.T) {
751 th.Mux.HandleFunc("/servers/asdfasdfasdf/ips", func(w http.ResponseWriter, r *http.Request) {
752 th.TestMethod(t, r, "GET")
753 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
754
755 w.Header().Add("Content-Type", "application/json")
756 fmt.Fprintf(w, `{
757 "addresses": {
758 "public": [
759 {
760 "version": 4,
761 "addr": "50.56.176.35"
762 },
763 {
764 "version": 6,
765 "addr": "2001:4800:780e:510:be76:4eff:fe04:84a8"
766 }
767 ],
768 "private": [
769 {
770 "version": 4,
771 "addr": "10.180.3.155"
772 }
773 ]
774 }
775 }`)
776 })
777}
Jon Perritt04d073c2015-02-19 21:46:23 -0700778
779// ListNetworkAddressesExpected represents an expected repsonse from a ListAddressesByNetwork request.
jrperritt3d966162016-06-06 14:08:54 -0500780var ListNetworkAddressesExpected = []servers.Address{
781 {
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700782 Version: 4,
783 Address: "50.56.176.35",
784 },
jrperritt3d966162016-06-06 14:08:54 -0500785 {
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700786 Version: 6,
787 Address: "2001:4800:780e:510:be76:4eff:fe04:84a8",
Jon Perritt04d073c2015-02-19 21:46:23 -0700788 },
789}
790
791// HandleNetworkAddressListSuccessfully sets up the test server to respond to a ListAddressesByNetwork request.
792func HandleNetworkAddressListSuccessfully(t *testing.T) {
793 th.Mux.HandleFunc("/servers/asdfasdfasdf/ips/public", func(w http.ResponseWriter, r *http.Request) {
794 th.TestMethod(t, r, "GET")
795 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
796
797 w.Header().Add("Content-Type", "application/json")
798 fmt.Fprintf(w, `{
799 "public": [
800 {
801 "version": 4,
802 "addr": "50.56.176.35"
803 },
804 {
805 "version": 6,
806 "addr": "2001:4800:780e:510:be76:4eff:fe04:84a8"
807 }
808 ]
809 }`)
810 })
811}
einarf2fc665e2015-04-16 20:16:21 +0000812
813// HandleCreateServerImageSuccessfully sets up the test server to respond to a TestCreateServerImage request.
814func HandleCreateServerImageSuccessfully(t *testing.T) {
815 th.Mux.HandleFunc("/servers/serverimage/action", func(w http.ResponseWriter, r *http.Request) {
816 th.TestMethod(t, r, "POST")
817 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
818 w.Header().Add("Location", "https://0.0.0.0/images/xxxx-xxxxx-xxxxx-xxxx")
819 w.WriteHeader(http.StatusAccepted)
820 })
821}
822
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100823// HandlePasswordGetSuccessfully sets up the test server to respond to a password Get request.
824func HandlePasswordGetSuccessfully(t *testing.T) {
825 th.Mux.HandleFunc("/servers/1234asdf/os-server-password", func(w http.ResponseWriter, r *http.Request) {
826 th.TestMethod(t, r, "GET")
827 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
828 th.TestHeader(t, r, "Accept", "application/json")
829
830 fmt.Fprintf(w, ServerPasswordBody)
831 })
832}