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

     1  /*
     2  Copyright The Kubernetes NMState Authors.
     3  
     4  
     5  Licensed under the Apache License, Version 2.0 (the "License");
     6  you may not use this file except in compliance with the License.
     7  You may obtain a copy of the License at
     8  
     9      http://www.apache.org/licenses/LICENSE-2.0
    10  
    11  Unless required by applicable law or agreed to in writing, software
    12  distributed under the License is distributed on an "AS IS" BASIS,
    13  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  See the License for the specific language governing permissions and
    15  limitations under the License.
    16  */
    17  
    18  package handler
    19  
    20  import (
    21  	"context"
    22  	"time"
    23  
    24  	. "github.com/onsi/ginkgo/v2"
    25  	. "github.com/onsi/gomega"
    26  	"k8s.io/apimachinery/pkg/api/errors"
    27  
    28  	nmstate "github.com/nmstate/kubernetes-nmstate/api/shared"
    29  	nmstatev1beta1 "github.com/nmstate/kubernetes-nmstate/api/v1beta1"
    30  	"github.com/nmstate/kubernetes-nmstate/test/e2e/policy"
    31  	testenv "github.com/nmstate/kubernetes-nmstate/test/env"
    32  )
    33  
    34  var _ = Describe("NNCP cleanup", func() {
    35  
    36  	BeforeEach(func() {
    37  		By("Create a policy")
    38  		setDesiredStateWithPolicy(bridge1, linuxBrUp(bridge1))
    39  
    40  		By("Wait for policy to be ready")
    41  		policy.WaitForAvailablePolicy(bridge1)
    42  	})
    43  
    44  	AfterEach(func() {
    45  		deletePolicy(bridge1)
    46  		updateDesiredStateAndWait(linuxBrAbsent(bridge1))
    47  		resetDesiredStateForNodes()
    48  	})
    49  
    50  	Context("when a policy is deleted", func() {
    51  		BeforeEach(func() {
    52  			By("Delete the policy")
    53  			deletePolicy(bridge1)
    54  		})
    55  
    56  		AfterEach(func() {
    57  			deletePolicy(bridge1)
    58  			updateDesiredStateAndWait(linuxBrAbsent(bridge1))
    59  			resetDesiredStateForNodes()
    60  		})
    61  
    62  		It("should also delete nodes enactments", func() {
    63  			for _, node := range nodes {
    64  				Eventually(func() bool {
    65  					key := nmstate.EnactmentKey(node, bridge1)
    66  					enactment := nmstatev1beta1.NodeNetworkConfigurationEnactment{}
    67  					err := testenv.Client.Get(context.TODO(), key, &enactment)
    68  					return errors.IsNotFound(err)
    69  				}, 10*time.Second, 1*time.Second).Should(BeTrue(), "Enactment has not being deleted")
    70  			}
    71  		})
    72  	})
    73  
    74  	Context("when a policy is deleted while one of the nodes is down", func() {
    75  		var restartedNode string
    76  
    77  		BeforeEach(func() {
    78  			restartedNode = nodes[0]
    79  			restartNodeWithoutWaiting(restartedNode)
    80  
    81  			By("Delete the policy")
    82  			deletePolicy(bridge1)
    83  		})
    84  
    85  		It("should also delete nodes enactments", func() {
    86  			for _, node := range nodes {
    87  				if node == restartedNode {
    88  					continue
    89  				}
    90  				verifyEnactmentRemoved(node, 10*time.Second)
    91  			}
    92  
    93  			waitForNodeToStart(restartedNode)
    94  			verifyEnactmentRemoved(restartedNode, 4*time.Minute)
    95  		})
    96  	})
    97  })
    98  
    99  func verifyEnactmentRemoved(node string, timeout time.Duration) {
   100  	Eventually(func() bool {
   101  		key := nmstate.EnactmentKey(node, bridge1)
   102  		enactment := nmstatev1beta1.NodeNetworkConfigurationEnactment{}
   103  		err := testenv.Client.Get(context.TODO(), key, &enactment)
   104  		return errors.IsNotFound(err)
   105  	}, timeout, 1*time.Second).Should(BeTrue(), "Enactment has not being deleted")
   106  }