Skip to content

Commit 196055a

Browse files
committed
Add test to assert local copy updated post patch
This patch adds a test to assert that the resource passed into CreateOrPatch is updated as part of the CreateOrPatch operation.
1 parent ae8a7c2 commit 196055a

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

pkg/controller/controllerutil/controllerutil_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,17 @@ var _ = Describe("Controllerutil", func() {
449449
specr = deploymentSpecr(deploy, deplSpec)
450450
})
451451

452+
assertLocalDeployWasUpdated := func(fetched *appsv1.Deployment) {
453+
By("local deploy object was updated during patch & has same spec, status, resource version as fetched")
454+
if fetched == nil {
455+
fetched = &appsv1.Deployment{}
456+
ExpectWithOffset(1, c.Get(context.TODO(), deplKey, fetched)).To(Succeed())
457+
}
458+
ExpectWithOffset(1, fetched.ResourceVersion).To(Equal(deploy.ResourceVersion))
459+
ExpectWithOffset(1, fetched.Spec).To(BeEquivalentTo(deploy.Spec))
460+
ExpectWithOffset(1, fetched.Status).To(BeEquivalentTo(deploy.Status))
461+
}
462+
452463
It("creates a new object if one doesn't exists", func() {
453464
op, err := controllerutil.CreateOrPatch(context.TODO(), c, deploy, specr)
454465

@@ -485,6 +496,7 @@ var _ = Describe("Controllerutil", func() {
485496
fetched := &appsv1.Deployment{}
486497
Expect(c.Get(context.TODO(), deplKey, fetched)).To(Succeed())
487498
Expect(*fetched.Spec.Replicas).To(Equal(scale))
499+
assertLocalDeployWasUpdated(fetched)
488500
})
489501

490502
It("patches only changed objects", func() {
@@ -499,6 +511,8 @@ var _ = Describe("Controllerutil", func() {
499511

500512
By("returning OperationResultNone")
501513
Expect(op).To(BeEquivalentTo(controllerutil.OperationResultNone))
514+
515+
assertLocalDeployWasUpdated(nil)
502516
})
503517

504518
It("patches only changed status", func() {
@@ -517,6 +531,8 @@ var _ = Describe("Controllerutil", func() {
517531

518532
By("returning OperationResultUpdatedStatusOnly")
519533
Expect(op).To(BeEquivalentTo(controllerutil.OperationResultUpdatedStatusOnly))
534+
535+
assertLocalDeployWasUpdated(nil)
520536
})
521537

522538
It("patches resource and status", func() {
@@ -539,6 +555,8 @@ var _ = Describe("Controllerutil", func() {
539555

540556
By("returning OperationResultUpdatedStatus")
541557
Expect(op).To(BeEquivalentTo(controllerutil.OperationResultUpdatedStatus))
558+
559+
assertLocalDeployWasUpdated(nil)
542560
})
543561

544562
It("errors when MutateFn changes object name on creation", func() {

0 commit comments

Comments
 (0)