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 | |
| 10 | th "github.com/rackspace/gophercloud/testhelper" |
| 11 | "github.com/rackspace/gophercloud/testhelper/client" |
| 12 | ) |
| 13 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 14 | // ServerListBody contains the canned body of a servers.List response. |
| 15 | const 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. |
| 163 | const 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 | |
| 238 | var ( |
| 239 | // ServerHerp is a Server struct that should correspond to the first result in ServerListBody. |
| 240 | ServerHerp = Server{ |
| 241 | Status: "ACTIVE", |
| 242 | Updated: "2014-09-25T13:10:10Z", |
| 243 | HostID: "29d3c8c896a45aa4c34e52247875d7fefc3d94bbcc9f622b5d204362", |
| 244 | Addresses: map[string]interface{}{ |
| 245 | "private": []interface{}{ |
| 246 | map[string]interface{}{ |
| 247 | "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:7c:1b:2b", |
| 248 | "version": float64(4), |
| 249 | "addr": "10.0.0.32", |
| 250 | "OS-EXT-IPS:type": "fixed", |
| 251 | }, |
| 252 | }, |
| 253 | }, |
| 254 | Links: []interface{}{ |
| 255 | map[string]interface{}{ |
| 256 | "href": "http://104.130.131.164:8774/v2/fcad67a6189847c4aecfa3c81a05783b/servers/ef079b0c-e610-4dfb-b1aa-b49f07ac48e5", |
| 257 | "rel": "self", |
| 258 | }, |
| 259 | map[string]interface{}{ |
| 260 | "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/servers/ef079b0c-e610-4dfb-b1aa-b49f07ac48e5", |
| 261 | "rel": "bookmark", |
| 262 | }, |
| 263 | }, |
| 264 | Image: map[string]interface{}{ |
| 265 | "id": "f90f6034-2570-4974-8351-6b49732ef2eb", |
| 266 | "links": []interface{}{ |
| 267 | map[string]interface{}{ |
| 268 | "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb", |
| 269 | "rel": "bookmark", |
| 270 | }, |
| 271 | }, |
| 272 | }, |
| 273 | Flavor: map[string]interface{}{ |
| 274 | "id": "1", |
| 275 | "links": []interface{}{ |
| 276 | map[string]interface{}{ |
| 277 | "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/flavors/1", |
| 278 | "rel": "bookmark", |
| 279 | }, |
| 280 | }, |
| 281 | }, |
| 282 | ID: "ef079b0c-e610-4dfb-b1aa-b49f07ac48e5", |
| 283 | UserID: "9349aff8be7545ac9d2f1d00999a23cd", |
| 284 | Name: "herp", |
| 285 | Created: "2014-09-25T13:10:02Z", |
| 286 | TenantID: "fcad67a6189847c4aecfa3c81a05783b", |
| 287 | Metadata: map[string]interface{}{}, |
| 288 | } |
| 289 | |
| 290 | // ServerDerp is a Server struct that should correspond to the second server in ServerListBody. |
| 291 | ServerDerp = Server{ |
| 292 | Status: "ACTIVE", |
| 293 | Updated: "2014-09-25T13:04:49Z", |
| 294 | HostID: "29d3c8c896a45aa4c34e52247875d7fefc3d94bbcc9f622b5d204362", |
| 295 | Addresses: map[string]interface{}{ |
| 296 | "private": []interface{}{ |
| 297 | map[string]interface{}{ |
| 298 | "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:9e:89:be", |
| 299 | "version": float64(4), |
| 300 | "addr": "10.0.0.31", |
| 301 | "OS-EXT-IPS:type": "fixed", |
| 302 | }, |
| 303 | }, |
| 304 | }, |
| 305 | Links: []interface{}{ |
| 306 | map[string]interface{}{ |
| 307 | "href": "http://104.130.131.164:8774/v2/fcad67a6189847c4aecfa3c81a05783b/servers/9e5476bd-a4ec-4653-93d6-72c93aa682ba", |
| 308 | "rel": "self", |
| 309 | }, |
| 310 | map[string]interface{}{ |
| 311 | "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/servers/9e5476bd-a4ec-4653-93d6-72c93aa682ba", |
| 312 | "rel": "bookmark", |
| 313 | }, |
| 314 | }, |
| 315 | Image: map[string]interface{}{ |
| 316 | "id": "f90f6034-2570-4974-8351-6b49732ef2eb", |
| 317 | "links": []interface{}{ |
| 318 | map[string]interface{}{ |
| 319 | "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb", |
| 320 | "rel": "bookmark", |
| 321 | }, |
| 322 | }, |
| 323 | }, |
| 324 | Flavor: map[string]interface{}{ |
| 325 | "id": "1", |
| 326 | "links": []interface{}{ |
| 327 | map[string]interface{}{ |
| 328 | "href": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/flavors/1", |
| 329 | "rel": "bookmark", |
| 330 | }, |
| 331 | }, |
| 332 | }, |
| 333 | ID: "9e5476bd-a4ec-4653-93d6-72c93aa682ba", |
| 334 | UserID: "9349aff8be7545ac9d2f1d00999a23cd", |
| 335 | Name: "derp", |
| 336 | Created: "2014-09-25T13:04:41Z", |
| 337 | TenantID: "fcad67a6189847c4aecfa3c81a05783b", |
| 338 | Metadata: map[string]interface{}{}, |
| 339 | } |
| 340 | ) |
Ash Wilson | 664fe33 | 2014-10-21 17:47:49 -0400 | [diff] [blame] | 341 | |
| 342 | // HandleServerCreationSuccessfully sets up the test server to respond to a server creation request |
| 343 | // with a given response. |
| 344 | func HandleServerCreationSuccessfully(t *testing.T, response string) { |
| 345 | th.Mux.HandleFunc("/servers", func(w http.ResponseWriter, r *http.Request) { |
| 346 | th.TestMethod(t, r, "POST") |
| 347 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 348 | th.TestJSONRequest(t, r, `{ |
| 349 | "server": { |
| 350 | "name": "derp", |
| 351 | "imageRef": "f90f6034-2570-4974-8351-6b49732ef2eb", |
| 352 | "flavorRef": "1" |
| 353 | } |
| 354 | }`) |
| 355 | |
| 356 | w.WriteHeader(http.StatusAccepted) |
| 357 | w.Header().Add("Content-Type", "application/json") |
| 358 | fmt.Fprintf(w, response) |
| 359 | }) |
| 360 | } |
| 361 | |
| 362 | // HandleServerDeletionSuccessfully sets up the test server to respond to a server deletion request. |
| 363 | func HandleServerDeletionSuccessfully(t *testing.T) { |
| 364 | th.Mux.HandleFunc("/servers/asdfasdfasdf", func(w http.ResponseWriter, r *http.Request) { |
| 365 | th.TestMethod(t, r, "DELETE") |
| 366 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 367 | |
| 368 | w.WriteHeader(http.StatusNoContent) |
| 369 | }) |
| 370 | } |
Ash Wilson | 1c1eb88 | 2014-10-21 18:14:31 -0400 | [diff] [blame] | 371 | |
| 372 | // HandleAdminPasswordChangeSuccessfully sets up the test server to respond to a server password |
| 373 | // change request. |
| 374 | func HandleAdminPasswordChangeSuccessfully(t *testing.T) { |
| 375 | th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 376 | th.TestMethod(t, r, "POST") |
| 377 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 378 | th.TestJSONRequest(t, r, `{ "changePassword": { "adminPass": "new-password" } }`) |
| 379 | |
| 380 | w.WriteHeader(http.StatusAccepted) |
| 381 | }) |
| 382 | } |
Ash Wilson | 2295ba5 | 2014-10-21 18:19:28 -0400 | [diff] [blame] | 383 | |
| 384 | // HandleRebootSuccessfully sets up the test server to respond to a reboot request with success. |
| 385 | func HandleRebootSuccessfully(t *testing.T) { |
| 386 | th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 387 | th.TestMethod(t, r, "POST") |
| 388 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 389 | th.TestJSONRequest(t, r, `{ "reboot": { "type": "SOFT" } }`) |
| 390 | |
| 391 | w.WriteHeader(http.StatusAccepted) |
| 392 | }) |
| 393 | } |
Ash Wilson | acf49c6 | 2014-10-21 18:25:11 -0400 | [diff] [blame] | 394 | |
| 395 | // HandleRebuildSuccessfully sets up the test server to respond to a rebuild request with success. |
| 396 | func HandleRebuildSuccessfully(t *testing.T, response string) { |
| 397 | th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 398 | th.TestMethod(t, r, "POST") |
| 399 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 400 | th.TestJSONRequest(t, r, ` |
| 401 | { |
| 402 | "rebuild": { |
| 403 | "name": "new-name", |
| 404 | "adminPass": "swordfish", |
| 405 | "imageRef": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb", |
| 406 | "accessIPv4": "1.2.3.4" |
| 407 | } |
| 408 | } |
| 409 | `) |
| 410 | |
| 411 | w.WriteHeader(http.StatusAccepted) |
| 412 | w.Header().Add("Content-Type", "application/json") |
| 413 | fmt.Fprintf(w, response) |
| 414 | }) |
| 415 | } |