...
1
2
3
4
5
6
7
8
9
10
11
12
13
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
33
34
35 var _ = Describe("Finalizer", func() {
36
37 BeforeEach(func() {
38
39 })
40
41 AfterEach(func() {
42
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.PollUntilContextTimeout(context.TODO(), time.Second, time.Second*10, true,
77 func(ctx context.Context) (ok bool, err error) {
78 err = k8sClient.Get(ctx, key, chaos)
79 if err != nil {
80 return false, err
81 }
82 return len(chaos.GetObjectMeta().GetFinalizers()) > 0 && chaos.GetObjectMeta().GetFinalizers()[0] == finalizers.RecordFinalizer, nil
83 })
84 Expect(err).ToNot(HaveOccurred())
85 }
86
87 By("deleting the created object")
88 {
89 Expect(k8sClient.Delete(context.TODO(), chaos)).To(Succeed())
90 }
91 })
92 })
93 })
94