github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/controller/component/monitor_utils_test.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package component
    21  
    22  import (
    23  	. "github.com/onsi/ginkgo/v2"
    24  	. "github.com/onsi/gomega"
    25  	"k8s.io/apimachinery/pkg/util/intstr"
    26  
    27  	appsv1alpha1 "github.com/1aal/kubeblocks/apis/apps/v1alpha1"
    28  )
    29  
    30  var _ = Describe("monitor_utils", func() {
    31  	Context("has the buildMonitorConfig function", func() {
    32  		var component *SynthesizedComponent
    33  		var clusterCompSpec *appsv1alpha1.ClusterComponentSpec
    34  		var clusterCompDef *appsv1alpha1.ClusterComponentDefinition
    35  
    36  		BeforeEach(func() {
    37  			component = &SynthesizedComponent{}
    38  			clusterCompSpec = &appsv1alpha1.ClusterComponentSpec{}
    39  			clusterCompSpec.Monitor = true
    40  			clusterCompDef = &appsv1alpha1.ClusterComponentDefinition{}
    41  			clusterCompDef.Monitor = &appsv1alpha1.MonitorConfig{
    42  				BuiltIn: false,
    43  				Exporter: &appsv1alpha1.ExporterConfig{
    44  					ScrapePort: intstr.FromInt(9144),
    45  					ScrapePath: "/metrics",
    46  				},
    47  			}
    48  		})
    49  
    50  		It("should disable monitor if ClusterComponentSpec.Monitor is false", func() {
    51  			clusterCompSpec.Monitor = false
    52  			buildMonitorConfig(clusterCompDef, clusterCompSpec, component)
    53  			monitorConfig := component.Monitor
    54  			Expect(monitorConfig.Enable).Should(BeFalse())
    55  			Expect(monitorConfig.BuiltIn).Should(BeFalse())
    56  			Expect(monitorConfig.ScrapePort).To(BeEquivalentTo(0))
    57  			Expect(monitorConfig.ScrapePath).To(Equal(""))
    58  		})
    59  
    60  		It("should disable builtin monitor if ClusterComponentDefinition.Monitor.BuiltIn is false and has valid ExporterConfig", func() {
    61  			clusterCompSpec.Monitor = true
    62  			clusterCompDef.Monitor.BuiltIn = false
    63  			buildMonitorConfig(clusterCompDef, clusterCompSpec, component)
    64  			monitorConfig := component.Monitor
    65  			Expect(monitorConfig.Enable).Should(BeTrue())
    66  			Expect(monitorConfig.BuiltIn).Should(BeFalse())
    67  			Expect(monitorConfig.ScrapePort).To(BeEquivalentTo(9144))
    68  			Expect(monitorConfig.ScrapePath).To(Equal("/metrics"))
    69  		})
    70  
    71  		It("should disable monitor if ClusterComponentDefinition.Monitor.BuiltIn is false and lack of ExporterConfig", func() {
    72  			clusterCompSpec.Monitor = true
    73  			clusterCompDef.Monitor.BuiltIn = false
    74  			clusterCompDef.Monitor.Exporter = nil
    75  			buildMonitorConfig(clusterCompDef, clusterCompSpec, component)
    76  			monitorConfig := component.Monitor
    77  			Expect(monitorConfig.Enable).Should(BeFalse())
    78  			Expect(monitorConfig.BuiltIn).Should(BeFalse())
    79  			Expect(monitorConfig.ScrapePort).To(BeEquivalentTo(0))
    80  			Expect(monitorConfig.ScrapePath).To(Equal(""))
    81  		})
    82  
    83  		It("should enable monitor if ClusterComponentDefinition.Monitor.BuiltIn is true", func() {
    84  			clusterCompSpec.Monitor = true
    85  			clusterCompDef.Monitor.BuiltIn = true
    86  			clusterCompDef.Monitor.Exporter = nil
    87  			buildMonitorConfig(clusterCompDef, clusterCompSpec, component)
    88  			monitorConfig := component.Monitor
    89  			Expect(monitorConfig.Enable).Should(BeTrue())
    90  			Expect(monitorConfig.BuiltIn).Should(BeTrue())
    91  			Expect(monitorConfig.ScrapePort).To(BeEquivalentTo(0))
    92  			Expect(monitorConfig.ScrapePath).To(Equal(""))
    93  		})
    94  	})
    95  })