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