forked from fluxcd/pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetter_test.go
More file actions
429 lines (357 loc) · 12.7 KB
/
Copy pathsetter_test.go
File metadata and controls
429 lines (357 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
Copyright 2020 The Kubernetes Authors.
Copyright 2021 The Flux authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
This file is modified from the source at
https://github.com/kubernetes-sigs/cluster-api/tree/7478817225e0a75acb6e14fc7b438231578073d2/util/conditions/setter_test.go,
and initially adapted to work with the `metav1.Condition` and `metav1.ConditionStatus` types.
More concretely, this includes the removal of "condition severity" related functionalities, as this is not supported by
the `metav1.Condition` type.
*/
package conditions
import (
"testing"
"time"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/format"
"github.com/onsi/gomega/types"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/fluxcd/pkg/apis/meta"
"github.com/fluxcd/pkg/runtime/conditions/testdata"
)
func TestHasSameState(t *testing.T) {
g := NewWithT(t)
// same condition
true2 := true1.DeepCopy()
g.Expect(hasSameState(true1, true2)).To(BeTrue())
// different LastTransitionTime does not impact state
true2 = true1.DeepCopy()
true2.LastTransitionTime = metav1.NewTime(time.Date(1900, time.November, 10, 23, 0, 0, 0, time.UTC))
g.Expect(hasSameState(true1, true2)).To(BeTrue())
// different ObservedGeneration does not impact state
true2 = true1.DeepCopy()
true2.ObservedGeneration = 1
g.Expect(hasSameState(true1, true2)).To(BeTrue())
// different Type, Status, Reason, and Message determine
// different state
true2 = true1.DeepCopy()
true2.Type = "another type"
g.Expect(hasSameState(true1, true2)).To(BeFalse())
true2 = true1.DeepCopy()
true2.Status = metav1.ConditionFalse
g.Expect(hasSameState(true1, true2)).To(BeFalse())
true2 = true1.DeepCopy()
true2.Message = "another message"
g.Expect(hasSameState(true1, true2)).To(BeFalse())
}
func TestLexicographicLess(t *testing.T) {
g := NewWithT(t)
// alphabetical order of Type is respected
a := TrueCondition("A", "", "")
b := TrueCondition("B", "", "")
g.Expect(lexicographicLess(a, b)).To(BeTrue())
a = TrueCondition("B", "", "")
b = TrueCondition("A", "", "")
g.Expect(lexicographicLess(a, b)).To(BeFalse())
// observed generation is respected
a = TrueCondition("A", "", "")
a.ObservedGeneration = 2
b = TrueCondition("B", "", "")
b.ObservedGeneration = 2
g.Expect(lexicographicLess(a, b)).To(BeTrue())
a = TrueCondition("A", "", "")
a.ObservedGeneration = 1
b = TrueCondition("B", "", "")
b.ObservedGeneration = 2
g.Expect(lexicographicLess(a, b)).To(BeFalse())
a = TrueCondition("A", "", "")
a.ObservedGeneration = 1
b = TrueCondition("B", "", "")
b.ObservedGeneration = 0
g.Expect(lexicographicLess(a, b)).To(BeTrue())
// Disregard Type when observed generations aren't equal.
c := TrueCondition("C", "", "")
c.ObservedGeneration = 1
b = TrueCondition("B", "", "")
b.ObservedGeneration = 0
g.Expect(lexicographicLess(c, b)).To(BeTrue())
// Stalled, Ready, and Reconciling conditions are threaded as an
// exception and always go first.
stalled := TrueCondition(meta.StalledCondition, "", "")
ready := FalseCondition(meta.ReadyCondition, "", "")
reconciling := TrueCondition(meta.ReconcilingCondition, "", "")
g.Expect(lexicographicLess(stalled, ready)).To(BeTrue())
g.Expect(lexicographicLess(ready, stalled)).To(BeFalse())
g.Expect(lexicographicLess(reconciling, ready)).To(BeTrue())
g.Expect(lexicographicLess(ready, reconciling)).To(BeFalse())
g.Expect(lexicographicLess(stalled, reconciling)).To(BeTrue())
g.Expect(lexicographicLess(reconciling, stalled)).To(BeFalse())
g.Expect(lexicographicLess(ready, b)).To(BeTrue())
g.Expect(lexicographicLess(b, ready)).To(BeFalse())
ready.ObservedGeneration = 1
b.ObservedGeneration = 2
g.Expect(lexicographicLess(ready, b)).To(BeTrue())
}
func TestSet(t *testing.T) {
a := TrueCondition("a", "", "")
b := TrueCondition("b", "", "")
ready := TrueCondition(meta.ReadyCondition, "", "")
tests := []struct {
name string
to Setter
condition *metav1.Condition
want []metav1.Condition
}{
{
name: "Set adds a condition",
to: setterWithConditions(),
condition: a,
want: conditionList(a),
},
{
name: "Set adds more conditions",
to: setterWithConditions(a),
condition: b,
want: conditionList(a, b),
},
{
name: "Set does not duplicate existing conditions",
to: setterWithConditions(a, b),
condition: a,
want: conditionList(a, b),
},
{
name: "Set sorts conditions in lexicographic order",
to: setterWithConditions(b, a),
condition: ready,
want: conditionList(ready, a, b),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
Set(tt.to, tt.condition)
g.Expect(tt.to.GetConditions()).To(haveSameConditionsOf(tt.want))
})
}
}
func TestSetLastTransitionTime(t *testing.T) {
x := metav1.Date(2012, time.January, 1, 12, 15, 30, 5e8, time.UTC)
foo := FalseCondition("foo", "reason foo", "message foo")
fooWithLastTransitionTime := FalseCondition("foo", "reason foo", "message foo")
fooWithLastTransitionTime.LastTransitionTime = x
fooWithAnotherState := TrueCondition("foo", "", "")
tests := []struct {
name string
to Setter
new *metav1.Condition
LastTransitionTimeCheck func(*WithT, metav1.Time)
}{
{
name: "Set a condition that does not exists should set the last transition time if not defined",
to: setterWithConditions(),
new: foo,
LastTransitionTimeCheck: func(g *WithT, lastTransitionTime metav1.Time) {
g.Expect(lastTransitionTime).ToNot(BeZero())
},
},
{
name: "Set a condition that does not exists should preserve the last transition time if defined",
to: setterWithConditions(),
new: fooWithLastTransitionTime,
LastTransitionTimeCheck: func(g *WithT, lastTransitionTime metav1.Time) {
g.Expect(lastTransitionTime).To(Equal(x))
},
},
{
name: "Set a condition that already exists with the same state should preserves the last transition time",
to: setterWithConditions(fooWithLastTransitionTime),
new: foo,
LastTransitionTimeCheck: func(g *WithT, lastTransitionTime metav1.Time) {
g.Expect(lastTransitionTime).To(Equal(x))
},
},
{
name: "Set a condition that already exists but with different state should changes the last transition time",
to: setterWithConditions(fooWithLastTransitionTime),
new: fooWithAnotherState,
LastTransitionTimeCheck: func(g *WithT, lastTransitionTime metav1.Time) {
g.Expect(lastTransitionTime).ToNot(Equal(x))
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
Set(tt.to, tt.new)
tt.LastTransitionTimeCheck(g, Get(tt.to, "foo").LastTransitionTime)
})
}
}
func TestSetObservedGeneration(t *testing.T) {
g := NewWithT(t)
obj := &testdata.Fake{}
x := metav1.Date(2012, time.January, 1, 12, 15, 30, 5e8, time.UTC)
// Conditions with stale observed generation.
foo1 := FalseCondition("foo1", "reasonFoo1", "messageFoo1")
foo1.ObservedGeneration = 2
foo1.LastTransitionTime = x
foo2 := TrueCondition("foo2", "reasonFoo2", "messageFoo2")
foo2.ObservedGeneration = 3
foo2.LastTransitionTime = x
// Object with higher generation and stale conditions.
obj.Generation = 4
obj.SetConditions([]metav1.Condition{*foo1, *foo2})
// Ensure the conditions haven't updated.
g.Expect(Get(obj, "foo1").ObservedGeneration).To(BeEquivalentTo(2))
g.Expect(Get(obj, "foo2").ObservedGeneration).To(BeEquivalentTo(3))
// Update the object's generation and Set the conditions without any state
// change.
obj.Generation = 5
Set(obj, foo1)
Set(obj, foo2)
// ObservedGeneration is updated but not the LastTransitionTime.
g.Expect(Get(obj, "foo1").ObservedGeneration).To(BeEquivalentTo(5))
g.Expect(Get(obj, "foo1").LastTransitionTime).To(Equal(x))
g.Expect(Get(obj, "foo2").ObservedGeneration).To(BeEquivalentTo(5))
g.Expect(Get(obj, "foo2").LastTransitionTime).To(Equal(x))
}
func TestMarkMethods(t *testing.T) {
g := NewWithT(t)
obj := &testdata.Fake{}
// test MarkTrue
MarkTrue(obj, "conditionFoo", "reasonFoo", "messageFoo")
g.Expect(Get(obj, "conditionFoo")).To(HaveSameStateOf(&metav1.Condition{
Type: "conditionFoo",
Status: metav1.ConditionTrue,
Reason: "reasonFoo",
Message: "messageFoo",
}))
// test MarkFalse
MarkFalse(obj, "conditionBar", "reasonBar", "messageBar")
g.Expect(Get(obj, "conditionBar")).To(HaveSameStateOf(&metav1.Condition{
Type: "conditionBar",
Status: metav1.ConditionFalse,
Reason: "reasonBar",
Message: "messageBar",
}))
// test MarkUnknown
MarkUnknown(obj, "conditionBaz", "reasonBaz", "messageBaz")
g.Expect(Get(obj, "conditionBaz")).To(HaveSameStateOf(&metav1.Condition{
Type: "conditionBaz",
Status: metav1.ConditionUnknown,
Reason: "reasonBaz",
Message: "messageBaz",
}))
// test MarkReconciling
MarkTrue(obj, meta.StalledCondition, "reasonStalled", "messageStalled")
MarkReconciling(obj, "reasonReconciling", "messageReconciling")
g.Expect(Get(obj, meta.ReconcilingCondition)).To(HaveSameStateOf(&metav1.Condition{
Type: meta.ReconcilingCondition,
Status: metav1.ConditionTrue,
Reason: "reasonReconciling",
Message: "messageReconciling",
}))
g.Expect(IsUnknown(obj, meta.StalledCondition)).To(BeTrue())
// test MarkStalled
MarkTrue(obj, meta.ReconcilingCondition, "reasonReconciling", "messageReconciling")
MarkStalled(obj, "reasonStalled", "messageStalled")
g.Expect(Get(obj, meta.StalledCondition)).To(HaveSameStateOf(&metav1.Condition{
Type: meta.StalledCondition,
Status: metav1.ConditionTrue,
Reason: "reasonStalled",
Message: "messageStalled",
}))
g.Expect(IsUnknown(obj, meta.ReconcilingCondition)).To(BeTrue())
}
func TestSetSummary(t *testing.T) {
g := NewWithT(t)
target := setterWithConditions(TrueCondition("foo", "", ""))
SetSummary(target, "test")
g.Expect(Has(target, "test")).To(BeTrue())
}
func TestSetMirror(t *testing.T) {
g := NewWithT(t)
source := getterWithConditions(TrueCondition(meta.ReadyCondition, "", ""))
target := setterWithConditions()
SetMirror(target, "foo", source)
g.Expect(Has(target, "foo")).To(BeTrue())
}
func TestSetAggregate(t *testing.T) {
g := NewWithT(t)
source1 := getterWithConditions(TrueCondition(meta.ReadyCondition, "", ""))
source2 := getterWithConditions(TrueCondition(meta.ReadyCondition, "", ""))
target := setterWithConditions()
SetAggregate(target, "foo", []Getter{source1, source2})
g.Expect(Has(target, "foo")).To(BeTrue())
}
func TestTrimConditionMessage(t *testing.T) {
tests := []struct {
message string
maxLength int
expected string
}{
{
message: "message too long",
maxLength: 10,
expected: "message...",
},
{
message: "message that fits",
maxLength: 17,
expected: "message that fits",
},
}
for _, tt := range tests {
t.Run(tt.message, func(t *testing.T) {
g := NewWithT(t)
condition := TrueCondition(meta.ReadyCondition, "", "%s", tt.message)
condition.Message = trimConditionMessage(condition.Message, tt.maxLength)
g.Expect(len(condition.Message)).To(Equal(tt.maxLength))
g.Expect(condition.Message).To(Equal(tt.expected))
})
}
}
func setterWithConditions(conditions ...*metav1.Condition) Setter {
obj := &testdata.Fake{}
obj.SetConditions(conditionList(conditions...))
return obj
}
func haveSameConditionsOf(expected []metav1.Condition) types.GomegaMatcher {
return &ConditionsMatcher{
Expected: expected,
}
}
type ConditionsMatcher struct {
Expected []metav1.Condition
}
func (matcher *ConditionsMatcher) Match(actual interface{}) (success bool, err error) {
actualConditions, ok := actual.([]metav1.Condition)
if !ok {
return false, errors.New("Value should be a conditions list")
}
if len(actualConditions) != len(matcher.Expected) {
return false, nil
}
for i := range actualConditions {
if !hasSameState(&actualConditions[i], &matcher.Expected[i]) {
return false, nil
}
}
return true, nil
}
func (matcher *ConditionsMatcher) FailureMessage(actual interface{}) (message string) {
return format.Message(actual, "to have the same conditions of", matcher.Expected)
}
func (matcher *ConditionsMatcher) NegatedFailureMessage(actual interface{}) (message string) {
return format.Message(actual, "not to have the same conditions of", matcher.Expected)
}