blob: 6884db860e0c63614e6426a5a43892a776c8ea10 [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
10 th "github.com/rackspace/gophercloud/testhelper"
11)
12
13func TestTemplateValidation(t *testing.T) {
14 templateJSON := new(Template)
15 templateJSON.Bin = []byte(ValidJSONTemplate)
16 err := templateJSON.Validate()
17 th.AssertNoErr(t, err)
18
19 templateYAML := new(Template)
20 templateYAML.Bin = []byte(ValidYAMLTemplate)
21 err = templateYAML.Validate()
22 th.AssertNoErr(t, err)
23
24 templateInvalid := new(Template)
25 templateInvalid.Bin = []byte(InvalidTemplateNoVersion)
26 if err = templateInvalid.Validate(); err == nil {
27 t.Error("Template validation did not catch invalid template")
28 }
29}
30
31func TestTemplateParsing(t *testing.T) {
32 templateJSON := new(Template)
33 templateJSON.Bin = []byte(ValidJSONTemplate)
34 err := templateJSON.Parse()
35 th.AssertNoErr(t, err)
36 th.AssertDeepEquals(t, ValidJSONTemplateParsed, templateJSON.Parsed)
37
38 templateYAML := new(Template)
39 templateYAML.Bin = []byte(ValidJSONTemplate)
40 err = templateYAML.Parse()
41 th.AssertNoErr(t, err)
42 th.AssertDeepEquals(t, ValidJSONTemplateParsed, templateYAML.Parsed)
43
44 templateInvalid := new(Template)
45 templateInvalid.Bin = []byte("Keep Austin Weird")
46 err = templateInvalid.Parse()
47 if err == nil {
48 t.Error("Template parsing did not catch invalid template")
49 }
50}
51
52func TestIgnoreIfTemplate(t *testing.T) {
53 var keyValueTests = []struct {
54 key string
55 value interface{}
56 out bool
57 }{
58 {"not_get_file", "afksdf", true},
59 {"not_type", "sdfd", true},
60 {"get_file", "shdfuisd", false},
61 {"type", "dfsdfsd", true},
62 {"type", "sdfubsduf.yaml", false},
63 {"type", "sdfsdufs.template", false},
64 {"type", "sdfsdf.file", true},
65 {"type", map[string]string{"key": "value"}, true},
66 }
67 var result bool
68 for _, kv := range keyValueTests {
69 result = ignoreIfTemplate(kv.key, kv.value)
70 if result != kv.out {
71 t.Errorf("key: %v, value: %v expected: %v, actual: %v", kv.key, kv.value, result, kv.out)
72 }
73 }
74}
75
76func TestGetFileContents(t *testing.T) {
77 th.SetupHTTP()
78 defer th.TeardownHTTP()
79 baseurl, err := getBasePath()
80 th.AssertNoErr(t, err)
81 fakeURL := strings.Join([]string{baseurl, "my_nova.yaml"}, "/")
82 urlparsed, err := url.Parse(fakeURL)
83 th.AssertNoErr(t, err)
Pratik Mallya3de347f2015-09-22 12:25:59 -050084 myNovaContent := `heat_template_version: 2014-10-16
Pratik Mallya5fddb2a2015-09-14 14:04:49 -050085parameters:
86 flavor:
87 type: string
88 description: Flavor for the server to be created
89 default: 4353
90 hidden: true
91resources:
92 test_server:
93 type: "OS::Nova::Server"
94 properties:
95 name: test-server
96 flavor: 2 GB General Purpose v1
97 image: Debian 7 (Wheezy) (PVHVM)
98 networks:
99 - {uuid: 11111111-1111-1111-1111-111111111111}`
100 th.Mux.HandleFunc(urlparsed.Path, func(w http.ResponseWriter, r *http.Request) {
101 th.TestMethod(t, r, "GET")
102 w.Header().Set("Content-Type", "application/jason")
103 w.WriteHeader(http.StatusOK)
Pratik Mallya3de347f2015-09-22 12:25:59 -0500104 fmt.Fprintf(w, myNovaContent)
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500105 })
106
107 client := fakeClient{BaseClient: getHTTPClient()}
108 te := new(Template)
109 te.Bin = []byte(`heat_template_version: 2015-04-30
110resources:
111 my_server:
112 type: my_nova.yaml`)
113 te.client = client
114
115 err = te.Parse()
116 th.AssertNoErr(t, err)
Pratik Mallyaa979f5b2015-09-22 03:10:55 -0500117 err = te.getFileContents(te.Parsed, ignoreIfTemplate, true)
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500118 th.AssertNoErr(t, err)
Pratik Mallya3de347f2015-09-22 12:25:59 -0500119 expectedFiles := map[string]string{
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500120 "my_nova.yaml": `heat_template_version: 2014-10-16
121parameters:
122 flavor:
123 type: string
124 description: Flavor for the server to be created
125 default: 4353
126 hidden: true
127resources:
128 test_server:
129 type: "OS::Nova::Server"
130 properties:
131 name: test-server
132 flavor: 2 GB General Purpose v1
133 image: Debian 7 (Wheezy) (PVHVM)
134 networks:
135 - {uuid: 11111111-1111-1111-1111-111111111111}`}
Pratik Mallya3de347f2015-09-22 12:25:59 -0500136 th.AssertEquals(t, expectedFiles["my_nova.yaml"], te.Files[fakeURL])
Pratik Mallyaa979f5b2015-09-22 03:10:55 -0500137 te.fixFileRefs()
Pratik Mallya3de347f2015-09-22 12:25:59 -0500138 expectedParsed := map[string]interface{}{
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500139 "heat_template_version": "2015-04-30",
140 "resources": map[string]interface{}{
141 "my_server": map[string]interface{}{
142 "type": fakeURL,
143 },
144 },
145 }
146 te.Parse()
Pratik Mallya3de347f2015-09-22 12:25:59 -0500147 th.AssertDeepEquals(t, expectedParsed, te.Parsed)
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500148}