Skip to content

Commit ef90685

Browse files
feedback
1 parent 76ebd3d commit ef90685

File tree

1 file changed

+46
-47
lines changed

1 file changed

+46
-47
lines changed

charts/member-agent-arc/charts_test.go

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package memberagentarc
22

33
import (
4+
"encoding/base64"
45
"os"
56
"path/filepath"
67
"strings"
@@ -15,7 +16,6 @@ import (
1516
// TestHelmChartTemplatesRenderValidYAML tests that Helm chart templates render
1617
// to valid YAML with maximal configuration values that activate all conditional paths.
1718
func TestHelmChartTemplatesRenderValidYAML(t *testing.T) {
18-
g := NewWithT(t)
1919
// Maximal configuration to activate all conditional template paths
2020
values := map[string]interface{}{
2121
"memberagent": map[string]interface{}{
@@ -82,53 +82,54 @@ func TestHelmChartTemplatesRenderValidYAML(t *testing.T) {
8282
},
8383
}
8484

85-
templatesDir := "templates"
86-
entries, err := os.ReadDir(templatesDir)
87-
g.Expect(err).ToNot(HaveOccurred(), "Failed to read templates directory")
88-
89-
validatedCount := 0
90-
for _, entry := range entries {
91-
// Skip directories and helper templates
92-
if entry.IsDir() || strings.HasPrefix(entry.Name(), "_") {
93-
continue
94-
}
95-
96-
templatePath := filepath.Join(templatesDir, entry.Name())
97-
templateBytes, err := os.ReadFile(templatePath)
98-
g.Expect(err).ToNot(HaveOccurred(), "Failed to read template %s", entry.Name())
99-
100-
// Parse and render the Go template
101-
tmpl := template.New(entry.Name()).Funcs(helmFuncMap())
102-
tmpl, err = tmpl.Parse(string(templateBytes))
103-
g.Expect(err).ToNot(HaveOccurred(), "Failed to parse template %s", entry.Name())
104-
105-
var rendered strings.Builder
106-
err = tmpl.Execute(&rendered, context)
107-
g.Expect(err).ToNot(HaveOccurred(), "Failed to render template %s. Err :s", entry.Name(), err)
85+
// Table-driven test for each template file
86+
tests := []struct {
87+
name string
88+
templateFile string
89+
}{
90+
{name: "deployment template", templateFile: "deployment.yaml"},
91+
{name: "rbac template", templateFile: "rbac.yaml"},
92+
{name: "serviceaccount template", templateFile: "serviceaccount.yaml"},
93+
{name: "azure-proxy-secrets template", templateFile: "azure-proxy-secrets.yaml"},
94+
}
10895

109-
renderedContent := strings.TrimSpace(rendered.String())
110-
g.Expect(renderedContent).ToNot(BeEmpty(), "Rendered template %s is empty", entry.Name())
96+
for _, tt := range tests {
97+
t.Run(tt.name, func(t *testing.T) {
98+
g := NewWithT(t)
99+
100+
templatePath := filepath.Join("templates", tt.templateFile)
101+
templateBytes, err := os.ReadFile(templatePath)
102+
g.Expect(err).ToNot(HaveOccurred(), "Failed to read template %s. Err %s", tt.templateFile, err)
103+
104+
// Parse and render the Go template
105+
tmpl := template.New(tt.templateFile).Funcs(helmFuncMap())
106+
tmpl, err = tmpl.Parse(string(templateBytes))
107+
g.Expect(err).ToNot(HaveOccurred(), "Failed to parse template %s. Err %s", tt.templateFile, err)
108+
109+
var rendered strings.Builder
110+
err = tmpl.Execute(&rendered, context)
111+
g.Expect(err).ToNot(HaveOccurred(), "Failed to render template %s. Err %s", tt.templateFile, err)
112+
renderedContent := strings.TrimSpace(rendered.String())
113+
114+
// Validate each YAML document in multi-doc files
115+
docs := strings.Split(renderedContent, "\n---\n")
116+
validDocsCount := 0
117+
for i, doc := range docs {
118+
doc = strings.TrimSpace(doc)
119+
if doc == "" {
120+
continue
121+
}
111122

112-
// Validate each YAML document in multi-doc files
113-
docs := strings.Split(renderedContent, "\n---\n")
114-
validDocsCount := 0
115-
for i, doc := range docs {
116-
doc = strings.TrimSpace(doc)
117-
if doc == "" {
118-
continue
123+
var obj interface{}
124+
err := yaml.Unmarshal([]byte(doc), &obj)
125+
g.Expect(err).ToNot(HaveOccurred(), "Template %s doc %d is invalid YAML\nContent:\n%s",
126+
tt.templateFile, i+1, doc)
127+
validDocsCount++
119128
}
120129

121-
var obj interface{}
122-
err := yaml.Unmarshal([]byte(doc), &obj)
123-
g.Expect(err).ToNot(HaveOccurred(), "Template %s doc %d is invalid YAML: %v\nContent:\n%s",
124-
entry.Name(), i+1, err, doc)
125-
validDocsCount++
126-
}
127-
128-
validatedCount++
130+
g.Expect(validDocsCount).To(BeNumerically(">", 0), "Template %s rendered but produced no valid YAML documents", tt.templateFile)
131+
})
129132
}
130-
131-
g.Expect(validatedCount).ToNot(BeZero(), "No templates were validated")
132133
}
133134

134135
// helmFuncMap returns template functions that mimic Helm's template functions
@@ -151,12 +152,10 @@ func helmFuncMap() template.FuncMap {
151152
return `"` + toString(s) + `"`
152153
},
153154
"b64enc": func(s string) string {
154-
// Simple mock - just return the string for testing purposes
155-
return s
155+
return base64.StdEncoding.EncodeToString([]byte(s))
156156
},
157157
"include": func(name string, data interface{}) string {
158-
// Simple mock - return empty string for testing
159-
return ""
158+
return "<included: " + name + ">"
160159
},
161160
}
162161
}

0 commit comments

Comments
 (0)