Skip to content

Commit 6ac98d1

Browse files
committed
Branch in tests based on the server version rather than environment
When there is no environment variable defined, the envtest tools use a default version of the Kubernetes API. Interrogating the API works regardless of any tooling. See: 7ed8677
1 parent d4f0ff1 commit 6ac98d1

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

internal/controller/postgrescluster/pgadmin_test.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ package postgrescluster
2121
import (
2222
"context"
2323
"io"
24-
"os"
2524
"testing"
2625

2726
"github.com/pkg/errors"
@@ -31,6 +30,8 @@ import (
3130
apierrors "k8s.io/apimachinery/pkg/api/errors"
3231
"k8s.io/apimachinery/pkg/api/resource"
3332
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33+
"k8s.io/apimachinery/pkg/util/version"
34+
"k8s.io/client-go/discovery"
3435
"k8s.io/client-go/tools/record"
3536
"sigs.k8s.io/controller-runtime/pkg/client"
3637
"sigs.k8s.io/controller-runtime/pkg/client/fake"
@@ -451,9 +452,18 @@ func TestReconcilePGAdminService(t *testing.T) {
451452

452453
func TestReconcilePGAdminStatefulSet(t *testing.T) {
453454
ctx := context.Background()
454-
_, cc := setupKubernetes(t)
455+
env, cc := setupKubernetes(t)
455456
require.ParallelCapacity(t, 1)
456457

458+
dc, err := discovery.NewDiscoveryClientForConfig(env.Config)
459+
assert.NilError(t, err)
460+
461+
server, err := dc.ServerVersion()
462+
assert.NilError(t, err)
463+
464+
serverVersion, err := version.ParseGeneric(server.GitVersion)
465+
assert.NilError(t, err)
466+
457467
reconciler := &Reconciler{Client: cc, Owner: client.FieldOwner(t.Name())}
458468

459469
ns := setupNamespace(t, cc)
@@ -521,7 +531,7 @@ securityContext:
521531
runAsNonRoot: true
522532
terminationGracePeriodSeconds: 30
523533
`
524-
if os.Getenv("ENVTEST_K8S_VERSION") == "1.19.2" {
534+
if serverVersion.LessThan(version.MustParseGeneric("1.20")) {
525535
compare = `
526536
automountServiceAccountToken: false
527537
containers: null
@@ -675,7 +685,7 @@ topologySpreadConstraints:
675685
topologyKey: fakekey
676686
whenUnsatisfiable: ScheduleAnyway
677687
`
678-
if os.Getenv("ENVTEST_K8S_VERSION") == "1.19.2" {
688+
if serverVersion.LessThan(version.MustParseGeneric("1.20")) {
679689
compare = `
680690
affinity:
681691
nodeAffinity:

internal/controller/postgrescluster/volumes_test.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ package postgrescluster
2121
import (
2222
"context"
2323
"errors"
24-
"os"
2524
"testing"
2625
"time"
2726

@@ -32,7 +31,9 @@ import (
3231
"k8s.io/apimachinery/pkg/api/resource"
3332
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3433
"k8s.io/apimachinery/pkg/util/validation/field"
34+
"k8s.io/apimachinery/pkg/util/version"
3535
"k8s.io/apimachinery/pkg/util/wait"
36+
"k8s.io/client-go/discovery"
3637
"sigs.k8s.io/controller-runtime/pkg/client"
3738

3839
"github.com/crunchydata/postgres-operator/internal/controller/runtime"
@@ -651,9 +652,18 @@ func TestReconcileConfigureExistingPVCs(t *testing.T) {
651652

652653
func TestReconcileMoveDirectories(t *testing.T) {
653654
ctx := context.Background()
654-
_, tClient := setupKubernetes(t)
655+
env, tClient := setupKubernetes(t)
655656
require.ParallelCapacity(t, 1)
656657

658+
dc, err := discovery.NewDiscoveryClientForConfig(env.Config)
659+
assert.NilError(t, err)
660+
661+
server, err := dc.ServerVersion()
662+
assert.NilError(t, err)
663+
664+
serverVersion, err := version.ParseGeneric(server.GitVersion)
665+
assert.NilError(t, err)
666+
657667
r := &Reconciler{Client: tClient, Owner: client.FieldOwner(t.Name())}
658668

659669
ns := setupNamespace(t, tClient)
@@ -809,7 +819,7 @@ volumes:
809819
claimName: testpgdata
810820
`
811821

812-
if os.Getenv("ENVTEST_K8S_VERSION") == "1.19.2" {
822+
if serverVersion.LessThan(version.MustParseGeneric("1.20")) {
813823
compare = `
814824
automountServiceAccountToken: false
815825
containers:
@@ -921,7 +931,7 @@ volumes:
921931
persistentVolumeClaim:
922932
claimName: testwal
923933
`
924-
if os.Getenv("ENVTEST_K8S_VERSION") == "1.19.2" {
934+
if serverVersion.LessThan(version.MustParseGeneric("1.20")) {
925935
compare = `
926936
automountServiceAccountToken: false
927937
containers:
@@ -1036,7 +1046,7 @@ volumes:
10361046
claimName: testrepo
10371047
`
10381048

1039-
if os.Getenv("ENVTEST_K8S_VERSION") == "1.19.2" {
1049+
if serverVersion.LessThan(version.MustParseGeneric("1.20")) {
10401050
compare = `
10411051
automountServiceAccountToken: false
10421052
containers:

0 commit comments

Comments
 (0)