Skip to content

Commit 9e4a650

Browse files
author
Moritz Clasmeier
committed
updater tests: factor our coming init code into a shared JustBeforeEach.
1 parent e85b41a commit 9e4a650

File tree

1 file changed

+36
-44
lines changed

1 file changed

+36
-44
lines changed

pkg/reconciler/internal/updater/updater_test.go

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -205,53 +205,45 @@ var _ = Describe("Updater", func() {
205205
Expect(cl.Get(context.TODO(), types.NamespacedName{Namespace: "testNamespace", Name: "testDeployment"}, obj)).To(Succeed())
206206
Expect(obj.GetFinalizers()).ToNot(ContainElement(testFinalizer))
207207
})
208+
Context("when in-cluster object has been updated", func() {
209+
JustBeforeEach(func() {
210+
// Add external status condition on cluster.
211+
clusterObj := obj.DeepCopy()
212+
unknownCondition := map[string]interface{}{
213+
"type": "UnknownCondition",
214+
"status": string(corev1.ConditionTrue),
215+
"reason": "ExternallyManaged",
216+
}
217+
Expect(unstructured.SetNestedSlice(clusterObj.Object, []interface{}{unknownCondition}, "status", "conditions")).To(Succeed())
218+
err := retryOnTransientError(func() error {
219+
return cl.Status().Update(context.TODO(), clusterObj)
220+
})
221+
Expect(err).ToNot(HaveOccurred())
222+
})
208223

209-
It("should preserve unknown status conditions", func() {
210-
// Add external status condition on cluster.
211-
clusterObj := obj.DeepCopy()
212-
unknownCondition := map[string]interface{}{
213-
"type": "UnknownCondition",
214-
"status": string(corev1.ConditionTrue),
215-
"reason": "ExternallyManaged",
216-
}
217-
Expect(unstructured.SetNestedSlice(clusterObj.Object, []interface{}{unknownCondition}, "status", "conditions")).To(Succeed())
218-
err := retryOnTransientError(func() error {
219-
return cl.Status().Update(context.TODO(), clusterObj)
224+
It("should preserve unknown status conditions", func() {
225+
// Add status condition using updater.
226+
u.UpdateStatus(EnsureCondition(conditions.Deployed(corev1.ConditionTrue, "", "")))
227+
u.EnableAggressiveConflictResolution()
228+
Expect(u.Apply(context.TODO(), obj)).To(Succeed())
229+
// Retrieve object from cluster and extract status conditions.
230+
Expect(cl.Get(context.TODO(), types.NamespacedName{Namespace: "testNamespace", Name: "testDeployment"}, obj)).To(Succeed())
231+
objConditionsSlice, _, err := unstructured.NestedSlice(obj.Object, "status", "conditions")
232+
Expect(err).ToNot(HaveOccurred())
233+
objConditions, err := pkgStatus.FromUnstructured(objConditionsSlice)
234+
Expect(err).ToNot(HaveOccurred())
235+
// Verify both status conditions are present.
236+
Expect(objConditions.IsTrueFor(pkgStatus.ConditionType("UnknownCondition"))).To(BeTrue())
237+
Expect(objConditions.IsTrueFor(pkgStatus.ConditionType("Deployed"))).To(BeTrue())
220238
})
221-
Expect(err).ToNot(HaveOccurred())
222-
// Add status condition using updater.
223-
u.UpdateStatus(EnsureCondition(conditions.Deployed(corev1.ConditionTrue, "", "")))
224-
u.EnableAggressiveConflictResolution()
225-
Expect(u.Apply(context.TODO(), obj)).To(Succeed())
226-
// Retrieve object from cluster and extract status conditions.
227-
Expect(cl.Get(context.TODO(), types.NamespacedName{Namespace: "testNamespace", Name: "testDeployment"}, obj)).To(Succeed())
228-
objConditionsSlice, _, err := unstructured.NestedSlice(obj.Object, "status", "conditions")
229-
Expect(err).ToNot(HaveOccurred())
230-
objConditions, err := pkgStatus.FromUnstructured(objConditionsSlice)
231-
Expect(err).ToNot(HaveOccurred())
232-
// Verify both status conditions are present.
233-
Expect(objConditions.IsTrueFor(pkgStatus.ConditionType("UnknownCondition"))).To(BeTrue())
234-
Expect(objConditions.IsTrueFor(pkgStatus.ConditionType("Deployed"))).To(BeTrue())
235-
})
236239

237-
It("should fail on conflict without aggressive resolution", func() {
238-
// Add external status condition on cluster.
239-
clusterObj := obj.DeepCopy()
240-
unknownCondition := map[string]interface{}{
241-
"type": "UnknownCondition",
242-
"status": string(corev1.ConditionTrue),
243-
"reason": "ExternallyManaged",
244-
}
245-
Expect(unstructured.SetNestedSlice(clusterObj.Object, []interface{}{unknownCondition}, "status", "conditions")).To(Succeed())
246-
Expect(retryOnTransientError(func() error {
247-
return cl.Status().Update(context.TODO(), clusterObj)
248-
})).ToNot(HaveOccurred())
249-
Expect(cl.Status().Update(context.TODO(), clusterObj)).To(Succeed())
250-
// Add status condition using updater.
251-
u.UpdateStatus(EnsureCondition(conditions.Deployed(corev1.ConditionTrue, "", "")))
252-
err := u.Apply(context.TODO(), obj)
253-
// Verify conflict error is returned.
254-
Expect(apierrors.IsConflict(err)).To(BeTrue())
240+
It("should fail on conflict without aggressive resolution", func() {
241+
// Add status condition using updater.
242+
u.UpdateStatus(EnsureCondition(conditions.Deployed(corev1.ConditionTrue, "", "")))
243+
err := u.Apply(context.TODO(), obj)
244+
// Verify conflict error is returned.
245+
Expect(apierrors.IsConflict(err)).To(BeTrue())
246+
})
255247
})
256248
})
257249
})

0 commit comments

Comments
 (0)