blob: 6d3e9597a21b36631cd4ec532ceee27c8dcdc458 [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
281// AbandonExpected represents the expected object from an Abandon request.
282var AbandonExpected = &AbandonedStack{
283 Status: "COMPLETE",
284 Name: "postman_stack",
Jon Perritt6ec27cf2015-02-09 12:51:41 -0700285 Template: map[string]interface{}{
286 "heat_template_version": "2013-05-23",
287 "description": "Simple template to test heat commands",
288 "parameters": map[string]interface{}{
289 "flavor": map[string]interface{}{
290 "default": "m1.tiny",
291 "type": "string",
292 },
293 },
294 "resources": map[string]interface{}{
295 "hello_world": map[string]interface{}{
296 "type": "OS::Nova::Server",
297 "properties": map[string]interface{}{
298 "key_name": "heat_key",
299 "flavor": map[string]interface{}{
300 "get_param": "flavor",
301 },
302 "image": "ad091b52-742f-469e-8f3c-fd81cadf0743",
303 "user_data": "#!/bin/bash -xv\necho \"hello world\" > /root/hello-world.txt\n",
304 },
305 },
306 },
307 },
Jon Perritt9209df42015-02-05 12:55:33 -0700308 Action: "CREATE",
309 ID: "16ef0584-4458-41eb-87c8-0dc8d5f66c87",
310 Resources: map[string]interface{}{
311 "hello_world": map[string]interface{}{
312 "status": "COMPLETE",
313 "name": "hello_world",
314 "resource_id": "8a310d36-46fc-436f-8be4-37a696b8ac63",
315 "action": "CREATE",
316 "type": "OS::Nova::Server",
317 },
318 },
319}
320
321// AbandonOutput represents the response body from an Abandon request.
322const AbandonOutput = `
323{
324 "status": "COMPLETE",
325 "name": "postman_stack",
326 "template": {
327 "heat_template_version": "2013-05-23",
328 "description": "Simple template to test heat commands",
329 "parameters": {
330 "flavor": {
331 "default": "m1.tiny",
332 "type": "string"
333 }
334 },
335 "resources": {
336 "hello_world": {
337 "type": "OS::Nova::Server",
338 "properties": {
339 "key_name": "heat_key",
340 "flavor": {
341 "get_param": "flavor"
342 },
343 "image": "ad091b52-742f-469e-8f3c-fd81cadf0743",
344 "user_data": "#!/bin/bash -xv\necho \"hello world\" > /root/hello-world.txt\n"
345 }
346 }
347 }
348 },
349 "action": "CREATE",
350 "id": "16ef0584-4458-41eb-87c8-0dc8d5f66c87",
351 "resources": {
352 "hello_world": {
353 "status": "COMPLETE",
354 "name": "hello_world",
355 "resource_id": "8a310d36-46fc-436f-8be4-37a696b8ac63",
356 "action": "CREATE",
357 "type": "OS::Nova::Server",
358 }
359 }
360}`
361
362// HandleAbandonSuccessfully creates an HTTP handler at `/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87/abandon`
363// on the test handler mux that responds with an `Abandon` response.
364func HandleAbandonSuccessfully(t *testing.T) {
365 th.Mux.HandleFunc("/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87/abandon", func(w http.ResponseWriter, r *http.Request) {
366 th.TestMethod(t, r, "DELETE")
367 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
368 th.TestHeader(t, r, "Accept", "application/json")
369
370 w.Header().Set("Content-Type", "application/json")
371 w.WriteHeader(http.StatusOK)
372 fmt.Fprintf(w, AbandonOutput)
373 })
374}