Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 1 | package stacks |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "net/url" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 10 | th "github.com/gophercloud/gophercloud/testhelper" |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 11 | ) |
| 12 | |
| 13 | func TestEnvironmentValidation(t *testing.T) { |
jrperritt | 3d96616 | 2016-06-06 14:08:54 -0500 | [diff] [blame] | 14 | |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 15 | 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 | |
| 32 | func 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 | |
| 53 | func 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 | |
| 77 | func TestGetRRFileContents(t *testing.T) { |
| 78 | th.SetupHTTP() |
| 79 | defer th.TeardownHTTP() |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 80 | environmentContent := ` |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 81 | heat_template_version: 2013-05-23 |
| 82 | |
| 83 | description: |
| 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 | |
| 90 | parameters: |
| 91 | |
| 92 | key_name: |
| 93 | type: string |
| 94 | description : Name of a KeyPair to enable SSH access to the instance |
| 95 | |
| 96 | resources: |
| 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 Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 104 | dbContent := ` |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 105 | heat_template_version: 2014-10-16 |
| 106 | |
| 107 | description: |
| 108 | Test template for Trove resource capabilities |
| 109 | |
| 110 | parameters: |
| 111 | db_pass: |
| 112 | type: string |
| 113 | hidden: true |
| 114 | description: Database access password |
| 115 | default: secrete |
| 116 | |
| 117 | resources: |
| 118 | |
| 119 | service_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 Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 143 | fmt.Fprintf(w, environmentContent) |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 144 | }) |
| 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 Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 155 | fmt.Fprintf(w, dbContent) |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 156 | }) |
| 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 Mallya | a979f5b | 2015-09-22 03:10:55 -0500 | [diff] [blame] | 165 | err = env.getRRFileContents(ignoreIfEnvironment) |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 166 | th.AssertNoErr(t, err) |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 167 | 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 Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 169 | |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 170 | th.AssertEquals(t, expectedEnvFilesContent, env.Files[fakeEnvURL]) |
| 171 | th.AssertEquals(t, expectedDBFilesContent, env.Files[fakeDBURL]) |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 172 | |
Pratik Mallya | a979f5b | 2015-09-22 03:10:55 -0500 | [diff] [blame] | 173 | env.fixFileRefs() |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 174 | expectedParsed := map[string]interface{}{ |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 175 | "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 Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 184 | th.AssertDeepEquals(t, expectedParsed, env.Parsed) |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 185 | } |