github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/rts/v1/stacks/template_test.go (about) 1 package stacks 2 3 import ( 4 "fmt" 5 "net/http" 6 "net/url" 7 "strings" 8 "testing" 9 10 th "github.com/huaweicloud/golangsdk/testhelper" 11 ) 12 13 func 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 31 func 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 52 func 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 76 func 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) 84 myNovaContent := `heat_template_version: 2014-10-16 85 parameters: 86 flavor: 87 type: string 88 description: Flavor for the server to be created 89 default: 4353 90 hidden: true 91 resources: 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/json") 103 w.WriteHeader(http.StatusOK) 104 fmt.Fprintf(w, myNovaContent) 105 }) 106 107 client := fakeClient{BaseClient: getHTTPClient()} 108 te := new(Template) 109 te.Bin = []byte(`heat_template_version: 2015-04-30 110 resources: 111 my_server: 112 type: my_nova.yaml`) 113 te.client = client 114 115 err = te.Parse() 116 th.AssertNoErr(t, err) 117 err = te.getFileContents(te.Parsed, ignoreIfTemplate, true) 118 th.AssertNoErr(t, err) 119 expectedFiles := map[string]string{ 120 "my_nova.yaml": `heat_template_version: 2014-10-16 121 parameters: 122 flavor: 123 type: string 124 description: Flavor for the server to be created 125 default: 4353 126 hidden: true 127 resources: 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}`} 136 th.AssertEquals(t, expectedFiles["my_nova.yaml"], te.Files[fakeURL]) 137 te.fixFileRefs() 138 expectedParsed := map[string]interface{}{ 139 "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() 147 th.AssertDeepEquals(t, expectedParsed, te.Parsed) 148 }