1 package conditions
2
3 import (
4 "testing"
5
6 fuzz "github.com/AdaLogics/go-fuzz-headers"
7 . "github.com/onsi/gomega"
8 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9 )
10
11 func TestMatchConditions(t *testing.T) {
12 testCases := []struct {
13 name string
14 actual interface{}
15 expected []metav1.Condition
16 expectMatch bool
17 }{
18 {
19 name: "with an empty conditions",
20 actual: []metav1.Condition{},
21 expected: []metav1.Condition{},
22 expectMatch: true,
23 },
24 {
25 name: "with matching conditions",
26 actual: []metav1.Condition{
27 {
28 Type: "type",
29 Status: metav1.ConditionTrue,
30 LastTransitionTime: metav1.Now(),
31 Reason: "reason",
32 Message: "message",
33 },
34 },
35 expected: []metav1.Condition{
36 {
37 Type: "type",
38 Status: metav1.ConditionTrue,
39 LastTransitionTime: metav1.Now(),
40 Reason: "reason",
41 Message: "message",
42 },
43 },
44 expectMatch: true,
45 },
46 {
47 name: "with non-matching conditions",
48 actual: []metav1.Condition{
49 {
50 Type: "type",
51 Status: metav1.ConditionTrue,
52 LastTransitionTime: metav1.Now(),
53 Reason: "reason",
54 Message: "message",
55 },
56 {
57 Type: "type",
58 Status: metav1.ConditionTrue,
59 LastTransitionTime: metav1.Now(),
60 Reason: "reason",
61 Message: "message",
62 },
63 },
64 expected: []metav1.Condition{
65 {
66 Type: "type",
67 Status: metav1.ConditionTrue,
68 LastTransitionTime: metav1.Now(),
69 Reason: "reason",
70 Message: "message",
71 },
72 {
73 Type: "different",
74 Status: metav1.ConditionTrue,
75 LastTransitionTime: metav1.Now(),
76 Reason: "different",
77 Message: "different",
78 },
79 },
80 expectMatch: false,
81 },
82 {
83 name: "with a different number of conditions",
84 actual: []metav1.Condition{
85 {
86 Type: "type",
87 Status: metav1.ConditionTrue,
88 LastTransitionTime: metav1.Now(),
89 Reason: "reason",
90 Message: "message",
91 },
92 {
93 Type: "type",
94 Status: metav1.ConditionTrue,
95 LastTransitionTime: metav1.Now(),
96 Reason: "reason",
97 Message: "message",
98 },
99 },
100 expected: []metav1.Condition{
101 {
102 Type: "type",
103 Status: metav1.ConditionTrue,
104 LastTransitionTime: metav1.Now(),
105 Reason: "reason",
106 Message: "message",
107 },
108 },
109 expectMatch: false,
110 },
111 }
112
113 for _, tc := range testCases {
114 t.Run(tc.name, func(t *testing.T) {
115 g := NewWithT(t)
116 if tc.expectMatch {
117 g.Expect(tc.actual).To(MatchConditions(tc.expected))
118 } else {
119 g.Expect(tc.actual).ToNot(MatchConditions(tc.expected))
120 }
121 })
122 }
123 }
124
125 func TestMatchCondition(t *testing.T) {
126 testCases := []struct {
127 name string
128 actual interface{}
129 expected metav1.Condition
130 expectMatch bool
131 }{
132 {
133 name: "with an empty condition",
134 actual: metav1.Condition{},
135 expected: metav1.Condition{},
136 expectMatch: true,
137 },
138 {
139 name: "with a matching condition",
140 actual: metav1.Condition{
141 Type: "type",
142 Status: metav1.ConditionTrue,
143 LastTransitionTime: metav1.Now(),
144 Reason: "reason",
145 Message: "message",
146 },
147 expected: metav1.Condition{
148 Type: "type",
149 Status: metav1.ConditionTrue,
150 LastTransitionTime: metav1.Now(),
151 Reason: "reason",
152 Message: "message",
153 },
154 expectMatch: true,
155 },
156 {
157 name: "with a different time",
158 actual: metav1.Condition{
159 Type: "type",
160 Status: metav1.ConditionTrue,
161 LastTransitionTime: metav1.Now(),
162 Reason: "reason",
163 Message: "message",
164 },
165 expected: metav1.Condition{
166 Type: "type",
167 Status: metav1.ConditionTrue,
168 LastTransitionTime: metav1.Time{},
169 Reason: "reason",
170 Message: "message",
171 },
172 expectMatch: true,
173 },
174 {
175 name: "with a different type",
176 actual: metav1.Condition{
177 Type: "type",
178 Status: metav1.ConditionTrue,
179 LastTransitionTime: metav1.Now(),
180 Reason: "reason",
181 Message: "message",
182 },
183 expected: metav1.Condition{
184 Type: "different",
185 Status: metav1.ConditionTrue,
186 LastTransitionTime: metav1.Now(),
187 Reason: "reason",
188 Message: "message",
189 },
190 expectMatch: false,
191 },
192 {
193 name: "with a different status",
194 actual: metav1.Condition{
195 Type: "type",
196 Status: metav1.ConditionTrue,
197 LastTransitionTime: metav1.Now(),
198 Reason: "reason",
199 Message: "message",
200 },
201 expected: metav1.Condition{
202 Type: "type",
203 Status: metav1.ConditionFalse,
204 LastTransitionTime: metav1.Now(),
205 Reason: "reason",
206 Message: "message",
207 },
208 expectMatch: false,
209 },
210 {
211 name: "with a different reason",
212 actual: metav1.Condition{
213 Type: "type",
214 Status: metav1.ConditionTrue,
215 LastTransitionTime: metav1.Now(),
216 Reason: "reason",
217 Message: "message",
218 },
219 expected: metav1.Condition{
220 Type: "type",
221 Status: metav1.ConditionTrue,
222 LastTransitionTime: metav1.Now(),
223 Reason: "different",
224 Message: "message",
225 },
226 expectMatch: false,
227 },
228 {
229 name: "with a different message",
230 actual: metav1.Condition{
231 Type: "type",
232 Status: metav1.ConditionTrue,
233 LastTransitionTime: metav1.Now(),
234 Reason: "reason",
235 Message: "message",
236 },
237 expected: metav1.Condition{
238 Type: "type",
239 Status: metav1.ConditionTrue,
240 LastTransitionTime: metav1.Now(),
241 Reason: "reason",
242 Message: "different",
243 },
244 expectMatch: false,
245 },
246 {
247 name: "with a subset message",
248 actual: metav1.Condition{
249 Type: "type",
250 Status: metav1.ConditionTrue,
251 LastTransitionTime: metav1.Now(),
252 Reason: "reason",
253 Message: "message",
254 },
255 expected: metav1.Condition{
256 Type: "type",
257 Status: metav1.ConditionTrue,
258 LastTransitionTime: metav1.Now(),
259 Reason: "reason",
260 Message: "mes",
261 },
262 expectMatch: true,
263 },
264 }
265
266 for _, tc := range testCases {
267 t.Run(tc.name, func(t *testing.T) {
268 g := NewWithT(t)
269 if tc.expectMatch {
270 g.Expect(tc.actual).To(MatchCondition(tc.expected))
271 } else {
272 g.Expect(tc.actual).ToNot(MatchCondition(tc.expected))
273 }
274 })
275 }
276 }
277
278 func Fuzz_Match(f *testing.F) {
279 f.Fuzz(func(_ *testing.T, data []byte) {
280 f := fuzz.NewConsumer(data)
281 condition := metav1.Condition{}
282 err := f.GenerateStruct(&condition)
283 if err != nil {
284 return
285 }
286 m := MatchCondition(condition)
287
288 actual := metav1.Condition{}
289 err = f.GenerateStruct(&actual)
290
291 if err == nil {
292 _, _ = m.Match(actual)
293 }
294 })
295 }
296
View as plain text