1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
package stacks
import (
"fmt"
"net/http"
"net/url"
"strings"
"testing"
th "github.com/gophercloud/gophercloud/testhelper"
)
func TestTemplateValidation(t *testing.T) {
templateJSON := new(Template)
templateJSON.Bin = []byte(ValidJSONTemplate)
err := templateJSON.Validate()
th.AssertNoErr(t, err)
templateYAML := new(Template)
templateYAML.Bin = []byte(ValidYAMLTemplate)
err = templateYAML.Validate()
th.AssertNoErr(t, err)
templateInvalid := new(Template)
templateInvalid.Bin = []byte(InvalidTemplateNoVersion)
if err = templateInvalid.Validate(); err == nil {
t.Error("Template validation did not catch invalid template")
}
}
func TestTemplateParsing(t *testing.T) {
templateJSON := new(Template)
templateJSON.Bin = []byte(ValidJSONTemplate)
err := templateJSON.Parse()
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, ValidJSONTemplateParsed, templateJSON.Parsed)
templateYAML := new(Template)
templateYAML.Bin = []byte(ValidJSONTemplate)
err = templateYAML.Parse()
th.AssertNoErr(t, err)
th.AssertDeepEquals(t, ValidJSONTemplateParsed, templateYAML.Parsed)
templateInvalid := new(Template)
templateInvalid.Bin = []byte("Keep Austin Weird")
err = templateInvalid.Parse()
if err == nil {
t.Error("Template parsing did not catch invalid template")
}
}
func TestIgnoreIfTemplate(t *testing.T) {
var keyValueTests = []struct {
key string
value interface{}
out bool
}{
{"not_get_file", "afksdf", true},
{"not_type", "sdfd", true},
{"get_file", "shdfuisd", false},
{"type", "dfsdfsd", true},
{"type", "sdfubsduf.yaml", false},
{"type", "sdfsdufs.template", false},
{"type", "sdfsdf.file", true},
{"type", map[string]string{"key": "value"}, true},
}
var result bool
for _, kv := range keyValueTests {
result = ignoreIfTemplate(kv.key, kv.value)
if result != kv.out {
t.Errorf("key: %v, value: %v expected: %v, actual: %v", kv.key, kv.value, result, kv.out)
}
}
}
func TestGetFileContentsWithType(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
baseurl, err := getBasePath()
th.AssertNoErr(t, err)
fakeURL := strings.Join([]string{baseurl, "my_nova.yaml"}, "/")
urlparsed, err := url.Parse(fakeURL)
th.AssertNoErr(t, err)
myNovaContent := `heat_template_version: 2014-10-16
parameters:
flavor:
type: string
description: Flavor for the server to be created
default: 4353
hidden: true
resources:
test_server:
type: "OS::Nova::Server"
properties:
name: test-server
flavor: 2 GB General Purpose v1
image: Debian 7 (Wheezy) (PVHVM)
networks:
- {uuid: 11111111-1111-1111-1111-111111111111}`
th.Mux.HandleFunc(urlparsed.Path, func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, myNovaContent)
})
client := fakeClient{BaseClient: getHTTPClient()}
te := new(Template)
te.Bin = []byte(`heat_template_version: 2015-04-30
resources:
my_server:
type: my_nova.yaml`)
te.client = client
err = te.Parse()
th.AssertNoErr(t, err)
err = te.getFileContents(te.Parsed, ignoreIfTemplate, true)
th.AssertNoErr(t, err)
expectedFiles := map[string]string{
"my_nova.yaml": `heat_template_version: 2014-10-16
parameters:
flavor:
type: string
description: Flavor for the server to be created
default: 4353
hidden: true
resources:
test_server:
type: "OS::Nova::Server"
properties:
name: test-server
flavor: 2 GB General Purpose v1
image: Debian 7 (Wheezy) (PVHVM)
networks:
- {uuid: 11111111-1111-1111-1111-111111111111}`}
th.AssertEquals(t, expectedFiles["my_nova.yaml"], te.Files[fakeURL])
te.fixFileRefs()
expectedParsed := map[string]interface{}{
"heat_template_version": "2015-04-30",
"resources": map[string]interface{}{
"my_server": map[string]interface{}{
"type": fakeURL,
},
},
}
te.Parse()
th.AssertDeepEquals(t, expectedParsed, te.Parsed)
}
func TestGetFileContentsWithFile(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
baseurl, err := getBasePath()
th.AssertNoErr(t, err)
fakeURL := strings.Join([]string{baseurl, "somefile"}, "/")
urlparsed, err := url.Parse(fakeURL)
th.AssertNoErr(t, err)
somefile := `Welcome!`
th.Mux.HandleFunc(urlparsed.Path, func(w http.ResponseWriter, r *http.Request) {
th.TestMethod(t, r, "GET")
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, somefile)
})
client := fakeClient{BaseClient: getHTTPClient()}
te := new(Template)
te.Bin = []byte(`heat_template_version: 2015-04-30
resources:
test_resource:
type: OS::Heat::TestResource
properties:
value: {get_file: somefile }`)
te.client = client
err = te.Parse()
th.AssertNoErr(t, err)
err = te.getFileContents(te.Parsed, ignoreIfTemplate, true)
th.AssertNoErr(t, err)
expectedFiles := map[string]string{
"somefile": "Welcome!",
}
th.AssertEquals(t, expectedFiles["somefile"], te.Files[fakeURL])
te.fixFileRefs()
expectedParsed := map[string]interface{}{
"heat_template_version": "2015-04-30",
"resources": map[string]interface{}{
"test_resource": map[string]interface{}{
"type": "OS::Heat::TestResource",
"properties": map[string]interface{}{
"value": map[string]interface{}{
"get_file": fakeURL,
},
},
},
},
}
te.Parse()
th.AssertDeepEquals(t, expectedParsed, te.Parsed)
}
|