Repository navigation
Expand file tree
/
Copy pathtemplate_test.go
More file actions
507 lines (463 loc) · 13.3 KB
/
Copy pathtemplate_test.go
File metadata and controls
507 lines (463 loc) · 13.3 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
package gomplate
import (
"fmt"
"sync"
"testing"
"time"
"cel.dev/cel-go/cel"
_ "github.com/flanksource/gomplate/v4/js"
_ "github.com/robertkrimen/otto/underscore"
)
func TestCacheKeyConsistency(t *testing.T) {
var hello = func() any {
return "world"
}
var foo = func() any {
return "bar"
}
{
tt := Template{
Expression: "{{.name}}{{.age}}",
Functions: map[string]any{
"hello": hello,
"Hello": foo,
"foo": foo,
"Foo": foo,
},
}
expectedCacheKey := tt.cacheKey(map[string]any{"age": 19, "name": "james"})
for i := 0; i < 10; i++ {
key := tt.cacheKey(map[string]any{"age": 19, "name": "james"})
if key != expectedCacheKey {
t.Errorf("cache key mismatch: %s != %s", key, expectedCacheKey)
}
}
}
{
tt := Template{
Template: "{{.name}}{{.age}}",
LeftDelim: "{{",
RightDelim: "}}",
Functions: map[string]any{
"hello": hello,
"Hello": foo,
"foo": foo,
"Foo": foo,
},
}
expectCacheKey := tt.cacheKey(map[string]any{"age": 19, "name": "james"})
for i := 0; i < 10; i++ {
key := tt.cacheKey(map[string]any{"age": 19, "name": "james"})
if key != expectCacheKey {
t.Errorf("cache key mismatch: %s != %s", key, expectCacheKey)
}
}
}
}
func TestExplicitCacheKey(t *testing.T) {
tt := Template{
Expression: "name + age",
CacheKey: "user-defined-key",
}
if got := tt.cacheKey(map[string]any{"foo": 1}); got != "user-defined-key" {
t.Errorf("expected explicit CacheKey to be used, got %q", got)
}
if got := tt.cacheKey(map[string]any{"bar": 2}); got != "user-defined-key" {
t.Errorf("explicit CacheKey must be stable across env shapes, got %q", got)
}
if !tt.IsCacheable() {
t.Errorf("template with explicit CacheKey must be cacheable")
}
withCelEnvs := Template{
Expression: "name",
CelEnvs: []cel.EnvOption{cel.Variable("hello", cel.StringType)},
}
if withCelEnvs.IsCacheable() {
t.Errorf("template with CelEnvs and no CacheKey must not be cacheable")
}
withCelEnvs.CacheKey = "stable"
if !withCelEnvs.IsCacheable() {
t.Errorf("template with CelEnvs but explicit CacheKey must be cacheable")
}
}
func TestCacheTime(t *testing.T) {
// No expiration: cache entry should have zero expiration time.
{
tpl := Template{
Expression: "1 + 1",
CacheKey: "cachetime-noexp",
CacheTime: -1,
}
if _, err := RunExpression(nil, tpl); err != nil {
t.Fatalf("eval: %v", err)
}
_, exp, ok := celExpressionCache.GetWithExpiration(tpl.celCacheKey(nil, currentNativeTypes().generation, tpl.celFunctions()))
if !ok {
t.Fatalf("entry not cached")
}
if !exp.IsZero() {
t.Errorf("expected no-expiration entry, got expiry %v", exp)
}
}
// Explicit short TTL: entry expiration should be close to now+CacheTime.
{
tpl := Template{
Expression: "1 + 1",
CacheKey: "cachetime-short",
CacheTime: 50 * time.Millisecond,
}
before := time.Now()
if _, err := RunExpression(nil, tpl); err != nil {
t.Fatalf("eval: %v", err)
}
_, exp, ok := celExpressionCache.GetWithExpiration(tpl.celCacheKey(nil, currentNativeTypes().generation, tpl.celFunctions()))
if !ok {
t.Fatalf("entry not cached")
}
diff := exp.Sub(before)
if diff < 40*time.Millisecond || diff > 200*time.Millisecond {
t.Errorf("expected expiry ~50ms after set, got %v", diff)
}
}
// Zero CacheTime: should fall back to the cache's default TTL (~1h).
{
tpl := Template{
Expression: "1 + 1",
CacheKey: "cachetime-default",
}
before := time.Now()
if _, err := RunExpression(nil, tpl); err != nil {
t.Fatalf("eval: %v", err)
}
_, exp, ok := celExpressionCache.GetWithExpiration(tpl.celCacheKey(nil, currentNativeTypes().generation, tpl.celFunctions()))
if !ok {
t.Fatalf("entry not cached")
}
diff := exp.Sub(before)
if diff < 30*time.Minute || diff > 90*time.Minute {
t.Errorf("expected ~1h default TTL, got %v", diff)
}
}
}
func TestRunTemplate_ValueFunctions(t *testing.T) {
out, err := RunTemplate(map[string]any{"msg": "world"}, Template{
Template: "hello {{ msg }}",
ValueFunctions: true,
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if out != "hello world" {
t.Errorf("got %q, want %q", out, "hello world")
}
}
func TestRunTemplate_ValueFunctionsCoexistWithDotAccess(t *testing.T) {
out, err := RunTemplate(map[string]any{"msg": "world"}, Template{
Template: "{{ msg }}-{{ .msg }}",
ValueFunctions: true,
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if out != "world-world" {
t.Errorf("got %q, want %q", out, "world-world")
}
}
func TestRunTemplate_ValueFunctionsOffKeepsDotOnly(t *testing.T) {
out, err := RunTemplate(map[string]any{"msg": "world"}, Template{
Template: "{{ .msg }}",
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if out != "world" {
t.Errorf("got %q, want %q", out, "world")
}
if _, err := RunTemplate(map[string]any{"msg": "world"}, Template{
Template: "{{ msg }}",
}); err == nil {
t.Error("expected error calling bare {{ msg }} without ValueFunctions, got nil")
}
}
func TestRunTemplate_ValueFunctionsDoNotMutateCallerFuncs(t *testing.T) {
callerFuncs := map[string]any{}
_, err := RunTemplate(map[string]any{"msg": "world"}, Template{
Template: "{{ msg }}",
Functions: callerFuncs,
ValueFunctions: true,
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(callerFuncs) != 0 {
t.Errorf("caller Functions map was mutated: %v", callerFuncs)
}
}
func TestRunTemplate_DelimSetsMultiPass(t *testing.T) {
out, err := RunTemplate(map[string]any{"msg": "world"}, Template{
Template: "hello $(msg)",
DelimSets: []Delims{
{Left: "{{", Right: "}}"},
{Left: "$(", Right: ")"},
},
ValueFunctions: true,
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if out != "hello world" {
t.Errorf("got %q, want %q", out, "hello world")
}
}
func TestRunTemplate_DelimSetsFeedsOutputForward(t *testing.T) {
// First pass replaces $(inner) with "msg", producing "{{ msg }}";
// second pass then resolves the {{ msg }}.
out, err := RunTemplate(
map[string]any{"inner": "msg", "msg": "world"},
Template{
Template: "{{ $(inner) }}",
DelimSets: []Delims{
{Left: "$(", Right: ")"},
{Left: "{{", Right: "}}"},
},
ValueFunctions: true,
},
)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if out != "world" {
t.Errorf("got %q, want %q", out, "world")
}
}
func TestRunTemplate_HeaderOverridesDelimSets(t *testing.T) {
// Header sets [[ ]] so DelimSets should be ignored and $(...) left literal.
out, err := RunTemplate(map[string]any{"msg": "world"}, Template{
Template: "# gotemplate: left-delim=[[ right-delim=]]\n[[ .msg ]] $(msg)",
DelimSets: []Delims{
{Left: "{{", Right: "}}"},
{Left: "$(", Right: ")"},
},
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if out != "world $(msg)" {
t.Errorf("got %q, want %q", out, "world $(msg)")
}
}
func TestRunTemplate_DelimSetsPreferredOverSingleDelimPair(t *testing.T) {
// Both DelimSets and LeftDelim/RightDelim set — DelimSets wins.
out, err := RunTemplate(map[string]any{"msg": "world"}, Template{
Template: "$(msg)",
LeftDelim: "[[",
RightDelim: "]]",
DelimSets: []Delims{
{Left: "$(", Right: ")"},
},
ValueFunctions: true,
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if out != "world" {
t.Errorf("got %q, want %q", out, "world")
}
}
func TestRunTemplate_SingleDelimPairStillWorks(t *testing.T) {
out, err := RunTemplate(map[string]any{"msg": "world"}, Template{
Template: "[[ .msg ]]",
LeftDelim: "[[",
RightDelim: "]]",
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if out != "world" {
t.Errorf("got %q, want %q", out, "world")
}
}
func TestRunExpressionReusesProgramAcrossDifferentData(t *testing.T) {
tpl := Template{
Expression: "name + age",
CacheKey: "reuse-test",
}
out1, err := RunExpression(map[string]any{"name": "alice-", "age": "30"}, tpl)
if err != nil {
t.Fatalf("first eval: %v", err)
}
if out1 != "alice-30" {
t.Errorf("first eval result: got %v", out1)
}
out2, err := RunExpression(map[string]any{"name": "bob-", "age": "42"}, tpl)
if err != nil {
t.Fatalf("second eval: %v", err)
}
if out2 != "bob-42" {
t.Errorf("second eval result: got %v (cached program should not leak first-call data)", out2)
}
}
// The go template path must ignore CacheKey. A parsed go template holds the
// Functions it was parsed with, and CacheKey used to bypass both the check that
// keeps such templates out of the cache and the key that keeps distinct parses
// apart. The tests below cover each way that used to go wrong.
func TestGoTemplate_CacheKeyDoesNotReuseValueFunctions(t *testing.T) {
template := Template{
Template: "node://$(tags.cluster)/$(.name)",
CacheKey: "gotemplate-value-functions",
CacheTime: time.Hour,
ValueFunctions: true,
DelimSets: []Delims{{Left: "$(", Right: ")"}},
}
first, err := RunTemplate(map[string]any{
"name": "node-a",
"tags": map[string]string{"cluster": "production"},
}, template)
if err != nil {
t.Fatalf("first render: %v", err)
}
if first != "node://production/node-a" {
t.Errorf("first render: got %q, want %q", first, "node://production/node-a")
}
second, err := RunTemplate(map[string]any{
"name": "node-b",
"tags": map[string]string{"cluster": "staging"},
}, template)
if err != nil {
t.Fatalf("second render: %v", err)
}
if second != "node://staging/node-b" {
t.Errorf("second render used the first render's value functions: got %q, want %q",
second, "node://staging/node-b")
}
}
func TestGoTemplate_CacheKeyDoesNotReuseCustomFunctions(t *testing.T) {
forUser := func(user string) Template {
return Template{
Template: "user is {{ whoami }}",
CacheKey: "gotemplate-custom-functions",
CacheTime: time.Hour,
Functions: map[string]any{
"whoami": func() any { return user },
},
}
}
first, err := RunTemplate(map[string]any{}, forUser("alice"))
if err != nil {
t.Fatalf("first render: %v", err)
}
if first != "user is alice" {
t.Errorf("first render: got %q, want %q", first, "user is alice")
}
second, err := RunTemplate(map[string]any{}, forUser("bob"))
if err != nil {
t.Fatalf("second render: %v", err)
}
if second != "user is bob" {
t.Errorf("second render used the first render's functions: got %q, want %q",
second, "user is bob")
}
}
func TestGoTemplate_CacheKeyDoesNotCollideAcrossDelimiterPasses(t *testing.T) {
// Each pass parses different text with different delimiters. Sharing one
// CacheKey made the second pass reuse the first pass's parsed template, so
// the $( ) placeholder was never resolved.
out, err := RunTemplate(map[string]any{}, Template{
Template: "A={{ 1 }} B=$( 2 )",
CacheKey: "gotemplate-delimiter-passes",
CacheTime: time.Hour,
DelimSets: []Delims{
{Left: "{{", Right: "}}"},
{Left: "$(", Right: ")"},
},
})
if err != nil {
t.Fatalf("render: %v", err)
}
if out != "A=1 B=2" {
t.Errorf("second delimiter pass did not run: got %q, want %q", out, "A=1 B=2")
}
}
func TestGoTemplate_CacheKeyDoesNotCollideAcrossTemplates(t *testing.T) {
first, err := RunTemplate(map[string]any{}, Template{
Template: "I am template A",
CacheKey: "gotemplate-shared-label",
CacheTime: time.Hour,
})
if err != nil {
t.Fatalf("first render: %v", err)
}
if first != "I am template A" {
t.Errorf("first render: got %q, want %q", first, "I am template A")
}
second, err := RunTemplate(map[string]any{}, Template{
Template: "I am template B",
CacheKey: "gotemplate-shared-label",
CacheTime: time.Hour,
})
if err != nil {
t.Fatalf("second render: %v", err)
}
if second != "I am template B" {
t.Errorf("templates sharing a CacheKey collided: got %q, want %q",
second, "I am template B")
}
}
func TestGoTemplate_CacheKeyIsSafeForConcurrentRenders(t *testing.T) {
template := Template{
Template: "$(.name)=$(tags.cluster)",
CacheKey: "gotemplate-concurrent",
CacheTime: time.Hour,
ValueFunctions: true,
DelimSets: []Delims{{Left: "$(", Right: ")"}},
}
const workers = 8
errs := make(chan error, workers)
var wg sync.WaitGroup
for i := 0; i < workers; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
name := fmt.Sprintf("node-%d", i)
cluster := fmt.Sprintf("cluster-%d", i)
want := name + "=" + cluster
for j := 0; j < 100; j++ {
got, err := RunTemplate(map[string]any{
"name": name,
"tags": map[string]string{"cluster": cluster},
}, template)
if err != nil {
errs <- err
return
}
if got != want {
errs <- fmt.Errorf("got %q, want %q", got, want)
return
}
}
}(i)
}
wg.Wait()
close(errs)
for err := range errs {
t.Error(err)
}
}
// isGoTemplateCacheable must not honour CacheKey, unlike IsCacheable.
func TestIsGoTemplateCacheable(t *testing.T) {
withFuncs := Template{
Template: "{{ hello }}",
Functions: map[string]any{"hello": func() any { return "world" }},
CacheKey: "stable",
}
if !withFuncs.IsCacheable() {
t.Error("IsCacheable must still honour CacheKey for cel expressions")
}
if withFuncs.isGoTemplateCacheable() {
t.Error("a go template with Functions must not be cached, even with a CacheKey")
}
plain := Template{Template: "{{ .name }}", CacheKey: "stable"}
if !plain.isGoTemplateCacheable() {
t.Error("a go template without Functions must be cacheable")
}
}