blob: 5f8a4305c545589c4a35d0ad11abfc0e67e4acc6 [file] [log] [blame]
Pratik Mallya5fddb2a2015-09-14 14:04:49 -05001package stacks
2
3import (
4 "fmt"
5 "net/http"
6 "net/url"
7 "strings"
8 "testing"
9
Jon Perritt27249f42016-02-18 10:35:59 -060010 th "github.com/gophercloud/gophercloud/testhelper"
Pratik Mallya5fddb2a2015-09-14 14:04:49 -050011)
12
13func TestEnvironmentValidation(t *testing.T) {
14 environmentJSON := new(Environment)
15 environmentJSON.Bin = []byte(ValidJSONEnvironment)
16 err := environmentJSON.Validate()
17 th.AssertNoErr(t, err)
18
19 environmentYAML := new(Environment)
20 environmentYAML.Bin = []byte(ValidYAMLEnvironment)
21 err = environmentYAML.Validate()
22 th.AssertNoErr(t, err)
23
24 environmentInvalid := new(Environment)
25 environmentInvalid.Bin = []byte(InvalidEnvironment)
26 if err = environmentInvalid.Validate(); err == nil {
27 t.Error("environment validation did not catch invalid environment")
28 }
29}
30
31func TestEnvironmentParsing(t *testing.T) {
32 environmentJSON := new(Environment)
33 environmentJSON.Bin = []byte(ValidJSONEnvironment)
34 err := environmentJSON.Parse()
35 th.AssertNoErr(t, err)
36 th.AssertDeepEquals(t, ValidJSONEnvironmentParsed, environmentJSON.Parsed)
37
38 environmentYAML := new(Environment)
39 environmentYAML.Bin = []byte(ValidJSONEnvironment)
40 err = environmentYAML.Parse()
41 th.AssertNoErr(t, err)
42 th.AssertDeepEquals(t, ValidJSONEnvironmentParsed, environmentYAML.Parsed)
43
44 environmentInvalid := new(Environment)
45 environmentInvalid.Bin = []byte("Keep Austin Weird")
46 err = environmentInvalid.Parse()
47 if err == nil {
48 t.Error("environment parsing did not catch invalid environment")
49 }
50}
51
52func TestIgnoreIfEnvironment(t *testing.T) {
53 var keyValueTests = []struct {
54 key string
55 value interface{}
56 out bool
57 }{
58 {"base_url", "afksdf", true},
59 {"not_type", "hooks", false},
60 {"get_file", "::", true},
61 {"hooks", "dfsdfsd", true},
62 {"type", "sdfubsduf.yaml", false},
63 {"type", "sdfsdufs.environment", false},
64 {"type", "sdfsdf.file", false},
65 {"type", map[string]string{"key": "value"}, true},
66 }
67 var result bool
68 for _, kv := range keyValueTests {
69 result = ignoreIfEnvironment(kv.key, kv.value)
70 if result != kv.out {
71 t.Errorf("key: %v, value: %v expected: %v, actual: %v", kv.key, kv.value, kv.out, result)
72 }
73 }
74}
75
76func TestGetRRFileContents(t *testing.T) {
77 th.SetupHTTP()
78 defer th.TeardownHTTP()
Pratik Mallya3de347f2015-09-22 12:25:59 -050079 environmentContent := `
Pratik Mallya5fddb2a2015-09-14 14:04:49 -050080heat_template_version: 2013-05-23
81
82description:
83 Heat WordPress template to support F18, using only Heat OpenStack-native
84 resource types, and without the requirement for heat-cfntools in the image.
85 WordPress is web software you can use to create a beautiful website or blog.
86 This template installs a single-instance WordPress deployment using a local
87 MySQL database to store the data.
88
89parameters:
90
91 key_name:
92 type: string
93 description : Name of a KeyPair to enable SSH access to the instance
94
95resources:
96 wordpress_instance:
97 type: OS::Nova::Server
98 properties:
99 image: { get_param: image_id }
100 flavor: { get_param: instance_type }
101 key_name: { get_param: key_name }`
102
Pratik Mallya3de347f2015-09-22 12:25:59 -0500103 dbContent := `
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500104heat_template_version: 2014-10-16
105
106description:
107 Test template for Trove resource capabilities
108
109parameters:
110 db_pass:
111 type: string
112 hidden: true
113 description: Database access password
114 default: secrete
115
116resources:
117
118service_db:
119 type: OS::Trove::Instance
120 properties:
121 name: trove_test_db
122 datastore_type: mariadb
123 flavor: 1GB Instance
124 size: 10
125 databases:
126 - name: test_data
127 users:
128 - name: kitchen_sink
129 password: { get_param: db_pass }
130 databases: [ test_data ]`
131 baseurl, err := getBasePath()
132 th.AssertNoErr(t, err)
133
134 fakeEnvURL := strings.Join([]string{baseurl, "my_env.yaml"}, "/")
135 urlparsed, err := url.Parse(fakeEnvURL)
136 th.AssertNoErr(t, err)
137 // handler for my_env.yaml
138 th.Mux.HandleFunc(urlparsed.Path, func(w http.ResponseWriter, r *http.Request) {
139 th.TestMethod(t, r, "GET")
140 w.Header().Set("Content-Type", "application/jason")
141 w.WriteHeader(http.StatusOK)
Pratik Mallya3de347f2015-09-22 12:25:59 -0500142 fmt.Fprintf(w, environmentContent)
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500143 })
144
145 fakeDBURL := strings.Join([]string{baseurl, "my_db.yaml"}, "/")
146 urlparsed, err = url.Parse(fakeDBURL)
147 th.AssertNoErr(t, err)
148
149 // handler for my_db.yaml
150 th.Mux.HandleFunc(urlparsed.Path, func(w http.ResponseWriter, r *http.Request) {
151 th.TestMethod(t, r, "GET")
152 w.Header().Set("Content-Type", "application/jason")
153 w.WriteHeader(http.StatusOK)
Pratik Mallya3de347f2015-09-22 12:25:59 -0500154 fmt.Fprintf(w, dbContent)
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500155 })
156
157 client := fakeClient{BaseClient: getHTTPClient()}
158 env := new(Environment)
159 env.Bin = []byte(`{"resource_registry": {"My::WP::Server": "my_env.yaml", "resources": {"my_db_server": {"OS::DBInstance": "my_db.yaml"}}}}`)
160 env.client = client
161
162 err = env.Parse()
163 th.AssertNoErr(t, err)
Pratik Mallyaa979f5b2015-09-22 03:10:55 -0500164 err = env.getRRFileContents(ignoreIfEnvironment)
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500165 th.AssertNoErr(t, err)
Pratik Mallya3de347f2015-09-22 12:25:59 -0500166 expectedEnvFilesContent := "\nheat_template_version: 2013-05-23\n\ndescription:\n Heat WordPress template to support F18, using only Heat OpenStack-native\n resource types, and without the requirement for heat-cfntools in the image.\n WordPress is web software you can use to create a beautiful website or blog.\n This template installs a single-instance WordPress deployment using a local\n MySQL database to store the data.\n\nparameters:\n\n key_name:\n type: string\n description : Name of a KeyPair to enable SSH access to the instance\n\nresources:\n wordpress_instance:\n type: OS::Nova::Server\n properties:\n image: { get_param: image_id }\n flavor: { get_param: instance_type }\n key_name: { get_param: key_name }"
167 expectedDBFilesContent := "\nheat_template_version: 2014-10-16\n\ndescription:\n Test template for Trove resource capabilities\n\nparameters:\n db_pass:\n type: string\n hidden: true\n description: Database access password\n default: secrete\n\nresources:\n\nservice_db:\n type: OS::Trove::Instance\n properties:\n name: trove_test_db\n datastore_type: mariadb\n flavor: 1GB Instance\n size: 10\n databases:\n - name: test_data\n users:\n - name: kitchen_sink\n password: { get_param: db_pass }\n databases: [ test_data ]"
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500168
Pratik Mallya3de347f2015-09-22 12:25:59 -0500169 th.AssertEquals(t, expectedEnvFilesContent, env.Files[fakeEnvURL])
170 th.AssertEquals(t, expectedDBFilesContent, env.Files[fakeDBURL])
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500171
Pratik Mallyaa979f5b2015-09-22 03:10:55 -0500172 env.fixFileRefs()
Pratik Mallya3de347f2015-09-22 12:25:59 -0500173 expectedParsed := map[string]interface{}{
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500174 "resource_registry": "2015-04-30",
175 "My::WP::Server": fakeEnvURL,
176 "resources": map[string]interface{}{
177 "my_db_server": map[string]interface{}{
178 "OS::DBInstance": fakeDBURL,
179 },
180 },
181 }
182 env.Parse()
Pratik Mallya3de347f2015-09-22 12:25:59 -0500183 th.AssertDeepEquals(t, expectedParsed, env.Parsed)
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500184}