-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathrender_test.go
More file actions
183 lines (159 loc) · 6.21 KB
/
render_test.go
File metadata and controls
183 lines (159 loc) · 6.21 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
// Copyright Cozystack 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 engine
import (
"os"
"path/filepath"
"strings"
"testing"
helmEngine "github.com/cozystack/talm/pkg/engine/helm"
"helm.sh/helm/v3/pkg/chart/loader"
)
// createTestChart creates a minimal Helm chart in a temp directory with the
// given template content. Returns the chart root path.
func createTestChart(t *testing.T, chartName, templateName, templateContent string) string {
t.Helper()
root := t.TempDir()
chartYAML := "apiVersion: v2\nname: " + chartName + "\ntype: application\nversion: 0.1.0\n"
if err := os.WriteFile(filepath.Join(root, "Chart.yaml"), []byte(chartYAML), 0o644); err != nil {
t.Fatalf("write Chart.yaml: %v", err)
}
if err := os.WriteFile(filepath.Join(root, "values.yaml"), []byte("{}\n"), 0o644); err != nil {
t.Fatalf("write values.yaml: %v", err)
}
templatesDir := filepath.Join(root, "templates")
if err := os.MkdirAll(templatesDir, 0o755); err != nil {
t.Fatalf("mkdir templates: %v", err)
}
if err := os.WriteFile(filepath.Join(templatesDir, templateName), []byte(templateContent), 0o644); err != nil {
t.Fatalf("write template: %v", err)
}
return root
}
// TestLookupOfflineProducesEmptyInterface is a regression test for the bug
// where `talm apply` rendered templates offline, causing lookup() to return
// empty maps. Templates that derive the interface name from discovery data
// (e.g., iterating routes) produced an empty interface field, which Talos v1.12
// rejects with:
//
// [networking.os.device.interface], [networking.os.device.deviceSelector]:
// required either config section to be set
//
// The fix: render templates online (with a real client and LookupFunc).
// This test verifies both the broken (offline) and fixed (online) paths at
// the Helm template rendering layer.
func TestLookupOfflineProducesEmptyInterface(t *testing.T) {
// Template that mimics the real talm.discovered.default_link_name_by_gateway
// pattern: iterate routes from lookup(), extract outLinkName. When offline,
// lookup returns an empty map → range produces nothing → empty interface.
const tmpl = `{{- $linkName := "" -}}
{{- range (lookup "routes" "" "").items -}}
{{- if and (eq .spec.dst "") (not (eq .spec.gateway "")) -}}
{{- $linkName = .spec.outLinkName -}}
{{- end -}}
{{- end -}}
machine:
network:
interfaces:
- interface: {{ $linkName }}
`
chartRoot := createTestChart(t, "testchart", "config.yaml", tmpl)
chrt, err := loader.LoadDir(chartRoot)
if err != nil {
t.Fatalf("LoadDir: %v", err)
}
rootValues := map[string]interface{}{
"Values": chrt.Values,
}
t.Run("offline_produces_empty_interface", func(t *testing.T) {
origLookup := helmEngine.LookupFunc
defer func() { helmEngine.LookupFunc = origLookup }()
// Default no-op: returns empty map (same as offline mode)
helmEngine.LookupFunc = func(string, string, string) (map[string]interface{}, error) {
return map[string]interface{}{}, nil
}
eng := helmEngine.Engine{}
out, err := eng.Render(chrt, rootValues)
if err != nil {
t.Fatalf("Render: %v", err)
}
rendered := out["testchart/templates/config.yaml"]
// With offline lookup, the interface name is empty — this is the bug.
if strings.Contains(rendered, "interface: eth0") {
t.Error("offline render should NOT produce 'interface: eth0'")
}
if !strings.Contains(rendered, "interface: ") {
t.Error("offline render should contain 'interface: ' (with empty value)")
}
})
t.Run("online_lookup_populates_interface", func(t *testing.T) {
origLookup := helmEngine.LookupFunc
defer func() { helmEngine.LookupFunc = origLookup }()
// Simulate online mode: return route data with a real interface name.
helmEngine.LookupFunc = func(resource, namespace, name string) (map[string]interface{}, error) {
if resource == "routes" && name == "" {
return map[string]interface{}{
"apiVersion": "v1",
"kind": "List",
"items": []interface{}{
map[string]interface{}{
"spec": map[string]interface{}{
"dst": "",
"gateway": "192.168.1.1",
"outLinkName": "eth0",
"table": "main",
},
},
},
}, nil
}
return map[string]interface{}{}, nil
}
eng := helmEngine.Engine{}
out, err := eng.Render(chrt, rootValues)
if err != nil {
t.Fatalf("Render: %v", err)
}
rendered := out["testchart/templates/config.yaml"]
if !strings.Contains(rendered, "interface: eth0") {
t.Errorf("online render should produce 'interface: eth0', got:\n%s", rendered)
}
})
}
// TestRenderOfflineSkipsLookupFunc verifies that Render with Offline=true does
// NOT replace the LookupFunc, and Offline=false does replace it. This is a
// unit check that the fix (Offline=false in apply) causes the real LookupFunc
// to be wired up.
func TestRenderOfflineSkipsLookupFunc(t *testing.T) {
origLookup := helmEngine.LookupFunc
defer func() { helmEngine.LookupFunc = origLookup }()
// Set a sentinel LookupFunc
helmEngine.LookupFunc = func(string, string, string) (map[string]interface{}, error) {
return map[string]interface{}{"sentinel": true}, nil
}
// Offline=true should leave the sentinel intact
opts := Options{Offline: true}
if !opts.Offline {
t.Fatal("test setup: expected Offline=true")
}
res, _ := helmEngine.LookupFunc("test", "", "")
if _, ok := res["sentinel"]; !ok {
t.Error("Offline=true must not replace LookupFunc")
}
// Verify: when Offline=false, Render() would call
// helmEngine.LookupFunc = newLookupFunction(ctx, c), replacing the sentinel.
// We can't call full Render without a chart/client, but the logic is:
// if !opts.Offline { helmEngine.LookupFunc = newLookupFunction(ctx, c) }
// This is tested implicitly by the online_lookup_populates_interface subtest.
}