|
| 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 | +}) |
0 commit comments