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