Skip to content

Commit

Permalink
Fix flaky controller tests
Browse files Browse the repository at this point in the history
  • Loading branch information
swiatekm committed Dec 19, 2024
1 parent 5eefae8 commit b1388fe
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
1 change: 0 additions & 1 deletion controllers/opentelemetrycollector_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ func (r *OpenTelemetryCollectorReconciler) findOtelOwnedObjects(ctx context.Cont
default:
}
}

// at this point we don't know if the most recent ConfigMap will still be the most recent after reconciliation, or
// if a new one will be created. We keep one additional ConfigMap to account for this. The next reconciliation that
// doesn't spawn a new ConfigMap will delete the extra one we kept here.
Expand Down
11 changes: 8 additions & 3 deletions controllers/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,8 @@ func TestOpenTelemetryCollectorReconciler_VersionedConfigMaps(t *testing.T) {

assert.EventuallyWithT(t, func(collect *assert.CollectT) {
configMaps := &v1.ConfigMapList{}
listErr := k8sClient.List(clientCtx, configMaps, opts...)
// use the reconciler client here to ensure it sees the new ConfigMap, before running the next reconciliation
listErr := reconciler.Client.List(clientCtx, configMaps, opts...)
assert.NoError(collect, listErr)
assert.NotEmpty(collect, configMaps)
assert.Len(collect, configMaps.Items, 4)
Expand All @@ -992,8 +993,12 @@ func TestOpenTelemetryCollectorReconciler_VersionedConfigMaps(t *testing.T) {
listErr := k8sClient.List(clientCtx, configMaps, opts...)
assert.NoError(collect, listErr)
assert.NotEmpty(collect, configMaps)
assert.Len(collect, configMaps.Items, 3)
}, time.Second*5, time.Millisecond)
// actual deletion can happen with a delay in a K8s cluster, check the timestamp instead to speed things up
items := slices.DeleteFunc(configMaps.Items, func(item v1.ConfigMap) bool {
return item.DeletionTimestamp != nil
})
assert.Len(collect, items, 3)
}, time.Second*30, time.Second) // not sure why this can take so long to bubble up
}

func TestOpAMPBridgeReconciler_Reconcile(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"testing"
"time"

"sigs.k8s.io/controller-runtime/pkg/log/zap"

routev1 "github.com/openshift/api/route/v1"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -139,6 +141,9 @@ func TestMain(m *testing.M) {
ctx, cancel = context.WithCancel(context.TODO())
defer cancel()

// logging is useful for these tests
logf.SetLogger(zap.New())

// +kubebuilder:scaffold:scheme
utilruntime.Must(monitoringv1.AddToScheme(testScheme))
utilruntime.Must(networkingv1.AddToScheme(testScheme))
Expand Down

0 comments on commit b1388fe

Please sign in to comment.