blob: b4fb7ff91aad3e947f0ca4a7e4d3b5797ac6ee53 [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": {}
157 }
158 ]
159}
160`
161
162// SingleServerBody is the canned body of a Get request on an existing server.
163const SingleServerBody = `
164{
165 "server": {
166 "status": "ACTIVE",
167 "updated": "2014-09-25T13:04:49Z",
168 "hostId": "29d3c8c896a45aa4c34e52247875d7fefc3d94bbcc9f622b5d204362",
169 "OS-EXT-SRV-ATTR:host": "devstack",
170 "addresses": {
171 "private": [
172 {
173 "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:9e:89:be",
174 "version": 4,
175 "addr": "10.0.0.31",
176 "OS-EXT-IPS:type": "fixed"
177 }
178 ]
179 },
180 "links": [
181 {
182 "href": "http://104.130.131.164:8774/v2/fcad67a6189847c4aecfa3c81a05783b/servers/9e5476bd-a4ec-4653-93d6-72c93aa682ba",
183 "rel": "self"
184 },
185 {
186 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/servers/9e5476bd-a4ec-4653-93d6-72c93aa682ba",
187 "rel": "bookmark"
188 }
189 ],
190 "key_name": null,
191 "image": {
192 "id": "f90f6034-2570-4974-8351-6b49732ef2eb",
193 "links": [
194 {
195 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
196 "rel": "bookmark"
197 }
198 ]
199 },
200 "OS-EXT-STS:task_state": null,
201 "OS-EXT-STS:vm_state": "active",
202 "OS-EXT-SRV-ATTR:instance_name": "instance-0000001d",
203 "OS-SRV-USG:launched_at": "2014-09-25T13:04:49.000000",
204 "OS-EXT-SRV-ATTR:hypervisor_hostname": "devstack",
205 "flavor": {
206 "id": "1",
207 "links": [
208 {
209 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/flavors/1",
210 "rel": "bookmark"
211 }
212 ]
213 },
214 "id": "9e5476bd-a4ec-4653-93d6-72c93aa682ba",
215 "security_groups": [
216 {
217 "name": "default"
218 }
219 ],
220 "OS-SRV-USG:terminated_at": null,
221 "OS-EXT-AZ:availability_zone": "nova",
222 "user_id": "9349aff8be7545ac9d2f1d00999a23cd",
223 "name": "derp",
224 "created": "2014-09-25T13:04:41Z",
225 "tenant_id": "fcad67a6189847c4aecfa3c81a05783b",
226 "OS-DCF:diskConfig": "MANUAL",
227 "os-extended-volumes:volumes_attached": [],
228 "accessIPv4": "",
229 "accessIPv6": "",
230 "progress": 0,
231 "OS-EXT-STS:power_state": 1,
232 "config_drive": "",
233 "metadata": {}
234 }
235}
236`
237
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100238const ServerPasswordBody = `
239{
240 "password": "xlozO3wLCBRWAa2yDjCCVx8vwNPypxnypmRYDa/zErlQ+EzPe1S/Gz6nfmC52mOlOSCRuUOmG7kqqgejPof6M7bOezS387zjq4LSvvwp28zUknzy4YzfFGhnHAdai3TxUJ26pfQCYrq8UTzmKF2Bq8ioSEtVVzM0A96pDh8W2i7BOz6MdoiVyiev/I1K2LsuipfxSJR7Wdke4zNXJjHHP2RfYsVbZ/k9ANu+Nz4iIH8/7Cacud/pphH7EjrY6a4RZNrjQskrhKYed0YERpotyjYk1eDtRe72GrSiXteqCM4biaQ5w3ruS+AcX//PXk3uJ5kC7d67fPXaVz4WaQRYMg=="
241}
242`
243
Ash Wilsone77ffb02014-10-20 13:10:26 -0400244var (
245 // ServerHerp is a Server struct that should correspond to the first result in ServerListBody.
jrperritt3d966162016-06-06 14:08:54 -0500246 ServerHerp = servers.Server{
Ash Wilsone77ffb02014-10-20 13:10:26 -0400247 Status: "ACTIVE",
248 Updated: "2014-09-25T13:10:10Z",
249 HostID: "29d3c8c896a45aa4c34e52247875d7fefc3d94bbcc9f622b5d204362",
250 Addresses: map[string]interface{}{
251 "private": []interface{}{
252 map[string]interface{}{
253 "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:7c:1b:2b",
254 "version": float64(4),
255 "addr": "10.0.0.32",
256 "OS-EXT-IPS:type": "fixed",
257 },
258 },
259 },
260 Links: []interface{}{
261 map[string]interface{}{
262 "href": "http://104.130.131.164:8774/v2/fcad67a6189847c4aecfa3c81a05783b/servers/ef079b0c-e610-4dfb-b1aa-b49f07ac48e5",
263 "rel": "self",
264 },
265 map[string]interface{}{
266 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/servers/ef079b0c-e610-4dfb-b1aa-b49f07ac48e5",
267 "rel": "bookmark",
268 },
269 },
270 Image: map[string]interface{}{
271 "id": "f90f6034-2570-4974-8351-6b49732ef2eb",
272 "links": []interface{}{
273 map[string]interface{}{
274 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
275 "rel": "bookmark",
276 },
277 },
278 },
279 Flavor: map[string]interface{}{
280 "id": "1",
281 "links": []interface{}{
282 map[string]interface{}{
283 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/flavors/1",
284 "rel": "bookmark",
285 },
286 },
287 },
288 ID: "ef079b0c-e610-4dfb-b1aa-b49f07ac48e5",
289 UserID: "9349aff8be7545ac9d2f1d00999a23cd",
290 Name: "herp",
291 Created: "2014-09-25T13:10:02Z",
292 TenantID: "fcad67a6189847c4aecfa3c81a05783b",
293 Metadata: map[string]interface{}{},
Joe Topjian978bb502015-02-12 20:55:31 +0000294 SecurityGroups: []map[string]interface{}{
295 map[string]interface{}{
296 "name": "default",
297 },
298 },
Ash Wilsone77ffb02014-10-20 13:10:26 -0400299 }
300
301 // ServerDerp is a Server struct that should correspond to the second server in ServerListBody.
jrperritt3d966162016-06-06 14:08:54 -0500302 ServerDerp = servers.Server{
Ash Wilsone77ffb02014-10-20 13:10:26 -0400303 Status: "ACTIVE",
304 Updated: "2014-09-25T13:04:49Z",
305 HostID: "29d3c8c896a45aa4c34e52247875d7fefc3d94bbcc9f622b5d204362",
306 Addresses: map[string]interface{}{
307 "private": []interface{}{
308 map[string]interface{}{
309 "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:9e:89:be",
310 "version": float64(4),
311 "addr": "10.0.0.31",
312 "OS-EXT-IPS:type": "fixed",
313 },
314 },
315 },
316 Links: []interface{}{
317 map[string]interface{}{
318 "href": "http://104.130.131.164:8774/v2/fcad67a6189847c4aecfa3c81a05783b/servers/9e5476bd-a4ec-4653-93d6-72c93aa682ba",
319 "rel": "self",
320 },
321 map[string]interface{}{
322 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/servers/9e5476bd-a4ec-4653-93d6-72c93aa682ba",
323 "rel": "bookmark",
324 },
325 },
326 Image: map[string]interface{}{
327 "id": "f90f6034-2570-4974-8351-6b49732ef2eb",
328 "links": []interface{}{
329 map[string]interface{}{
330 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
331 "rel": "bookmark",
332 },
333 },
334 },
335 Flavor: map[string]interface{}{
336 "id": "1",
337 "links": []interface{}{
338 map[string]interface{}{
339 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/flavors/1",
340 "rel": "bookmark",
341 },
342 },
343 },
344 ID: "9e5476bd-a4ec-4653-93d6-72c93aa682ba",
345 UserID: "9349aff8be7545ac9d2f1d00999a23cd",
346 Name: "derp",
347 Created: "2014-09-25T13:04:41Z",
348 TenantID: "fcad67a6189847c4aecfa3c81a05783b",
349 Metadata: map[string]interface{}{},
Joe Topjian978bb502015-02-12 20:55:31 +0000350 SecurityGroups: []map[string]interface{}{
351 map[string]interface{}{
352 "name": "default",
353 },
354 },
Ash Wilsone77ffb02014-10-20 13:10:26 -0400355 }
356)
Ash Wilson664fe332014-10-21 17:47:49 -0400357
Jon Perrittdb0ae142016-03-13 00:33:41 -0600358type CreateOptsWithCustomField struct {
jrperritt3d966162016-06-06 14:08:54 -0500359 servers.CreateOpts
Jon Perrittdb0ae142016-03-13 00:33:41 -0600360 Foo string `json:"foo,omitempty"`
361}
362
363func (opts CreateOptsWithCustomField) ToServerCreateMap() (map[string]interface{}, error) {
364 return gophercloud.BuildRequestBody(opts, "server")
365}
366
Ash Wilson664fe332014-10-21 17:47:49 -0400367// HandleServerCreationSuccessfully sets up the test server to respond to a server creation request
368// with a given response.
369func HandleServerCreationSuccessfully(t *testing.T, response string) {
370 th.Mux.HandleFunc("/servers", func(w http.ResponseWriter, r *http.Request) {
371 th.TestMethod(t, r, "POST")
372 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
373 th.TestJSONRequest(t, r, `{
374 "server": {
375 "name": "derp",
376 "imageRef": "f90f6034-2570-4974-8351-6b49732ef2eb",
377 "flavorRef": "1"
378 }
379 }`)
380
381 w.WriteHeader(http.StatusAccepted)
382 w.Header().Add("Content-Type", "application/json")
383 fmt.Fprintf(w, response)
384 })
385}
386
Jon Perrittdb0ae142016-03-13 00:33:41 -0600387// HandleServerCreationWithCustomFieldSuccessfully sets up the test server to respond to a server creation request
388// with a given response.
389func HandleServerCreationWithCustomFieldSuccessfully(t *testing.T, response string) {
390 th.Mux.HandleFunc("/servers", func(w http.ResponseWriter, r *http.Request) {
391 th.TestMethod(t, r, "POST")
392 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
393 th.TestJSONRequest(t, r, `{
394 "server": {
395 "name": "derp",
396 "imageRef": "f90f6034-2570-4974-8351-6b49732ef2eb",
397 "flavorRef": "1",
398 "foo": "bar"
399 }
400 }`)
401
402 w.WriteHeader(http.StatusAccepted)
403 w.Header().Add("Content-Type", "application/json")
404 fmt.Fprintf(w, response)
405 })
406}
407
Ash Wilsona70510a2014-10-23 11:54:03 -0400408// HandleServerListSuccessfully sets up the test server to respond to a server List request.
409func HandleServerListSuccessfully(t *testing.T) {
410 th.Mux.HandleFunc("/servers/detail", func(w http.ResponseWriter, r *http.Request) {
411 th.TestMethod(t, r, "GET")
412 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
413
414 w.Header().Add("Content-Type", "application/json")
415 r.ParseForm()
416 marker := r.Form.Get("marker")
417 switch marker {
418 case "":
419 fmt.Fprintf(w, ServerListBody)
420 case "9e5476bd-a4ec-4653-93d6-72c93aa682ba":
421 fmt.Fprintf(w, `{ "servers": [] }`)
422 default:
423 t.Fatalf("/servers/detail invoked with unexpected marker=[%s]", marker)
424 }
425 })
426}
427
Ash Wilson664fe332014-10-21 17:47:49 -0400428// HandleServerDeletionSuccessfully sets up the test server to respond to a server deletion request.
429func HandleServerDeletionSuccessfully(t *testing.T) {
430 th.Mux.HandleFunc("/servers/asdfasdfasdf", func(w http.ResponseWriter, r *http.Request) {
431 th.TestMethod(t, r, "DELETE")
432 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
433
434 w.WriteHeader(http.StatusNoContent)
435 })
436}
Ash Wilson1c1eb882014-10-21 18:14:31 -0400437
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100438// HandleServerForceDeletionSuccessfully sets up the test server to respond to a server force deletion
439// request.
Ian Duffy370c4302016-01-21 10:44:56 +0000440func HandleServerForceDeletionSuccessfully(t *testing.T) {
441 th.Mux.HandleFunc("/servers/asdfasdfasdf/action", func(w http.ResponseWriter, r *http.Request) {
442 th.TestMethod(t, r, "POST")
443 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
444 th.TestJSONRequest(t, r, `{ "forceDelete": "" }`)
445
446 w.WriteHeader(http.StatusAccepted)
447 })
448}
449
Ash Wilson189c95c2014-10-23 11:41:35 -0400450// HandleServerGetSuccessfully sets up the test server to respond to a server Get request.
451func HandleServerGetSuccessfully(t *testing.T) {
452 th.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) {
453 th.TestMethod(t, r, "GET")
454 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
455 th.TestHeader(t, r, "Accept", "application/json")
456
457 fmt.Fprintf(w, SingleServerBody)
458 })
459}
460
461// HandleServerUpdateSuccessfully sets up the test server to respond to a server Update request.
462func HandleServerUpdateSuccessfully(t *testing.T) {
463 th.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) {
464 th.TestMethod(t, r, "PUT")
465 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
466 th.TestHeader(t, r, "Accept", "application/json")
467 th.TestHeader(t, r, "Content-Type", "application/json")
468 th.TestJSONRequest(t, r, `{ "server": { "name": "new-name" } }`)
469
470 fmt.Fprintf(w, SingleServerBody)
471 })
472}
473
Ash Wilson1c1eb882014-10-21 18:14:31 -0400474// HandleAdminPasswordChangeSuccessfully sets up the test server to respond to a server password
475// change request.
476func HandleAdminPasswordChangeSuccessfully(t *testing.T) {
477 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
478 th.TestMethod(t, r, "POST")
479 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
480 th.TestJSONRequest(t, r, `{ "changePassword": { "adminPass": "new-password" } }`)
481
482 w.WriteHeader(http.StatusAccepted)
483 })
484}
Ash Wilson2295ba52014-10-21 18:19:28 -0400485
486// HandleRebootSuccessfully sets up the test server to respond to a reboot request with success.
487func HandleRebootSuccessfully(t *testing.T) {
488 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
489 th.TestMethod(t, r, "POST")
490 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
491 th.TestJSONRequest(t, r, `{ "reboot": { "type": "SOFT" } }`)
492
493 w.WriteHeader(http.StatusAccepted)
494 })
495}
Ash Wilsonacf49c62014-10-21 18:25:11 -0400496
497// HandleRebuildSuccessfully sets up the test server to respond to a rebuild request with success.
498func HandleRebuildSuccessfully(t *testing.T, response string) {
499 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
500 th.TestMethod(t, r, "POST")
501 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
502 th.TestJSONRequest(t, r, `
503 {
504 "rebuild": {
505 "name": "new-name",
506 "adminPass": "swordfish",
507 "imageRef": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
508 "accessIPv4": "1.2.3.4"
509 }
510 }
511 `)
512
513 w.WriteHeader(http.StatusAccepted)
514 w.Header().Add("Content-Type", "application/json")
515 fmt.Fprintf(w, response)
516 })
517}
Alex Gaynorfa8fbeb2014-11-13 13:39:31 -0800518
Jon Perrittcc77da62014-11-16 13:14:21 -0700519// HandleServerRescueSuccessfully sets up the test server to respond to a server Rescue request.
Alex Gaynorfa8fbeb2014-11-13 13:39:31 -0800520func HandleServerRescueSuccessfully(t *testing.T) {
521 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
522 th.TestMethod(t, r, "POST")
523 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
524 th.TestJSONRequest(t, r, `{ "rescue": { "adminPass": "1234567890" } }`)
525
526 w.WriteHeader(http.StatusOK)
527 w.Write([]byte(`{ "adminPass": "1234567890" }`))
528 })
529}
Jon Perrittcc77da62014-11-16 13:14:21 -0700530
Jon Perritt78c57ce2014-11-20 11:07:18 -0700531// HandleMetadatumGetSuccessfully sets up the test server to respond to a metadatum Get request.
532func HandleMetadatumGetSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700533 th.Mux.HandleFunc("/servers/1234asdf/metadata/foo", func(w http.ResponseWriter, r *http.Request) {
534 th.TestMethod(t, r, "GET")
535 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
536 th.TestHeader(t, r, "Accept", "application/json")
537
538 w.WriteHeader(http.StatusOK)
539 w.Header().Add("Content-Type", "application/json")
540 w.Write([]byte(`{ "meta": {"foo":"bar"}}`))
541 })
542}
543
Jon Perritt78c57ce2014-11-20 11:07:18 -0700544// HandleMetadatumCreateSuccessfully sets up the test server to respond to a metadatum Create request.
545func HandleMetadatumCreateSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700546 th.Mux.HandleFunc("/servers/1234asdf/metadata/foo", func(w http.ResponseWriter, r *http.Request) {
547 th.TestMethod(t, r, "PUT")
548 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
549 th.TestJSONRequest(t, r, `{
550 "meta": {
551 "foo": "bar"
552 }
553 }`)
554
555 w.WriteHeader(http.StatusOK)
556 w.Header().Add("Content-Type", "application/json")
557 w.Write([]byte(`{ "meta": {"foo":"bar"}}`))
558 })
559}
560
Jon Perritt78c57ce2014-11-20 11:07:18 -0700561// HandleMetadatumDeleteSuccessfully sets up the test server to respond to a metadatum Delete request.
562func HandleMetadatumDeleteSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700563 th.Mux.HandleFunc("/servers/1234asdf/metadata/foo", func(w http.ResponseWriter, r *http.Request) {
564 th.TestMethod(t, r, "DELETE")
565 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
566
567 w.WriteHeader(http.StatusNoContent)
568 })
569}
570
Jon Perritt78c57ce2014-11-20 11:07:18 -0700571// HandleMetadataGetSuccessfully sets up the test server to respond to a metadata Get request.
572func HandleMetadataGetSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700573 th.Mux.HandleFunc("/servers/1234asdf/metadata", func(w http.ResponseWriter, r *http.Request) {
574 th.TestMethod(t, r, "GET")
575 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
576 th.TestHeader(t, r, "Accept", "application/json")
577
578 w.WriteHeader(http.StatusOK)
579 w.Write([]byte(`{ "metadata": {"foo":"bar", "this":"that"}}`))
580 })
581}
582
Jon Perritt789f8322014-11-21 08:20:04 -0700583// HandleMetadataResetSuccessfully sets up the test server to respond to a metadata Create request.
584func HandleMetadataResetSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700585 th.Mux.HandleFunc("/servers/1234asdf/metadata", func(w http.ResponseWriter, r *http.Request) {
586 th.TestMethod(t, r, "PUT")
587 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
588 th.TestJSONRequest(t, r, `{
589 "metadata": {
590 "foo": "bar",
591 "this": "that"
592 }
593 }`)
594
595 w.WriteHeader(http.StatusOK)
596 w.Header().Add("Content-Type", "application/json")
597 w.Write([]byte(`{ "metadata": {"foo":"bar", "this":"that"}}`))
598 })
599}
600
Jon Perritt78c57ce2014-11-20 11:07:18 -0700601// HandleMetadataUpdateSuccessfully sets up the test server to respond to a metadata Update request.
602func HandleMetadataUpdateSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700603 th.Mux.HandleFunc("/servers/1234asdf/metadata", func(w http.ResponseWriter, r *http.Request) {
604 th.TestMethod(t, r, "POST")
605 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
606 th.TestJSONRequest(t, r, `{
607 "metadata": {
608 "foo": "baz",
609 "this": "those"
610 }
611 }`)
612
613 w.WriteHeader(http.StatusOK)
614 w.Header().Add("Content-Type", "application/json")
615 w.Write([]byte(`{ "metadata": {"foo":"baz", "this":"those"}}`))
616 })
617}
Jon Perritt5cb49482015-02-19 12:19:58 -0700618
619// ListAddressesExpected represents an expected repsonse from a ListAddresses request.
jrperritt3d966162016-06-06 14:08:54 -0500620var ListAddressesExpected = map[string][]servers.Address{
621 "public": []servers.Address{
622 {
Jon Perritt5cb49482015-02-19 12:19:58 -0700623 Version: 4,
624 Address: "80.56.136.39",
625 },
jrperritt3d966162016-06-06 14:08:54 -0500626 {
Jon Perritt5cb49482015-02-19 12:19:58 -0700627 Version: 6,
628 Address: "2001:4800:790e:510:be76:4eff:fe04:82a8",
629 },
630 },
jrperritt3d966162016-06-06 14:08:54 -0500631 "private": []servers.Address{
632 {
Jon Perritt5cb49482015-02-19 12:19:58 -0700633 Version: 4,
634 Address: "10.880.3.154",
635 },
636 },
637}
638
639// HandleAddressListSuccessfully sets up the test server to respond to a ListAddresses request.
640func HandleAddressListSuccessfully(t *testing.T) {
641 th.Mux.HandleFunc("/servers/asdfasdfasdf/ips", func(w http.ResponseWriter, r *http.Request) {
642 th.TestMethod(t, r, "GET")
643 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
644
645 w.Header().Add("Content-Type", "application/json")
646 fmt.Fprintf(w, `{
647 "addresses": {
648 "public": [
649 {
650 "version": 4,
651 "addr": "50.56.176.35"
652 },
653 {
654 "version": 6,
655 "addr": "2001:4800:780e:510:be76:4eff:fe04:84a8"
656 }
657 ],
658 "private": [
659 {
660 "version": 4,
661 "addr": "10.180.3.155"
662 }
663 ]
664 }
665 }`)
666 })
667}
Jon Perritt04d073c2015-02-19 21:46:23 -0700668
669// ListNetworkAddressesExpected represents an expected repsonse from a ListAddressesByNetwork request.
jrperritt3d966162016-06-06 14:08:54 -0500670var ListNetworkAddressesExpected = []servers.Address{
671 {
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700672 Version: 4,
673 Address: "50.56.176.35",
674 },
jrperritt3d966162016-06-06 14:08:54 -0500675 {
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700676 Version: 6,
677 Address: "2001:4800:780e:510:be76:4eff:fe04:84a8",
Jon Perritt04d073c2015-02-19 21:46:23 -0700678 },
679}
680
681// HandleNetworkAddressListSuccessfully sets up the test server to respond to a ListAddressesByNetwork request.
682func HandleNetworkAddressListSuccessfully(t *testing.T) {
683 th.Mux.HandleFunc("/servers/asdfasdfasdf/ips/public", func(w http.ResponseWriter, r *http.Request) {
684 th.TestMethod(t, r, "GET")
685 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
686
687 w.Header().Add("Content-Type", "application/json")
688 fmt.Fprintf(w, `{
689 "public": [
690 {
691 "version": 4,
692 "addr": "50.56.176.35"
693 },
694 {
695 "version": 6,
696 "addr": "2001:4800:780e:510:be76:4eff:fe04:84a8"
697 }
698 ]
699 }`)
700 })
701}
einarf2fc665e2015-04-16 20:16:21 +0000702
703// HandleCreateServerImageSuccessfully sets up the test server to respond to a TestCreateServerImage request.
704func HandleCreateServerImageSuccessfully(t *testing.T) {
705 th.Mux.HandleFunc("/servers/serverimage/action", func(w http.ResponseWriter, r *http.Request) {
706 th.TestMethod(t, r, "POST")
707 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
708 w.Header().Add("Location", "https://0.0.0.0/images/xxxx-xxxxx-xxxxx-xxxx")
709 w.WriteHeader(http.StatusAccepted)
710 })
711}
712
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100713// HandlePasswordGetSuccessfully sets up the test server to respond to a password Get request.
714func HandlePasswordGetSuccessfully(t *testing.T) {
715 th.Mux.HandleFunc("/servers/1234asdf/os-server-password", func(w http.ResponseWriter, r *http.Request) {
716 th.TestMethod(t, r, "GET")
717 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
718 th.TestHeader(t, r, "Accept", "application/json")
719
720 fmt.Fprintf(w, ServerPasswordBody)
721 })
722}