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.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 chaos.GetStatus().Experiment.DesiredPhase == v1alpha1.RunningPhase, nil
82 })
83 Expect(err).ToNot(HaveOccurred())
84 err = wait.Poll(time.Second*1, time.Second*10, func() (ok bool, err error) {
85 err = k8sClient.Get(context.TODO(), key, chaos)
86 if err != nil {
87 return false, err
88 }
89 return chaos.GetStatus().Experiment.DesiredPhase == v1alpha1.StoppedPhase, nil
90 })
91 Expect(err).ToNot(HaveOccurred())
92 }
93
94 By("deleting the created object")
95 {
96 Expect(k8sClient.Delete(context.TODO(), chaos)).To(Succeed())
97 }
98 })
99 It("should stop paused chaos", func() {
100 key := types.NamespacedName{
101 Name: "foo2",
102 Namespace: "default",
103 }
104 duration := "1000s"
105 chaos := &v1alpha1.TimeChaos{
106 ObjectMeta: metav1.ObjectMeta{
107 Name: "foo2",
108 Namespace: "default",
109 },
110 Spec: v1alpha1.TimeChaosSpec{
111 TimeOffset: "100ms",
112 ClockIds: []string{"CLOCK_REALTIME"},
113 Duration: &duration,
114 ContainerSelector: v1alpha1.ContainerSelector{
115 PodSelector: v1alpha1.PodSelector{
116 Mode: v1alpha1.OneMode,
117 },
118 },
119 },
120 }
121
122 By("creating a chaos")
123 {
124 Expect(k8sClient.Create(context.TODO(), chaos)).To(Succeed())
125 }
126
127 By("Reconciling desired phase")
128 {
129 err := wait.Poll(time.Second*1, time.Second*10, func() (ok bool, err error) {
130 err = k8sClient.Get(context.TODO(), key, chaos)
131 if err != nil {
132 return false, err
133 }
134 return chaos.GetStatus().Experiment.DesiredPhase == v1alpha1.RunningPhase, nil
135 })
136 Expect(err).ToNot(HaveOccurred())
137 }
138 By("Pause chaos")
139 {
140 err := retry.RetryOnConflict(retry.DefaultRetry, func() (err error) {
141 err = k8sClient.Get(context.TODO(), key, chaos)
142 if err != nil {
143 return err
144 }
145 chaos.SetAnnotations(map[string]string{v1alpha1.PauseAnnotationKey: "true"})
146 return k8sClient.Update(context.TODO(), chaos)
147 })
148 Expect(err).ToNot(HaveOccurred())
149 err = wait.Poll(time.Second*5, time.Second*60, func() (ok bool, err error) {
150 err = k8sClient.Get(context.TODO(), key, chaos)
151 if err != nil {
152 return false, err
153 }
154 return chaos.GetStatus().Experiment.DesiredPhase == v1alpha1.StoppedPhase, nil
155 })
156 Expect(err).ToNot(HaveOccurred())
157 }
158
159 By("Resume chaos")
160 {
161 err := retry.RetryOnConflict(retry.DefaultRetry, func() (err error) {
162 err = k8sClient.Get(context.TODO(), key, chaos)
163 if err != nil {
164 return err
165 }
166 chaos.SetAnnotations(map[string]string{v1alpha1.PauseAnnotationKey: "false"})
167 return k8sClient.Update(context.TODO(), chaos)
168 })
169 Expect(err).ToNot(HaveOccurred())
170 err = wait.Poll(time.Second*5, time.Second*60, func() (ok bool, err error) {
171 err = k8sClient.Get(context.TODO(), key, chaos)
172 if err != nil {
173 return false, err
174 }
175 return chaos.GetStatus().Experiment.DesiredPhase == v1alpha1.RunningPhase, nil
176 })
177 Expect(err).ToNot(HaveOccurred())
178 }
179
180 By("deleting the created object")
181 {
182 Expect(k8sClient.Delete(context.TODO(), chaos)).To(Succeed())
183 }
184 })
185 })
186 })
187