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 | |
| 10 | th "github.com/rackspace/gophercloud/testhelper" |
| 11 | ) |
| 12 | |
| 13 | func 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 | |
| 31 | func 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 | |
| 52 | func 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 | |
| 76 | func TestGetRRFileContents(t *testing.T) { |
| 77 | th.SetupHTTP() |
| 78 | defer th.TeardownHTTP() |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 79 | environmentContent := ` |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 80 | heat_template_version: 2013-05-23 |
| 81 | |
| 82 | description: |
| 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 | |
| 89 | parameters: |
| 90 | |
| 91 | key_name: |
| 92 | type: string |
| 93 | description : Name of a KeyPair to enable SSH access to the instance |
| 94 | |
| 95 | resources: |
| 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 Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 103 | dbContent := ` |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 104 | heat_template_version: 2014-10-16 |
| 105 | |
| 106 | description: |
| 107 | Test template for Trove resource capabilities |
| 108 | |
| 109 | parameters: |
| 110 | db_pass: |
| 111 | type: string |
| 112 | hidden: true |
| 113 | description: Database access password |
| 114 | default: secrete |
| 115 | |
| 116 | resources: |
| 117 | |
| 118 | service_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 Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 142 | fmt.Fprintf(w, environmentContent) |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 143 | }) |
| 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 Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 154 | fmt.Fprintf(w, dbContent) |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 155 | }) |
| 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 Mallya | a979f5b | 2015-09-22 03:10:55 -0500 | [diff] [blame] | 164 | err = env.getRRFileContents(ignoreIfEnvironment) |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 165 | th.AssertNoErr(t, err) |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 166 | 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 Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 168 | |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 169 | th.AssertEquals(t, expectedEnvFilesContent, env.Files[fakeEnvURL]) |
| 170 | th.AssertEquals(t, expectedDBFilesContent, env.Files[fakeDBURL]) |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 171 | |
Pratik Mallya | a979f5b | 2015-09-22 03:10:55 -0500 | [diff] [blame] | 172 | env.fixFileRefs() |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 173 | expectedParsed := map[string]interface{}{ |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 174 | "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 Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 183 | th.AssertDeepEquals(t, expectedParsed, env.Parsed) |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 184 | } |