forked from fluxcd/pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch_test.go
More file actions
291 lines (267 loc) · 8.58 KB
/
Copy pathpatch_test.go
File metadata and controls
291 lines (267 loc) · 8.58 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
/*
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/patch_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"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/fluxcd/pkg/runtime/conditions/testdata"
)
func TestNewPatch(t *testing.T) {
fooTrue := TrueCondition("foo", "reason true", "message true")
fooFalse := FalseCondition("foo", "reason false", "message false")
tests := []struct {
name string
before Getter
after Getter
want Patch
}{
{
name: "No changes return empty patch",
before: getterWithConditions(),
after: getterWithConditions(),
want: nil,
},
{
name: "No changes return empty patch",
before: getterWithConditions(fooTrue),
after: getterWithConditions(fooTrue),
want: nil,
},
{
name: "Detects AddConditionPatch",
before: getterWithConditions(),
after: getterWithConditions(fooTrue),
want: Patch{
{
Before: nil,
After: fooTrue,
Op: AddConditionPatch,
},
},
},
{
name: "Detects ChangeConditionPatch",
before: getterWithConditions(fooTrue),
after: getterWithConditions(fooFalse),
want: Patch{
{
Before: fooTrue,
After: fooFalse,
Op: ChangeConditionPatch,
},
},
},
{
name: "Detects RemoveConditionPatch",
before: getterWithConditions(fooTrue),
after: getterWithConditions(),
want: Patch{
{
Before: fooTrue,
After: nil,
Op: RemoveConditionPatch,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
got := NewPatch(tt.before, tt.after)
g.Expect(got).To(Equal(tt.want))
})
}
}
func TestApply(t *testing.T) {
fooTrue := TrueCondition("foo", "reason true", "message true")
fooFalse := FalseCondition("foo", "reason false", "message false")
fooUnknown := UnknownCondition("foo", "reason unknown", "message unknown")
tests := []struct {
name string
before Getter
after Getter
latest Setter
options []ApplyOption
want []metav1.Condition
wantErr bool
}{
{
name: "No patch return same list",
before: getterWithConditions(fooTrue),
after: getterWithConditions(fooTrue),
latest: setterWithConditions(fooTrue),
want: conditionList(fooTrue),
wantErr: false,
},
{
name: "Add: When a condition does not exists, it should add",
before: getterWithConditions(),
after: getterWithConditions(fooTrue),
latest: setterWithConditions(),
want: conditionList(fooTrue),
wantErr: false,
},
{
name: "Add: When a condition already exists but without conflicts, it should add",
before: getterWithConditions(),
after: getterWithConditions(fooTrue),
latest: setterWithConditions(fooTrue),
want: conditionList(fooTrue),
wantErr: false,
},
{
name: "Add: When a condition already exists but with conflicts, it should error",
before: getterWithConditions(),
after: getterWithConditions(fooTrue),
latest: setterWithConditions(fooFalse),
want: nil,
wantErr: true,
},
{
name: "Add: When a condition already exists but with conflicts, it should not error if the condition is owned",
before: getterWithConditions(),
after: getterWithConditions(fooTrue),
latest: setterWithConditions(fooFalse),
options: []ApplyOption{WithOwnedConditions("foo")},
want: conditionList(fooTrue), // after condition should be kept in case of error
wantErr: false,
},
{
name: "Remove: When a condition was already deleted, it should pass",
before: getterWithConditions(fooTrue),
after: getterWithConditions(),
latest: setterWithConditions(),
want: conditionList(),
wantErr: false,
},
{
name: "Remove: When a condition already exists but without conflicts, it should delete",
before: getterWithConditions(fooTrue),
after: getterWithConditions(),
latest: setterWithConditions(fooTrue),
want: conditionList(),
wantErr: false,
},
{
name: "Remove: When a condition already exists but with conflicts, it should error",
before: getterWithConditions(fooTrue),
after: getterWithConditions(),
latest: setterWithConditions(fooFalse),
want: nil,
wantErr: true,
},
{
name: "Remove: When a condition already exists but with conflicts, it should not error if the condition is owned",
before: getterWithConditions(fooTrue),
after: getterWithConditions(),
latest: setterWithConditions(fooFalse),
options: []ApplyOption{WithOwnedConditions("foo")},
want: conditionList(), // after condition should be kept in case of error
wantErr: false,
},
{
name: "Change: When a condition exists without conflicts, it should change",
before: getterWithConditions(fooTrue),
after: getterWithConditions(fooFalse),
latest: setterWithConditions(fooTrue),
want: conditionList(fooFalse),
wantErr: false,
},
{
name: "Change: When a condition exists with conflicts but there is agreement on the final state, it should change",
before: getterWithConditions(fooFalse),
after: getterWithConditions(fooTrue),
latest: setterWithConditions(fooTrue),
want: conditionList(fooTrue),
wantErr: false,
},
{
name: "Change: When a condition exists with conflicts but there is no agreement on the final state, it should error",
before: getterWithConditions(fooUnknown),
after: getterWithConditions(fooFalse),
latest: setterWithConditions(fooTrue),
want: nil,
wantErr: true,
},
{
name: "Change: When a condition exists with conflicts but there is no agreement on the final state, it should not error if the condition is owned",
before: getterWithConditions(fooUnknown),
after: getterWithConditions(fooFalse),
latest: setterWithConditions(fooTrue),
options: []ApplyOption{WithOwnedConditions("foo")},
want: conditionList(fooFalse), // after condition should be kept in case of error
wantErr: false,
},
{
name: "Change: When a condition was deleted, it should error",
before: getterWithConditions(fooTrue),
after: getterWithConditions(fooFalse),
latest: setterWithConditions(),
want: nil,
wantErr: true,
},
{
name: "Change: When a condition was deleted, it should not error if the condition is owned",
before: getterWithConditions(fooTrue),
after: getterWithConditions(fooFalse),
latest: setterWithConditions(),
options: []ApplyOption{WithOwnedConditions("foo")},
want: conditionList(fooFalse), // after condition should be kept in case of error
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
patch := NewPatch(tt.before, tt.after)
err := patch.Apply(tt.latest, tt.options...)
if tt.wantErr {
g.Expect(err).To(HaveOccurred())
return
}
g.Expect(err).ToNot(HaveOccurred())
g.Expect(tt.latest.GetConditions()).To(haveSameConditionsOf(tt.want))
})
}
}
func TestApplyDoesNotAlterLastTransitionTime(t *testing.T) {
g := NewWithT(t)
before := &testdata.Fake{}
after := &testdata.Fake{
Status: testdata.FakeStatus{
Conditions: []metav1.Condition{
{
Type: "foo",
Status: metav1.ConditionTrue,
LastTransitionTime: metav1.NewTime(time.Now().UTC().Truncate(time.Second)),
},
},
},
}
latest := &testdata.Fake{}
// latest has no conditions, so we are actually adding the
// condition but in this case we should not set the LastTransitionTime
// but we should preserve the LastTransition set in after
diff := NewPatch(before, after)
err := diff.Apply(latest)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(latest.GetConditions()).To(Equal(after.GetConditions()))
}