sigs.k8s.io/kubebuilder/v3@v3.14.0/test/e2e/grafana/generate_test.go (about)

     1  /*
     2  Copyright 2022 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 grafana
    18  
    19  import (
    20  	"path/filepath"
    21  
    22  	pluginutil "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"
    23  
    24  	//nolint:golint
    25  	//nolint:revive
    26  	. "github.com/onsi/ginkgo/v2"
    27  
    28  	//nolint:golint
    29  	//nolint:revive
    30  	. "github.com/onsi/gomega"
    31  
    32  	"sigs.k8s.io/kubebuilder/v3/test/e2e/utils"
    33  )
    34  
    35  var _ = Describe("kubebuilder", func() {
    36  	Context("plugin grafana/v1-alpha", func() {
    37  		var (
    38  			kbc *utils.TestContext
    39  		)
    40  
    41  		BeforeEach(func() {
    42  			var err error
    43  			kbc, err = utils.NewTestContext(pluginutil.KubebuilderBinName, "GO111MODULE=on")
    44  			Expect(err).NotTo(HaveOccurred())
    45  			Expect(kbc.Prepare()).To(Succeed())
    46  		})
    47  
    48  		AfterEach(func() {
    49  			kbc.Destroy()
    50  		})
    51  
    52  		It("should generate a runnable project with grafana plugin", func() {
    53  			GenerateProject(kbc)
    54  		})
    55  
    56  	})
    57  })
    58  
    59  // GenerateProject implements a grafana/v1(-alpha) plugin project defined by a TestContext.
    60  func GenerateProject(kbc *utils.TestContext) {
    61  	var err error
    62  
    63  	By("initializing a project")
    64  	err = kbc.Init(
    65  		"--plugins", "grafana.kubebuilder.io/v1-alpha",
    66  	)
    67  	ExpectWithOffset(1, err).NotTo(HaveOccurred())
    68  
    69  	By("verifying the initial template content and updating for real custom metrics")
    70  	ExpectWithOffset(1, pluginutil.ReplaceInFile(
    71  		filepath.Join(kbc.Dir, "grafana", "custom-metrics", "config.yaml"),
    72  		`---
    73  customMetrics:
    74  #  - metric: # Raw custom metric (required)
    75  #    type:   # Metric type: counter/gauge/histogram (required)
    76  #    expr:   # Prom_ql for the metric (optional)
    77  `,
    78  		`---
    79  customMetrics:
    80    - metric: foo_bar
    81      type: counter
    82    - metric: foo_bar
    83      type: histogram
    84  `)).To(Succeed())
    85  
    86  	By("editing a project based on grafana/custom-metrics/config.yaml")
    87  	err = kbc.Edit(
    88  		"--plugins", "grafana.kubebuilder.io/v1-alpha",
    89  	)
    90  	ExpectWithOffset(1, err).NotTo(HaveOccurred())
    91  
    92  	fileContainsExpr, err := pluginutil.HasFileContentWith(
    93  		filepath.Join(kbc.Dir, "grafana", "custom-metrics", "custom-metrics-dashboard.json"),
    94  		`sum(rate(foo_bar{job=\"$job\", namespace=\"$namespace\"}[5m])) by (instance, pod)`)
    95  	ExpectWithOffset(1, err).NotTo(HaveOccurred())
    96  	ExpectWithOffset(1, fileContainsExpr).To(BeTrue())
    97  
    98  	fileContainsExpr, err = pluginutil.HasFileContentWith(
    99  		filepath.Join(kbc.Dir, "grafana", "custom-metrics", "custom-metrics-dashboard.json"),
   100  		`histogram_quantile(0.90, sum by(instance, le) (rate(foo_bar{job=\"$job\", namespace=\"$namespace\"}[5m])))`)
   101  	ExpectWithOffset(1, err).NotTo(HaveOccurred())
   102  	ExpectWithOffset(1, fileContainsExpr).To(BeTrue())
   103  }