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 "k8s.io/client-go/util/retry"
28
29 "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
30 )
31
32
33
34
35 var _ = Describe("Schedule", func() {
36
37 BeforeEach(func() {
38
39 })
40
41 AfterEach(func() {
42
43 })
44
45 Context("Setting phase", func() {
46 It("should set phase to running", func() {
47 key := types.NamespacedName{
48 Name: "foo1",
49 Namespace: "default",
50 }
51 duration := "10s"
52 chaos := &v1alpha1.TimeChaos{
53 ObjectMeta: metav1.ObjectMeta{
54 Name: "foo1",
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("Reconciling desired phase")
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 chaos.GetStatus().Experiment.DesiredPhase == v1alpha1.RunningPhase, nil
83 })
84 Expect(err).ToNot(HaveOccurred())
85 err = wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Second*10, true,
86 func(ctx context.Context) (ok bool, err error) {
87 err = k8sClient.Get(ctx, key, chaos)
88 if err != nil {
89 return false, err
90 }
91 return chaos.GetStatus().Experiment.DesiredPhase == v1alpha1.StoppedPhase, nil
92 })
93 Expect(err).ToNot(HaveOccurred())
94 }
95
96 By("deleting the created object")
97 {
98 Expect(k8sClient.Delete(context.TODO(), chaos)).To(Succeed())
99 }
100 })
101 It("should stop paused chaos", func() {
102 key := types.NamespacedName{
103 Name: "foo2",
104 Namespace: "default",
105 }
106 duration := "1000s"
107 chaos := &v1alpha1.TimeChaos{
108 ObjectMeta: metav1.ObjectMeta{
109 Name: "foo2",
110 Namespace: "default",
111 },
112 Spec: v1alpha1.TimeChaosSpec{
113 TimeOffset: "100ms",
114 ClockIds: []string{"CLOCK_REALTIME"},
115 Duration: &duration,
116 ContainerSelector: v1alpha1.ContainerSelector{
117 PodSelector: v1alpha1.PodSelector{
118 Mode: v1alpha1.OneMode,
119 },
120 },
121 },
122 }
123
124 By("creating a chaos")
125 {
126 Expect(k8sClient.Create(context.TODO(), chaos)).To(Succeed())
127 }
128
129 By("Reconciling desired phase")
130 {
131 err := wait.PollUntilContextTimeout(context.TODO(), time.Second, time.Second*10, true,
132 func(ctx context.Context) (ok bool, err error) {
133 err = k8sClient.Get(ctx, key, chaos)
134 if err != nil {
135 return false, err
136 }
137 return chaos.GetStatus().Experiment.DesiredPhase == v1alpha1.RunningPhase, nil
138 })
139 Expect(err).ToNot(HaveOccurred())
140 }
141 By("Pause chaos")
142 {
143 err := retry.RetryOnConflict(retry.DefaultRetry, func() (err error) {
144 err = k8sClient.Get(context.TODO(), key, chaos)
145 if err != nil {
146 return err
147 }
148 chaos.SetAnnotations(map[string]string{v1alpha1.PauseAnnotationKey: "true"})
149 return k8sClient.Update(context.TODO(), chaos)
150 })
151 Expect(err).ToNot(HaveOccurred())
152 err = wait.PollUntilContextTimeout(context.TODO(), time.Second*5, time.Second*60, true,
153 func(ctx context.Context) (ok bool, err error) {
154 err = k8sClient.Get(ctx, key, chaos)
155 if err != nil {
156 return false, err
157 }
158 return chaos.GetStatus().Experiment.DesiredPhase == v1alpha1.StoppedPhase, nil
159 })
160 Expect(err).ToNot(HaveOccurred())
161 }
162
163 By("Resume chaos")
164 {
165 err := retry.RetryOnConflict(retry.DefaultRetry, func() (err error) {
166 err = k8sClient.Get(context.TODO(), key, chaos)
167 if err != nil {
168 return err
169 }
170 chaos.SetAnnotations(map[string]string{v1alpha1.PauseAnnotationKey: "false"})
171 return k8sClient.Update(context.TODO(), chaos)
172 })
173 Expect(err).ToNot(HaveOccurred())
174 err = wait.PollUntilContextTimeout(context.TODO(), time.Second*5, time.Second*60, true,
175 func(ctx context.Context) (ok bool, err error) {
176 err = k8sClient.Get(ctx, key, chaos)
177 if err != nil {
178 return false, err
179 }
180 return chaos.GetStatus().Experiment.DesiredPhase == v1alpha1.RunningPhase, nil
181 })
182 Expect(err).ToNot(HaveOccurred())
183 }
184
185 By("deleting the created object")
186 {
187 Expect(k8sClient.Delete(context.TODO(), chaos)).To(Succeed())
188 }
189 })
190 })
191 })
192