@@ -28,6 +28,7 @@ import (
2828 "sigs.k8s.io/controller-tools/pkg/genall"
2929 "sigs.k8s.io/controller-tools/pkg/loader"
3030 "sigs.k8s.io/controller-tools/pkg/markers"
31+ "sigs.k8s.io/controller-tools/pkg/metrics/internal/config"
3132)
3233
3334func Test_Generate(t *testing.T) {
@@ -68,28 +69,33 @@ func Test_Generate(t *testing.T) {
6869 t.Error(err)
6970 }
7071
71- foo := out.buf.String()
72- fmt.Println("<<>>")
73- fmt.Println(foo)
74- fmt.Println("<<>>")
7572 output := strings.Split(out.buf.String(), "\n---\n")
7673
77- if len(output) != 2 {
74+ header := fmt.Sprintf(headerText, "(devel)", config.KubeStateMetricsVersion)
75+
76+ if len(output) != 3 {
7877 t.Error("Expected two output files, metrics configuration followed by rbac.")
78+ return
79+ }
80+
81+ generatedData := map[string]string{
82+ "metrics.yaml": header + "---\n" + string(output[1]),
83+ "rbac.yaml": "---\n" + string(output[2]),
7984 }
8085
8186 t.Log("Comparing output to testdata to check for regressions")
8287
83- for i , golden := range []string{"metrics.yaml", "rbac.yaml"} {
84- generatedRaw := strings.TrimSpace(output[i])
88+ for _ , golden := range []string{"metrics.yaml", "rbac.yaml"} {
89+ // generatedRaw := strings.TrimSpace(output[i])
8590
8691 expectedRaw, err := os.ReadFile(path.Clean(path.Join(cwd, "testdata", golden)))
8792 if err != nil {
8893 t.Error(err)
94+ return
8995 }
9096
9197 // Remove leading `---` and trim newlines
92- generated := strings.TrimSpace(strings.TrimPrefix(generatedRaw , "---"))
98+ generated := strings.TrimSpace(strings.TrimPrefix(generatedData[golden] , "---"))
9399 expected := strings.TrimSpace(strings.TrimPrefix(string(expectedRaw), "---"))
94100
95101 diff := cmp.Diff(expected, generated)
@@ -101,6 +107,7 @@ func Test_Generate(t *testing.T) {
101107 t.Logf("Expected output to match file `testdata/%s` but it does not.", golden)
102108 t.Logf("If the change is intended, use `go generate ./pkg/metrics/testdata` to regenerate the `testdata/%s` file.", golden)
103109 t.Errorf("Detected a diff between the output of the integration test and the file `testdata/%s`.", golden)
110+ return
104111 }
105112 }
106113}
0 commit comments