Skip to content

Commit

Permalink
Add option to specify test namespace name using TEST_NAMESPACE_NAME e…
Browse files Browse the repository at this point in the history
…nv var
  • Loading branch information
sutaakar authored and openshift-merge-bot[bot] committed Nov 12, 2024
1 parent 0d76fd2 commit 575d4c4
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
7 changes: 7 additions & 0 deletions support/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ const (
storageSecretKey = "AWS_SECRET_ACCESS_KEY"
storageBucketName = "AWS_STORAGE_BUCKET"
storageBucketMnistDir = "AWS_STORAGE_BUCKET_MNIST_DIR"

// Name of existing namespace to be used for test
testNamespaceNameEnvVar = "TEST_NAMESPACE_NAME"
)

type ClusterType string
Expand Down Expand Up @@ -174,6 +177,10 @@ func GetPipTrustedHost() string {
return lookupEnvOrDefault(pipTrustedHost, "")
}

func GetTestNamespaceName() (string, bool) {
return os.LookupEnv(testNamespaceNameEnvVar)
}

func lookupEnvOrDefault(key, value string) string {
if v, ok := os.LookupEnv(key); ok {
return v
Expand Down
24 changes: 24 additions & 0 deletions support/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"github.com/onsi/gomega"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
)

Expand All @@ -39,6 +41,7 @@ type Test interface {
gomega.Gomega

NewTestNamespace(...Option[*corev1.Namespace]) *corev1.Namespace
CreateOrGetTestNamespace(...Option[*corev1.Namespace]) *corev1.Namespace
}

type Option[T any] interface {
Expand Down Expand Up @@ -156,3 +159,24 @@ func (t *T) NewTestNamespace(options ...Option[*corev1.Namespace]) *corev1.Names
})
return namespace
}

func (t *T) CreateOrGetTestNamespace(options ...Option[*corev1.Namespace]) *corev1.Namespace {
t.T().Helper()

testNamespaceName, testNamespaceNameExists := GetTestNamespaceName()

if testNamespaceNameExists {
// Verify that the namespace really exists and return it, create it if doesn't exist yet
namespace, err := t.Client().Core().CoreV1().Namespaces().Get(t.Ctx(), testNamespaceName, metav1.GetOptions{})
if err == nil {
t.T().Logf("Using the namespace name which is provided using environment variable..")
return namespace
} else if errors.IsNotFound(err) {
t.T().Logf("%s namespace doesn't exists. Creating ...", testNamespaceName)
return CreateTestNamespaceWithName(t, testNamespaceName, options...)
} else {
t.T().Fatalf("Error retrieving namespace with name `%s`: %v", testNamespaceName, err)
}
}
return t.NewTestNamespace(options...)
}
57 changes: 57 additions & 0 deletions support/test_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright 2024.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package support

import (
"os"
"testing"

. "github.com/onsi/gomega"
)

func TestCreateOrGetTestNamespaceCreatingNamespace(t *testing.T) {
test := NewTest(t)

namespace := test.CreateOrGetTestNamespace()

test.Expect(namespace).NotTo(BeNil())
test.Expect(namespace.GenerateName).To(Equal("test-ns-"))
}

func TestCreateOrGetTestNamespaceGettingExistingNamespace(t *testing.T) {
test := NewTest(t)

CreateTestNamespaceWithName(test, "test-namespace")
os.Setenv(testNamespaceNameEnvVar, "test-namespace")
defer os.Unsetenv(testNamespaceNameEnvVar)

namespace := test.CreateOrGetTestNamespace()

test.Expect(namespace).NotTo(BeNil())
test.Expect(namespace.Name).To(Equal("test-namespace"))
}

func TestCreateOrGetTestNamespaceGettingNonExistingNamespace(t *testing.T) {
test := NewTest(t)

os.Setenv(testNamespaceNameEnvVar, "non-existing-namespace")
defer os.Unsetenv(testNamespaceNameEnvVar)

namespace := test.CreateOrGetTestNamespace()

test.Expect(namespace).NotTo(BeNil())
test.Expect(namespace.Name).To(Equal("non-existing-namespace"))
}

0 comments on commit 575d4c4

Please sign in to comment.