...
1
16
17 package feature
18
19 import (
20 "context"
21 "strings"
22 "testing"
23
24 "k8s.io/component-base/metrics/legacyregistry"
25 "k8s.io/component-base/metrics/testutil"
26 )
27
28 var (
29 testedMetrics = []string{"kubernetes_feature_enabled"}
30 )
31
32 func TestObserveHealthcheck(t *testing.T) {
33 defer legacyregistry.Reset()
34 defer ResetFeatureInfoMetric()
35
36 testCases := []struct {
37 desc string
38 name string
39 stage string
40 enabled bool
41 want string
42 }{
43 {
44 desc: "test enabled",
45 name: "feature-a",
46 stage: "ALPHA",
47 enabled: true,
48 want: `
49 # HELP kubernetes_feature_enabled [BETA] This metric records the data about the stage and enablement of a k8s feature.
50 # TYPE kubernetes_feature_enabled gauge
51 kubernetes_feature_enabled{name="feature-a",stage="ALPHA"} 1
52 `,
53 },
54 {
55 desc: "test disabled",
56 name: "feature-b",
57 stage: "BETA",
58 enabled: false,
59 want: `
60 # HELP kubernetes_feature_enabled [BETA] This metric records the data about the stage and enablement of a k8s feature.
61 # TYPE kubernetes_feature_enabled gauge
62 kubernetes_feature_enabled{name="feature-b",stage="BETA"} 0
63 `,
64 },
65 }
66
67 for _, test := range testCases {
68 t.Run(test.desc, func(t *testing.T) {
69 defer ResetFeatureInfoMetric()
70 RecordFeatureInfo(context.Background(), test.name, test.stage, test.enabled)
71
72 if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(test.want), testedMetrics...); err != nil {
73 t.Fatal(err)
74 }
75 })
76 }
77 }
78
View as plain text