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