Skip to content

Commit bb189ef

Browse files
committed
metrics: update integration test for rbac creation
1 parent e5e2071 commit bb189ef

File tree

4 files changed

+50
-18
lines changed

4 files changed

+50
-18
lines changed

pkg/metrics/generate_integration_test.go

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ package metrics
1717

1818
import (
1919
"bytes"
20+
"fmt"
2021
"io"
2122
"os"
2223
"path"
24+
"strings"
2325
"testing"
2426

2527
"github.com/google/go-cmp/cmp"
@@ -65,24 +67,41 @@ func Test_Generate(t *testing.T) {
6567
if err := gen.Generate(generationContext); err != nil {
6668
t.Error(err)
6769
}
68-
output := out.buf.String()
6970

70-
t.Log("Comparing output to testdata to check for regressions")
71+
foo := out.buf.String()
72+
fmt.Println("<<>>")
73+
fmt.Println(foo)
74+
fmt.Println("<<>>")
75+
output := strings.Split(out.buf.String(), "\n---\n")
7176

72-
expectedFile, err := os.ReadFile(path.Clean(path.Join(cwd, "testdata", "foo-config.yaml")))
73-
if err != nil {
74-
t.Error(err)
77+
if len(output) != 2 {
78+
t.Error("Expected two output files, metrics configuration followed by rbac.")
7579
}
7680

77-
diff := cmp.Diff(string(expectedFile), output)
78-
if diff != "" {
79-
t.Log("output:")
80-
t.Log(output)
81-
t.Log("diff:")
82-
t.Log(diff)
83-
t.Log("Expected output to match file `testdata/foo-config.yaml` but it does not.")
84-
t.Log("If the change is intended, use `go generate ./pkg/customresourcestate/generate/generator/testdata` to regenerate the `testdata/foo-config.yaml` file.")
85-
t.Error("Detected a diff between the output of the integration test and the file `testdata/foo-config.yaml`.")
81+
t.Log("Comparing output to testdata to check for regressions")
82+
83+
for i, golden := range []string{"metrics.yaml", "rbac.yaml"} {
84+
generatedRaw := strings.TrimSpace(output[i])
85+
86+
expectedRaw, err := os.ReadFile(path.Clean(path.Join(cwd, "testdata", golden)))
87+
if err != nil {
88+
t.Error(err)
89+
}
90+
91+
// Remove leading `---` and trim newlines
92+
generated := strings.TrimSpace(strings.TrimPrefix(generatedRaw, "---"))
93+
expected := strings.TrimSpace(strings.TrimPrefix(string(expectedRaw), "---"))
94+
95+
diff := cmp.Diff(expected, generated)
96+
if diff != "" {
97+
t.Log("generated:")
98+
t.Log(generated)
99+
t.Log("diff:")
100+
t.Log(diff)
101+
t.Logf("Expected output to match file `testdata/%s` but it does not.", golden)
102+
t.Logf("If the change is intended, use `go generate ./pkg/metrics/testdata` to regenerate the `testdata/%s` file.", golden)
103+
t.Errorf("Detected a diff between the output of the integration test and the file `testdata/%s`.", golden)
104+
}
86105
}
87106
}
88107

pkg/metrics/testdata/foo_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ limitations under the License.
1919
// The below marker can be used to regenerate the `foo-config.yaml` file by running
2020
// the following command:
2121
// $ go generate ./pkg/customresourcestate/generate/generator/testdata
22-
//go:generate sh -c "go run ../../../../../ generate ./... > foo-config.yaml"
22+
//go:generate sh -c "go run ../../../cmd/controller-gen metrics output:metrics:dir=. paths=./"
2323
//go:generate sh -c "controller-gen crd paths=./ output:stdout > foo-crd.yaml"
2424

2525
// +groupName=bar.example.com

pkg/metrics/testdata/foo-config.yaml renamed to pkg/metrics/testdata/metrics.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ spec:
1919
metrics:
2020
- each:
2121
gauge:
22-
labelFromKey: ""
2322
nilIsZero: false
2423
path:
2524
- metadata
@@ -30,7 +29,6 @@ spec:
3029
name: created
3130
- each:
3231
info:
33-
labelFromKey: ""
3432
labelsFromPath:
3533
owner_is_controller:
3634
- controller
@@ -66,7 +64,6 @@ spec:
6664
name: status_condition
6765
- each:
6866
gauge:
69-
labelFromKey: ""
7067
labelsFromPath:
7168
status:
7269
- status

pkg/metrics/testdata/rbac.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
apiVersion: rbac.authorization.k8s.io/v1
3+
kind: ClusterRole
4+
metadata:
5+
labels:
6+
kube-state-metrics/aggregate-to-manager: "true"
7+
name: manager-metrics-role
8+
rules:
9+
- apiGroups:
10+
- bar.example.com
11+
resources:
12+
- foos
13+
verbs:
14+
- get
15+
- list
16+
- watch

0 commit comments

Comments
 (0)