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
|
package stacks
import (
"fmt"
"net/http"
"net/url"
"strings"
"testing"
th "github.com/rackspace/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 TestGetFileContents(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/jason")
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)
}
|