blob: 8b2c94d1b78d96626309baf074844f2c8a3f3a7c [file] [log] [blame]
Ash Wilsone77ffb02014-10-20 13:10:26 -04001// +build fixtures
Ash Wilson15e0f272014-10-21 15:33:02 -04002
Ash Wilsone77ffb02014-10-20 13:10:26 -04003package servers
4
Ash Wilson664fe332014-10-21 17:47:49 -04005import (
6 "fmt"
7 "net/http"
8 "testing"
9
Jon Perrittdb0ae142016-03-13 00:33:41 -060010 "github.com/gophercloud/gophercloud"
Jon Perritt27249f42016-02-18 10:35:59 -060011 th "github.com/gophercloud/gophercloud/testhelper"
12 "github.com/gophercloud/gophercloud/testhelper/client"
Ash Wilson664fe332014-10-21 17:47:49 -040013)
14
Ash Wilsone77ffb02014-10-20 13:10:26 -040015// ServerListBody contains the canned body of a servers.List response.
16const ServerListBody = `
17{
18 "servers": [
19 {
20 "status": "ACTIVE",
21 "updated": "2014-09-25T13:10:10Z",
22 "hostId": "29d3c8c896a45aa4c34e52247875d7fefc3d94bbcc9f622b5d204362",
23 "OS-EXT-SRV-ATTR:host": "devstack",
24 "addresses": {
25 "private": [
26 {
27 "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:7c:1b:2b",
28 "version": 4,
29 "addr": "10.0.0.32",
30 "OS-EXT-IPS:type": "fixed"
31 }
32 ]
33 },
34 "links": [
35 {
36 "href": "http://104.130.131.164:8774/v2/fcad67a6189847c4aecfa3c81a05783b/servers/ef079b0c-e610-4dfb-b1aa-b49f07ac48e5",
37 "rel": "self"
38 },
39 {
40 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/servers/ef079b0c-e610-4dfb-b1aa-b49f07ac48e5",
41 "rel": "bookmark"
42 }
43 ],
44 "key_name": null,
45 "image": {
46 "id": "f90f6034-2570-4974-8351-6b49732ef2eb",
47 "links": [
48 {
49 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
50 "rel": "bookmark"
51 }
52 ]
53 },
54 "OS-EXT-STS:task_state": null,
55 "OS-EXT-STS:vm_state": "active",
56 "OS-EXT-SRV-ATTR:instance_name": "instance-0000001e",
57 "OS-SRV-USG:launched_at": "2014-09-25T13:10:10.000000",
58 "OS-EXT-SRV-ATTR:hypervisor_hostname": "devstack",
59 "flavor": {
60 "id": "1",
61 "links": [
62 {
63 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/flavors/1",
64 "rel": "bookmark"
65 }
66 ]
67 },
68 "id": "ef079b0c-e610-4dfb-b1aa-b49f07ac48e5",
69 "security_groups": [
70 {
71 "name": "default"
72 }
73 ],
74 "OS-SRV-USG:terminated_at": null,
75 "OS-EXT-AZ:availability_zone": "nova",
76 "user_id": "9349aff8be7545ac9d2f1d00999a23cd",
77 "name": "herp",
78 "created": "2014-09-25T13:10:02Z",
79 "tenant_id": "fcad67a6189847c4aecfa3c81a05783b",
80 "OS-DCF:diskConfig": "MANUAL",
81 "os-extended-volumes:volumes_attached": [],
82 "accessIPv4": "",
83 "accessIPv6": "",
84 "progress": 0,
85 "OS-EXT-STS:power_state": 1,
86 "config_drive": "",
87 "metadata": {}
88 },
89 {
90 "status": "ACTIVE",
91 "updated": "2014-09-25T13:04:49Z",
92 "hostId": "29d3c8c896a45aa4c34e52247875d7fefc3d94bbcc9f622b5d204362",
93 "OS-EXT-SRV-ATTR:host": "devstack",
94 "addresses": {
95 "private": [
96 {
97 "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:9e:89:be",
98 "version": 4,
99 "addr": "10.0.0.31",
100 "OS-EXT-IPS:type": "fixed"
101 }
102 ]
103 },
104 "links": [
105 {
106 "href": "http://104.130.131.164:8774/v2/fcad67a6189847c4aecfa3c81a05783b/servers/9e5476bd-a4ec-4653-93d6-72c93aa682ba",
107 "rel": "self"
108 },
109 {
110 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/servers/9e5476bd-a4ec-4653-93d6-72c93aa682ba",
111 "rel": "bookmark"
112 }
113 ],
114 "key_name": null,
115 "image": {
116 "id": "f90f6034-2570-4974-8351-6b49732ef2eb",
117 "links": [
118 {
119 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
120 "rel": "bookmark"
121 }
122 ]
123 },
124 "OS-EXT-STS:task_state": null,
125 "OS-EXT-STS:vm_state": "active",
126 "OS-EXT-SRV-ATTR:instance_name": "instance-0000001d",
127 "OS-SRV-USG:launched_at": "2014-09-25T13:04:49.000000",
128 "OS-EXT-SRV-ATTR:hypervisor_hostname": "devstack",
129 "flavor": {
130 "id": "1",
131 "links": [
132 {
133 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/flavors/1",
134 "rel": "bookmark"
135 }
136 ]
137 },
138 "id": "9e5476bd-a4ec-4653-93d6-72c93aa682ba",
139 "security_groups": [
140 {
141 "name": "default"
142 }
143 ],
144 "OS-SRV-USG:terminated_at": null,
145 "OS-EXT-AZ:availability_zone": "nova",
146 "user_id": "9349aff8be7545ac9d2f1d00999a23cd",
147 "name": "derp",
148 "created": "2014-09-25T13:04:41Z",
149 "tenant_id": "fcad67a6189847c4aecfa3c81a05783b",
150 "OS-DCF:diskConfig": "MANUAL",
151 "os-extended-volumes:volumes_attached": [],
152 "accessIPv4": "",
153 "accessIPv6": "",
154 "progress": 0,
155 "OS-EXT-STS:power_state": 1,
156 "config_drive": "",
157 "metadata": {}
158 }
159 ]
160}
161`
162
163// SingleServerBody is the canned body of a Get request on an existing server.
164const SingleServerBody = `
165{
166 "server": {
167 "status": "ACTIVE",
168 "updated": "2014-09-25T13:04:49Z",
169 "hostId": "29d3c8c896a45aa4c34e52247875d7fefc3d94bbcc9f622b5d204362",
170 "OS-EXT-SRV-ATTR:host": "devstack",
171 "addresses": {
172 "private": [
173 {
174 "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:9e:89:be",
175 "version": 4,
176 "addr": "10.0.0.31",
177 "OS-EXT-IPS:type": "fixed"
178 }
179 ]
180 },
181 "links": [
182 {
183 "href": "http://104.130.131.164:8774/v2/fcad67a6189847c4aecfa3c81a05783b/servers/9e5476bd-a4ec-4653-93d6-72c93aa682ba",
184 "rel": "self"
185 },
186 {
187 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/servers/9e5476bd-a4ec-4653-93d6-72c93aa682ba",
188 "rel": "bookmark"
189 }
190 ],
191 "key_name": null,
192 "image": {
193 "id": "f90f6034-2570-4974-8351-6b49732ef2eb",
194 "links": [
195 {
196 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
197 "rel": "bookmark"
198 }
199 ]
200 },
201 "OS-EXT-STS:task_state": null,
202 "OS-EXT-STS:vm_state": "active",
203 "OS-EXT-SRV-ATTR:instance_name": "instance-0000001d",
204 "OS-SRV-USG:launched_at": "2014-09-25T13:04:49.000000",
205 "OS-EXT-SRV-ATTR:hypervisor_hostname": "devstack",
206 "flavor": {
207 "id": "1",
208 "links": [
209 {
210 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/flavors/1",
211 "rel": "bookmark"
212 }
213 ]
214 },
215 "id": "9e5476bd-a4ec-4653-93d6-72c93aa682ba",
216 "security_groups": [
217 {
218 "name": "default"
219 }
220 ],
221 "OS-SRV-USG:terminated_at": null,
222 "OS-EXT-AZ:availability_zone": "nova",
223 "user_id": "9349aff8be7545ac9d2f1d00999a23cd",
224 "name": "derp",
225 "created": "2014-09-25T13:04:41Z",
226 "tenant_id": "fcad67a6189847c4aecfa3c81a05783b",
227 "OS-DCF:diskConfig": "MANUAL",
228 "os-extended-volumes:volumes_attached": [],
229 "accessIPv4": "",
230 "accessIPv6": "",
231 "progress": 0,
232 "OS-EXT-STS:power_state": 1,
233 "config_drive": "",
234 "metadata": {}
235 }
236}
237`
238
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100239const ServerPasswordBody = `
240{
241 "password": "xlozO3wLCBRWAa2yDjCCVx8vwNPypxnypmRYDa/zErlQ+EzPe1S/Gz6nfmC52mOlOSCRuUOmG7kqqgejPof6M7bOezS387zjq4LSvvwp28zUknzy4YzfFGhnHAdai3TxUJ26pfQCYrq8UTzmKF2Bq8ioSEtVVzM0A96pDh8W2i7BOz6MdoiVyiev/I1K2LsuipfxSJR7Wdke4zNXJjHHP2RfYsVbZ/k9ANu+Nz4iIH8/7Cacud/pphH7EjrY6a4RZNrjQskrhKYed0YERpotyjYk1eDtRe72GrSiXteqCM4biaQ5w3ruS+AcX//PXk3uJ5kC7d67fPXaVz4WaQRYMg=="
242}
243`
244
Ash Wilsone77ffb02014-10-20 13:10:26 -0400245var (
246 // ServerHerp is a Server struct that should correspond to the first result in ServerListBody.
247 ServerHerp = Server{
248 Status: "ACTIVE",
249 Updated: "2014-09-25T13:10:10Z",
250 HostID: "29d3c8c896a45aa4c34e52247875d7fefc3d94bbcc9f622b5d204362",
251 Addresses: map[string]interface{}{
252 "private": []interface{}{
253 map[string]interface{}{
254 "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:7c:1b:2b",
255 "version": float64(4),
256 "addr": "10.0.0.32",
257 "OS-EXT-IPS:type": "fixed",
258 },
259 },
260 },
261 Links: []interface{}{
262 map[string]interface{}{
263 "href": "http://104.130.131.164:8774/v2/fcad67a6189847c4aecfa3c81a05783b/servers/ef079b0c-e610-4dfb-b1aa-b49f07ac48e5",
264 "rel": "self",
265 },
266 map[string]interface{}{
267 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/servers/ef079b0c-e610-4dfb-b1aa-b49f07ac48e5",
268 "rel": "bookmark",
269 },
270 },
271 Image: map[string]interface{}{
272 "id": "f90f6034-2570-4974-8351-6b49732ef2eb",
273 "links": []interface{}{
274 map[string]interface{}{
275 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
276 "rel": "bookmark",
277 },
278 },
279 },
280 Flavor: map[string]interface{}{
281 "id": "1",
282 "links": []interface{}{
283 map[string]interface{}{
284 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/flavors/1",
285 "rel": "bookmark",
286 },
287 },
288 },
289 ID: "ef079b0c-e610-4dfb-b1aa-b49f07ac48e5",
290 UserID: "9349aff8be7545ac9d2f1d00999a23cd",
291 Name: "herp",
292 Created: "2014-09-25T13:10:02Z",
293 TenantID: "fcad67a6189847c4aecfa3c81a05783b",
294 Metadata: map[string]interface{}{},
Joe Topjian978bb502015-02-12 20:55:31 +0000295 SecurityGroups: []map[string]interface{}{
296 map[string]interface{}{
297 "name": "default",
298 },
299 },
Ash Wilsone77ffb02014-10-20 13:10:26 -0400300 }
301
302 // ServerDerp is a Server struct that should correspond to the second server in ServerListBody.
303 ServerDerp = Server{
304 Status: "ACTIVE",
305 Updated: "2014-09-25T13:04:49Z",
306 HostID: "29d3c8c896a45aa4c34e52247875d7fefc3d94bbcc9f622b5d204362",
307 Addresses: map[string]interface{}{
308 "private": []interface{}{
309 map[string]interface{}{
310 "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:9e:89:be",
311 "version": float64(4),
312 "addr": "10.0.0.31",
313 "OS-EXT-IPS:type": "fixed",
314 },
315 },
316 },
317 Links: []interface{}{
318 map[string]interface{}{
319 "href": "http://104.130.131.164:8774/v2/fcad67a6189847c4aecfa3c81a05783b/servers/9e5476bd-a4ec-4653-93d6-72c93aa682ba",
320 "rel": "self",
321 },
322 map[string]interface{}{
323 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/servers/9e5476bd-a4ec-4653-93d6-72c93aa682ba",
324 "rel": "bookmark",
325 },
326 },
327 Image: map[string]interface{}{
328 "id": "f90f6034-2570-4974-8351-6b49732ef2eb",
329 "links": []interface{}{
330 map[string]interface{}{
331 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
332 "rel": "bookmark",
333 },
334 },
335 },
336 Flavor: map[string]interface{}{
337 "id": "1",
338 "links": []interface{}{
339 map[string]interface{}{
340 "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/flavors/1",
341 "rel": "bookmark",
342 },
343 },
344 },
345 ID: "9e5476bd-a4ec-4653-93d6-72c93aa682ba",
346 UserID: "9349aff8be7545ac9d2f1d00999a23cd",
347 Name: "derp",
348 Created: "2014-09-25T13:04:41Z",
349 TenantID: "fcad67a6189847c4aecfa3c81a05783b",
350 Metadata: map[string]interface{}{},
Joe Topjian978bb502015-02-12 20:55:31 +0000351 SecurityGroups: []map[string]interface{}{
352 map[string]interface{}{
353 "name": "default",
354 },
355 },
Ash Wilsone77ffb02014-10-20 13:10:26 -0400356 }
357)
Ash Wilson664fe332014-10-21 17:47:49 -0400358
Jon Perrittdb0ae142016-03-13 00:33:41 -0600359type CreateOptsWithCustomField struct {
360 CreateOpts
361 Foo string `json:"foo,omitempty"`
362}
363
364func (opts CreateOptsWithCustomField) ToServerCreateMap() (map[string]interface{}, error) {
365 return gophercloud.BuildRequestBody(opts, "server")
366}
367
Ash Wilson664fe332014-10-21 17:47:49 -0400368// HandleServerCreationSuccessfully sets up the test server to respond to a server creation request
369// with a given response.
370func HandleServerCreationSuccessfully(t *testing.T, response string) {
371 th.Mux.HandleFunc("/servers", func(w http.ResponseWriter, r *http.Request) {
372 th.TestMethod(t, r, "POST")
373 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
374 th.TestJSONRequest(t, r, `{
375 "server": {
376 "name": "derp",
377 "imageRef": "f90f6034-2570-4974-8351-6b49732ef2eb",
378 "flavorRef": "1"
379 }
380 }`)
381
382 w.WriteHeader(http.StatusAccepted)
383 w.Header().Add("Content-Type", "application/json")
384 fmt.Fprintf(w, response)
385 })
386}
387
Jon Perrittdb0ae142016-03-13 00:33:41 -0600388// HandleServerCreationWithCustomFieldSuccessfully sets up the test server to respond to a server creation request
389// with a given response.
390func HandleServerCreationWithCustomFieldSuccessfully(t *testing.T, response string) {
391 th.Mux.HandleFunc("/servers", func(w http.ResponseWriter, r *http.Request) {
392 th.TestMethod(t, r, "POST")
393 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
394 th.TestJSONRequest(t, r, `{
395 "server": {
396 "name": "derp",
397 "imageRef": "f90f6034-2570-4974-8351-6b49732ef2eb",
398 "flavorRef": "1",
399 "foo": "bar"
400 }
401 }`)
402
403 w.WriteHeader(http.StatusAccepted)
404 w.Header().Add("Content-Type", "application/json")
405 fmt.Fprintf(w, response)
406 })
407}
408
Ash Wilsona70510a2014-10-23 11:54:03 -0400409// HandleServerListSuccessfully sets up the test server to respond to a server List request.
410func HandleServerListSuccessfully(t *testing.T) {
411 th.Mux.HandleFunc("/servers/detail", func(w http.ResponseWriter, r *http.Request) {
412 th.TestMethod(t, r, "GET")
413 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
414
415 w.Header().Add("Content-Type", "application/json")
416 r.ParseForm()
417 marker := r.Form.Get("marker")
418 switch marker {
419 case "":
420 fmt.Fprintf(w, ServerListBody)
421 case "9e5476bd-a4ec-4653-93d6-72c93aa682ba":
422 fmt.Fprintf(w, `{ "servers": [] }`)
423 default:
424 t.Fatalf("/servers/detail invoked with unexpected marker=[%s]", marker)
425 }
426 })
427}
428
Ash Wilson664fe332014-10-21 17:47:49 -0400429// HandleServerDeletionSuccessfully sets up the test server to respond to a server deletion request.
430func HandleServerDeletionSuccessfully(t *testing.T) {
431 th.Mux.HandleFunc("/servers/asdfasdfasdf", func(w http.ResponseWriter, r *http.Request) {
432 th.TestMethod(t, r, "DELETE")
433 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
434
435 w.WriteHeader(http.StatusNoContent)
436 })
437}
Ash Wilson1c1eb882014-10-21 18:14:31 -0400438
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100439// HandleServerForceDeletionSuccessfully sets up the test server to respond to a server force deletion
440// request.
Ian Duffy370c4302016-01-21 10:44:56 +0000441func HandleServerForceDeletionSuccessfully(t *testing.T) {
442 th.Mux.HandleFunc("/servers/asdfasdfasdf/action", func(w http.ResponseWriter, r *http.Request) {
443 th.TestMethod(t, r, "POST")
444 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
445 th.TestJSONRequest(t, r, `{ "forceDelete": "" }`)
446
447 w.WriteHeader(http.StatusAccepted)
448 })
449}
450
Ash Wilson189c95c2014-10-23 11:41:35 -0400451// HandleServerGetSuccessfully sets up the test server to respond to a server Get request.
452func HandleServerGetSuccessfully(t *testing.T) {
453 th.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) {
454 th.TestMethod(t, r, "GET")
455 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
456 th.TestHeader(t, r, "Accept", "application/json")
457
458 fmt.Fprintf(w, SingleServerBody)
459 })
460}
461
462// HandleServerUpdateSuccessfully sets up the test server to respond to a server Update request.
463func HandleServerUpdateSuccessfully(t *testing.T) {
464 th.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) {
465 th.TestMethod(t, r, "PUT")
466 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
467 th.TestHeader(t, r, "Accept", "application/json")
468 th.TestHeader(t, r, "Content-Type", "application/json")
469 th.TestJSONRequest(t, r, `{ "server": { "name": "new-name" } }`)
470
471 fmt.Fprintf(w, SingleServerBody)
472 })
473}
474
Ash Wilson1c1eb882014-10-21 18:14:31 -0400475// HandleAdminPasswordChangeSuccessfully sets up the test server to respond to a server password
476// change request.
477func HandleAdminPasswordChangeSuccessfully(t *testing.T) {
478 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
479 th.TestMethod(t, r, "POST")
480 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
481 th.TestJSONRequest(t, r, `{ "changePassword": { "adminPass": "new-password" } }`)
482
483 w.WriteHeader(http.StatusAccepted)
484 })
485}
Ash Wilson2295ba52014-10-21 18:19:28 -0400486
487// HandleRebootSuccessfully sets up the test server to respond to a reboot request with success.
488func HandleRebootSuccessfully(t *testing.T) {
489 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
490 th.TestMethod(t, r, "POST")
491 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
492 th.TestJSONRequest(t, r, `{ "reboot": { "type": "SOFT" } }`)
493
494 w.WriteHeader(http.StatusAccepted)
495 })
496}
Ash Wilsonacf49c62014-10-21 18:25:11 -0400497
498// HandleRebuildSuccessfully sets up the test server to respond to a rebuild request with success.
499func HandleRebuildSuccessfully(t *testing.T, response string) {
500 th.Mux.HandleFunc("/servers/1234asdf/action", 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 {
505 "rebuild": {
506 "name": "new-name",
507 "adminPass": "swordfish",
508 "imageRef": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
509 "accessIPv4": "1.2.3.4"
510 }
511 }
512 `)
513
514 w.WriteHeader(http.StatusAccepted)
515 w.Header().Add("Content-Type", "application/json")
516 fmt.Fprintf(w, response)
517 })
518}
Alex Gaynorfa8fbeb2014-11-13 13:39:31 -0800519
Jon Perrittcc77da62014-11-16 13:14:21 -0700520// HandleServerRescueSuccessfully sets up the test server to respond to a server Rescue request.
Alex Gaynorfa8fbeb2014-11-13 13:39:31 -0800521func HandleServerRescueSuccessfully(t *testing.T) {
522 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
523 th.TestMethod(t, r, "POST")
524 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
525 th.TestJSONRequest(t, r, `{ "rescue": { "adminPass": "1234567890" } }`)
526
527 w.WriteHeader(http.StatusOK)
528 w.Write([]byte(`{ "adminPass": "1234567890" }`))
529 })
530}
Jon Perrittcc77da62014-11-16 13:14:21 -0700531
Jon Perritt78c57ce2014-11-20 11:07:18 -0700532// HandleMetadatumGetSuccessfully sets up the test server to respond to a metadatum Get request.
533func HandleMetadatumGetSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700534 th.Mux.HandleFunc("/servers/1234asdf/metadata/foo", func(w http.ResponseWriter, r *http.Request) {
535 th.TestMethod(t, r, "GET")
536 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
537 th.TestHeader(t, r, "Accept", "application/json")
538
539 w.WriteHeader(http.StatusOK)
540 w.Header().Add("Content-Type", "application/json")
541 w.Write([]byte(`{ "meta": {"foo":"bar"}}`))
542 })
543}
544
Jon Perritt78c57ce2014-11-20 11:07:18 -0700545// HandleMetadatumCreateSuccessfully sets up the test server to respond to a metadatum Create request.
546func HandleMetadatumCreateSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700547 th.Mux.HandleFunc("/servers/1234asdf/metadata/foo", func(w http.ResponseWriter, r *http.Request) {
548 th.TestMethod(t, r, "PUT")
549 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
550 th.TestJSONRequest(t, r, `{
551 "meta": {
552 "foo": "bar"
553 }
554 }`)
555
556 w.WriteHeader(http.StatusOK)
557 w.Header().Add("Content-Type", "application/json")
558 w.Write([]byte(`{ "meta": {"foo":"bar"}}`))
559 })
560}
561
Jon Perritt78c57ce2014-11-20 11:07:18 -0700562// HandleMetadatumDeleteSuccessfully sets up the test server to respond to a metadatum Delete request.
563func HandleMetadatumDeleteSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700564 th.Mux.HandleFunc("/servers/1234asdf/metadata/foo", func(w http.ResponseWriter, r *http.Request) {
565 th.TestMethod(t, r, "DELETE")
566 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
567
568 w.WriteHeader(http.StatusNoContent)
569 })
570}
571
Jon Perritt78c57ce2014-11-20 11:07:18 -0700572// HandleMetadataGetSuccessfully sets up the test server to respond to a metadata Get request.
573func HandleMetadataGetSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700574 th.Mux.HandleFunc("/servers/1234asdf/metadata", func(w http.ResponseWriter, r *http.Request) {
575 th.TestMethod(t, r, "GET")
576 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
577 th.TestHeader(t, r, "Accept", "application/json")
578
579 w.WriteHeader(http.StatusOK)
580 w.Write([]byte(`{ "metadata": {"foo":"bar", "this":"that"}}`))
581 })
582}
583
Jon Perritt789f8322014-11-21 08:20:04 -0700584// HandleMetadataResetSuccessfully sets up the test server to respond to a metadata Create request.
585func HandleMetadataResetSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700586 th.Mux.HandleFunc("/servers/1234asdf/metadata", func(w http.ResponseWriter, r *http.Request) {
587 th.TestMethod(t, r, "PUT")
588 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
589 th.TestJSONRequest(t, r, `{
590 "metadata": {
591 "foo": "bar",
592 "this": "that"
593 }
594 }`)
595
596 w.WriteHeader(http.StatusOK)
597 w.Header().Add("Content-Type", "application/json")
598 w.Write([]byte(`{ "metadata": {"foo":"bar", "this":"that"}}`))
599 })
600}
601
Jon Perritt78c57ce2014-11-20 11:07:18 -0700602// HandleMetadataUpdateSuccessfully sets up the test server to respond to a metadata Update request.
603func HandleMetadataUpdateSuccessfully(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700604 th.Mux.HandleFunc("/servers/1234asdf/metadata", func(w http.ResponseWriter, r *http.Request) {
605 th.TestMethod(t, r, "POST")
606 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
607 th.TestJSONRequest(t, r, `{
608 "metadata": {
609 "foo": "baz",
610 "this": "those"
611 }
612 }`)
613
614 w.WriteHeader(http.StatusOK)
615 w.Header().Add("Content-Type", "application/json")
616 w.Write([]byte(`{ "metadata": {"foo":"baz", "this":"those"}}`))
617 })
618}
Jon Perritt5cb49482015-02-19 12:19:58 -0700619
620// ListAddressesExpected represents an expected repsonse from a ListAddresses request.
621var ListAddressesExpected = map[string][]Address{
622 "public": []Address{
623 Address{
624 Version: 4,
625 Address: "80.56.136.39",
626 },
627 Address{
628 Version: 6,
629 Address: "2001:4800:790e:510:be76:4eff:fe04:82a8",
630 },
631 },
632 "private": []Address{
633 Address{
634 Version: 4,
635 Address: "10.880.3.154",
636 },
637 },
638}
639
640// HandleAddressListSuccessfully sets up the test server to respond to a ListAddresses request.
641func HandleAddressListSuccessfully(t *testing.T) {
642 th.Mux.HandleFunc("/servers/asdfasdfasdf/ips", func(w http.ResponseWriter, r *http.Request) {
643 th.TestMethod(t, r, "GET")
644 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
645
646 w.Header().Add("Content-Type", "application/json")
647 fmt.Fprintf(w, `{
648 "addresses": {
649 "public": [
650 {
651 "version": 4,
652 "addr": "50.56.176.35"
653 },
654 {
655 "version": 6,
656 "addr": "2001:4800:780e:510:be76:4eff:fe04:84a8"
657 }
658 ],
659 "private": [
660 {
661 "version": 4,
662 "addr": "10.180.3.155"
663 }
664 ]
665 }
666 }`)
667 })
668}
Jon Perritt04d073c2015-02-19 21:46:23 -0700669
670// ListNetworkAddressesExpected represents an expected repsonse from a ListAddressesByNetwork request.
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700671var ListNetworkAddressesExpected = []Address{
672 Address{
673 Version: 4,
674 Address: "50.56.176.35",
675 },
676 Address{
677 Version: 6,
678 Address: "2001:4800:780e:510:be76:4eff:fe04:84a8",
Jon Perritt04d073c2015-02-19 21:46:23 -0700679 },
680}
681
682// HandleNetworkAddressListSuccessfully sets up the test server to respond to a ListAddressesByNetwork request.
683func HandleNetworkAddressListSuccessfully(t *testing.T) {
684 th.Mux.HandleFunc("/servers/asdfasdfasdf/ips/public", func(w http.ResponseWriter, r *http.Request) {
685 th.TestMethod(t, r, "GET")
686 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
687
688 w.Header().Add("Content-Type", "application/json")
689 fmt.Fprintf(w, `{
690 "public": [
691 {
692 "version": 4,
693 "addr": "50.56.176.35"
694 },
695 {
696 "version": 6,
697 "addr": "2001:4800:780e:510:be76:4eff:fe04:84a8"
698 }
699 ]
700 }`)
701 })
702}
einarf2fc665e2015-04-16 20:16:21 +0000703
704// HandleCreateServerImageSuccessfully sets up the test server to respond to a TestCreateServerImage request.
705func HandleCreateServerImageSuccessfully(t *testing.T) {
706 th.Mux.HandleFunc("/servers/serverimage/action", func(w http.ResponseWriter, r *http.Request) {
707 th.TestMethod(t, r, "POST")
708 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
709 w.Header().Add("Location", "https://0.0.0.0/images/xxxx-xxxxx-xxxxx-xxxx")
710 w.WriteHeader(http.StatusAccepted)
711 })
712}
713
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100714// HandlePasswordGetSuccessfully sets up the test server to respond to a password Get request.
715func HandlePasswordGetSuccessfully(t *testing.T) {
716 th.Mux.HandleFunc("/servers/1234asdf/os-server-password", func(w http.ResponseWriter, r *http.Request) {
717 th.TestMethod(t, r, "GET")
718 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
719 th.TestHeader(t, r, "Accept", "application/json")
720
721 fmt.Fprintf(w, ServerPasswordBody)
722 })
723}