github.com/nmstate/kubernetes-nmstate@v0.82.0/test/e2e/handler/metrics_test.go (about)

     1  /*
     2  Copyright The Kubernetes NMState 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 handler
    18  
    19  import (
    20  	"fmt"
    21  	"time"
    22  
    23  	nmstate "github.com/nmstate/kubernetes-nmstate/api/shared"
    24  	"github.com/nmstate/kubernetes-nmstate/pkg/monitoring"
    25  	"github.com/nmstate/kubernetes-nmstate/test/e2e/policy"
    26  	. "github.com/onsi/ginkgo/v2"
    27  	. "github.com/onsi/gomega"
    28  )
    29  
    30  var _ = Describe("Metrics", func() {
    31  	var (
    32  		extraBridgeName               = func() string { return bridge1 + "-extra" }
    33  		linuxBridgeWithCustomHostname = func(bridge string) nmstate.State {
    34  			return nmstate.NewState(fmt.Sprintf(`
    35  interfaces:
    36    - name: %s
    37      type: linux-bridge
    38      state: up
    39      ipv4:
    40        enabled: true
    41        dhcp: true
    42        dhcp-custom-hostname: foo
    43      bridge:
    44        options:
    45          stp:
    46            enabled: false
    47        port: []
    48  `, bridge))
    49  		}
    50  	)
    51  	Context("when desiredState is configured", func() {
    52  		Context("with a state that increase gauge", func() {
    53  			BeforeEach(func() {
    54  				By("Apply first NNCP")
    55  				updateDesiredStateAndWait(linuxBridgeWithCustomHostname(bridge1))
    56  
    57  				By("Apply second NNCP with same features")
    58  				setDesiredStateWithPolicyAndCapture(extraBridgeName(), linuxBridgeWithCustomHostname(extraBridgeName()), map[string]string{})
    59  				policy.WaitForAvailablePolicy(extraBridgeName())
    60  			})
    61  			AfterEach(func() {
    62  				updateDesiredStateAndWait(linuxBrAbsent(bridge1))
    63  				setDesiredStateWithPolicyAndCapture(extraBridgeName(), linuxBrAbsent(extraBridgeName()), map[string]string{})
    64  				policy.WaitForAvailablePolicy(extraBridgeName())
    65  				resetDesiredStateForNodes()
    66  			})
    67  			It("should report a metrics with proper gauge increased", func() {
    68  
    69  				token, err := getPrometheusToken()
    70  				Expect(err).ToNot(HaveOccurred())
    71  				Eventually(func() map[string]string {
    72  					return getMetrics(token)
    73  				}).
    74  					WithPolling(time.Second).
    75  					WithTimeout(2 * time.Second).
    76  					Should(HaveKeyWithValue(monitoring.AppliedFeaturesOpts.Name+`{name="dhcpv4-custom-hostname"}`, "1"))
    77  			})
    78  			Context("and update with an state that decrease the gaugue", func() {
    79  				BeforeEach(func() {
    80  					updateDesiredStateAndWait(linuxBrAbsent(bridge1))
    81  					setDesiredStateWithPolicyAndCapture(extraBridgeName(), linuxBrAbsent(extraBridgeName()), map[string]string{})
    82  					policy.WaitForAvailablePolicy(extraBridgeName())
    83  				})
    84  				It("should report a metrics with proper gauge decrease", func() {
    85  					token, err := getPrometheusToken()
    86  					Expect(err).ToNot(HaveOccurred())
    87  					Eventually(func() map[string]string {
    88  						return getMetrics(token)
    89  					}).
    90  						WithPolling(time.Second).
    91  						WithTimeout(2 * time.Second).
    92  						Should(HaveKeyWithValue(monitoring.AppliedFeaturesOpts.Name+`{name="dhcpv4-custom-hostname"}`, "0"))
    93  				})
    94  			})
    95  		})
    96  	})
    97  })