Skip to content

Commit

Permalink
Run upgrade tests in isolated namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
cbandy committed Nov 18, 2024
1 parent 09ec49d commit c2a3b88
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 29 deletions.
8 changes: 4 additions & 4 deletions internal/upgradecheck/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func TestGenerateHeader(t *testing.T) {
setupDeploymentID(t)
ctx := context.Background()
cfg, cc := require.Kubernetes2(t)
setupNamespace(t, cc)

dc, err := discovery.NewDiscoveryClientForConfig(cfg)
assert.NilError(t, err)
Expand All @@ -43,6 +42,7 @@ func TestGenerateHeader(t *testing.T) {

t.Setenv("PGO_INSTALLER", "test")
t.Setenv("PGO_INSTALLER_ORIGIN", "test-origin")
t.Setenv("PGO_NAMESPACE", require.Namespace(t, cc).Name)
t.Setenv("BUILD_SOURCE", "developer")

t.Run("error ensuring ID", func(t *testing.T) {
Expand Down Expand Up @@ -146,7 +146,7 @@ func TestGenerateHeader(t *testing.T) {
func TestEnsureID(t *testing.T) {
ctx := context.Background()
cc := require.Kubernetes(t)
setupNamespace(t, cc)
t.Setenv("PGO_NAMESPACE", require.Namespace(t, cc).Name)

t.Run("success, no id set in mem or configmap", func(t *testing.T) {
deploymentID = ""
Expand Down Expand Up @@ -282,7 +282,7 @@ func TestEnsureID(t *testing.T) {
func TestManageUpgradeCheckConfigMap(t *testing.T) {
ctx := context.Background()
cc := require.Kubernetes(t)
setupNamespace(t, cc)
t.Setenv("PGO_NAMESPACE", require.Namespace(t, cc).Name)

t.Run("no namespace given", func(t *testing.T) {
ctx, calls := setupLogCapture(ctx)
Expand Down Expand Up @@ -408,7 +408,7 @@ func TestManageUpgradeCheckConfigMap(t *testing.T) {
func TestApplyConfigMap(t *testing.T) {
ctx := context.Background()
cc := require.Kubernetes(t)
setupNamespace(t, cc)
t.Setenv("PGO_NAMESPACE", require.Namespace(t, cc).Name)

t.Run("successful create", func(t *testing.T) {
cmRetrieved := &corev1.ConfigMap{}
Expand Down
25 changes: 0 additions & 25 deletions internal/upgradecheck/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"testing"

"github.com/go-logr/logr/funcr"
"gotest.tools/v3/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/uuid"
Expand Down Expand Up @@ -154,26 +152,3 @@ func setupLogCapture(ctx context.Context) (context.Context, *[]string) {
})
return logging.NewContext(ctx, testlog), &calls
}

// setupNamespace creates a namespace that will be deleted by t.Cleanup.
// For upgradechecking, this namespace is set to `postgres-operator`,
// which sometimes is created by other parts of the testing apparatus,
// cf., the createnamespace call in `make check-envtest-existing`.
// When creation fails, it calls t.Fatal. The caller may delete the namespace
// at any time.
func setupNamespace(t testing.TB, cc crclient.Client) {
t.Helper()
ns := &corev1.Namespace{}
ns.Name = "postgres-operator"
ns.Labels = map[string]string{"postgres-operator-test": t.Name()}

ctx := context.Background()
exists := &corev1.Namespace{}
assert.NilError(t, crclient.IgnoreNotFound(
cc.Get(ctx, crclient.ObjectKeyFromObject(ns), exists)))
if exists.Name != "" {
return
}
assert.NilError(t, cc.Create(ctx, ns))
t.Cleanup(func() { assert.Check(t, crclient.IgnoreNotFound(cc.Delete(ctx, ns))) })
}

0 comments on commit c2a3b88

Please sign in to comment.