From e1add05dd20ade32aac305777b3d979d5318bd00 Mon Sep 17 00:00:00 2001 From: LiZhenCheng9527 Date: Mon, 5 Feb 2024 10:18:34 +0800 Subject: [PATCH] add function about create random namespace Signed-off-by: LiZhenCheng9527 --- e2e/attachedcluster_test.go | 2 +- e2e/resources/namespace.go | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/e2e/attachedcluster_test.go b/e2e/attachedcluster_test.go index 34606591..b29e3262 100644 --- a/e2e/attachedcluster_test.go +++ b/e2e/attachedcluster_test.go @@ -43,7 +43,7 @@ var _ = ginkgo.Describe("[AttachedClusters] AttachedClusters testing", func() { ) ginkgo.BeforeEach(func() { - namespace = "e2e-test" + namespace = resources.RandomNamespace(5) fleetname = "e2etest" memberClusterName = "kurator-member" homeDir, err := os.UserHomeDir() diff --git a/e2e/resources/namespace.go b/e2e/resources/namespace.go index ff614f84..429fc7ce 100644 --- a/e2e/resources/namespace.go +++ b/e2e/resources/namespace.go @@ -18,6 +18,8 @@ package resources import ( "context" + "math/rand" + "strings" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -25,6 +27,8 @@ import ( "k8s.io/client-go/kubernetes" ) +const chartSet = "abcdefghijklmnopqrstuvwxyz" + // NewNamespace will build a Namespace object. func NewNamespace(namespace string) *corev1.Namespace { return &corev1.Namespace{ @@ -57,3 +61,12 @@ func RemoveNamespace(client kubernetes.Interface, name string) error { } return nil } + +func RandomNamespace(nameLength int) string { + randomNS := strings.Builder{} + randomNS.Grow(nameLength) + for i := 0; i < nameLength; i++ { + randomNS.WriteByte(chartSet[rand.Intn(len(chartSet))]) + } + return randomNS.String() +}