Skip to content

Commit b1388fe

Browse files
committed
Fix flaky controller tests
1 parent 5eefae8 commit b1388fe

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

controllers/opentelemetrycollector_controller.go

-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ func (r *OpenTelemetryCollectorReconciler) findOtelOwnedObjects(ctx context.Cont
113113
default:
114114
}
115115
}
116-
117116
// at this point we don't know if the most recent ConfigMap will still be the most recent after reconciliation, or
118117
// if a new one will be created. We keep one additional ConfigMap to account for this. The next reconciliation that
119118
// doesn't spawn a new ConfigMap will delete the extra one we kept here.

controllers/reconcile_test.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,8 @@ func TestOpenTelemetryCollectorReconciler_VersionedConfigMaps(t *testing.T) {
978978

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

9991004
func TestOpAMPBridgeReconciler_Reconcile(t *testing.T) {

controllers/suite_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
"testing"
2626
"time"
2727

28+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
29+
2830
routev1 "github.com/openshift/api/route/v1"
2931
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
3032
"github.com/stretchr/testify/assert"
@@ -139,6 +141,9 @@ func TestMain(m *testing.M) {
139141
ctx, cancel = context.WithCancel(context.TODO())
140142
defer cancel()
141143

144+
// logging is useful for these tests
145+
logf.SetLogger(zap.New())
146+
142147
// +kubebuilder:scaffold:scheme
143148
utilruntime.Must(monitoringv1.AddToScheme(testScheme))
144149
utilruntime.Must(networkingv1.AddToScheme(testScheme))

0 commit comments

Comments
 (0)