Skip to content

Commit 2d7cc92

Browse files
committed
🌱 (chore): add grafana plugin test suite
Add initial test suite and plugin tests for Grafana plugin. Includes tests for plugin initialization, versioning, and scaffolding functionalities.
1 parent 5d3e33b commit 2d7cc92

File tree

4 files changed

+147
-2
lines changed

4 files changed

+147
-2
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha_test
18+
19+
import (
20+
. "github.com/onsi/ginkgo/v2"
21+
. "github.com/onsi/gomega"
22+
"github.com/spf13/afero"
23+
v3 "sigs.k8s.io/kubebuilder/v4/pkg/config/v3"
24+
"sigs.k8s.io/kubebuilder/v4/pkg/machinery"
25+
plugin2 "sigs.k8s.io/kubebuilder/v4/pkg/plugin"
26+
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/grafana/v1alpha"
27+
)
28+
29+
var _ = Describe("Grafana Plugin", func() {
30+
var (
31+
plugin *v1alpha.Plugin
32+
mockFS machinery.Filesystem
33+
)
34+
35+
BeforeEach(func() {
36+
plugin = &v1alpha.Plugin{}
37+
mockFS = machinery.Filesystem{FS: afero.NewMemMapFs()}
38+
39+
cfg := v3.New()
40+
Expect(plugin.GetInitSubcommand().(plugin2.RequiresConfig).InjectConfig(cfg)).ToNot(HaveOccurred())
41+
Expect(plugin.GetEditSubcommand().(plugin2.RequiresConfig).InjectConfig(cfg)).ToNot(HaveOccurred())
42+
})
43+
44+
It("should return the correct plugin name", func() {
45+
Expect(plugin.Name()).To(Equal("grafana.kubebuilder.io"))
46+
})
47+
48+
It("should return the correct plugin version number and stage", func() {
49+
ver := plugin.Version()
50+
Expect(ver.Number).To(Equal(1))
51+
Expect(ver.Stage.String()).To(Equal("alpha"))
52+
})
53+
54+
It("should support project version 3", func() {
55+
versions := plugin.SupportedProjectVersions()
56+
Expect(versions).ToNot(BeEmpty())
57+
Expect(versions[0].String()).To(Equal("3"))
58+
})
59+
60+
It("should scaffold grafana init successfully", func() {
61+
plg := plugin.GetInitSubcommand()
62+
err := plg.Scaffold(mockFS)
63+
Expect(err).NotTo(HaveOccurred())
64+
65+
directory, err := mockFS.FS.Open("grafana")
66+
Expect(err).ToNot(HaveOccurred())
67+
Expect(directory).ToNot(BeNil())
68+
Expect(directory.Readdir(-1)).To(HaveLen(3))
69+
70+
fileInfoResourceMetrics, err := mockFS.FS.Stat("grafana/controller-resources-metrics.json")
71+
Expect(err).ToNot(HaveOccurred())
72+
Expect(fileInfoResourceMetrics.IsDir()).To(BeFalse())
73+
Expect(fileInfoResourceMetrics.Name()).To(Equal("controller-resources-metrics.json"))
74+
Expect(fileInfoResourceMetrics.Size()).To(BeNumerically(">", 0))
75+
76+
fileInfoRuntimeMetrics, err := mockFS.FS.Stat("grafana/controller-runtime-metrics.json")
77+
Expect(err).ToNot(HaveOccurred())
78+
Expect(fileInfoRuntimeMetrics.IsDir()).To(BeFalse())
79+
Expect(fileInfoRuntimeMetrics.Name()).To(Equal("controller-runtime-metrics.json"))
80+
Expect(fileInfoRuntimeMetrics.Size()).To(BeNumerically(">", 0))
81+
82+
fileInfoConfig, err := mockFS.FS.Stat("grafana/custom-metrics/config.yaml")
83+
Expect(err).ToNot(HaveOccurred())
84+
Expect(fileInfoConfig.IsDir()).To(BeFalse())
85+
Expect(fileInfoConfig.Name()).To(Equal("config.yaml"))
86+
Expect(fileInfoConfig.Size()).To(BeNumerically(">", 0))
87+
})
88+
89+
It("should scaffold grafana edit successfully", func() {
90+
err := plugin.GetEditSubcommand().Scaffold(mockFS)
91+
Expect(err).NotTo(HaveOccurred())
92+
93+
directory, err := mockFS.FS.Open("grafana")
94+
Expect(err).ToNot(HaveOccurred())
95+
Expect(directory).ToNot(BeNil())
96+
Expect(directory.Readdir(-1)).To(HaveLen(3))
97+
98+
fileInfoResourceMetrics, err := mockFS.FS.Stat("grafana/controller-resources-metrics.json")
99+
Expect(err).ToNot(HaveOccurred())
100+
Expect(fileInfoResourceMetrics.IsDir()).To(BeFalse())
101+
Expect(fileInfoResourceMetrics.Name()).To(Equal("controller-resources-metrics.json"))
102+
Expect(fileInfoResourceMetrics.Size()).To(BeNumerically(">", 0))
103+
104+
fileInfoRuntimeMetrics, err := mockFS.FS.Stat("grafana/controller-runtime-metrics.json")
105+
Expect(err).ToNot(HaveOccurred())
106+
Expect(fileInfoRuntimeMetrics.IsDir()).To(BeFalse())
107+
Expect(fileInfoRuntimeMetrics.Name()).To(Equal("controller-runtime-metrics.json"))
108+
Expect(fileInfoRuntimeMetrics.Size()).To(BeNumerically(">", 0))
109+
110+
fileInfoConfig, err := mockFS.FS.Stat("grafana/custom-metrics/config.yaml")
111+
Expect(err).ToNot(HaveOccurred())
112+
Expect(fileInfoConfig.IsDir()).To(BeFalse())
113+
Expect(fileInfoConfig.Name()).To(Equal("config.yaml"))
114+
Expect(fileInfoConfig.Size()).To(BeNumerically(">", 0))
115+
})
116+
})
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha_test
18+
19+
import (
20+
"testing"
21+
22+
. "github.com/onsi/ginkgo/v2"
23+
. "github.com/onsi/gomega"
24+
)
25+
26+
func TestGrafanaPlugin(t *testing.T) {
27+
RegisterFailHandler(Fail)
28+
RunSpecs(t, "Grafana Plugin Suite")
29+
}

testdata/project-v4-multigroup/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module sigs.k8s.io/kubebuilder/testdata/project-v4-multigroup
33
go 1.24.0
44

55
require (
6-
github.com/cert-manager/cert-manager v1.18.0
6+
github.com/cert-manager/cert-manager v1.18.1
77
github.com/onsi/ginkgo/v2 v2.22.0
88
github.com/onsi/gomega v1.36.1
99
k8s.io/api v0.33.0

testdata/project-v4/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module sigs.k8s.io/kubebuilder/testdata/project-v4
33
go 1.24.0
44

55
require (
6-
github.com/cert-manager/cert-manager v1.18.0
6+
github.com/cert-manager/cert-manager v1.18.1
77
github.com/onsi/ginkgo/v2 v2.22.0
88
github.com/onsi/gomega v1.36.1
99
k8s.io/api v0.33.0

0 commit comments

Comments
 (0)