Skip to content

Commit 0466d4c

Browse files
committed
Add option to specify test namespace name using TEST_NAMESPACE_NAME env var
1 parent 0d76fd2 commit 0466d4c

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

support/environment.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ const (
5858
storageSecretKey = "AWS_SECRET_ACCESS_KEY"
5959
storageBucketName = "AWS_STORAGE_BUCKET"
6060
storageBucketMnistDir = "AWS_STORAGE_BUCKET_MNIST_DIR"
61+
62+
// Name of existing namespace to be used for test
63+
testNamespaceNameEnvVar = "TEST_NAMESPACE_NAME"
6164
)
6265

6366
type ClusterType string
@@ -174,6 +177,10 @@ func GetPipTrustedHost() string {
174177
return lookupEnvOrDefault(pipTrustedHost, "")
175178
}
176179

180+
func GetTestNamespaceName() (string, bool) {
181+
return os.LookupEnv(testNamespaceNameEnvVar)
182+
}
183+
177184
func lookupEnvOrDefault(key, value string) string {
178185
if v, ok := os.LookupEnv(key); ok {
179186
return v

support/test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626
"github.com/onsi/gomega"
2727

2828
corev1 "k8s.io/api/core/v1"
29+
"k8s.io/apimachinery/pkg/api/errors"
30+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2931
"k8s.io/client-go/rest"
3032
)
3133

@@ -39,6 +41,7 @@ type Test interface {
3941
gomega.Gomega
4042

4143
NewTestNamespace(...Option[*corev1.Namespace]) *corev1.Namespace
44+
CreateOrGetTestNamespace(...Option[*corev1.Namespace]) *corev1.Namespace
4245
}
4346

4447
type Option[T any] interface {
@@ -156,3 +159,24 @@ func (t *T) NewTestNamespace(options ...Option[*corev1.Namespace]) *corev1.Names
156159
})
157160
return namespace
158161
}
162+
163+
func (t *T) CreateOrGetTestNamespace(options ...Option[*corev1.Namespace]) *corev1.Namespace {
164+
t.T().Helper()
165+
166+
testNamespaceName, testNamespaceNameExists := GetTestNamespaceName()
167+
168+
if testNamespaceNameExists {
169+
// Verify that the namespace really exists and return it, create it if doesn't exist yet
170+
namespace, err := t.Client().Core().CoreV1().Namespaces().Get(t.Ctx(), testNamespaceName, metav1.GetOptions{})
171+
if err == nil {
172+
t.T().Logf("Using the namespace name which is provided using environment variable..")
173+
return namespace
174+
} else if errors.IsNotFound(err) {
175+
t.T().Logf("%s namespace doesn't exists. Creating ...", testNamespaceName)
176+
return CreateTestNamespaceWithName(t, testNamespaceName, options...)
177+
} else {
178+
t.T().Fatalf("Error retrieving namespace with name `%s`: %v", testNamespaceName, err)
179+
}
180+
}
181+
return t.NewTestNamespace(options...)
182+
}

support/test_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Copyright 2024.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package support
17+
18+
import (
19+
"os"
20+
"testing"
21+
22+
. "github.com/onsi/gomega"
23+
)
24+
25+
func TestCreateOrGetTestNamespaceCreatingNamespace(t *testing.T) {
26+
test := NewTest(t)
27+
28+
namespace := test.CreateOrGetTestNamespace()
29+
30+
test.Expect(namespace).NotTo(BeNil())
31+
test.Expect(namespace.GenerateName).To(Equal("test-ns-"))
32+
}
33+
34+
func TestCreateOrGetTestNamespaceGettingExistingNamespace(t *testing.T) {
35+
test := NewTest(t)
36+
37+
CreateTestNamespaceWithName(test, "test-namespace")
38+
os.Setenv(testNamespaceNameEnvVar, "test-namespace")
39+
defer os.Unsetenv(testNamespaceNameEnvVar)
40+
41+
namespace := test.CreateOrGetTestNamespace()
42+
43+
test.Expect(namespace).NotTo(BeNil())
44+
test.Expect(namespace.Name).To(Equal("test-namespace"))
45+
}
46+
47+
func TestCreateOrGetTestNamespaceGettingNonExistingNamespace(t *testing.T) {
48+
test := NewTest(t)
49+
50+
os.Setenv(testNamespaceNameEnvVar, "non-existing-namespace")
51+
defer os.Unsetenv(testNamespaceNameEnvVar)
52+
53+
namespace := test.CreateOrGetTestNamespace()
54+
55+
test.Expect(namespace).NotTo(BeNil())
56+
test.Expect(namespace.Name).To(Equal("non-existing-namespace"))
57+
}

0 commit comments

Comments
 (0)