Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
chrischdi committed Aug 30, 2024
1 parent 1222fc8 commit 9b0a996
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
23 changes: 15 additions & 8 deletions pkg/metrics/generate_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"sigs.k8s.io/controller-tools/pkg/genall"
"sigs.k8s.io/controller-tools/pkg/loader"
"sigs.k8s.io/controller-tools/pkg/markers"
"sigs.k8s.io/controller-tools/pkg/metrics/internal/config"
)

func Test_Generate(t *testing.T) {
Expand Down Expand Up @@ -68,28 +69,33 @@ func Test_Generate(t *testing.T) {
t.Error(err)
}

foo := out.buf.String()
fmt.Println("<<>>")
fmt.Println(foo)
fmt.Println("<<>>")
output := strings.Split(out.buf.String(), "\n---\n")

if len(output) != 2 {
header := fmt.Sprintf(headerText, "(devel)", config.KubeStateMetricsVersion)

if len(output) != 3 {
t.Error("Expected two output files, metrics configuration followed by rbac.")
return
}

generatedData := map[string]string{
"metrics.yaml": header + "---\n" + string(output[1]),
"rbac.yaml": "---\n" + string(output[2]),
}

t.Log("Comparing output to testdata to check for regressions")

for i, golden := range []string{"metrics.yaml", "rbac.yaml"} {
generatedRaw := strings.TrimSpace(output[i])
for _, golden := range []string{"metrics.yaml", "rbac.yaml"} {
// generatedRaw := strings.TrimSpace(output[i])

expectedRaw, err := os.ReadFile(path.Clean(path.Join(cwd, "testdata", golden)))
if err != nil {
t.Error(err)
return
}

// Remove leading `---` and trim newlines
generated := strings.TrimSpace(strings.TrimPrefix(generatedRaw, "---"))
generated := strings.TrimSpace(strings.TrimPrefix(generatedData[golden], "---"))
expected := strings.TrimSpace(strings.TrimPrefix(string(expectedRaw), "---"))

diff := cmp.Diff(expected, generated)
Expand All @@ -101,6 +107,7 @@ func Test_Generate(t *testing.T) {
t.Logf("Expected output to match file `testdata/%s` but it does not.", golden)
t.Logf("If the change is intended, use `go generate ./pkg/metrics/testdata` to regenerate the `testdata/%s` file.", golden)
t.Errorf("Detected a diff between the output of the integration test and the file `testdata/%s`.", golden)
return
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/metrics/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ func (g Generator) RegisterMarkers(into *ctrlmarkers.Registry) error {
return nil
}

const headerText = `---
# Generated by controller-gen version %s
const headerText = `# Generated by controller-gen version %s
# Generated based on types for kube-state-metrics %s
`

Expand Down
2 changes: 1 addition & 1 deletion pkg/metrics/markers/zz_generated.markerhelp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pkg/metrics/testdata/metrics.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---
# Generated by controller-gen version (devel)
# Generated based on types for kube-state-metrics v2.13.0
---
Expand Down

0 comments on commit 9b0a996

Please sign in to comment.