@@ -8,13 +8,10 @@ import (
8
8
"sync"
9
9
"testing"
10
10
11
- "github.com/docker/infrakit/pkg/log"
12
11
"github.com/docker/infrakit/pkg/types"
13
12
"github.com/stretchr/testify/require"
14
13
)
15
14
16
- var logger = log .New ("module" , "template" )
17
-
18
15
func TestTemplateInclusionFromDifferentSources (t * testing.T ) {
19
16
prefix := testSetupTemplates (t , testFiles )
20
17
@@ -88,11 +85,11 @@ echo "this is common/setup.sh"
88
85
"test" : "test1",
89
86
"description" : "simple template to test the various template functions",
90
87
{{/* 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 }},
92
89
{{/* Load from an URL */}}
93
90
"sample" : {{ include "https://httpbin.org/get" }},
94
91
{{/* 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" }}"
96
93
}` ,
97
94
98
95
"plugin/script.tpl" : `
@@ -164,7 +161,7 @@ The message is {{str}}
164
161
}
165
162
166
163
func 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}}`
168
165
tt , err := NewTemplate ("str://" + s , Options {})
169
166
require .NoError (t , err )
170
167
view , err := tt .Render (nil )
@@ -173,28 +170,18 @@ func TestMissingGlobal(t *testing.T) {
173
170
}
174
171
175
172
func 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"}}`
178
175
tt , err := NewTemplate ("str://" + s , Options {})
179
176
require .NoError (t , err )
180
177
view , err := tt .Render (nil )
181
178
require .NoError (t , err )
182
179
require .Equal (t , "foo=100" , view )
183
180
}
184
181
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
-
195
182
func 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"}}`
198
185
tt , err := NewTemplate ("str://" + s , Options {})
199
186
require .NoError (t , err )
200
187
view , err := tt .Render (nil )
@@ -203,8 +190,8 @@ func TestSourceAndGlobal(t *testing.T) {
203
190
}
204
191
205
192
func 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"}}`
208
195
tt , err := NewTemplate ("str://" + s , Options {})
209
196
require .NoError (t , err )
210
197
tt .Global ("foo" , 200 ) // set the global of the calling / parent template
@@ -218,7 +205,7 @@ func TestSourceAndGlobalWithContext(t *testing.T) {
218
205
"a" : 1 ,
219
206
"b" : 2 ,
220
207
}
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
222
209
s := `{{ source "str://` + r + `" }}a={{.a}}`
223
210
tt , err := NewTemplate ("str://" + s , Options {})
224
211
require .NoError (t , err )
@@ -232,7 +219,7 @@ func TestIncludeAndGlobalWithContext(t *testing.T) {
232
219
"a" : 1 ,
233
220
"b" : 2 ,
234
221
}
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
236
223
s := `{{ include "str://` + r + `" }}a={{.a}}`
237
224
tt , err := NewTemplate ("str://" + s , Options {})
238
225
require .NoError (t , err )
@@ -264,27 +251,27 @@ func TestWithFunctions(t *testing.T) {
264
251
func TestSourceWithHeaders (t * testing.T ) {
265
252
266
253
h , context := headersAndContext ("foo=bar" )
267
- logger .Info ("result" , "context" , context , "headers" , h )
254
+ log .Info ("result" , "context" , context , "headers" , h )
268
255
require .Equal (t , interface {}(nil ), context )
269
256
require .Equal (t , map [string ][]string {"foo" : {"bar" }}, h )
270
257
271
258
h , context = headersAndContext ("foo=bar" , "bar=baz" , 224 )
272
- logger .Info ("result" , "context" , context , "headers" , h )
259
+ log .Info ("result" , "context" , context , "headers" , h )
273
260
require .Equal (t , 224 , context )
274
261
require .Equal (t , map [string ][]string {"foo" : {"bar" }, "bar" : {"baz" }}, h )
275
262
276
263
h , context = headersAndContext ("foo=bar" , "bar=baz" )
277
- logger .Info ("result" , "context" , context , "headers" , h )
264
+ log .Info ("result" , "context" , context , "headers" , h )
278
265
require .Equal (t , nil , context )
279
266
require .Equal (t , map [string ][]string {"foo" : {"bar" }, "bar" : {"baz" }}, h )
280
267
281
268
h , context = headersAndContext ("foo" )
282
- logger .Info ("result" , "context" , context , "headers" , h )
269
+ log .Info ("result" , "context" , context , "headers" , h )
283
270
require .Equal (t , "foo" , context )
284
271
require .Equal (t , map [string ][]string {}, h )
285
272
286
273
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 )
288
275
require .Equal (t , map [string ]string {"hello" : "world" }, context )
289
276
require .Equal (t , map [string ][]string {"foo" : {"bar" }}, h )
290
277
0 commit comments