Skip to content

Commit

Permalink
Fix flaky envtest test (#3564)
Browse files Browse the repository at this point in the history
Kubernetes creation timestamps only have a seconds-level precision.
The ConfigMap versioning feature for collectors deleted the oldest
ConfigMaps. For the test to work correctly, we need to wait at least 1
second between CR changes.
  • Loading branch information
swiatekm authored Dec 20, 2024
1 parent 7dd0727 commit c399686
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions controllers/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,9 @@ func TestOpenTelemetryCollectorReconciler_VersionedConfigMaps(t *testing.T) {
Namespace: metav1.NamespaceDefault,
},
Spec: v1beta1.OpenTelemetryCollectorSpec{
OpenTelemetryCommonFields: v1beta1.OpenTelemetryCommonFields{
PodDisruptionBudget: &v1beta1.PodDisruptionBudgetSpec{},
},
ConfigVersions: 1,
TargetAllocator: v1beta1.TargetAllocatorEmbedded{
Enabled: true,
Expand Down Expand Up @@ -937,6 +940,8 @@ func TestOpenTelemetryCollectorReconciler_VersionedConfigMaps(t *testing.T) {
}, time.Second*5, time.Millisecond)

// modify the ConfigMap, it should be kept
// wait a second first, as K8s creation timestamps only have second precision
time.Sleep(time.Second)
err = k8sClient.Get(clientCtx, nsn, collector)
require.NoError(t, err)
collector.Spec.Config.Exporters.Object["debug"] = map[string]interface{}{}
Expand All @@ -961,6 +966,8 @@ func TestOpenTelemetryCollectorReconciler_VersionedConfigMaps(t *testing.T) {
}, time.Second*5, time.Millisecond)

// modify the ConfigMap again, the oldest one is still kept, but is dropped after next reconciliation
// wait a second first, as K8s creation timestamps only have second precision
time.Sleep(time.Second)
err = k8sClient.Get(clientCtx, nsn, collector)
require.NoError(t, err)
collector.Spec.Config.Exporters.Object["debug/2"] = map[string]interface{}{}
Expand All @@ -978,8 +985,7 @@ func TestOpenTelemetryCollectorReconciler_VersionedConfigMaps(t *testing.T) {

assert.EventuallyWithT(t, func(collect *assert.CollectT) {
configMaps := &v1.ConfigMapList{}
// use the reconciler client here to ensure it sees the new ConfigMap, before running the next reconciliation
listErr := reconciler.Client.List(clientCtx, configMaps, opts...)
listErr := k8sClient.List(clientCtx, configMaps, opts...)
assert.NoError(collect, listErr)
assert.NotEmpty(collect, configMaps)
assert.Len(collect, configMaps.Items, 4)
Expand All @@ -993,12 +999,8 @@ func TestOpenTelemetryCollectorReconciler_VersionedConfigMaps(t *testing.T) {
listErr := k8sClient.List(clientCtx, configMaps, opts...)
assert.NoError(collect, listErr)
assert.NotEmpty(collect, configMaps)
// 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
assert.Len(collect, configMaps.Items, 3)
}, time.Second*5, time.Second)
}

func TestOpAMPBridgeReconciler_Reconcile(t *testing.T) {
Expand Down

0 comments on commit c399686

Please sign in to comment.