@@ -8,13 +8,10 @@ import (
88 "sync"
99 "testing"
1010
11- "github.com/docker/infrakit/pkg/log"
1211 "github.com/docker/infrakit/pkg/types"
1312 "github.com/stretchr/testify/require"
1413)
1514
16- var logger = log .New ("module" , "template" )
17-
1815func TestTemplateInclusionFromDifferentSources (t * testing.T ) {
1916 prefix := testSetupTemplates (t , testFiles )
2017
@@ -88,11 +85,11 @@ echo "this is common/setup.sh"
8885 "test" : "test1",
8986 "description" : "simple template to test the various template functions",
9087 {{/* Load from from ./ using relative path notation. Then split into lines and json encode */}}
91- "userData" : {{ include "script.tpl" . | lines | to_json }},
88+ "userData" : {{ include "script.tpl" . | lines | jsonEncode }},
9289 {{/* Load from an URL */}}
9390 "sample" : {{ include "https://httpbin.org/get" }},
9491 {{/* Load from URL and then parse as JSON then select an attribute */}}
95- "originIp" : "{{ include "https://httpbin.org/get" | from_json | q "origin" }}"
92+ "originIp" : "{{ include "https://httpbin.org/get" | jsonDecode | q "origin" }}"
9693}` ,
9794
9895 "plugin/script.tpl" : `
@@ -164,7 +161,7 @@ The message is {{str}}
164161}
165162
166163func TestMissingGlobal (t * testing.T ) {
167- s := `{{ if not (ref "/not/exist")}}none{{else}}here{{end}}`
164+ s := `{{ if not (var "/not/exist")}}none{{else}}here{{end}}`
168165 tt , err := NewTemplate ("str://" + s , Options {})
169166 require .NoError (t , err )
170167 view , err := tt .Render (nil )
@@ -173,28 +170,18 @@ func TestMissingGlobal(t *testing.T) {
173170}
174171
175172func TestSourceAndDef (t * testing.T ) {
176- r := `{{ def \"foo\" 100 }}`
177- s := `{{ source "str://` + r + `" }}foo={{ref "foo"}}`
173+ r := `{{ var \"foo\" 100 }}`
174+ s := `{{ source "str://` + r + `" }}foo={{var "foo"}}`
178175 tt , err := NewTemplate ("str://" + s , Options {})
179176 require .NoError (t , err )
180177 view , err := tt .Render (nil )
181178 require .NoError (t , err )
182179 require .Equal (t , "foo=100" , view )
183180}
184181
185- func TestAddDef (t * testing.T ) {
186- s := `{{ ref "message" }}: x + y = {{ add (ref "x") (ref "y") }}`
187- tt , err := NewTemplate ("str://" + s , Options {})
188- require .NoError (t , err )
189-
190- view , err := tt .Def ("x" , 25 , "Default value for x" ).Def ("y" , 100 , "no doc" ).Def ("message" , "hello" , "" ).Render (nil )
191- require .NoError (t , err )
192- require .Equal (t , "hello: x + y = 125" , view )
193- }
194-
195182func TestSourceAndGlobal (t * testing.T ) {
196- r := `{{ global \"foo\" 100 }}`
197- s := `{{ source "str://` + r + `" }}foo={{ref "foo"}}`
183+ r := `{{ var \"foo\" 100 }}`
184+ s := `{{ source "str://` + r + `" }}foo={{var "foo"}}`
198185 tt , err := NewTemplate ("str://" + s , Options {})
199186 require .NoError (t , err )
200187 view , err := tt .Render (nil )
@@ -203,8 +190,8 @@ func TestSourceAndGlobal(t *testing.T) {
203190}
204191
205192func TestIncludeAndGlobal (t * testing.T ) {
206- r := `{{ global \"foo\" 100 }}` // the child template tries to mutate the global
207- s := `{{ include "str://` + r + `" }}foo={{ref "foo"}}`
193+ r := `{{ var \"foo\" 100 }}` // the child template tries to mutate the global
194+ s := `{{ include "str://` + r + `" }}foo={{var "foo"}}`
208195 tt , err := NewTemplate ("str://" + s , Options {})
209196 require .NoError (t , err )
210197 tt .Global ("foo" , 200 ) // set the global of the calling / parent template
@@ -218,7 +205,7 @@ func TestSourceAndGlobalWithContext(t *testing.T) {
218205 "a" : 1 ,
219206 "b" : 2 ,
220207 }
221- r := `{{ global \"foo\" 100 }}{{$void := set . \"a\" 100}}` // sourced mutates the context
208+ r := `{{ var \"foo\" 100 }}{{$void := set . \"a\" 100}}` // sourced mutates the context
222209 s := `{{ source "str://` + r + `" }}a={{.a}}`
223210 tt , err := NewTemplate ("str://" + s , Options {})
224211 require .NoError (t , err )
@@ -232,7 +219,7 @@ func TestIncludeAndGlobalWithContext(t *testing.T) {
232219 "a" : 1 ,
233220 "b" : 2 ,
234221 }
235- r := `{{ global \"foo\" 100 }}{{$void := set . \"a\" 100}}` // included tries to mutate the context
222+ r := `{{ var \"foo\" 100 }}{{$void := set . \"a\" 100}}` // included tries to mutate the context
236223 s := `{{ include "str://` + r + `" }}a={{.a}}`
237224 tt , err := NewTemplate ("str://" + s , Options {})
238225 require .NoError (t , err )
@@ -264,27 +251,27 @@ func TestWithFunctions(t *testing.T) {
264251func TestSourceWithHeaders (t * testing.T ) {
265252
266253 h , context := headersAndContext ("foo=bar" )
267- logger .Info ("result" , "context" , context , "headers" , h )
254+ log .Info ("result" , "context" , context , "headers" , h )
268255 require .Equal (t , interface {}(nil ), context )
269256 require .Equal (t , map [string ][]string {"foo" : {"bar" }}, h )
270257
271258 h , context = headersAndContext ("foo=bar" , "bar=baz" , 224 )
272- logger .Info ("result" , "context" , context , "headers" , h )
259+ log .Info ("result" , "context" , context , "headers" , h )
273260 require .Equal (t , 224 , context )
274261 require .Equal (t , map [string ][]string {"foo" : {"bar" }, "bar" : {"baz" }}, h )
275262
276263 h , context = headersAndContext ("foo=bar" , "bar=baz" )
277- logger .Info ("result" , "context" , context , "headers" , h )
264+ log .Info ("result" , "context" , context , "headers" , h )
278265 require .Equal (t , nil , context )
279266 require .Equal (t , map [string ][]string {"foo" : {"bar" }, "bar" : {"baz" }}, h )
280267
281268 h , context = headersAndContext ("foo" )
282- logger .Info ("result" , "context" , context , "headers" , h )
269+ log .Info ("result" , "context" , context , "headers" , h )
283270 require .Equal (t , "foo" , context )
284271 require .Equal (t , map [string ][]string {}, h )
285272
286273 h , context = headersAndContext ("foo=bar" , map [string ]string {"hello" : "world" })
287- logger .Info ("result" , "context" , context , "headers" , h )
274+ log .Info ("result" , "context" , context , "headers" , h )
288275 require .Equal (t , map [string ]string {"hello" : "world" }, context )
289276 require .Equal (t , map [string ][]string {"foo" : {"bar" }}, h )
290277
0 commit comments