blob: a1fb3931299b18e72b2ebc7b946da4fd489f7e7e [file] [log] [blame]
Jon Perritt034ef072015-02-03 13:14:44 -07001package stacks
2
3import (
4 "testing"
5
6 os "github.com/rackspace/gophercloud/openstack/orchestration/v1/stacks"
Jon Perritt2870e042015-02-04 15:50:08 -07007 "github.com/rackspace/gophercloud/pagination"
Jon Perritt034ef072015-02-03 13:14:44 -07008 th "github.com/rackspace/gophercloud/testhelper"
9 fake "github.com/rackspace/gophercloud/testhelper/client"
10)
11
12func TestCreateStack(t *testing.T) {
13 th.SetupHTTP()
14 defer th.TeardownHTTP()
15 os.HandleCreateSuccessfully(t, CreateOutput)
16
17 createOpts := os.CreateOpts{
18 Name: "stackcreated",
19 Timeout: 60,
20 Template: `{
21 "outputs": {
22 "db_host": {
23 "value": {
24 "get_attr": [
25 "db",
26 "hostname"
27 ]
28 }
29 }
30 },
31 "heat_template_version": "2014-10-16",
32 "description": "HEAT template for creating a Cloud Database.\n",
33 "parameters": {
34 "db_name": {
35 "default": "wordpress",
36 "type": "string",
37 "description": "the name for the database",
38 "constraints": [
39 {
40 "length": {
41 "max": 64,
42 "min": 1
43 },
44 "description": "must be between 1 and 64 characters"
45 },
46 {
47 "allowed_pattern": "[a-zA-Z][a-zA-Z0-9]*",
48 "description": "must begin with a letter and contain only alphanumeric characters."
49 }
50 ]
51 },
52 "db_instance_name": {
53 "default": "Cloud_DB",
54 "type": "string",
55 "description": "the database instance name"
56 },
57 "db_username": {
58 "default": "admin",
59 "hidden": true,
60 "type": "string",
61 "description": "database admin account username",
62 "constraints": [
63 {
64 "length": {
65 "max": 16,
66 "min": 1
67 },
68 "description": "must be between 1 and 16 characters"
69 },
70 {
71 "allowed_pattern": "[a-zA-Z][a-zA-Z0-9]*",
72 "description": "must begin with a letter and contain only alphanumeric characters."
73 }
74 ]
75 },
76 "db_volume_size": {
77 "default": 30,
78 "type": "number",
79 "description": "database volume size (in GB)",
80 "constraints": [
81 {
82 "range": {
83 "max": 1024,
84 "min": 1
85 },
86 "description": "must be between 1 and 1024 GB"
87 }
88 ]
89 },
90 "db_flavor": {
91 "default": "1GB Instance",
92 "type": "string",
93 "description": "database instance size",
94 "constraints": [
95 {
96 "description": "must be a valid cloud database flavor",
97 "allowed_values": [
98 "1GB Instance",
99 "2GB Instance",
100 "4GB Instance",
101 "8GB Instance",
102 "16GB Instance"
103 ]
104 }
105 ]
106 },
107 "db_password": {
108 "default": "admin",
109 "hidden": true,
110 "type": "string",
111 "description": "database admin account password",
112 "constraints": [
113 {
114 "length": {
115 "max": 41,
116 "min": 1
117 },
118 "description": "must be between 1 and 14 characters"
119 },
120 {
121 "allowed_pattern": "[a-zA-Z0-9]*",
122 "description": "must contain only alphanumeric characters."
123 }
124 ]
125 }
126 },
127 "resources": {
128 "db": {
129 "type": "OS::Trove::Instance",
130 "properties": {
131 "flavor": {
132 "get_param": "db_flavor"
133 },
134 "size": {
135 "get_param": "db_volume_size"
136 },
137 "users": [
138 {
139 "password": {
140 "get_param": "db_password"
141 },
142 "name": {
143 "get_param": "db_username"
144 },
145 "databases": [
146 {
147 "get_param": "db_name"
148 }
149 ]
150 }
151 ],
152 "name": {
153 "get_param": "db_instance_name"
154 },
155 "databases": [
156 {
157 "name": {
158 "get_param": "db_name"
159 }
160 }
161 ]
162 }
163 }
164 }
165 }`,
166 DisableRollback: os.Disable,
167 }
168 actual, err := Create(fake.ServiceClient(), createOpts).Extract()
169 th.AssertNoErr(t, err)
170
171 expected := CreateExpected
172 th.AssertDeepEquals(t, expected, actual)
173}
Jon Perritt1d3bc4b2015-02-04 12:06:19 -0700174
175func TestAdoptStack(t *testing.T) {
176 th.SetupHTTP()
177 defer th.TeardownHTTP()
178 os.HandleCreateSuccessfully(t, CreateOutput)
179
180 adoptOpts := os.AdoptOpts{
181 AdoptStackData: `{\"environment\":{\"parameters\":{}}, \"status\":\"COMPLETE\",\"name\": \"trovestack\",\n \"template\": {\n \"outputs\": {\n \"db_host\": {\n \"value\": {\n \"get_attr\": [\n \"db\",\n \"hostname\"\n ]\n }\n }\n },\n \"heat_template_version\": \"2014-10-16\",\n \"description\": \"HEAT template for creating a Cloud Database.\\n\",\n \"parameters\": {\n \"db_instance_name\": {\n \"default\": \"Cloud_DB\",\n \"type\": \"string\",\n \"description\": \"the database instance name\"\n },\n \"db_flavor\": {\n \"default\": \"1GB Instance\",\n \"type\": \"string\",\n \"description\": \"database instance size\",\n \"constraints\": [\n {\n \"description\": \"must be a valid cloud database flavor\",\n \"allowed_values\": [\n \"1GB Instance\",\n \"2GB Instance\",\n \"4GB Instance\",\n \"8GB Instance\",\n \"16GB Instance\"\n ]\n }\n ]\n },\n \"db_password\": {\n \"default\": \"admin\",\n \"hidden\": true,\n \"type\": \"string\",\n \"description\": \"database admin account password\",\n \"constraints\": [\n {\n \"length\": {\n \"max\": 41,\n \"min\": 1\n },\n \"description\": \"must be between 1 and 14 characters\"\n },\n {\n \"allowed_pattern\": \"[a-zA-Z0-9]*\",\n \"description\": \"must contain only alphanumeric characters.\"\n }\n ]\n },\n \"db_name\": {\n \"default\": \"wordpress\",\n \"type\": \"string\",\n \"description\": \"the name for the database\",\n \"constraints\": [\n {\n \"length\": {\n \"max\": 64,\n \"min\": 1\n },\n \"description\": \"must be between 1 and 64 characters\"\n },\n {\n \"allowed_pattern\": \"[a-zA-Z][a-zA-Z0-9]*\",\n \"description\": \"must begin with a letter and contain only alphanumeric characters.\"\n }\n ]\n },\n \"db_username\": {\n \"default\": \"admin\",\n \"hidden\": true,\n \"type\": \"string\",\n \"description\": \"database admin account username\",\n \"constraints\": [\n {\n \"length\": {\n \"max\": 16,\n \"min\": 1\n },\n \"description\": \"must be between 1 and 16 characters\"\n },\n {\n \"allowed_pattern\": \"[a-zA-Z][a-zA-Z0-9]*\",\n \"description\": \"must begin with a letter and contain only alphanumeric characters.\"\n }\n ]\n },\n \"db_volume_size\": {\n \"default\": 30,\n \"type\": \"number\",\n \"description\": \"database volume size (in GB)\",\n \"constraints\": [\n {\n \"range\": {\n \"max\": 1024,\n \"min\": 1\n },\n \"description\": \"must be between 1 and 1024 GB\"\n }\n ]\n }\n },\n \"resources\": {\n \"db\": {\n \"type\": \"OS::Trove::Instance\",\n \"properties\": {\n \"flavor\": {\n \"get_param\": \"db_flavor\"\n },\n \"databases\": [\n {\n \"name\": {\n \"get_param\": \"db_name\"\n }\n }\n ],\n \"users\": [\n {\n \"password\": {\n \"get_param\": \"db_password\"\n },\n \"name\": {\n \"get_param\": \"db_username\"\n },\n \"databases\": [\n {\n \"get_param\": \"db_name\"\n }\n ]\n }\n ],\n \"name\": {\n \"get_param\": \"db_instance_name\"\n },\n \"size\": {\n \"get_param\": \"db_volume_size\"\n }\n }\n }\n }\n },\n \"action\": \"CREATE\",\n \"id\": \"exxxxd-7xx5-4xxb-bxx2-cxxxxxx5\",\n \"resources\": {\n \"db\": {\n \"status\": \"COMPLETE\",\n \"name\": \"db\",\n \"resource_data\": {},\n \"resource_id\": \"exxxx2-9xx0-4xxxb-bxx2-dxxxxxx4\",\n \"action\": \"CREATE\",\n \"type\": \"OS::Trove::Instance\",\n \"metadata\": {}\n }\n }\n},`,
182 Name: "stackadopted",
183 Timeout: 60,
184 Template: `{
185 "outputs": {
186 "db_host": {
187 "value": {
188 "get_attr": [
189 "db",
190 "hostname"
191 ]
192 }
193 }
194 },
195 "heat_template_version": "2014-10-16",
196 "description": "HEAT template for creating a Cloud Database.\n",
197 "parameters": {
198 "db_name": {
199 "default": "wordpress",
200 "type": "string",
201 "description": "the name for the database",
202 "constraints": [
203 {
204 "length": {
205 "max": 64,
206 "min": 1
207 },
208 "description": "must be between 1 and 64 characters"
209 },
210 {
211 "allowed_pattern": "[a-zA-Z][a-zA-Z0-9]*",
212 "description": "must begin with a letter and contain only alphanumeric characters."
213 }
214 ]
215 },
216 "db_instance_name": {
217 "default": "Cloud_DB",
218 "type": "string",
219 "description": "the database instance name"
220 },
221 "db_username": {
222 "default": "admin",
223 "hidden": true,
224 "type": "string",
225 "description": "database admin account username",
226 "constraints": [
227 {
228 "length": {
229 "max": 16,
230 "min": 1
231 },
232 "description": "must be between 1 and 16 characters"
233 },
234 {
235 "allowed_pattern": "[a-zA-Z][a-zA-Z0-9]*",
236 "description": "must begin with a letter and contain only alphanumeric characters."
237 }
238 ]
239 },
240 "db_volume_size": {
241 "default": 30,
242 "type": "number",
243 "description": "database volume size (in GB)",
244 "constraints": [
245 {
246 "range": {
247 "max": 1024,
248 "min": 1
249 },
250 "description": "must be between 1 and 1024 GB"
251 }
252 ]
253 },
254 "db_flavor": {
255 "default": "1GB Instance",
256 "type": "string",
257 "description": "database instance size",
258 "constraints": [
259 {
260 "description": "must be a valid cloud database flavor",
261 "allowed_values": [
262 "1GB Instance",
263 "2GB Instance",
264 "4GB Instance",
265 "8GB Instance",
266 "16GB Instance"
267 ]
268 }
269 ]
270 },
271 "db_password": {
272 "default": "admin",
273 "hidden": true,
274 "type": "string",
275 "description": "database admin account password",
276 "constraints": [
277 {
278 "length": {
279 "max": 41,
280 "min": 1
281 },
282 "description": "must be between 1 and 14 characters"
283 },
284 {
285 "allowed_pattern": "[a-zA-Z0-9]*",
286 "description": "must contain only alphanumeric characters."
287 }
288 ]
289 }
290 },
291 "resources": {
292 "db": {
293 "type": "OS::Trove::Instance",
294 "properties": {
295 "flavor": {
296 "get_param": "db_flavor"
297 },
298 "size": {
299 "get_param": "db_volume_size"
300 },
301 "users": [
302 {
303 "password": {
304 "get_param": "db_password"
305 },
306 "name": {
307 "get_param": "db_username"
308 },
309 "databases": [
310 {
311 "get_param": "db_name"
312 }
313 ]
314 }
315 ],
316 "name": {
317 "get_param": "db_instance_name"
318 },
319 "databases": [
320 {
321 "name": {
322 "get_param": "db_name"
323 }
324 }
325 ]
326 }
327 }
328 }
329 }`,
330 DisableRollback: os.Disable,
331 }
332 actual, err := Adopt(fake.ServiceClient(), adoptOpts).Extract()
333 th.AssertNoErr(t, err)
334
335 expected := CreateExpected
336 th.AssertDeepEquals(t, expected, actual)
337}
Jon Perritt2870e042015-02-04 15:50:08 -0700338
339func TestListStack(t *testing.T) {
340 th.SetupHTTP()
341 defer th.TeardownHTTP()
342 os.HandleListSuccessfully(t, os.FullListOutput)
343
344 count := 0
345 err := List(fake.ServiceClient(), nil).EachPage(func(page pagination.Page) (bool, error) {
346 count++
347 actual, err := os.ExtractStacks(page)
348 th.AssertNoErr(t, err)
349
350 th.CheckDeepEquals(t, os.ListExpected, actual)
351
352 return true, nil
353 })
354 th.AssertNoErr(t, err)
355 th.CheckEquals(t, count, 1)
356}
Jon Perritta6346d92015-02-04 17:54:52 -0700357
358func TestUpdateStack(t *testing.T) {
359 th.SetupHTTP()
360 defer th.TeardownHTTP()
361 os.HandleUpdateSuccessfully(t)
362
363 updateOpts := os.UpdateOpts{
364 Template: `
365 {
366 "heat_template_version": "2013-05-23",
367 "description": "Simple template to test heat commands",
368 "parameters": {
369 "flavor": {
370 "default": "m1.tiny",
371 "type": "string"
372 }
373 },
374 "resources": {
375 "hello_world": {
376 "type":"OS::Nova::Server",
377 "properties": {
378 "key_name": "heat_key",
379 "flavor": {
380 "get_param": "flavor"
381 },
382 "image": "ad091b52-742f-469e-8f3c-fd81cadf0743",
383 "user_data": "#!/bin/bash -xv\necho \"hello world\" > /root/hello-world.txt\n"
384 }
385 }
386 }
387 }`,
388 }
389 err := Update(fake.ServiceClient(), "gophercloud-test-stack-2", "db6977b2-27aa-4775-9ae7-6213212d4ada", updateOpts).ExtractErr()
390 th.AssertNoErr(t, err)
391}
Jon Perritt27a0b3e2015-02-04 18:04:35 -0700392
393func TestDeleteStack(t *testing.T) {
394 th.SetupHTTP()
395 defer th.TeardownHTTP()
396 os.HandleDeleteSuccessfully(t)
397
398 err := Delete(fake.ServiceClient(), "gophercloud-test-stack-2", "db6977b2-27aa-4775-9ae7-6213212d4ada").ExtractErr()
399 th.AssertNoErr(t, err)
400}
Jon Perritt68c27172015-02-04 18:55:38 -0700401
402func TestPreviewStack(t *testing.T) {
403 th.SetupHTTP()
404 defer th.TeardownHTTP()
405 os.HandlePreviewSuccessfully(t, os.GetOutput)
406
407 previewOpts := os.PreviewOpts{
408 Name: "stackcreated",
409 Timeout: 60,
410 Template: `
411 {
412 "stack_name": "postman_stack",
413 "template": {
414 "heat_template_version": "2013-05-23",
415 "description": "Simple template to test heat commands",
416 "parameters": {
417 "flavor": {
418 "default": "m1.tiny",
419 "type": "string"
420 }
421 },
422 "resources": {
423 "hello_world": {
424 "type":"OS::Nova::Server",
425 "properties": {
426 "key_name": "heat_key",
427 "flavor": {
428 "get_param": "flavor"
429 },
430 "image": "ad091b52-742f-469e-8f3c-fd81cadf0743",
431 "user_data": "#!/bin/bash -xv\necho \"hello world\" > /root/hello-world.txt\n"
432 }
433 }
434 }
435 }
436 }`,
437 DisableRollback: os.Disable,
438 }
439 actual, err := Preview(fake.ServiceClient(), previewOpts).Extract()
440 th.AssertNoErr(t, err)
441
442 expected := os.PreviewExpected
443 th.AssertDeepEquals(t, expected, actual)
444}
Jon Perritt9b81b8c2015-02-05 12:55:53 -0700445
Jon Perritt714d1b12015-02-07 13:18:49 -0700446/*
Jon Perritt9b81b8c2015-02-05 12:55:53 -0700447func TestAbandonStack(t *testing.T) {
448 th.SetupHTTP()
449 defer th.TeardownHTTP()
450 os.HandleAbandonSuccessfully(t)
451
452 //actual, err := Abandon(fake.ServiceClient(), "postman_stack", "16ef0584-4458-41eb-87c8-0dc8d5f66c87").Extract()
453 //th.AssertNoErr(t, err)
454 res := Abandon(fake.ServiceClient(), "postman_stack", "16ef0584-4458-41eb-87c8-0dc8d5f66c87") //.Extract()
455 th.AssertNoErr(t, res.Err)
456 t.Logf("actual: %+v", res)
457
458 //expected := os.AbandonExpected
459 //th.AssertDeepEquals(t, expected, actual)
460}
Jon Perritt714d1b12015-02-07 13:18:49 -0700461*/