|
| 1 | +// Copyright Cozystack Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package engine |
| 16 | + |
| 17 | +import ( |
| 18 | + "os" |
| 19 | + "path/filepath" |
| 20 | + "strconv" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | +) |
| 24 | + |
| 25 | +// updateGolden reports whether to regenerate the committed golden |
| 26 | +// snapshots instead of comparing against them. Set TALM_UPDATE_GOLDEN=1 |
| 27 | +// to regenerate (an env toggle avoids a package-level flag global): |
| 28 | +// |
| 29 | +// TALM_UPDATE_GOLDEN=1 go test ./pkg/engine/ -run TestGoldenRender |
| 30 | +func updateGolden() bool { |
| 31 | + return os.Getenv("TALM_UPDATE_GOLDEN") != "" |
| 32 | +} |
| 33 | + |
| 34 | +// TestGoldenRender pins the full, byte-for-byte rendered output of the |
| 35 | +// shipped charts across the whole schema matrix. Unlike the substring |
| 36 | +// contract tests elsewhere in this package — which assert only the |
| 37 | +// specific fields each one names — this catches any change that alters |
| 38 | +// whitespace, key ordering, number/bool formatting, document count, |
| 39 | +// `---` separator placement, the trailing newline, or any field no |
| 40 | +// other test happens to check. |
| 41 | +// |
| 42 | +// The matrix is charts/cozystack + charts/generic × controlplane + |
| 43 | +// worker × multi-doc (v1.12+) + legacy single-doc (pre-v1.12) schemas. |
| 44 | +// Discovery is driven by the deterministic simpleNicLookup fixture so |
| 45 | +// the output depends on nothing outside the repo. |
| 46 | +// |
| 47 | +// It exists as a safety net for library upgrades (e.g. the Helm v3→v4 |
| 48 | +// swap): regenerate the golden files on the old version, then run this |
| 49 | +// test unchanged on the new one — any diff is a behavioral regression |
| 50 | +// to investigate, not something to regenerate away. |
| 51 | +func TestGoldenRender(t *testing.T) { |
| 52 | + cases := []struct { |
| 53 | + name string |
| 54 | + chartPath string |
| 55 | + templateFile string |
| 56 | + talosVersion string |
| 57 | + }{ |
| 58 | + {"cozystack-controlplane-multidoc", cozystackChartPath, "templates/controlplane.yaml", "v1.12"}, |
| 59 | + {"cozystack-worker-multidoc", cozystackChartPath, "templates/worker.yaml", "v1.12"}, |
| 60 | + {"cozystack-controlplane-legacy", cozystackChartPath, "templates/controlplane.yaml", "v1.11"}, |
| 61 | + {"cozystack-worker-legacy", cozystackChartPath, "templates/worker.yaml", "v1.11"}, |
| 62 | + {"generic-controlplane-multidoc", genericChartPath, "templates/controlplane.yaml", "v1.12"}, |
| 63 | + {"generic-worker-multidoc", genericChartPath, "templates/worker.yaml", "v1.12"}, |
| 64 | + {"generic-controlplane-legacy", genericChartPath, "templates/controlplane.yaml", "v1.11"}, |
| 65 | + {"generic-worker-legacy", genericChartPath, "templates/worker.yaml", "v1.11"}, |
| 66 | + } |
| 67 | + |
| 68 | + for _, tc := range cases { |
| 69 | + t.Run(tc.name, func(t *testing.T) { |
| 70 | + got := renderChartTemplateWithLookup(t, tc.chartPath, tc.templateFile, simpleNicLookup(), tc.talosVersion) |
| 71 | + goldenPath := filepath.Join("testdata", "golden", tc.name+".golden.yaml") |
| 72 | + |
| 73 | + if updateGolden() { |
| 74 | + if err := os.MkdirAll(filepath.Dir(goldenPath), 0o755); err != nil { |
| 75 | + t.Fatalf("mkdir golden dir: %v", err) |
| 76 | + } |
| 77 | + if err := os.WriteFile(goldenPath, []byte(got), 0o644); err != nil { |
| 78 | + t.Fatalf("write golden %s: %v", goldenPath, err) |
| 79 | + } |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + wantBytes, err := os.ReadFile(goldenPath) |
| 84 | + if err != nil { |
| 85 | + t.Fatalf("read golden %s: %v (regenerate with TALM_UPDATE_GOLDEN=1)", goldenPath, err) |
| 86 | + } |
| 87 | + |
| 88 | + if got != string(wantBytes) { |
| 89 | + t.Errorf("rendered output for %s differs from golden %s.\n"+ |
| 90 | + "A byte-level diff here means rendering behavior changed — investigate before regenerating.\n%s", |
| 91 | + tc.name, goldenPath, firstLineDiff(string(wantBytes), got)) |
| 92 | + } |
| 93 | + }) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// firstLineDiff returns a compact description of the first line where |
| 98 | +// want and got differ, with a little surrounding context, so a golden |
| 99 | +// mismatch points at the exact drift instead of dumping both full files. |
| 100 | +func firstLineDiff(want, got string) string { |
| 101 | + wantLines := strings.Split(want, "\n") |
| 102 | + gotLines := strings.Split(got, "\n") |
| 103 | + |
| 104 | + maxLen := max(len(wantLines), len(gotLines)) |
| 105 | + |
| 106 | + for i := range maxLen { |
| 107 | + var wl, gl string |
| 108 | + if i < len(wantLines) { |
| 109 | + wl = wantLines[i] |
| 110 | + } |
| 111 | + if i < len(gotLines) { |
| 112 | + gl = gotLines[i] |
| 113 | + } |
| 114 | + if wl != gl { |
| 115 | + return "first difference at line " + itoa(i+1) + ":\n" + |
| 116 | + " want: " + quote(wl) + "\n" + |
| 117 | + " got: " + quote(gl) |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return "files differ only in trailing content (line count: want " + |
| 122 | + itoa(len(wantLines)) + ", got " + itoa(len(gotLines)) + ")" |
| 123 | +} |
| 124 | + |
| 125 | +// itoa is a tiny strconv.Itoa alias kept local so the diff helper reads |
| 126 | +// without an extra import at the call sites. |
| 127 | +func itoa(n int) string { |
| 128 | + return strconv.Itoa(n) |
| 129 | +} |
| 130 | + |
| 131 | +// quote renders a line with visible quotes so trailing-whitespace drift |
| 132 | +// is legible in the failure message. |
| 133 | +func quote(s string) string { |
| 134 | + return "\"" + s + "\"" |
| 135 | +} |
0 commit comments