-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_test.go
More file actions
639 lines (559 loc) · 18.4 KB
/
convert_test.go
File metadata and controls
639 lines (559 loc) · 18.4 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
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
// Copyright 2026 The CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"flag"
"fmt"
"io"
"io/fs"
"os"
"os/exec"
"path/filepath"
"reflect"
"regexp"
"strings"
"testing"
"text/template"
"github.com/rogpeppe/go-internal/diff"
"golang.org/x/tools/txtar"
"gopkg.in/yaml.v3"
)
var update = flag.Bool("update", false, "update golden files in testdata")
var contextDefRe = regexp.MustCompile(`(?m)^(#\w+):\s`)
// helmContextFixtures maps CUE definition names from HelmConfig's
// ContextObjects to fixture CUE data for round-trip testing.
// #values is excluded because it is loaded from each test's values.yaml.
var helmContextFixtures = map[string]string{
"#release": "#release: {\n\tName: \"test\"\n\tNamespace: \"default\"\n\tService: \"Helm\"\n\tIsUpgrade: false\n\tIsInstall: true\n\tRevision: 1\n}\n",
"#chart": "#chart: {\n\tName: \"test\"\n\tVersion: \"0.1.0\"\n\tAppVersion: \"0.1.0\"\n}\n",
"#capabilities": "#capabilities: {\n\tKubeVersion: {\n\t\tVersion: \"v1.25.0\"\n\t\tMajor: \"1\"\n\t\tMinor: \"25\"\n\t\tGitVersion: \"v1.25.0\"\n\t}\n\tAPIVersions: [\"v1\"]\n}\n",
"#template": "#template: {\n\tName: \"test\"\n\tBasePath: \"test/templates\"\n}\n",
"#files": "#files: {}\n",
}
func TestConvert(t *testing.T) {
helmPath, err := exec.LookPath("helm")
if err != nil {
t.Fatal("helm not found in PATH")
}
files, err := fs.Glob(testdataFS, "*.txtar")
if err != nil {
t.Fatal(err)
}
if len(files) == 0 {
t.Fatal("no testdata/*.txtar files found")
}
for _, file := range files {
name := strings.TrimSuffix(filepath.Base(file), ".txtar")
t.Run(name, func(t *testing.T) {
t.Parallel()
ar, err := parseTxtarFS(testdataFS, file)
if err != nil {
t.Fatal(err)
}
runHelmConvertTest(t, helmPath, ar, file, true)
})
}
}
func TestConvertNoVerify(t *testing.T) {
helmPath, err := exec.LookPath("helm")
if err != nil {
t.Fatal("helm not found in PATH")
}
files, err := fs.Glob(testdataFS, "noverify/*.txtar")
if err != nil {
t.Fatal(err)
}
if len(files) == 0 {
t.Fatal("no testdata/noverify/*.txtar files found")
}
for _, file := range files {
name := strings.TrimSuffix(filepath.Base(file), ".txtar")
t.Run(name, func(t *testing.T) {
t.Parallel()
ar, err := parseTxtarFS(testdataFS, file)
if err != nil {
t.Fatal(err)
}
// Enforce noverify invariants: helm_output.yaml must not
// be present, and the txtar comment must be non-empty.
for _, f := range ar.Files {
if f.Name == "helm_output.yaml" {
t.Fatal("noverify test must not contain helm_output.yaml; move to testdata/ if Helm comparison is possible")
}
}
if len(bytes.TrimSpace(ar.Comment)) == 0 {
t.Fatal("noverify test must have a txtar comment explaining why Helm comparison is not possible")
}
runHelmConvertTest(t, helmPath, ar, file, false)
})
}
}
// runHelmConvertTest runs a single Helm conversion test from a txtar archive.
// When requireHelmOutput is true, non-error tests without helm_output.yaml
// are failed.
func runHelmConvertTest(t *testing.T, helmPath string, ar *txtar.Archive,
file string, requireHelmOutput bool) {
t.Helper()
var input, expectedOutput, expectedExpOutput, valuesYAML, expectedHelmOutput, expectedError, expectedBroken []byte
var helpers [][]byte
var hasOutput, hasExpOutput, hasHelmOutput, hasError, hasBroken bool
for _, f := range ar.Files {
switch {
case f.Name == "input.yaml":
input = f.Data
case f.Name == "output.cue":
expectedOutput = f.Data
hasOutput = true
case f.Name == "experiments_output.cue":
expectedExpOutput = f.Data
hasExpOutput = true
case f.Name == "values.yaml":
valuesYAML = f.Data
case f.Name == "helm_output.yaml":
expectedHelmOutput = f.Data
hasHelmOutput = true
case strings.HasSuffix(f.Name, ".tpl"):
helpers = append(helpers, f.Data)
case f.Name == "error":
expectedError = f.Data
hasError = true
case f.Name == "broken":
expectedBroken = f.Data
hasBroken = true
}
}
if input == nil {
t.Fatal("missing input.yaml section")
}
// Enforce mutual exclusivity of error, broken, and output.cue.
if hasBroken && hasError {
t.Fatal("broken and error sections are mutually exclusive")
}
if hasBroken && hasOutput {
t.Fatal("broken and output.cue sections are mutually exclusive")
}
// If an error section is present, verify Convert returns
// an error containing the expected substring.
if hasError {
_, err := Convert(HelmConfig(), input, helpers...)
if err == nil {
t.Fatal("expected Convert() to fail, but it succeeded")
}
wantErr := strings.TrimSpace(string(expectedError))
if !strings.Contains(err.Error(), wantErr) {
t.Errorf("error mismatch:\n want substring: %s\n got: %s", wantErr, err)
}
return
}
if requireHelmOutput && !hasHelmOutput {
t.Fatal("missing helm_output.yaml section (move to testdata/noverify/ if Helm comparison is not possible)")
}
// Validate that the input is a valid Helm template and
// check rendered output if expected. Skip helm validation
// if it fails (e.g., undefined helpers).
helmOut, helmErr := helmTemplate(t, helmPath, input, valuesYAML, helpers)
if helmErr != nil {
if hasHelmOutput {
t.Fatalf("helm template failed: %v", helmErr)
}
} else if hasHelmOutput {
if !bytes.Equal(helmOut, expectedHelmOutput) {
t.Errorf("helm output mismatch:\n%s", diff.Diff("want", expectedHelmOutput, "got", helmOut))
}
}
// If a broken section is present, the CUE conversion is expected
// to fail (known bug). Verify the error matches and return early.
if hasBroken {
_, err := Convert(HelmConfig(), input, helpers...)
if err == nil {
t.Fatal("expected Convert() to fail (broken), but it succeeded; remove the broken section")
}
wantErr := strings.TrimSpace(string(expectedBroken))
if !strings.Contains(err.Error(), wantErr) {
t.Errorf("broken error mismatch:\n want substring: %s\n got: %s", wantErr, err)
}
return
}
got, err := Convert(HelmConfig(), input, helpers...)
if err != nil {
t.Fatalf("Convert() error: %v", err)
}
// If values and expected helm output are provided, verify that
// cue export of the generated CUE with those values produces
// semantically equivalent output to helm template.
if valuesYAML != nil && hasHelmOutput && helmErr == nil {
cueOut := cueExport(t, got, valuesYAML)
if err := yamlSemanticEqual(helmOut, cueOut); err != nil {
t.Errorf("cue export vs helm template: %v", err)
}
}
// Run experiments conversion if opted in (has experiments_output.cue section).
var expGot []byte
if hasExpOutput {
expCfg := HelmConfig()
expCfg.Experiments = true
var expErr error
expGot, expErr = Convert(expCfg, input, helpers...)
if expErr != nil {
t.Fatalf("Convert(experiments) error: %v", expErr)
}
// Round-trip check: experiments output should produce the same
// YAML as helm template.
if valuesYAML != nil && hasHelmOutput && helmErr == nil {
expCueOut := cueExport(t, expGot, valuesYAML)
if err := yamlSemanticEqual(helmOut, expCueOut); err != nil {
t.Errorf("experiments cue export vs helm template: %v", err)
}
}
}
if *update {
var newFiles []txtar.File
for _, f := range ar.Files {
if f.Name == "output.cue" || f.Name == "experiments_output.cue" {
continue
}
newFiles = append(newFiles, f)
}
newFiles = append(newFiles, txtar.File{
Name: "output.cue",
Data: got,
})
if hasExpOutput {
newFiles = append(newFiles, txtar.File{
Name: "experiments_output.cue",
Data: expGot,
})
}
ar.Files = newFiles
if err := os.WriteFile(filepath.Join("testdata", file), txtar.Format(ar), 0o644); err != nil {
t.Fatal(err)
}
return
}
if !hasOutput {
t.Fatal("missing output.cue section (run with -update to generate)")
}
if !bytes.Equal(got, expectedOutput) {
t.Errorf("output mismatch:\n%s", diff.Diff("want", expectedOutput, "got", got))
}
if hasExpOutput && !bytes.Equal(expGot, expectedExpOutput) {
t.Errorf("experiments output mismatch:\n%s",
diff.Diff("want", expectedExpOutput, "got", expGot))
}
}
// helmTemplate validates that the input is a valid Helm template by
// constructing a minimal chart in a temp directory and running helm template.
// It returns the rendered YAML body (after the "---" and "# Source:" header).
func helmTemplate(t *testing.T, helmPath string, template, values []byte, helpers [][]byte) ([]byte, error) {
t.Helper()
dir := t.TempDir()
chartYAML := []byte("apiVersion: v2\nname: test\nversion: 0.1.0\n")
if err := os.WriteFile(filepath.Join(dir, "Chart.yaml"), chartYAML, 0o644); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(dir, "templates"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "templates", "test.yaml"), template, 0o644); err != nil {
t.Fatal(err)
}
for i, helper := range helpers {
name := fmt.Sprintf("_helpers%d.tpl", i)
if i == 0 {
name = "_helpers.tpl"
}
if err := os.WriteFile(filepath.Join(dir, "templates", name), helper, 0o644); err != nil {
t.Fatal(err)
}
}
if values != nil {
if err := os.WriteFile(filepath.Join(dir, "values.yaml"), values, 0o644); err != nil {
t.Fatal(err)
}
}
cmd := exec.Command(helmPath, "template", "test", dir)
out, err := cmd.CombinedOutput()
if err != nil {
return nil, fmt.Errorf("helm template failed: %v\n%s", err, out)
}
// Strip # Source: comment lines and leading/trailing --- lines.
var lines []string
for _, line := range strings.Split(string(out), "\n") {
if strings.HasPrefix(line, "# Source:") {
continue
}
lines = append(lines, line)
}
body := strings.Join(lines, "\n")
// Strip a leading "---\n".
body = strings.TrimPrefix(body, "---\n")
// Strip trailing whitespace.
body = strings.TrimRight(body, "\n \t") + "\n"
return []byte(body), nil
}
// cueExport runs cue export on the generated CUE, placing values.yaml at
// #values:, and returns the YAML output. It also provides other context objects
// (#release, #chart, etc.) as needed.
func cueExport(t *testing.T, cueSrc, valuesYAML []byte) []byte {
t.Helper()
dir := t.TempDir()
cueFile := filepath.Join(dir, "output.cue")
if err := os.WriteFile(cueFile, cueSrc, 0o644); err != nil {
t.Fatal(err)
}
// Detect which context objects are referenced in the CUE source.
defs := contextDefRe.FindAllStringSubmatch(string(cueSrc), -1)
usedDefs := make(map[string]bool)
for _, m := range defs {
usedDefs[m[1]] = true
}
args := []string{"export", cueFile}
for defName, fixture := range helmContextFixtures {
if !usedDefs[defName] {
continue
}
p := filepath.Join(dir, strings.TrimPrefix(defName, "#")+".cue")
if err := os.WriteFile(p, []byte(fixture), 0o644); err != nil {
t.Fatal(err)
}
args = append(args, p)
}
if usedDefs["#values"] {
valuesPath := filepath.Join(dir, "values.yaml")
if err := os.WriteFile(valuesPath, valuesYAML, 0o644); err != nil {
t.Fatal(err)
}
args = append(args, "-l", "#values:", valuesPath)
}
args = append(args, "-e", "yaml.MarshalStream(output)", "--out", "text")
cmd := exec.Command("go", append([]string{"tool", "cue"}, args...)...)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("cue export failed: %v\n%s\ncue source:\n%s", err, out, cueSrc)
}
return out
}
// yamlSemanticEqual parses two YAML streams (possibly multi-document)
// and compares them document-by-document.
func yamlSemanticEqual(a, b []byte) error {
da := yaml.NewDecoder(bytes.NewReader(a))
db := yaml.NewDecoder(bytes.NewReader(b))
for i := 0; ; i++ {
var va, vb any
errA := da.Decode(&va)
errB := db.Decode(&vb)
if errA == io.EOF && errB == io.EOF {
return nil
}
if errA == io.EOF {
return fmt.Errorf("helm has fewer documents than cue:\n%s", diff.Diff("helm", a, "cue", b))
}
if errB == io.EOF {
return fmt.Errorf("cue has fewer documents than helm:\n%s", diff.Diff("helm", a, "cue", b))
}
if errA != nil {
return fmt.Errorf("parsing helm YAML document %d: %w", i, errA)
}
if errB != nil {
return fmt.Errorf("parsing cue YAML document %d: %w", i, errB)
}
if !reflect.DeepEqual(va, vb) {
return fmt.Errorf("semantic mismatch in document %d:\n%s", i, diff.Diff("helm", a, "cue", b))
}
}
}
// coreTemplateExecute executes a template using Go's text/template with
// values from valuesYAML passed as .input.
func coreTemplateExecute(t *testing.T, input, valuesYAML []byte) []byte {
t.Helper()
var values any
if err := yaml.Unmarshal(valuesYAML, &values); err != nil {
t.Fatalf("parsing values.yaml: %v", err)
}
tmpl, err := template.New("test").Parse(string(input))
if err != nil {
t.Fatalf("parsing template: %v", err)
}
data := map[string]any{"input": values}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, data); err != nil {
t.Fatalf("executing template: %v", err)
}
return buf.Bytes()
}
// cueExportCore runs cue export on generated CUE with values loaded as #input.
func cueExportCore(t *testing.T, cueSrc, valuesYAML []byte) []byte {
t.Helper()
dir := t.TempDir()
cueFile := filepath.Join(dir, "output.cue")
if err := os.WriteFile(cueFile, cueSrc, 0o644); err != nil {
t.Fatal(err)
}
args := []string{"export", cueFile}
// Detect if #input is referenced in the CUE source.
defs := contextDefRe.FindAllStringSubmatch(string(cueSrc), -1)
for _, m := range defs {
if m[1] == "#input" {
valuesPath := filepath.Join(dir, "values.yaml")
if err := os.WriteFile(valuesPath, valuesYAML, 0o644); err != nil {
t.Fatal(err)
}
args = append(args, "-l", "#input:", valuesPath)
break
}
}
args = append(args, "-e", "yaml.MarshalStream(output)", "--out", "text")
cmd := exec.Command("go", append([]string{"tool", "cue"}, args...)...)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("cue export failed: %v\n%s\ncue source:\n%s", err, out, cueSrc)
}
return out
}
// testCoreConfig returns a non-Helm Config for testing the core converter.
// It derives from TemplateConfig() so that any changes to core functions
// are automatically picked up, then overrides ContextObjects and RootExpr
// for the simpler core test data model.
func testCoreConfig() *Config {
cfg := TemplateConfig()
cfg.ContextObjects = map[string]string{"input": "#input"}
cfg.RootExpr = ""
return cfg
}
func TestConvertCore(t *testing.T) {
files, err := fs.Glob(testdataFS, "core/*.txtar")
if err != nil {
t.Fatal(err)
}
if len(files) == 0 {
t.Fatal("no testdata/core/*.txtar files found")
}
cfg := testCoreConfig()
for _, file := range files {
name := strings.TrimSuffix(filepath.Base(file), ".txtar")
t.Run(name, func(t *testing.T) {
t.Parallel()
ar, err := parseTxtarFS(testdataFS, file)
if err != nil {
t.Fatal(err)
}
var input, expectedOutput, valuesYAML, expectedError []byte
var helpers [][]byte
var hasOutput, hasError bool
for _, f := range ar.Files {
switch {
case f.Name == "input.yaml":
input = f.Data
case f.Name == "output.cue":
expectedOutput = f.Data
hasOutput = true
case strings.HasSuffix(f.Name, ".tpl"):
helpers = append(helpers, f.Data)
case f.Name == "values.yaml":
valuesYAML = f.Data
case f.Name == "error":
expectedError = f.Data
hasError = true
}
}
if input == nil {
t.Fatal("missing input.yaml section")
}
// If an error section is present, verify Convert returns
// an error containing the expected substring.
if hasError {
_, err := Convert(cfg, input, helpers...)
if err == nil {
t.Fatal("expected Convert() to fail, but it succeeded")
}
wantErr := strings.TrimSpace(string(expectedError))
if !strings.Contains(err.Error(), wantErr) {
t.Errorf("error mismatch:\n want substring: %s\n got: %s", wantErr, err)
}
return
}
got, err := Convert(cfg, input, helpers...)
if err != nil {
t.Fatalf("Convert() error: %v", err)
}
if *update {
var newFiles []txtar.File
for _, f := range ar.Files {
if f.Name == "output.cue" {
continue
}
newFiles = append(newFiles, f)
}
newFiles = append(newFiles, txtar.File{
Name: "output.cue",
Data: got,
})
ar.Files = newFiles
if err := os.WriteFile(filepath.Join("testdata", file), txtar.Format(ar), 0o644); err != nil {
t.Fatal(err)
}
return
}
if !hasOutput {
t.Fatal("missing output.cue section (run with -update to generate)")
}
if !bytes.Equal(got, expectedOutput) {
t.Errorf("output mismatch:\n%s", diff.Diff("want", expectedOutput, "got", got))
}
// If values.yaml is provided, verify the generated CUE produces
// semantically equivalent output to executing the template.
if valuesYAML != nil {
templateOut := coreTemplateExecute(t, input, valuesYAML)
cueOut := cueExportCore(t, got, valuesYAML)
if err := yamlSemanticEqual(templateOut, cueOut); err != nil {
t.Errorf("cue export vs template: %v", err)
}
}
})
}
}
// TestTemplateConfig verifies TemplateConfig-specific behavior:
// bare dot support (via RootExpr) and HelmConfig rejecting bare dot.
func TestTemplateConfig(t *testing.T) {
// TemplateConfig accepts bare {{ . }} because RootExpr is set.
t.Run("bare_dot", func(t *testing.T) {
_, err := Convert(TemplateConfig(), []byte("name: {{ . }}"))
if err != nil {
t.Fatalf("expected success, got error: %v", err)
}
})
// HelmConfig rejects bare {{ . }} because RootExpr is empty.
t.Run("helm_config_rejects_bare_dot", func(t *testing.T) {
_, err := Convert(HelmConfig(), []byte("name: {{ . }}"))
if err == nil {
t.Fatal("expected HelmConfig to reject bare dot, but it succeeded")
}
if !strings.Contains(err.Error(), "outside range/with not supported") {
t.Errorf("unexpected error: %v", err)
}
})
}
// TestHelmContextFixtures verifies that helmContextFixtures has an entry
// for every context object in HelmConfig except #values.
func TestHelmContextFixtures(t *testing.T) {
for _, cueDef := range HelmConfig().ContextObjects {
if cueDef == "#values" {
continue
}
if _, ok := helmContextFixtures[cueDef]; !ok {
t.Errorf("helmContextFixtures missing entry for %s", cueDef)
}
}
}