Skip to content

Commit 7254fa4

Browse files
committed
Adding uniqueness to cluster names when testing service type changes to work around race condition that is causing these tests to flake.
[sc-16571]
1 parent 655247e commit 7254fa4

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

internal/controller/postgrescluster/patroni_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,21 @@ func TestReconcilePatroniLeaderLease(t *testing.T) {
283283
// CRD validation looks only at the new/incoming value of fields. Confirm
284284
// that each ServiceType can change to any other ServiceType. Forbidding
285285
// certain transitions requires a validating webhook.
286+
serviceTypeChangeClusterCounter := 0
286287
for _, beforeType := range serviceTypes {
287288
for _, changeType := range serviceTypes {
288289
t.Run(beforeType+"To"+changeType, func(t *testing.T) {
289-
cluster := cluster.DeepCopy()
290+
// Creating fresh clusters for these tests
291+
cluster := testCluster()
292+
cluster.Namespace = ns.Name
293+
294+
// Note (dsessler): Adding a number to each cluster name to make cluster/service
295+
// names unique to work around an intermittent race condition where a service
296+
// from a prior test has not been deleted yet when the next test runs, causing
297+
// the test to fail due to non-matching IP addresses.
298+
cluster.Name += "-" + strconv.Itoa(serviceTypeChangeClusterCounter)
299+
assert.NilError(t, cc.Create(ctx, cluster))
300+
290301
cluster.Spec.Service = &v1beta1.ServiceSpec{Type: beforeType}
291302

292303
before, err := reconciler.reconcilePatroniLeaderLease(ctx, cluster)
@@ -309,6 +320,7 @@ func TestReconcilePatroniLeaderLease(t *testing.T) {
309320
assert.NilError(t, err, "\n%#v", errors.Unwrap(err))
310321
assert.Equal(t, after.Spec.ClusterIP, before.Spec.ClusterIP,
311322
"expected to keep the same ClusterIP")
323+
serviceTypeChangeClusterCounter++
312324
})
313325
}
314326
}

internal/controller/postgrescluster/pgadmin_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package postgrescluster
2121
import (
2222
"context"
2323
"io"
24+
"strconv"
2425
"testing"
2526

2627
"github.com/pkg/errors"
@@ -417,10 +418,25 @@ func TestReconcilePGAdminService(t *testing.T) {
417418
// CRD validation looks only at the new/incoming value of fields. Confirm
418419
// that each ServiceType can change to any other ServiceType. Forbidding
419420
// certain transitions requires a validating webhook.
421+
serviceTypeChangeClusterCounter := 0
420422
for _, beforeType := range serviceTypes {
421423
for _, changeType := range serviceTypes {
422424
t.Run(beforeType+"To"+changeType, func(t *testing.T) {
423-
cluster := cluster.DeepCopy()
425+
// Creating fresh clusters for these tests
426+
clusterNamespace := cluster.Namespace
427+
cluster := testCluster()
428+
cluster.Namespace = clusterNamespace
429+
430+
// Note (dsessler): Adding a number to each cluster name to make cluster/service
431+
// names unique to work around an intermittent race condition where a service
432+
// from a prior test has not been deleted yet when the next test runs, causing
433+
// the test to fail due to non-matching IP addresses.
434+
cluster.Name += "-" + strconv.Itoa(serviceTypeChangeClusterCounter)
435+
assert.NilError(t, cc.Create(ctx, cluster))
436+
437+
cluster.Spec.UserInterface = &v1beta1.UserInterfaceSpec{
438+
PGAdmin: &v1beta1.PGAdminPodSpec{},
439+
}
424440
cluster.Spec.UserInterface.PGAdmin.Service = &v1beta1.ServiceSpec{Type: beforeType}
425441

426442
before, err := reconciler.reconcilePGAdminService(ctx, cluster)
@@ -443,6 +459,7 @@ func TestReconcilePGAdminService(t *testing.T) {
443459
assert.NilError(t, err, "\n%#v", errors.Unwrap(err))
444460
assert.Equal(t, after.Spec.ClusterIP, before.Spec.ClusterIP,
445461
"expected to keep the same ClusterIP")
462+
serviceTypeChangeClusterCounter++
446463
})
447464
}
448465
}

internal/controller/postgrescluster/pgbouncer_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package postgrescluster
2020

2121
import (
2222
"context"
23+
"strconv"
2324
"testing"
2425

2526
"github.com/pkg/errors"
@@ -326,10 +327,27 @@ func TestReconcilePGBouncerService(t *testing.T) {
326327
// CRD validation looks only at the new/incoming value of fields. Confirm
327328
// that each ServiceType can change to any other ServiceType. Forbidding
328329
// certain transitions requires a validating webhook.
330+
serviceTypeChangeClusterCounter := 0
329331
for _, beforeType := range serviceTypes {
330332
for _, changeType := range serviceTypes {
331333
t.Run(beforeType+"To"+changeType, func(t *testing.T) {
332-
cluster := cluster.DeepCopy()
334+
// Creating fresh clusters for these tests
335+
clusterNamespace := cluster.Namespace
336+
cluster := testCluster()
337+
cluster.Namespace = clusterNamespace
338+
339+
// Note (dsessler): Adding a number to each cluster name to make cluster/service
340+
// names unique to work around an intermittent race condition where a service
341+
// from a prior test has not been deleted yet when the next test runs, causing
342+
// the test to fail due to non-matching IP addresses.
343+
cluster.Name += "-" + strconv.Itoa(serviceTypeChangeClusterCounter)
344+
assert.NilError(t, cc.Create(ctx, cluster))
345+
346+
cluster.Spec.Proxy = &v1beta1.PostgresProxySpec{
347+
PGBouncer: &v1beta1.PGBouncerPodSpec{
348+
Port: initialize.Int32(19041),
349+
},
350+
}
333351
cluster.Spec.Proxy.PGBouncer.Service = &v1beta1.ServiceSpec{Type: beforeType}
334352

335353
before, err := reconciler.reconcilePGBouncerService(ctx, cluster)
@@ -352,6 +370,7 @@ func TestReconcilePGBouncerService(t *testing.T) {
352370
assert.NilError(t, err, "\n%#v", errors.Unwrap(err))
353371
assert.Equal(t, after.Spec.ClusterIP, before.Spec.ClusterIP,
354372
"expected to keep the same ClusterIP")
373+
serviceTypeChangeClusterCounter++
355374
})
356375
}
357376
}

0 commit comments

Comments
 (0)