Skip to content

Commit 99e1a23

Browse files
committed
pkg/controller/operators/operator_controller_test.go: fix flaky test
Signed-off-by: Joe Lanford <[email protected]>
1 parent 39463ca commit 99e1a23

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ all: test build
4444
test: clean cover.out
4545

4646
unit: kubebuilder
47-
KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) go test $(MOD_FLAGS) $(SPECIFIC_UNIT_TEST) -tags "json1" -v -race -count=1 ./pkg/...
47+
KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) go test $(MOD_FLAGS) $(SPECIFIC_UNIT_TEST) -tags "json1" -race -count=1 ./pkg/...
4848

4949
# Ensure kubebuilder is installed before continuing
5050
KUBEBUILDER_ASSETS_ERR := not detected in $(KUBEBUILDER_ASSETS), to override the assets path set the KUBEBUILDER_ASSETS environment variable, for install instructions see https://book.kubebuilder.io/quick-start.html

pkg/controller/operators/operator_controller_test.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,10 @@ var _ = Describe("Operator Controller", func() {
4545
})
4646

4747
Describe("operator deletion", func() {
48+
var originalUID types.UID
4849
JustBeforeEach(func() {
50+
originalUID = operator.GetUID()
4951
Expect(k8sClient.Delete(ctx, operator)).To(Succeed())
50-
Eventually(func() bool {
51-
err := k8sClient.Get(ctx, name, operator)
52-
return apierrors.IsNotFound(err)
53-
}, timeout, interval).Should(BeTrue())
5452
})
5553
Context("with components bearing its label", func() {
5654
var (
@@ -77,16 +75,30 @@ var _ = Describe("Operator Controller", func() {
7775
})
7876

7977
It("should re-create it", func() {
80-
Eventually(func() error {
81-
return k8sClient.Get(ctx, name, operator)
82-
}).Should(Succeed())
78+
// There's a race condition between this test and the controller. By the time,
79+
// this function is running, we may be in one of three states.
80+
// 1. The original deletion in the test setup has not yet finished, so the original
81+
// operator resource still exists.
82+
// 2. The operator doesn't exist, and the controller has not yet re-created it.
83+
// 3. The operator has already been deleted and re-created.
84+
//
85+
// To solve this problem, we simply compare the UIDs and expect to eventually see a
86+
// a different UID.
87+
Eventually(func() (types.UID, error) {
88+
err := k8sClient.Get(ctx, name, operator)
89+
return operator.GetUID(), err
90+
}, timeout, interval).ShouldNot(Equal(originalUID))
8391
})
8492
})
8593
Context("with no components bearing its label", func() {
8694
It("should not re-create it", func() {
95+
// We expect the operator deletion to eventually complete, and then we
96+
// expect the operator to consistently never be found.
97+
Eventually(func() bool {
98+
return apierrors.IsNotFound(k8sClient.Get(ctx, name, operator))
99+
}, timeout, interval).Should(BeTrue())
87100
Consistently(func() bool {
88-
err := k8sClient.Get(ctx, name, operator)
89-
return apierrors.IsNotFound(err)
101+
return apierrors.IsNotFound(k8sClient.Get(ctx, name, operator))
90102
}, timeout, interval).Should(BeTrue())
91103
})
92104
})
@@ -204,5 +216,4 @@ var _ = Describe("Operator Controller", func() {
204216
})
205217
})
206218
})
207-
208219
})

0 commit comments

Comments
 (0)