Skip to content

Commit 896825b

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

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-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

+4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import (
4848
"sigs.k8s.io/controller-runtime/pkg/client"
4949
"sigs.k8s.io/controller-runtime/pkg/envtest"
5050
logf "sigs.k8s.io/controller-runtime/pkg/log"
51+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
5152
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
5253
"sigs.k8s.io/controller-runtime/pkg/webhook"
5354
"sigs.k8s.io/yaml"
@@ -139,6 +140,9 @@ func TestMain(m *testing.M) {
139140
ctx, cancel = context.WithCancel(context.TODO())
140141
defer cancel()
141142

143+
// logging is useful for these tests
144+
logf.SetLogger(zap.New())
145+
142146
// +kubebuilder:scaffold:scheme
143147
utilruntime.Must(monitoringv1.AddToScheme(testScheme))
144148
utilruntime.Must(networkingv1.AddToScheme(testScheme))

0 commit comments

Comments
 (0)