-
Notifications
You must be signed in to change notification settings - Fork 1
/
tgen_test.go
559 lines (502 loc) · 11.3 KB
/
tgen_test.go
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
package main
import (
"bytes"
"fmt"
"os"
"reflect"
"strings"
"testing"
"gopkg.in/yaml.v3"
)
func Test_tgen_setTemplate(t *testing.T) {
type args struct {
name string
content string
}
tests := []struct {
name string
args args
}{
{
name: "test",
args: args{
name: "test",
content: "test test test test test test test",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tr := &tgen{}
tr.setTemplate(tt.args.name, tt.args.content)
if tr.templateFileName != tt.args.name {
t.Errorf("templateFileName = %v, want %v", tr.templateFileName, tt.args.name)
}
if tr.templateFileContent != tt.args.content {
t.Errorf("templateFileContent = %v, want %v", tr.templateFileContent, tt.args.content)
}
})
}
}
func writeTemporaryTestFile(t *testing.T, content string) *os.File {
f, err := os.CreateTemp("", "tgen_test_*")
if err != nil {
t.Fatalf("unable to create temporary file: %s", err.Error())
}
if err := os.WriteFile(f.Name(), []byte(content), 0644); err != nil {
t.Fatalf("unable to write to temporary file: %s", err.Error())
}
return f
}
func Test_tgen_loadTemplatePath(t *testing.T) {
tests := []struct {
name string
content string
wantErr bool
}{
{
name: "basic",
content: "test test test test test test test",
},
{
name: "empty",
content: "",
},
{
name: "not found",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var location string
switch tt.name {
case "not found":
location = "/var/log/notfound"
default:
f := writeTemporaryTestFile(t, tt.content)
defer os.Remove(f.Name())
location = f.Name()
}
tg := &tgen{}
err := tg.loadTemplatePath(location)
if tt.wantErr {
if err == nil {
t.Fatalf("expected error, got nil")
}
return
}
if !tt.wantErr {
if err != nil {
t.Fatalf("unexpected error: %s", err.Error())
}
if tg.templateFileName != location {
t.Errorf("templateFileName = %v, want %v", tg.templateFileName, location)
}
if tg.templateFileContent != tt.content {
t.Errorf("templateFileContent = %v, want %v", tg.templateFileContent, tt.content)
}
}
})
}
}
func Test_tgen_loadTemplateFile(t *testing.T) {
tests := []struct {
name string
content string
overwriteName string
wantErr bool
}{
{
name: "basic",
content: "test test test test test test test",
},
{
name: "nil pointer",
wantErr: true,
},
{
name: "file name overwritten",
overwriteName: "test",
content: "foo bar baz qux quux corge grault garply waldo fred plugh xyzzy thud",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var f *os.File
switch tt.name {
case "nil pointer":
f = nil
default:
f = writeTemporaryTestFile(t, tt.content)
defer os.Remove(f.Name())
}
var fileSize int64
if f != nil {
fi, err := f.Stat()
if err != nil {
t.Fatalf("unable to stat file: %s", err.Error())
}
fileSize = fi.Size()
}
tr := &tgen{}
err := tr.loadTemplateFile(tt.overwriteName, f)
if tt.wantErr {
if err == nil {
t.Fatalf("expected error, got nil")
}
}
if !tt.wantErr {
if err != nil {
t.Fatalf("unexpected error: %s", err.Error())
}
if tt.overwriteName != "" && tr.templateFileName != tt.overwriteName {
t.Errorf("templateFileName = %q, want %q", tr.templateFileName, tt.overwriteName)
}
if a := int64(len(tr.templateFileContent)); a != fileSize {
t.Errorf("template file size = %v, want %v", a, fileSize)
}
if tr.templateFileContent != tt.content {
t.Errorf("templateFileContent = %q, want %q", tr.templateFileContent, tt.content)
}
}
})
}
}
func Test_tgen_loadYAMLValues(t *testing.T) {
tests := []struct {
name string
content string
wantErr bool
}{
{
name: "basic",
content: "foo: bar",
},
{
name: "empty",
content: "",
},
{
name: "invalid",
content: "[foo90802394uuj^%#",
wantErr: true,
},
{
name: "not found",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var path string
switch tt.name {
case "not found":
path = "/var/log/notfound"
default:
f := writeTemporaryTestFile(t, tt.content)
defer os.Remove(f.Name())
path = f.Name()
}
tr := &tgen{}
err := tr.loadYAMLValues(path)
if tt.wantErr {
if err == nil {
t.Fatalf("expected error, got nil")
}
}
if !tt.wantErr {
if err != nil {
t.Fatalf("unexpected error: %s", err.Error())
}
want := map[string]any{}
if err := yaml.Unmarshal([]byte(tt.content), &want); err != nil {
t.Fatalf("unable to unmarshal test content: %s", err.Error())
}
want["Values"] = copyMap(want)
if !reflect.DeepEqual(tr.yamlValues, want) {
t.Errorf("values = %v, want %v", tr.yamlValues, want)
}
}
})
}
}
func mapToKV(m map[string]string) string {
var b bytes.Buffer
for k, v := range m {
b.WriteString(fmt.Sprintf("%s=%s", k, v) + "\n")
}
return b.String()
}
func Test_tgen_loadEnvValues(t *testing.T) {
tests := []struct {
name string
content map[string]string
wantErr bool
}{
{
name: "basic",
content: map[string]string{
"foo": "bar",
"baz": "qux",
"NAME": "VALUE",
},
},
{
name: "empty",
content: map[string]string{},
},
{
name: "more than one equal",
content: map[string]string{
"foo": "===bar",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var path string
switch tt.name {
case "not found":
path = "/var/log/notfound"
default:
f := writeTemporaryTestFile(t, mapToKV(tt.content))
defer os.Remove(f.Name())
path = f.Name()
}
tr := &tgen{}
err := tr.loadEnvValues(path)
if tt.wantErr {
if err == nil {
t.Fatalf("expected error, got nil\ncontents: %#v", tr.envValues)
}
}
if !tt.wantErr {
if err != nil {
t.Fatalf("unexpected error: %s", err.Error())
}
for kin, vin := range tt.content {
if v, ok := tr.envValues[strings.ToUpper(kin)]; !ok {
t.Fatalf("missing key %q", kin)
} else if v != vin {
t.Fatalf("value for key %q = %q, want %q", kin, v, vin)
}
}
}
})
}
}
func Test_tgen_setDelimiters(t *testing.T) {
tests := []struct {
name string
delimiters string
wantLeft string
wantRight string
wantErr bool
}{
{
name: "basic",
delimiters: "{{}}",
wantLeft: "{{",
wantRight: "}}",
},
{
name: "alt",
delimiters: `<%%>`,
wantLeft: "<%",
wantRight: "%>",
},
{
name: "empty",
delimiters: "",
wantErr: true,
},
{
name: "invalid",
delimiters: "foo",
wantErr: true,
},
{
name: "invalid",
delimiters: "foo}}",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tr := &tgen{}
if err := tr.setDelimiters(tt.delimiters); (err != nil) != tt.wantErr {
t.Errorf("error = %v, wantErr %v", err, tt.wantErr)
}
if tr.preDelimiter != tt.wantLeft {
t.Errorf("preDelimiter = %q, want %q", tr.preDelimiter, tt.wantLeft)
}
if tr.postDelimiter != tt.wantRight {
t.Errorf("postDelimiter = %q, want %q", tr.postDelimiter, tt.wantRight)
}
})
}
}
func Test_tgen_render(t *testing.T) {
type fields struct {
Strict bool
templateFileName string
templateFileContent string
yamlValues map[string]any
envValues map[string]string
preDelimiter string
postDelimiter string
}
tests := []struct {
name string
fields fields
want string
wantErr bool
}{
{
name: "basic",
fields: fields{
templateFileName: "test",
templateFileContent: `The {{ .Values.foo }} says {{ env "bar" }}`,
yamlValues: map[string]any{
"foo": "cow",
},
envValues: map[string]string{
"BAR": "moo",
},
},
want: `The cow says moo`,
},
{
name: "custom delimiters",
fields: fields{
templateFileName: "test",
templateFileContent: `The <% .Values.foo %> says <% env "bar" %>`,
yamlValues: map[string]any{
"foo": "cow",
},
envValues: map[string]string{
"BAR": "moo",
},
preDelimiter: "<%",
postDelimiter: "%>",
},
want: `The cow says moo`,
},
{
name: "missing env no strict",
fields: fields{
templateFileName: "test",
templateFileContent: `The {{ .Values.foo }} says {{ env "bar" }}`,
yamlValues: map[string]any{
"foo": "cow",
},
envValues: map[string]string{},
},
want: `The cow says `,
},
{
name: "missing env strict",
fields: fields{
templateFileName: "test",
templateFileContent: `The {{ .Values.foo }} says {{ env "bar" }}`,
yamlValues: map[string]any{
"foo": "cow",
},
envValues: map[string]string{},
Strict: true,
},
wantErr: true,
},
{
name: "missing yaml no strict",
fields: fields{
templateFileName: "test",
templateFileContent: `The {{ .Values.foo }} says {{ env "bar" }}`,
yamlValues: map[string]any{},
envValues: map[string]string{
"BAR": "moo",
},
},
want: `The says moo`,
},
{
name: "missing yaml strict",
fields: fields{
templateFileName: "test",
templateFileContent: `The {{ .Values.foo }} says {{ env "bar" }}`,
yamlValues: map[string]any{},
envValues: map[string]string{
"BAR": "moo",
},
Strict: true,
},
wantErr: true,
},
{
name: "missing both no strict",
fields: fields{
templateFileName: "test",
templateFileContent: `The {{ .Values.foo }} says {{ env "bar" }}`,
yamlValues: map[string]any{},
envValues: map[string]string{},
},
want: `The says `,
},
{
name: "missing both strict",
fields: fields{
templateFileName: "test",
templateFileContent: `The {{ .Values.foo }} says {{ env "bar" }}`,
yamlValues: map[string]any{},
envValues: map[string]string{},
Strict: true,
},
wantErr: true,
},
{
name: "invalid template",
fields: fields{
templateFileName: "test",
templateFileContent: `The {{ .Values.foo }} says {{ env "bar" }`,
yamlValues: map[string]any{
"foo": "cow",
},
envValues: map[string]string{
"BAR": "moo",
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
preDelim, postDelim := tt.fields.preDelimiter, tt.fields.postDelimiter
if preDelim == "" {
preDelim = "{{"
}
if postDelim == "" {
postDelim = "}}"
}
newvals := copyMap(tt.fields.yamlValues)
newvals["Values"] = copyMap(newvals)
tr := &tgen{
Strict: tt.fields.Strict,
templateFileName: tt.fields.templateFileName,
templateFileContent: tt.fields.templateFileContent,
yamlValues: newvals,
envValues: tt.fields.envValues,
preDelimiter: preDelim,
postDelimiter: postDelim,
}
var w bytes.Buffer
if err := tr.render(&w); (err != nil) != tt.wantErr {
t.Errorf("render() not expecting an error, got: %q", err.Error())
return
}
if gotW := w.String(); gotW != tt.want {
t.Errorf("render() = %q, want %q", gotW, tt.want)
}
})
}
}