1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package common
17
18 import (
19 "testing"
20
21 . "github.com/onsi/gomega"
22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23 "sigs.k8s.io/controller-runtime/pkg/event"
24
25 "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
26 )
27
28 func TestStatusRecordEventsChangePredicateEventsChange(t *testing.T) {
29 g := NewWithT(t)
30
31
32 newObj := &v1alpha1.HTTPChaos{
33 TypeMeta: metav1.TypeMeta{},
34 ObjectMeta: metav1.ObjectMeta{
35 ResourceVersion: "1",
36 Generation: 1,
37 },
38 Spec: v1alpha1.HTTPChaosSpec{},
39 Status: v1alpha1.HTTPChaosStatus{
40 ChaosStatus: v1alpha1.ChaosStatus{
41 Conditions: nil,
42 Experiment: v1alpha1.ExperimentStatus{
43 DesiredPhase: "",
44 Records: []*v1alpha1.Record{
45 {
46 Id: "",
47 SelectorKey: "",
48 Phase: "",
49 InjectedCount: 0,
50 RecoveredCount: 0,
51 Events: []v1alpha1.RecordEvent{
52 {
53 Type: v1alpha1.TypeFailed,
54 Operation: v1alpha1.Apply,
55 Message: "apply failed",
56 Timestamp: nil,
57 },
58 },
59 },
60 },
61 },
62 },
63 Instances: nil,
64 },
65 }
66 oldObj := &v1alpha1.HTTPChaos{
67 TypeMeta: metav1.TypeMeta{},
68 ObjectMeta: metav1.ObjectMeta{
69 ResourceVersion: "0",
70 Generation: 0,
71 },
72 Spec: v1alpha1.HTTPChaosSpec{},
73 Status: v1alpha1.HTTPChaosStatus{
74 ChaosStatus: v1alpha1.ChaosStatus{
75 Conditions: nil,
76 Experiment: v1alpha1.ExperimentStatus{
77 DesiredPhase: "",
78 Records: []*v1alpha1.Record{
79 {
80 Id: "",
81 SelectorKey: "",
82 Phase: "",
83 InjectedCount: 0,
84 RecoveredCount: 0,
85 Events: nil,
86 },
87 },
88 },
89 },
90 Instances: nil,
91 },
92 }
93
94 predicate := StatusRecordEventsChangePredicate{}
95 updateEvent := event.UpdateEvent{ObjectOld: oldObj, ObjectNew: newObj}
96 pick := predicate.Update(updateEvent)
97 g.Expect(pick).Should(Equal(false))
98 }
99
100 func TestStatusRecordEventsChangePredicatePhaseChange(t *testing.T) {
101 g := NewWithT(t)
102
103 newObj := &v1alpha1.HTTPChaos{
104 TypeMeta: metav1.TypeMeta{},
105 ObjectMeta: metav1.ObjectMeta{
106 ResourceVersion: "1",
107 Generation: 1,
108 },
109 Spec: v1alpha1.HTTPChaosSpec{},
110 Status: v1alpha1.HTTPChaosStatus{
111 ChaosStatus: v1alpha1.ChaosStatus{
112 Conditions: nil,
113 Experiment: v1alpha1.ExperimentStatus{
114 DesiredPhase: v1alpha1.StoppedPhase,
115 Records: nil,
116 },
117 },
118 Instances: nil,
119 },
120 }
121 oldObj := &v1alpha1.HTTPChaos{
122 TypeMeta: metav1.TypeMeta{},
123 ObjectMeta: metav1.ObjectMeta{
124 ResourceVersion: "0",
125 Generation: 0,
126 },
127 Spec: v1alpha1.HTTPChaosSpec{},
128 Status: v1alpha1.HTTPChaosStatus{
129 ChaosStatus: v1alpha1.ChaosStatus{
130 Conditions: nil,
131 Experiment: v1alpha1.ExperimentStatus{
132 DesiredPhase: v1alpha1.RunningPhase,
133 Records: nil,
134 },
135 },
136 Instances: nil,
137 },
138 }
139
140 predicate := StatusRecordEventsChangePredicate{}
141 updateEvent := event.UpdateEvent{ObjectOld: oldObj, ObjectNew: newObj}
142 pick := predicate.Update(updateEvent)
143 g.Expect(pick).Should(Equal(true))
144 }
145
146 func TestStatusRecordEventsChangePredicateChildCRDs(t *testing.T) {
147 g := NewWithT(t)
148
149
150 newObj := &v1alpha1.PodIOChaos{
151 TypeMeta: metav1.TypeMeta{},
152 ObjectMeta: metav1.ObjectMeta{},
153 Spec: v1alpha1.PodIOChaosSpec{},
154 Status: v1alpha1.PodIOChaosStatus{},
155 }
156 oldObj := &v1alpha1.PodIOChaos{
157 TypeMeta: metav1.TypeMeta{},
158 ObjectMeta: metav1.ObjectMeta{},
159 Spec: v1alpha1.PodIOChaosSpec{},
160 Status: v1alpha1.PodIOChaosStatus{},
161 }
162
163 predicate := StatusRecordEventsChangePredicate{}
164 updateEvent := event.UpdateEvent{ObjectOld: oldObj, ObjectNew: newObj}
165 pick := predicate.Update(updateEvent)
166 g.Expect(pick).Should(Equal(false))
167 }
168