1
2
3
4
5
6
7
8
9
10
11
12
13
14 package controller
15
16 import (
17 "fmt"
18 "testing"
19 "time"
20
21 . "github.com/onsi/gomega"
22 v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23 "k8s.io/utils/pointer"
24
25 "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
26 )
27
28 func makeTestPodKill(creationTime time.Time, duration *string, desiredPhase v1alpha1.DesiredPhase, records []*v1alpha1.Record) v1alpha1.InnerObject {
29 return &v1alpha1.PodChaos{
30 ObjectMeta: v1.ObjectMeta{
31 CreationTimestamp: v1.Time{
32 Time: creationTime,
33 },
34 },
35 Spec: v1alpha1.PodChaosSpec{
36 Action: v1alpha1.PodKillAction,
37 Duration: duration,
38 },
39 Status: v1alpha1.PodChaosStatus{
40 ChaosStatus: v1alpha1.ChaosStatus{
41 Experiment: v1alpha1.ExperimentStatus{
42 DesiredPhase: desiredPhase,
43 Records: records,
44 },
45 },
46 },
47 }
48 }
49 func makeTestNetworkChaos(creationTime time.Time, duration *string, desiredPhase v1alpha1.DesiredPhase, records []*v1alpha1.Record) v1alpha1.InnerObject {
50 return &v1alpha1.NetworkChaos{
51 ObjectMeta: v1.ObjectMeta{
52 CreationTimestamp: v1.Time{
53 Time: creationTime,
54 },
55 },
56 Spec: v1alpha1.NetworkChaosSpec{
57 Duration: duration,
58 },
59 Status: v1alpha1.NetworkChaosStatus{
60 ChaosStatus: v1alpha1.ChaosStatus{
61 Experiment: v1alpha1.ExperimentStatus{
62 DesiredPhase: desiredPhase,
63 Records: records,
64 },
65 },
66 },
67 }
68 }
69
70 func TestIsChaosFinished(t *testing.T) {
71 g := NewGomegaWithT(t)
72
73 type testCase struct {
74 chaos v1alpha1.InnerObject
75 now time.Time
76
77 expected bool
78 }
79
80 beginTime := time.Now()
81 cases := []testCase{
82 {
83 chaos: makeTestNetworkChaos(beginTime, pointer.StringPtr("20s"), v1alpha1.RunningPhase, []*v1alpha1.Record{
84 {
85 Id: "some",
86 SelectorKey: "some",
87 Phase: v1alpha1.Injected,
88 },
89 }),
90 now: beginTime.Add(10 * time.Second),
91
92 expected: false,
93 },
94 {
95 chaos: makeTestNetworkChaos(beginTime, pointer.StringPtr("20s"), v1alpha1.RunningPhase, []*v1alpha1.Record{
96 {
97 Id: "some",
98 SelectorKey: "some",
99 Phase: v1alpha1.NotInjected,
100 },
101 }),
102 now: beginTime.Add(10 * time.Second),
103
104 expected: false,
105 },
106 {
107 chaos: makeTestNetworkChaos(beginTime, pointer.StringPtr("20s"), v1alpha1.RunningPhase, []*v1alpha1.Record{
108 {
109 Id: "some",
110 SelectorKey: "some",
111 Phase: v1alpha1.NotInjected,
112 },
113 }),
114 now: beginTime.Add(30 * time.Second),
115
116 expected: false,
117 },
118 {
119 chaos: makeTestNetworkChaos(beginTime, pointer.StringPtr("20s"), v1alpha1.StoppedPhase, []*v1alpha1.Record{
120 {
121 Id: "some",
122 SelectorKey: "some",
123 Phase: v1alpha1.NotInjected,
124 },
125 }),
126 now: beginTime.Add(30 * time.Second),
127
128 expected: true,
129 },
130 {
131 chaos: makeTestNetworkChaos(beginTime, nil, v1alpha1.RunningPhase, []*v1alpha1.Record{
132 {
133 Id: "some",
134 SelectorKey: "some",
135 Phase: v1alpha1.NotInjected,
136 },
137 }),
138 now: beginTime.Add(30 * time.Second),
139
140 expected: false,
141 },
142 {
143 chaos: makeTestNetworkChaos(beginTime, nil, v1alpha1.RunningPhase, []*v1alpha1.Record{
144 {
145 Id: "some",
146 SelectorKey: "some",
147 Phase: v1alpha1.Injected,
148 },
149 }),
150 now: beginTime.Add(30 * time.Second),
151
152 expected: false,
153 },
154
155 {
156 chaos: makeTestNetworkChaos(beginTime, nil, v1alpha1.StoppedPhase, []*v1alpha1.Record{
157 {
158 Id: "some",
159 SelectorKey: "some",
160 Phase: v1alpha1.Injected,
161 },
162 }),
163 now: beginTime.Add(30 * time.Second),
164
165 expected: false,
166 },
167 {
168 chaos: makeTestPodKill(beginTime, pointer.StringPtr("20s"), v1alpha1.StoppedPhase, []*v1alpha1.Record{
169 {
170 Id: "some",
171 SelectorKey: "some",
172 Phase: v1alpha1.NotInjected,
173 },
174 }),
175 now: beginTime.Add(30 * time.Second),
176
177 expected: false,
178 },
179 {
180 chaos: makeTestPodKill(beginTime, pointer.StringPtr("20s"), v1alpha1.StoppedPhase, []*v1alpha1.Record{
181 {
182 Id: "some",
183 SelectorKey: "some",
184 Phase: v1alpha1.Injected,
185 },
186 }),
187 now: beginTime.Add(30 * time.Second),
188
189 expected: true,
190 },
191 {
192 chaos: makeTestPodKill(beginTime, nil, v1alpha1.StoppedPhase, []*v1alpha1.Record{
193 {
194 Id: "some",
195 SelectorKey: "some",
196 Phase: v1alpha1.Injected,
197 },
198 }),
199 now: beginTime.Add(30 * time.Second),
200
201 expected: true,
202 },
203 }
204
205 for index, c := range cases {
206 if index == 5 {
207 fmt.Println("some")
208 }
209 fmt.Println(index)
210 g.Expect(IsChaosFinished(c.chaos, c.now)).To(Equal(c.expected))
211 }
212 }
213