Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 1 | // +build fixtures |
Ash Wilson | 15e0f27 | 2014-10-21 15:33:02 -0400 | [diff] [blame] | 2 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 3 | package servers |
| 4 | |
Ash Wilson | 664fe33 | 2014-10-21 17:47:49 -0400 | [diff] [blame] | 5 | import ( |
| 6 | "fmt" |
| 7 | "net/http" |
| 8 | "testing" |
| 9 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 10 | "github.com/gophercloud/gophercloud" |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 11 | th "github.com/gophercloud/gophercloud/testhelper" |
| 12 | "github.com/gophercloud/gophercloud/testhelper/client" |
Ash Wilson | 664fe33 | 2014-10-21 17:47:49 -0400 | [diff] [blame] | 13 | ) |
| 14 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 15 | // ServerListBody contains the canned body of a servers.List response. |
| 16 | const 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. |
| 164 | const 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 | |
| 239 | var ( |
| 240 | // ServerHerp is a Server struct that should correspond to the first result in ServerListBody. |
| 241 | ServerHerp = Server{ |
| 242 | Status: "ACTIVE", |
| 243 | Updated: "2014-09-25T13:10:10Z", |
| 244 | HostID: "29d3c8c896a45aa4c34e52247875d7fefc3d94bbcc9f622b5d204362", |
| 245 | Addresses: map[string]interface{}{ |
| 246 | "private": []interface{}{ |
| 247 | map[string]interface{}{ |
| 248 | "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:7c:1b:2b", |
| 249 | "version": float64(4), |
| 250 | "addr": "10.0.0.32", |
| 251 | "OS-EXT-IPS:type": "fixed", |
| 252 | }, |
| 253 | }, |
| 254 | }, |
| 255 | Links: []interface{}{ |
| 256 | map[string]interface{}{ |
| 257 | "href": "http://104.130.131.164:8774/v2/fcad67a6189847c4aecfa3c81a05783b/servers/ef079b0c-e610-4dfb-b1aa-b49f07ac48e5", |
| 258 | "rel": "self", |
| 259 | }, |
| 260 | map[string]interface{}{ |
| 261 | "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/servers/ef079b0c-e610-4dfb-b1aa-b49f07ac48e5", |
| 262 | "rel": "bookmark", |
| 263 | }, |
| 264 | }, |
| 265 | Image: map[string]interface{}{ |
| 266 | "id": "f90f6034-2570-4974-8351-6b49732ef2eb", |
| 267 | "links": []interface{}{ |
| 268 | map[string]interface{}{ |
| 269 | "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb", |
| 270 | "rel": "bookmark", |
| 271 | }, |
| 272 | }, |
| 273 | }, |
| 274 | Flavor: map[string]interface{}{ |
| 275 | "id": "1", |
| 276 | "links": []interface{}{ |
| 277 | map[string]interface{}{ |
| 278 | "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/flavors/1", |
| 279 | "rel": "bookmark", |
| 280 | }, |
| 281 | }, |
| 282 | }, |
| 283 | ID: "ef079b0c-e610-4dfb-b1aa-b49f07ac48e5", |
| 284 | UserID: "9349aff8be7545ac9d2f1d00999a23cd", |
| 285 | Name: "herp", |
| 286 | Created: "2014-09-25T13:10:02Z", |
| 287 | TenantID: "fcad67a6189847c4aecfa3c81a05783b", |
| 288 | Metadata: map[string]interface{}{}, |
Joe Topjian | 978bb50 | 2015-02-12 20:55:31 +0000 | [diff] [blame] | 289 | SecurityGroups: []map[string]interface{}{ |
| 290 | map[string]interface{}{ |
| 291 | "name": "default", |
| 292 | }, |
| 293 | }, |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | // ServerDerp is a Server struct that should correspond to the second server in ServerListBody. |
| 297 | ServerDerp = Server{ |
| 298 | Status: "ACTIVE", |
| 299 | Updated: "2014-09-25T13:04:49Z", |
| 300 | HostID: "29d3c8c896a45aa4c34e52247875d7fefc3d94bbcc9f622b5d204362", |
| 301 | Addresses: map[string]interface{}{ |
| 302 | "private": []interface{}{ |
| 303 | map[string]interface{}{ |
| 304 | "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:9e:89:be", |
| 305 | "version": float64(4), |
| 306 | "addr": "10.0.0.31", |
| 307 | "OS-EXT-IPS:type": "fixed", |
| 308 | }, |
| 309 | }, |
| 310 | }, |
| 311 | Links: []interface{}{ |
| 312 | map[string]interface{}{ |
| 313 | "href": "http://104.130.131.164:8774/v2/fcad67a6189847c4aecfa3c81a05783b/servers/9e5476bd-a4ec-4653-93d6-72c93aa682ba", |
| 314 | "rel": "self", |
| 315 | }, |
| 316 | map[string]interface{}{ |
| 317 | "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/servers/9e5476bd-a4ec-4653-93d6-72c93aa682ba", |
| 318 | "rel": "bookmark", |
| 319 | }, |
| 320 | }, |
| 321 | Image: map[string]interface{}{ |
| 322 | "id": "f90f6034-2570-4974-8351-6b49732ef2eb", |
| 323 | "links": []interface{}{ |
| 324 | map[string]interface{}{ |
| 325 | "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb", |
| 326 | "rel": "bookmark", |
| 327 | }, |
| 328 | }, |
| 329 | }, |
| 330 | Flavor: map[string]interface{}{ |
| 331 | "id": "1", |
| 332 | "links": []interface{}{ |
| 333 | map[string]interface{}{ |
| 334 | "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/flavors/1", |
| 335 | "rel": "bookmark", |
| 336 | }, |
| 337 | }, |
| 338 | }, |
| 339 | ID: "9e5476bd-a4ec-4653-93d6-72c93aa682ba", |
| 340 | UserID: "9349aff8be7545ac9d2f1d00999a23cd", |
| 341 | Name: "derp", |
| 342 | Created: "2014-09-25T13:04:41Z", |
| 343 | TenantID: "fcad67a6189847c4aecfa3c81a05783b", |
| 344 | Metadata: map[string]interface{}{}, |
Joe Topjian | 978bb50 | 2015-02-12 20:55:31 +0000 | [diff] [blame] | 345 | SecurityGroups: []map[string]interface{}{ |
| 346 | map[string]interface{}{ |
| 347 | "name": "default", |
| 348 | }, |
| 349 | }, |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 350 | } |
| 351 | ) |
Ash Wilson | 664fe33 | 2014-10-21 17:47:49 -0400 | [diff] [blame] | 352 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 353 | type CreateOptsWithCustomField struct { |
| 354 | CreateOpts |
| 355 | Foo string `json:"foo,omitempty"` |
| 356 | } |
| 357 | |
| 358 | func (opts CreateOptsWithCustomField) ToServerCreateMap() (map[string]interface{}, error) { |
| 359 | return gophercloud.BuildRequestBody(opts, "server") |
| 360 | } |
| 361 | |
Ash Wilson | 664fe33 | 2014-10-21 17:47:49 -0400 | [diff] [blame] | 362 | // HandleServerCreationSuccessfully sets up the test server to respond to a server creation request |
| 363 | // with a given response. |
| 364 | func HandleServerCreationSuccessfully(t *testing.T, response string) { |
| 365 | th.Mux.HandleFunc("/servers", func(w http.ResponseWriter, r *http.Request) { |
| 366 | th.TestMethod(t, r, "POST") |
| 367 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 368 | th.TestJSONRequest(t, r, `{ |
| 369 | "server": { |
| 370 | "name": "derp", |
| 371 | "imageRef": "f90f6034-2570-4974-8351-6b49732ef2eb", |
| 372 | "flavorRef": "1" |
| 373 | } |
| 374 | }`) |
| 375 | |
| 376 | w.WriteHeader(http.StatusAccepted) |
| 377 | w.Header().Add("Content-Type", "application/json") |
| 378 | fmt.Fprintf(w, response) |
| 379 | }) |
| 380 | } |
| 381 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 382 | // HandleServerCreationWithCustomFieldSuccessfully sets up the test server to respond to a server creation request |
| 383 | // with a given response. |
| 384 | func HandleServerCreationWithCustomFieldSuccessfully(t *testing.T, response string) { |
| 385 | th.Mux.HandleFunc("/servers", func(w http.ResponseWriter, r *http.Request) { |
| 386 | th.TestMethod(t, r, "POST") |
| 387 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 388 | th.TestJSONRequest(t, r, `{ |
| 389 | "server": { |
| 390 | "name": "derp", |
| 391 | "imageRef": "f90f6034-2570-4974-8351-6b49732ef2eb", |
| 392 | "flavorRef": "1", |
| 393 | "foo": "bar" |
| 394 | } |
| 395 | }`) |
| 396 | |
| 397 | w.WriteHeader(http.StatusAccepted) |
| 398 | w.Header().Add("Content-Type", "application/json") |
| 399 | fmt.Fprintf(w, response) |
| 400 | }) |
| 401 | } |
| 402 | |
Ash Wilson | a70510a | 2014-10-23 11:54:03 -0400 | [diff] [blame] | 403 | // HandleServerListSuccessfully sets up the test server to respond to a server List request. |
| 404 | func HandleServerListSuccessfully(t *testing.T) { |
| 405 | th.Mux.HandleFunc("/servers/detail", func(w http.ResponseWriter, r *http.Request) { |
| 406 | th.TestMethod(t, r, "GET") |
| 407 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 408 | |
| 409 | w.Header().Add("Content-Type", "application/json") |
| 410 | r.ParseForm() |
| 411 | marker := r.Form.Get("marker") |
| 412 | switch marker { |
| 413 | case "": |
| 414 | fmt.Fprintf(w, ServerListBody) |
| 415 | case "9e5476bd-a4ec-4653-93d6-72c93aa682ba": |
| 416 | fmt.Fprintf(w, `{ "servers": [] }`) |
| 417 | default: |
| 418 | t.Fatalf("/servers/detail invoked with unexpected marker=[%s]", marker) |
| 419 | } |
| 420 | }) |
| 421 | } |
| 422 | |
Ash Wilson | 664fe33 | 2014-10-21 17:47:49 -0400 | [diff] [blame] | 423 | // HandleServerDeletionSuccessfully sets up the test server to respond to a server deletion request. |
| 424 | func HandleServerDeletionSuccessfully(t *testing.T) { |
| 425 | th.Mux.HandleFunc("/servers/asdfasdfasdf", func(w http.ResponseWriter, r *http.Request) { |
| 426 | th.TestMethod(t, r, "DELETE") |
| 427 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 428 | |
| 429 | w.WriteHeader(http.StatusNoContent) |
| 430 | }) |
| 431 | } |
Ash Wilson | 1c1eb88 | 2014-10-21 18:14:31 -0400 | [diff] [blame] | 432 | |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame] | 433 | // HandleServerForceDeletionSuccessfully sets up the test server to respond to a server password |
Ian Duffy | 370c430 | 2016-01-21 10:44:56 +0000 | [diff] [blame] | 434 | // change request. |
| 435 | func HandleServerForceDeletionSuccessfully(t *testing.T) { |
| 436 | th.Mux.HandleFunc("/servers/asdfasdfasdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 437 | th.TestMethod(t, r, "POST") |
| 438 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 439 | th.TestJSONRequest(t, r, `{ "forceDelete": "" }`) |
| 440 | |
| 441 | w.WriteHeader(http.StatusAccepted) |
| 442 | }) |
| 443 | } |
| 444 | |
Ash Wilson | 189c95c | 2014-10-23 11:41:35 -0400 | [diff] [blame] | 445 | // HandleServerGetSuccessfully sets up the test server to respond to a server Get request. |
| 446 | func HandleServerGetSuccessfully(t *testing.T) { |
| 447 | th.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) { |
| 448 | th.TestMethod(t, r, "GET") |
| 449 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 450 | th.TestHeader(t, r, "Accept", "application/json") |
| 451 | |
| 452 | fmt.Fprintf(w, SingleServerBody) |
| 453 | }) |
| 454 | } |
| 455 | |
| 456 | // HandleServerUpdateSuccessfully sets up the test server to respond to a server Update request. |
| 457 | func HandleServerUpdateSuccessfully(t *testing.T) { |
| 458 | th.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) { |
| 459 | th.TestMethod(t, r, "PUT") |
| 460 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 461 | th.TestHeader(t, r, "Accept", "application/json") |
| 462 | th.TestHeader(t, r, "Content-Type", "application/json") |
| 463 | th.TestJSONRequest(t, r, `{ "server": { "name": "new-name" } }`) |
| 464 | |
| 465 | fmt.Fprintf(w, SingleServerBody) |
| 466 | }) |
| 467 | } |
| 468 | |
Ash Wilson | 1c1eb88 | 2014-10-21 18:14:31 -0400 | [diff] [blame] | 469 | // HandleAdminPasswordChangeSuccessfully sets up the test server to respond to a server password |
| 470 | // change request. |
| 471 | func HandleAdminPasswordChangeSuccessfully(t *testing.T) { |
| 472 | th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 473 | th.TestMethod(t, r, "POST") |
| 474 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 475 | th.TestJSONRequest(t, r, `{ "changePassword": { "adminPass": "new-password" } }`) |
| 476 | |
| 477 | w.WriteHeader(http.StatusAccepted) |
| 478 | }) |
| 479 | } |
Ash Wilson | 2295ba5 | 2014-10-21 18:19:28 -0400 | [diff] [blame] | 480 | |
| 481 | // HandleRebootSuccessfully sets up the test server to respond to a reboot request with success. |
| 482 | func HandleRebootSuccessfully(t *testing.T) { |
| 483 | th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 484 | th.TestMethod(t, r, "POST") |
| 485 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 486 | th.TestJSONRequest(t, r, `{ "reboot": { "type": "SOFT" } }`) |
| 487 | |
| 488 | w.WriteHeader(http.StatusAccepted) |
| 489 | }) |
| 490 | } |
Ash Wilson | acf49c6 | 2014-10-21 18:25:11 -0400 | [diff] [blame] | 491 | |
| 492 | // HandleRebuildSuccessfully sets up the test server to respond to a rebuild request with success. |
| 493 | func HandleRebuildSuccessfully(t *testing.T, response string) { |
| 494 | th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 495 | th.TestMethod(t, r, "POST") |
| 496 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 497 | th.TestJSONRequest(t, r, ` |
| 498 | { |
| 499 | "rebuild": { |
| 500 | "name": "new-name", |
| 501 | "adminPass": "swordfish", |
| 502 | "imageRef": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb", |
| 503 | "accessIPv4": "1.2.3.4" |
| 504 | } |
| 505 | } |
| 506 | `) |
| 507 | |
| 508 | w.WriteHeader(http.StatusAccepted) |
| 509 | w.Header().Add("Content-Type", "application/json") |
| 510 | fmt.Fprintf(w, response) |
| 511 | }) |
| 512 | } |
Alex Gaynor | fa8fbeb | 2014-11-13 13:39:31 -0800 | [diff] [blame] | 513 | |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 514 | // HandleServerRescueSuccessfully sets up the test server to respond to a server Rescue request. |
Alex Gaynor | fa8fbeb | 2014-11-13 13:39:31 -0800 | [diff] [blame] | 515 | func HandleServerRescueSuccessfully(t *testing.T) { |
| 516 | th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 517 | th.TestMethod(t, r, "POST") |
| 518 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 519 | th.TestJSONRequest(t, r, `{ "rescue": { "adminPass": "1234567890" } }`) |
| 520 | |
| 521 | w.WriteHeader(http.StatusOK) |
| 522 | w.Write([]byte(`{ "adminPass": "1234567890" }`)) |
| 523 | }) |
| 524 | } |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 525 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 526 | // HandleMetadatumGetSuccessfully sets up the test server to respond to a metadatum Get request. |
| 527 | func HandleMetadatumGetSuccessfully(t *testing.T) { |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 528 | th.Mux.HandleFunc("/servers/1234asdf/metadata/foo", func(w http.ResponseWriter, r *http.Request) { |
| 529 | th.TestMethod(t, r, "GET") |
| 530 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 531 | th.TestHeader(t, r, "Accept", "application/json") |
| 532 | |
| 533 | w.WriteHeader(http.StatusOK) |
| 534 | w.Header().Add("Content-Type", "application/json") |
| 535 | w.Write([]byte(`{ "meta": {"foo":"bar"}}`)) |
| 536 | }) |
| 537 | } |
| 538 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 539 | // HandleMetadatumCreateSuccessfully sets up the test server to respond to a metadatum Create request. |
| 540 | func HandleMetadatumCreateSuccessfully(t *testing.T) { |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 541 | th.Mux.HandleFunc("/servers/1234asdf/metadata/foo", func(w http.ResponseWriter, r *http.Request) { |
| 542 | th.TestMethod(t, r, "PUT") |
| 543 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 544 | th.TestJSONRequest(t, r, `{ |
| 545 | "meta": { |
| 546 | "foo": "bar" |
| 547 | } |
| 548 | }`) |
| 549 | |
| 550 | w.WriteHeader(http.StatusOK) |
| 551 | w.Header().Add("Content-Type", "application/json") |
| 552 | w.Write([]byte(`{ "meta": {"foo":"bar"}}`)) |
| 553 | }) |
| 554 | } |
| 555 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 556 | // HandleMetadatumDeleteSuccessfully sets up the test server to respond to a metadatum Delete request. |
| 557 | func HandleMetadatumDeleteSuccessfully(t *testing.T) { |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 558 | th.Mux.HandleFunc("/servers/1234asdf/metadata/foo", func(w http.ResponseWriter, r *http.Request) { |
| 559 | th.TestMethod(t, r, "DELETE") |
| 560 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 561 | |
| 562 | w.WriteHeader(http.StatusNoContent) |
| 563 | }) |
| 564 | } |
| 565 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 566 | // HandleMetadataGetSuccessfully sets up the test server to respond to a metadata Get request. |
| 567 | func HandleMetadataGetSuccessfully(t *testing.T) { |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 568 | th.Mux.HandleFunc("/servers/1234asdf/metadata", func(w http.ResponseWriter, r *http.Request) { |
| 569 | th.TestMethod(t, r, "GET") |
| 570 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 571 | th.TestHeader(t, r, "Accept", "application/json") |
| 572 | |
| 573 | w.WriteHeader(http.StatusOK) |
| 574 | w.Write([]byte(`{ "metadata": {"foo":"bar", "this":"that"}}`)) |
| 575 | }) |
| 576 | } |
| 577 | |
Jon Perritt | 789f832 | 2014-11-21 08:20:04 -0700 | [diff] [blame] | 578 | // HandleMetadataResetSuccessfully sets up the test server to respond to a metadata Create request. |
| 579 | func HandleMetadataResetSuccessfully(t *testing.T) { |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 580 | th.Mux.HandleFunc("/servers/1234asdf/metadata", func(w http.ResponseWriter, r *http.Request) { |
| 581 | th.TestMethod(t, r, "PUT") |
| 582 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 583 | th.TestJSONRequest(t, r, `{ |
| 584 | "metadata": { |
| 585 | "foo": "bar", |
| 586 | "this": "that" |
| 587 | } |
| 588 | }`) |
| 589 | |
| 590 | w.WriteHeader(http.StatusOK) |
| 591 | w.Header().Add("Content-Type", "application/json") |
| 592 | w.Write([]byte(`{ "metadata": {"foo":"bar", "this":"that"}}`)) |
| 593 | }) |
| 594 | } |
| 595 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 596 | // HandleMetadataUpdateSuccessfully sets up the test server to respond to a metadata Update request. |
| 597 | func HandleMetadataUpdateSuccessfully(t *testing.T) { |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 598 | th.Mux.HandleFunc("/servers/1234asdf/metadata", 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, `{ |
| 602 | "metadata": { |
| 603 | "foo": "baz", |
| 604 | "this": "those" |
| 605 | } |
| 606 | }`) |
| 607 | |
| 608 | w.WriteHeader(http.StatusOK) |
| 609 | w.Header().Add("Content-Type", "application/json") |
| 610 | w.Write([]byte(`{ "metadata": {"foo":"baz", "this":"those"}}`)) |
| 611 | }) |
| 612 | } |
Jon Perritt | 5cb4948 | 2015-02-19 12:19:58 -0700 | [diff] [blame] | 613 | |
| 614 | // ListAddressesExpected represents an expected repsonse from a ListAddresses request. |
| 615 | var ListAddressesExpected = map[string][]Address{ |
| 616 | "public": []Address{ |
| 617 | Address{ |
| 618 | Version: 4, |
| 619 | Address: "80.56.136.39", |
| 620 | }, |
| 621 | Address{ |
| 622 | Version: 6, |
| 623 | Address: "2001:4800:790e:510:be76:4eff:fe04:82a8", |
| 624 | }, |
| 625 | }, |
| 626 | "private": []Address{ |
| 627 | Address{ |
| 628 | Version: 4, |
| 629 | Address: "10.880.3.154", |
| 630 | }, |
| 631 | }, |
| 632 | } |
| 633 | |
| 634 | // HandleAddressListSuccessfully sets up the test server to respond to a ListAddresses request. |
| 635 | func HandleAddressListSuccessfully(t *testing.T) { |
| 636 | th.Mux.HandleFunc("/servers/asdfasdfasdf/ips", func(w http.ResponseWriter, r *http.Request) { |
| 637 | th.TestMethod(t, r, "GET") |
| 638 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 639 | |
| 640 | w.Header().Add("Content-Type", "application/json") |
| 641 | fmt.Fprintf(w, `{ |
| 642 | "addresses": { |
| 643 | "public": [ |
| 644 | { |
| 645 | "version": 4, |
| 646 | "addr": "50.56.176.35" |
| 647 | }, |
| 648 | { |
| 649 | "version": 6, |
| 650 | "addr": "2001:4800:780e:510:be76:4eff:fe04:84a8" |
| 651 | } |
| 652 | ], |
| 653 | "private": [ |
| 654 | { |
| 655 | "version": 4, |
| 656 | "addr": "10.180.3.155" |
| 657 | } |
| 658 | ] |
| 659 | } |
| 660 | }`) |
| 661 | }) |
| 662 | } |
Jon Perritt | 04d073c | 2015-02-19 21:46:23 -0700 | [diff] [blame] | 663 | |
| 664 | // ListNetworkAddressesExpected represents an expected repsonse from a ListAddressesByNetwork request. |
Jon Perritt | b51ba9c | 2015-02-23 10:56:35 -0700 | [diff] [blame] | 665 | var ListNetworkAddressesExpected = []Address{ |
| 666 | Address{ |
| 667 | Version: 4, |
| 668 | Address: "50.56.176.35", |
| 669 | }, |
| 670 | Address{ |
| 671 | Version: 6, |
| 672 | Address: "2001:4800:780e:510:be76:4eff:fe04:84a8", |
Jon Perritt | 04d073c | 2015-02-19 21:46:23 -0700 | [diff] [blame] | 673 | }, |
| 674 | } |
| 675 | |
| 676 | // HandleNetworkAddressListSuccessfully sets up the test server to respond to a ListAddressesByNetwork request. |
| 677 | func HandleNetworkAddressListSuccessfully(t *testing.T) { |
| 678 | th.Mux.HandleFunc("/servers/asdfasdfasdf/ips/public", func(w http.ResponseWriter, r *http.Request) { |
| 679 | th.TestMethod(t, r, "GET") |
| 680 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 681 | |
| 682 | w.Header().Add("Content-Type", "application/json") |
| 683 | fmt.Fprintf(w, `{ |
| 684 | "public": [ |
| 685 | { |
| 686 | "version": 4, |
| 687 | "addr": "50.56.176.35" |
| 688 | }, |
| 689 | { |
| 690 | "version": 6, |
| 691 | "addr": "2001:4800:780e:510:be76:4eff:fe04:84a8" |
| 692 | } |
| 693 | ] |
| 694 | }`) |
| 695 | }) |
| 696 | } |
einarf | 2fc665e | 2015-04-16 20:16:21 +0000 | [diff] [blame] | 697 | |
| 698 | // HandleCreateServerImageSuccessfully sets up the test server to respond to a TestCreateServerImage request. |
| 699 | func HandleCreateServerImageSuccessfully(t *testing.T) { |
| 700 | th.Mux.HandleFunc("/servers/serverimage/action", func(w http.ResponseWriter, r *http.Request) { |
| 701 | th.TestMethod(t, r, "POST") |
| 702 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 703 | w.Header().Add("Location", "https://0.0.0.0/images/xxxx-xxxxx-xxxxx-xxxx") |
| 704 | w.WriteHeader(http.StatusAccepted) |
| 705 | }) |
| 706 | } |