...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/common/finalizers_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/controllers/common

     1  // Copyright 2021 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package common
    17  
    18  import (
    19  	"context"
    20  	"time"
    21  
    22  	. "github.com/onsi/ginkgo/v2"
    23  	. "github.com/onsi/gomega"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/apimachinery/pkg/types"
    26  	"k8s.io/apimachinery/pkg/util/wait"
    27  
    28  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    29  	"github.com/chaos-mesh/chaos-mesh/controllers/common/finalizers"
    30  )
    31  
    32  // These tests use Ginkgo (BDD-style Go testing framework). Refer to
    33  // http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
    34  
    35  var _ = Describe("Finalizer", func() {
    36  
    37  	BeforeEach(func() {
    38  		// Add any setup steps that needs to be executed before each test
    39  	})
    40  
    41  	AfterEach(func() {
    42  		// Add any teardown steps that needs to be executed after each test
    43  	})
    44  
    45  	Context("Adding finalizer", func() {
    46  		It("should add record finalizer", func() {
    47  			key := types.NamespacedName{
    48  				Name:      "final1",
    49  				Namespace: "default",
    50  			}
    51  			duration := "1000s"
    52  			chaos := &v1alpha1.TimeChaos{
    53  				ObjectMeta: metav1.ObjectMeta{
    54  					Name:      "final1",
    55  					Namespace: "default",
    56  				},
    57  				Spec: v1alpha1.TimeChaosSpec{
    58  					TimeOffset: "100ms",
    59  					ClockIds:   []string{"CLOCK_REALTIME"},
    60  					Duration:   &duration,
    61  					ContainerSelector: v1alpha1.ContainerSelector{
    62  						PodSelector: v1alpha1.PodSelector{
    63  							Mode: v1alpha1.OneMode,
    64  						},
    65  					},
    66  				},
    67  			}
    68  
    69  			By("creating a chaos")
    70  			{
    71  				Expect(k8sClient.Create(context.TODO(), chaos)).To(Succeed())
    72  			}
    73  
    74  			By("Adding finalizers")
    75  			{
    76  				err := wait.Poll(time.Second*1, time.Second*10, func() (ok bool, err error) {
    77  					err = k8sClient.Get(context.TODO(), key, chaos)
    78  					if err != nil {
    79  						return false, err
    80  					}
    81  					return len(chaos.GetObjectMeta().GetFinalizers()) > 0 && chaos.GetObjectMeta().GetFinalizers()[0] == finalizers.RecordFinalizer, nil
    82  				})
    83  				Expect(err).ToNot(HaveOccurred())
    84  			}
    85  
    86  			By("deleting the created object")
    87  			{
    88  				Expect(k8sClient.Delete(context.TODO(), chaos)).To(Succeed())
    89  			}
    90  		})
    91  	})
    92  })
    93