Skip to content

Commit

Permalink
Fix flaky reconciler test
Browse files Browse the repository at this point in the history
The reconciler now uses a caching client, which means it might not
immediately see resources we create for the test. Try to ensure we
always check for this first.
  • Loading branch information
swiatekm committed Dec 28, 2024
1 parent 1ca601b commit 8c572ce
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions controllers/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ func TestOpenTelemetryCollectorReconciler_Reconcile(t *testing.T) {
registry := colfeaturegate.GlobalRegistry()
current := featuregate.CollectorUsesTargetAllocatorCR.IsEnabled()
require.False(t, current, "don't set gates which are enabled by default")
err := registry.Set(featuregate.CollectorUsesTargetAllocatorCR.ID(), true)
require.NoError(t, err)
regErr := registry.Set(featuregate.CollectorUsesTargetAllocatorCR.ID(), true)
require.NoError(t, regErr)
t.Cleanup(func() {
err := registry.Set(featuregate.CollectorUsesTargetAllocatorCR.ID(), current)
require.NoError(t, err)
Expand Down Expand Up @@ -648,6 +648,14 @@ func TestOpenTelemetryCollectorReconciler_Reconcile(t *testing.T) {
if !firstCheck.validateErr(t, createErr) {
return
}
// wait until the reconciler sees the object in its cache
if createErr == nil {
assert.EventuallyWithT(t, func(collect *assert.CollectT) {
actual := &v1beta1.OpenTelemetryCollector{}
err := reconciler.Get(testContext, nsn, actual)
assert.NoError(collect, err)
}, time.Second*5, time.Millisecond)
}
if deletionTimestamp != nil {
err := k8sClient.Delete(testContext, &tt.args.params, client.PropagationPolicy(metav1.DeletePropagationForeground))
assert.NoError(t, err)
Expand Down Expand Up @@ -679,6 +687,13 @@ func TestOpenTelemetryCollectorReconciler_Reconcile(t *testing.T) {
if err != nil {
continue
}
// wait until the reconciler sees the object in its cache
assert.EventuallyWithT(t, func(collect *assert.CollectT) {
actual := &v1alpha1.OpenTelemetryCollector{}
err = reconciler.Get(testContext, nsn, actual)
assert.NoError(collect, err)
assert.Equal(collect, updateParam.Spec, actual.Spec)
}, time.Second*5, time.Millisecond)
req := k8sreconcile.Request{
NamespacedName: nsn,
}
Expand Down Expand Up @@ -1265,6 +1280,13 @@ service:
// delete collector and check if the cluster role was deleted
clientErr = k8sClient.Delete(context.Background(), otelcol)
require.NoError(t, clientErr)
// wait until the reconciler sees the object as deleted in its cache
assert.EventuallyWithT(t, func(collect *assert.CollectT) {
actual := &v1beta1.OpenTelemetryCollector{}
err := reconciler.Get(context.Background(), nsn, actual)
assert.NoError(collect, err)
assert.NotNil(t, actual.GetDeletionTimestamp())
}, time.Second*5, time.Millisecond)

reconcile, reconcileErr = reconciler.Reconcile(context.Background(), req)
require.NoError(t, reconcileErr)
Expand Down

0 comments on commit 8c572ce

Please sign in to comment.