-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgolden_test.go
83 lines (66 loc) · 2.31 KB
/
golden_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
package golden
import (
"flag"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"github.com/gruntwork-io/terratest/modules/helm"
"gopkg.in/yaml.v2"
)
var update = flag.Bool("update", false, "update golden test output files")
type ChartYaml struct {
Name string `yaml:"name"`
Version string `yaml:"version"`
}
func GetChartYaml(t *testing.T) ChartYaml {
chartYamlFile, err := os.ReadFile("./stable/connector/Chart.yaml")
if err != nil {
t.Fatalf("Error reading Chart.yaml: %v", err)
}
var chartYaml ChartYaml
if err := yaml.Unmarshal(chartYamlFile, &chartYaml); err != nil {
t.Fatalf("Error unmarshaling YAML data: %v", err)
}
return chartYaml
}
func TestHelmRender(t *testing.T) {
files, err := os.ReadDir("./test/golden")
if err != nil {
t.Fatal(err)
}
chartYaml := GetChartYaml(t)
for _, f := range files {
if !f.IsDir() && strings.HasSuffix(f.Name(), ".yaml") && !strings.HasSuffix(f.Name(), ".golden.yaml") {
// Render this values.yaml file
output := helm.RenderTemplate(t,
&helm.Options{
ValuesFiles: []string{"test/golden/" + f.Name()},
},
"./stable/connector",
"test",
nil,
)
goldenFile := "test/golden/" + strings.TrimSuffix(f.Name(), filepath.Ext(".yaml")) + ".golden.yaml"
// Replace `cn.chart` helper value with a stable value for testing
regex := regexp.MustCompile(fmt.Sprintf("%s-%s", chartYaml.Name, chartYaml.Version))
bytes := regex.ReplaceAll([]byte(output), []byte(fmt.Sprintf("%s-major.minor.patch-test", chartYaml.Name)))
output = fmt.Sprintf("%s\n", string(bytes))
if *update {
err := os.WriteFile(goldenFile, []byte(output), 0644)
if err != nil {
t.Fatal(err)
}
}
expected, err := os.ReadFile(goldenFile)
if err != nil {
t.Fatal(err)
}
if string(expected) != output {
t.Fatalf("Expected %s, but got %s\n. Update golden files by running `go test -v ./... -update`", string(expected), output)
}
}
}
}