...

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

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

     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package finalizers
    15  
    16  import (
    17  	"context"
    18  	"time"
    19  
    20  	. "github.com/onsi/ginkgo"
    21  	. "github.com/onsi/gomega"
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  	"k8s.io/apimachinery/pkg/types"
    24  	"k8s.io/apimachinery/pkg/util/wait"
    25  
    26  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    27  )
    28  
    29  // These tests use Ginkgo (BDD-style Go testing framework). Refer to
    30  // http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
    31  
    32  var _ = Describe("Finalizer", func() {
    33  
    34  	BeforeEach(func() {
    35  		// Add any setup steps that needs to be executed before each test
    36  	})
    37  
    38  	AfterEach(func() {
    39  		// Add any teardown steps that needs to be executed after each test
    40  	})
    41  
    42  	Context("Adding finalizer", func() {
    43  		It("should add record finalizer", func() {
    44  			key := types.NamespacedName{
    45  				Name:      "foo1",
    46  				Namespace: "default",
    47  			}
    48  			duration := "1000s"
    49  			chaos := &v1alpha1.TimeChaos{
    50  				ObjectMeta: metav1.ObjectMeta{
    51  					Name:      "foo1",
    52  					Namespace: "default",
    53  				},
    54  				Spec: v1alpha1.TimeChaosSpec{
    55  					TimeOffset: "100ms",
    56  					ClockIds:   []string{"CLOCK_REALTIME"},
    57  					Duration:   &duration,
    58  					ContainerSelector: v1alpha1.ContainerSelector{
    59  						PodSelector: v1alpha1.PodSelector{
    60  							Mode: v1alpha1.OnePodMode,
    61  						},
    62  					},
    63  				},
    64  			}
    65  
    66  			By("creating a chaos")
    67  			{
    68  				Expect(k8sClient.Create(context.TODO(), chaos)).To(Succeed())
    69  			}
    70  
    71  			By("Adding finalizers")
    72  			{
    73  				err := wait.Poll(time.Second*1, time.Second*10, func() (ok bool, err error) {
    74  					err = k8sClient.Get(context.TODO(), key, chaos)
    75  					if err != nil {
    76  						return false, err
    77  					}
    78  					return len(chaos.GetObjectMeta().GetFinalizers()) > 0 && chaos.GetObjectMeta().GetFinalizers()[0] == RecordFinalizer, nil
    79  				})
    80  				Expect(err).ToNot(HaveOccurred())
    81  			}
    82  
    83  			By("deleting the created object")
    84  			{
    85  				Expect(k8sClient.Delete(context.TODO(), chaos)).To(Succeed())
    86  			}
    87  		})
    88  	})
    89  })
    90