Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add function about create random namespace #600

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions e2e/fleet_attachedcluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package e2e

import (
"time"

"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
Expand All @@ -27,11 +29,13 @@ import (

var _ = ginkgo.Describe("[AttachedClusters] AttachedClusters testing", func() {
var (
fleetname string
fleet *fleetv1a1.Fleet
fleetNamespace string
fleetname string
fleet *fleetv1a1.Fleet
)

ginkgo.BeforeEach(func() {
fleetNamespace = e2ePrefix + resources.RandomNamespace(4)
fleetname = "e2e"
// build fleet
clusters := []*corev1.ObjectReference{
Expand All @@ -40,11 +44,11 @@ var _ = ginkgo.Describe("[AttachedClusters] AttachedClusters testing", func() {
Kind: "AttachedCluster",
},
}
fleet = resources.NewFleet(namespace, fleetname, clusters)
fleet = resources.NewFleet(fleetNamespace, fleetname, clusters)
})

ginkgo.AfterEach(func() {
fleerRemoveErr := resources.RemoveFleet(kuratorClient, namespace, fleetname)
fleerRemoveErr := resources.RemoveFleet(kuratorClient, fleetNamespace, fleetname)
gomega.Expect(fleerRemoveErr).ShouldNot(gomega.HaveOccurred())

attachedclusterRemoveErr := resources.RemoveAttachedCluster(kuratorClient, namespace, memberClusterName)
Expand All @@ -53,15 +57,20 @@ var _ = ginkgo.Describe("[AttachedClusters] AttachedClusters testing", func() {
secretRemoveErr := resources.RemoveSecret(kubeClient, namespace, memberClusterName)
gomega.Expect(secretRemoveErr).ShouldNot(gomega.HaveOccurred())

namespaceRemoveErr := resources.RemoveNamespace(kubeClient, namespace)
namespaceRemoveErr := resources.RemoveNamespace(kubeClient, fleetNamespace)
gomega.Expect(namespaceRemoveErr).ShouldNot(gomega.HaveOccurred())
})

ginkgo.It("Create Fleet", func() {
// create a namespace for fleet e2e test
fleetNamespaceCfg := resources.NewNamespace(fleetNamespace)
createNSErr := resources.CreateNamespace(kubeClient, fleetNamespaceCfg)
gomega.Expect(createNSErr).ShouldNot(gomega.HaveOccurred())
time.Sleep(3 * time.Second)
// create fleet and checkout fleet status
fleetCreateErr := resources.CreateFleet(kuratorClient, fleet)
gomega.Expect(fleetCreateErr).ShouldNot(gomega.HaveOccurred())
resources.WaitFleetFitWith(kuratorClient, namespace, fleetname, func(fleet *fleetv1a1.Fleet) bool {
resources.WaitFleetFitWith(kuratorClient, fleetNamespace, fleetname, func(fleet *fleetv1a1.Fleet) bool {
return fleet.Status.Phase == fleetv1a1.ReadyPhase
})
})
Expand Down
13 changes: 13 additions & 0 deletions e2e/resources/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ package resources

import (
"context"
"math/rand"
"strings"

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

const chartSet = "abcdefghijklmnopqrstuvwxyz"

// NewNamespace will build a Namespace object.
func NewNamespace(namespace string) *corev1.Namespace {
return &corev1.Namespace{
Expand Down Expand Up @@ -57,3 +61,12 @@ func RemoveNamespace(client kubernetes.Interface, name string) error {
}
return nil
}

func RandomNamespace(nameLength int) string {
LiZhenCheng9527 marked this conversation as resolved.
Show resolved Hide resolved
randomNS := strings.Builder{}
randomNS.Grow(nameLength)
for i := 0; i < nameLength; i++ {
randomNS.WriteByte(chartSet[rand.Intn(len(chartSet))])
}
return randomNS.String()
}
2 changes: 2 additions & 0 deletions e2e/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var (
kuratorContext string

namespace string
e2ePrefix string
memberClusterName string
kubeconfigPath string
secret *corev1.Secret
Expand Down Expand Up @@ -67,6 +68,7 @@ var _ = ginkgo.SynchronizedBeforeSuite(func() []byte {
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())

namespace = "e2e-test"
e2ePrefix = "e2e-"
memberClusterName = "kurator-member"
homeDir, err := os.UserHomeDir()
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
Expand Down
Loading