jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 1 | package testing |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/gophercloud/gophercloud" |
| 10 | "github.com/gophercloud/gophercloud/openstack/orchestration/v1/stacks" |
| 11 | th "github.com/gophercloud/gophercloud/testhelper" |
| 12 | fake "github.com/gophercloud/gophercloud/testhelper/client" |
| 13 | ) |
| 14 | |
| 15 | // CreateExpected represents the expected object from a Create request. |
| 16 | var CreateExpected = &stacks.CreatedStack{ |
| 17 | ID: "16ef0584-4458-41eb-87c8-0dc8d5f66c87", |
| 18 | Links: []gophercloud.Link{ |
| 19 | { |
| 20 | Href: "http://168.28.170.117:8004/v1/98606384f58drad0bhdb7d02779549ac/stacks/stackcreated/16ef0584-4458-41eb-87c8-0dc8d5f66c87", |
| 21 | Rel: "self", |
| 22 | }, |
| 23 | }, |
| 24 | } |
| 25 | |
| 26 | // CreateOutput represents the response body from a Create request. |
| 27 | const CreateOutput = ` |
| 28 | { |
| 29 | "stack": { |
| 30 | "id": "16ef0584-4458-41eb-87c8-0dc8d5f66c87", |
| 31 | "links": [ |
| 32 | { |
| 33 | "href": "http://168.28.170.117:8004/v1/98606384f58drad0bhdb7d02779549ac/stacks/stackcreated/16ef0584-4458-41eb-87c8-0dc8d5f66c87", |
| 34 | "rel": "self" |
| 35 | } |
| 36 | ] |
| 37 | } |
| 38 | }` |
| 39 | |
| 40 | // HandleCreateSuccessfully creates an HTTP handler at `/stacks` on the test handler mux |
| 41 | // that responds with a `Create` response. |
| 42 | func HandleCreateSuccessfully(t *testing.T, output string) { |
| 43 | th.Mux.HandleFunc("/stacks", func(w http.ResponseWriter, r *http.Request) { |
| 44 | th.TestMethod(t, r, "POST") |
| 45 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 46 | th.TestHeader(t, r, "Accept", "application/json") |
| 47 | w.WriteHeader(http.StatusCreated) |
| 48 | fmt.Fprintf(w, output) |
| 49 | }) |
| 50 | } |
| 51 | |
| 52 | // ListExpected represents the expected object from a List request. |
| 53 | var ListExpected = []stacks.ListedStack{ |
| 54 | { |
| 55 | Description: "Simple template to test heat commands", |
| 56 | Links: []gophercloud.Link{ |
| 57 | { |
| 58 | Href: "http://166.76.160.117:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87", |
| 59 | Rel: "self", |
| 60 | }, |
| 61 | }, |
| 62 | StatusReason: "Stack CREATE completed successfully", |
| 63 | Name: "postman_stack", |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 64 | CreationTime: time.Date(2015, 2, 3, 20, 7, 39, 0, time.UTC), |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 65 | Status: "CREATE_COMPLETE", |
| 66 | ID: "16ef0584-4458-41eb-87c8-0dc8d5f66c87", |
| 67 | Tags: []string{"rackspace", "atx"}, |
| 68 | }, |
| 69 | { |
| 70 | Description: "Simple template to test heat commands", |
| 71 | Links: []gophercloud.Link{ |
| 72 | { |
| 73 | Href: "http://166.76.160.117:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/gophercloud-test-stack-2/db6977b2-27aa-4775-9ae7-6213212d4ada", |
| 74 | Rel: "self", |
| 75 | }, |
| 76 | }, |
| 77 | StatusReason: "Stack successfully updated", |
| 78 | Name: "gophercloud-test-stack-2", |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 79 | CreationTime: time.Date(2014, 12, 11, 17, 39, 16, 0, time.UTC), |
| 80 | UpdatedTime: time.Date(2014, 12, 11, 17, 40, 37, 0, time.UTC), |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 81 | Status: "UPDATE_COMPLETE", |
| 82 | ID: "db6977b2-27aa-4775-9ae7-6213212d4ada", |
| 83 | Tags: []string{"sfo", "satx"}, |
| 84 | }, |
| 85 | } |
| 86 | |
| 87 | // FullListOutput represents the response body from a List request without a marker. |
| 88 | const FullListOutput = ` |
| 89 | { |
| 90 | "stacks": [ |
| 91 | { |
| 92 | "description": "Simple template to test heat commands", |
| 93 | "links": [ |
| 94 | { |
| 95 | "href": "http://166.76.160.117:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87", |
| 96 | "rel": "self" |
| 97 | } |
| 98 | ], |
| 99 | "stack_status_reason": "Stack CREATE completed successfully", |
| 100 | "stack_name": "postman_stack", |
| 101 | "creation_time": "2015-02-03T20:07:39", |
| 102 | "updated_time": null, |
| 103 | "stack_status": "CREATE_COMPLETE", |
| 104 | "id": "16ef0584-4458-41eb-87c8-0dc8d5f66c87", |
| 105 | "tags": ["rackspace", "atx"] |
| 106 | }, |
| 107 | { |
| 108 | "description": "Simple template to test heat commands", |
| 109 | "links": [ |
| 110 | { |
| 111 | "href": "http://166.76.160.117:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/gophercloud-test-stack-2/db6977b2-27aa-4775-9ae7-6213212d4ada", |
| 112 | "rel": "self" |
| 113 | } |
| 114 | ], |
| 115 | "stack_status_reason": "Stack successfully updated", |
| 116 | "stack_name": "gophercloud-test-stack-2", |
| 117 | "creation_time": "2014-12-11T17:39:16", |
| 118 | "updated_time": "2014-12-11T17:40:37", |
| 119 | "stack_status": "UPDATE_COMPLETE", |
| 120 | "id": "db6977b2-27aa-4775-9ae7-6213212d4ada", |
| 121 | "tags": ["sfo", "satx"] |
| 122 | } |
| 123 | ] |
| 124 | } |
| 125 | ` |
| 126 | |
| 127 | // HandleListSuccessfully creates an HTTP handler at `/stacks` on the test handler mux |
| 128 | // that responds with a `List` response. |
| 129 | func HandleListSuccessfully(t *testing.T, output string) { |
| 130 | th.Mux.HandleFunc("/stacks", func(w http.ResponseWriter, r *http.Request) { |
| 131 | th.TestMethod(t, r, "GET") |
| 132 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 133 | th.TestHeader(t, r, "Accept", "application/json") |
| 134 | |
| 135 | w.Header().Set("Content-Type", "application/json") |
| 136 | r.ParseForm() |
| 137 | marker := r.Form.Get("marker") |
| 138 | switch marker { |
| 139 | case "": |
| 140 | fmt.Fprintf(w, output) |
| 141 | case "db6977b2-27aa-4775-9ae7-6213212d4ada": |
| 142 | fmt.Fprintf(w, `[]`) |
| 143 | default: |
| 144 | t.Fatalf("Unexpected marker: [%s]", marker) |
| 145 | } |
| 146 | }) |
| 147 | } |
| 148 | |
| 149 | // GetExpected represents the expected object from a Get request. |
| 150 | var GetExpected = &stacks.RetrievedStack{ |
| 151 | DisableRollback: true, |
| 152 | Description: "Simple template to test heat commands", |
| 153 | Parameters: map[string]string{ |
| 154 | "flavor": "m1.tiny", |
| 155 | "OS::stack_name": "postman_stack", |
| 156 | "OS::stack_id": "16ef0584-4458-41eb-87c8-0dc8d5f66c87", |
| 157 | }, |
| 158 | StatusReason: "Stack CREATE completed successfully", |
| 159 | Name: "postman_stack", |
| 160 | Outputs: []map[string]interface{}{}, |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 161 | CreationTime: time.Date(2015, 2, 3, 20, 7, 39, 0, time.UTC), |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 162 | Links: []gophercloud.Link{ |
| 163 | { |
| 164 | Href: "http://166.76.160.117:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87", |
| 165 | Rel: "self", |
| 166 | }, |
| 167 | }, |
| 168 | Capabilities: []interface{}{}, |
| 169 | NotificationTopics: []interface{}{}, |
| 170 | Status: "CREATE_COMPLETE", |
| 171 | ID: "16ef0584-4458-41eb-87c8-0dc8d5f66c87", |
| 172 | TemplateDescription: "Simple template to test heat commands", |
| 173 | Tags: []string{"rackspace", "atx"}, |
| 174 | } |
| 175 | |
| 176 | // GetOutput represents the response body from a Get request. |
| 177 | const GetOutput = ` |
| 178 | { |
| 179 | "stack": { |
| 180 | "disable_rollback": true, |
| 181 | "description": "Simple template to test heat commands", |
| 182 | "parameters": { |
| 183 | "flavor": "m1.tiny", |
| 184 | "OS::stack_name": "postman_stack", |
| 185 | "OS::stack_id": "16ef0584-4458-41eb-87c8-0dc8d5f66c87" |
| 186 | }, |
| 187 | "stack_status_reason": "Stack CREATE completed successfully", |
| 188 | "stack_name": "postman_stack", |
| 189 | "outputs": [], |
| 190 | "creation_time": "2015-02-03T20:07:39", |
| 191 | "links": [ |
| 192 | { |
| 193 | "href": "http://166.76.160.117:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87", |
| 194 | "rel": "self" |
| 195 | } |
| 196 | ], |
| 197 | "capabilities": [], |
| 198 | "notification_topics": [], |
| 199 | "timeout_mins": null, |
| 200 | "stack_status": "CREATE_COMPLETE", |
| 201 | "updated_time": null, |
| 202 | "id": "16ef0584-4458-41eb-87c8-0dc8d5f66c87", |
| 203 | "template_description": "Simple template to test heat commands", |
| 204 | "tags": ["rackspace", "atx"] |
| 205 | } |
| 206 | } |
| 207 | ` |
| 208 | |
| 209 | // HandleGetSuccessfully creates an HTTP handler at `/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87` |
| 210 | // on the test handler mux that responds with a `Get` response. |
| 211 | func HandleGetSuccessfully(t *testing.T, output string) { |
| 212 | th.Mux.HandleFunc("/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87", func(w http.ResponseWriter, r *http.Request) { |
| 213 | th.TestMethod(t, r, "GET") |
| 214 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 215 | th.TestHeader(t, r, "Accept", "application/json") |
| 216 | |
| 217 | w.Header().Set("Content-Type", "application/json") |
| 218 | w.WriteHeader(http.StatusOK) |
| 219 | fmt.Fprintf(w, output) |
| 220 | }) |
| 221 | } |
| 222 | |
| 223 | // HandleUpdateSuccessfully creates an HTTP handler at `/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87` |
| 224 | // on the test handler mux that responds with an `Update` response. |
| 225 | func HandleUpdateSuccessfully(t *testing.T) { |
| 226 | th.Mux.HandleFunc("/stacks/gophercloud-test-stack-2/db6977b2-27aa-4775-9ae7-6213212d4ada", func(w http.ResponseWriter, r *http.Request) { |
| 227 | th.TestMethod(t, r, "PUT") |
| 228 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 229 | th.TestHeader(t, r, "Accept", "application/json") |
| 230 | |
| 231 | w.Header().Set("Content-Type", "application/json") |
| 232 | w.WriteHeader(http.StatusAccepted) |
| 233 | }) |
| 234 | } |
| 235 | |
| 236 | // HandleDeleteSuccessfully creates an HTTP handler at `/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87` |
| 237 | // on the test handler mux that responds with a `Delete` response. |
| 238 | func HandleDeleteSuccessfully(t *testing.T) { |
| 239 | th.Mux.HandleFunc("/stacks/gophercloud-test-stack-2/db6977b2-27aa-4775-9ae7-6213212d4ada", func(w http.ResponseWriter, r *http.Request) { |
| 240 | th.TestMethod(t, r, "DELETE") |
| 241 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 242 | th.TestHeader(t, r, "Accept", "application/json") |
| 243 | |
| 244 | w.Header().Set("Content-Type", "application/json") |
| 245 | w.WriteHeader(http.StatusNoContent) |
| 246 | }) |
| 247 | } |
| 248 | |
| 249 | // GetExpected represents the expected object from a Get request. |
| 250 | var PreviewExpected = &stacks.PreviewedStack{ |
| 251 | DisableRollback: true, |
| 252 | Description: "Simple template to test heat commands", |
| 253 | Parameters: map[string]string{ |
| 254 | "flavor": "m1.tiny", |
| 255 | "OS::stack_name": "postman_stack", |
| 256 | "OS::stack_id": "16ef0584-4458-41eb-87c8-0dc8d5f66c87", |
| 257 | }, |
| 258 | Name: "postman_stack", |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 259 | CreationTime: time.Date(2015, 2, 3, 20, 7, 39, 0, time.UTC), |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 260 | Links: []gophercloud.Link{ |
| 261 | { |
| 262 | Href: "http://166.76.160.117:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87", |
| 263 | Rel: "self", |
| 264 | }, |
| 265 | }, |
| 266 | Capabilities: []interface{}{}, |
| 267 | NotificationTopics: []interface{}{}, |
| 268 | ID: "16ef0584-4458-41eb-87c8-0dc8d5f66c87", |
| 269 | TemplateDescription: "Simple template to test heat commands", |
| 270 | } |
| 271 | |
| 272 | // HandlePreviewSuccessfully creates an HTTP handler at `/stacks/preview` |
| 273 | // on the test handler mux that responds with a `Preview` response. |
| 274 | func HandlePreviewSuccessfully(t *testing.T, output string) { |
| 275 | th.Mux.HandleFunc("/stacks/preview", func(w http.ResponseWriter, r *http.Request) { |
| 276 | th.TestMethod(t, r, "POST") |
| 277 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 278 | th.TestHeader(t, r, "Accept", "application/json") |
| 279 | |
| 280 | w.Header().Set("Content-Type", "application/json") |
| 281 | w.WriteHeader(http.StatusOK) |
| 282 | fmt.Fprintf(w, output) |
| 283 | }) |
| 284 | } |
| 285 | |
| 286 | // AbandonExpected represents the expected object from an Abandon request. |
| 287 | var AbandonExpected = &stacks.AbandonedStack{ |
| 288 | Status: "COMPLETE", |
| 289 | Name: "postman_stack", |
| 290 | Template: map[string]interface{}{ |
| 291 | "heat_template_version": "2013-05-23", |
| 292 | "description": "Simple template to test heat commands", |
| 293 | "parameters": map[string]interface{}{ |
| 294 | "flavor": map[string]interface{}{ |
| 295 | "default": "m1.tiny", |
| 296 | "type": "string", |
| 297 | }, |
| 298 | }, |
| 299 | "resources": map[string]interface{}{ |
| 300 | "hello_world": map[string]interface{}{ |
| 301 | "type": "OS::Nova::Server", |
| 302 | "properties": map[string]interface{}{ |
| 303 | "key_name": "heat_key", |
| 304 | "flavor": map[string]interface{}{ |
| 305 | "get_param": "flavor", |
| 306 | }, |
| 307 | "image": "ad091b52-742f-469e-8f3c-fd81cadf0743", |
| 308 | "user_data": "#!/bin/bash -xv\necho \"hello world\" > /root/hello-world.txt\n", |
| 309 | }, |
| 310 | }, |
| 311 | }, |
| 312 | }, |
| 313 | Action: "CREATE", |
| 314 | ID: "16ef0584-4458-41eb-87c8-0dc8d5f66c87", |
| 315 | Resources: map[string]interface{}{ |
| 316 | "hello_world": map[string]interface{}{ |
| 317 | "status": "COMPLETE", |
| 318 | "name": "hello_world", |
| 319 | "resource_id": "8a310d36-46fc-436f-8be4-37a696b8ac63", |
| 320 | "action": "CREATE", |
| 321 | "type": "OS::Nova::Server", |
| 322 | }, |
| 323 | }, |
| 324 | Files: map[string]string{ |
| 325 | "file:///Users/prat8228/go/src/github.com/rackspace/rack/my_nova.yaml": "heat_template_version: 2014-10-16\nparameters:\n flavor:\n type: string\n description: Flavor for the server to be created\n default: 4353\n hidden: true\nresources:\n test_server:\n type: \"OS::Nova::Server\"\n properties:\n name: test-server\n flavor: 2 GB General Purpose v1\n image: Debian 7 (Wheezy) (PVHVM)\n", |
| 326 | }, |
| 327 | StackUserProjectID: "897686", |
| 328 | ProjectID: "897686", |
| 329 | Environment: map[string]interface{}{ |
| 330 | "encrypted_param_names": make([]map[string]interface{}, 0), |
| 331 | "parameter_defaults": make(map[string]interface{}), |
| 332 | "parameters": make(map[string]interface{}), |
| 333 | "resource_registry": map[string]interface{}{ |
| 334 | "file:///Users/prat8228/go/src/github.com/rackspace/rack/my_nova.yaml": "file:///Users/prat8228/go/src/github.com/rackspace/rack/my_nova.yaml", |
| 335 | "resources": make(map[string]interface{}), |
| 336 | }, |
| 337 | }, |
| 338 | } |
| 339 | |
| 340 | // AbandonOutput represents the response body from an Abandon request. |
| 341 | const AbandonOutput = ` |
| 342 | { |
| 343 | "status": "COMPLETE", |
| 344 | "name": "postman_stack", |
| 345 | "template": { |
| 346 | "heat_template_version": "2013-05-23", |
| 347 | "description": "Simple template to test heat commands", |
| 348 | "parameters": { |
| 349 | "flavor": { |
| 350 | "default": "m1.tiny", |
| 351 | "type": "string" |
| 352 | } |
| 353 | }, |
| 354 | "resources": { |
| 355 | "hello_world": { |
| 356 | "type": "OS::Nova::Server", |
| 357 | "properties": { |
| 358 | "key_name": "heat_key", |
| 359 | "flavor": { |
| 360 | "get_param": "flavor" |
| 361 | }, |
| 362 | "image": "ad091b52-742f-469e-8f3c-fd81cadf0743", |
| 363 | "user_data": "#!/bin/bash -xv\necho \"hello world\" > /root/hello-world.txt\n" |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | }, |
| 368 | "action": "CREATE", |
| 369 | "id": "16ef0584-4458-41eb-87c8-0dc8d5f66c87", |
| 370 | "resources": { |
| 371 | "hello_world": { |
| 372 | "status": "COMPLETE", |
| 373 | "name": "hello_world", |
| 374 | "resource_id": "8a310d36-46fc-436f-8be4-37a696b8ac63", |
| 375 | "action": "CREATE", |
| 376 | "type": "OS::Nova::Server" |
| 377 | } |
| 378 | }, |
| 379 | "files": { |
| 380 | "file:///Users/prat8228/go/src/github.com/rackspace/rack/my_nova.yaml": "heat_template_version: 2014-10-16\nparameters:\n flavor:\n type: string\n description: Flavor for the server to be created\n default: 4353\n hidden: true\nresources:\n test_server:\n type: \"OS::Nova::Server\"\n properties:\n name: test-server\n flavor: 2 GB General Purpose v1\n image: Debian 7 (Wheezy) (PVHVM)\n" |
| 381 | }, |
| 382 | "environment": { |
| 383 | "encrypted_param_names": [], |
| 384 | "parameter_defaults": {}, |
| 385 | "parameters": {}, |
| 386 | "resource_registry": { |
| 387 | "file:///Users/prat8228/go/src/github.com/rackspace/rack/my_nova.yaml": "file:///Users/prat8228/go/src/github.com/rackspace/rack/my_nova.yaml", |
| 388 | "resources": {} |
| 389 | } |
| 390 | }, |
| 391 | "stack_user_project_id": "897686", |
| 392 | "project_id": "897686" |
| 393 | }` |
| 394 | |
| 395 | // HandleAbandonSuccessfully creates an HTTP handler at `/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87/abandon` |
| 396 | // on the test handler mux that responds with an `Abandon` response. |
| 397 | func HandleAbandonSuccessfully(t *testing.T, output string) { |
| 398 | th.Mux.HandleFunc("/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c8/abandon", func(w http.ResponseWriter, r *http.Request) { |
| 399 | th.TestMethod(t, r, "DELETE") |
| 400 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 401 | th.TestHeader(t, r, "Accept", "application/json") |
| 402 | |
| 403 | w.Header().Set("Content-Type", "application/json") |
| 404 | w.WriteHeader(http.StatusOK) |
| 405 | fmt.Fprintf(w, output) |
| 406 | }) |
| 407 | } |