diff --git a/e2e/attachedcluster_test.go b/e2e/attachedcluster_test.go index 5f218ede..4b5810cc 100644 --- a/e2e/attachedcluster_test.go +++ b/e2e/attachedcluster_test.go @@ -17,6 +17,7 @@ limitations under the License. package e2e import ( + "context" "os" "path/filepath" "time" @@ -24,9 +25,11 @@ import ( "github.com/onsi/ginkgo/v2" "github.com/onsi/gomega" corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "kurator.dev/kurator/e2e/resources" clusterv1a1 "kurator.dev/kurator/pkg/apis/cluster/v1alpha1" + fleetv1a1 "kurator.dev/kurator/pkg/apis/fleet/v1alpha1" ) var _ = ginkgo.Describe("[AttachedClusters] AttachedClusters testing", func() { @@ -40,13 +43,19 @@ var _ = ginkgo.Describe("[AttachedClusters] AttachedClusters testing", func() { ) ginkgo.BeforeEach(func() { - namespace = "default" + namespace = "e2e-test" fleetname = "e2etest" memberClusterName = "kurator-member" homeDir, err := os.UserHomeDir() gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) kubeconfigPath = filepath.Join(homeDir, ".kube/kurator-member.config") + // create namespace for e2e test + e2eNamespace := resources.NewNamespace(namespace) + createNSErr := resources.CreateNamespace(kubeClient, e2eNamespace) + gomega.Expect(createNSErr).ShouldNot(gomega.HaveOccurred()) + time.Sleep(3 * time.Second) + // build secrets use member cluster kubeconfig kubeconfig, readfileErr := os.ReadFile(kubeconfigPath) gomega.Expect(readfileErr).ShouldNot(gomega.HaveOccurred()) @@ -62,6 +71,20 @@ var _ = ginkgo.Describe("[AttachedClusters] AttachedClusters testing", func() { attachedcluster = resources.NewAttachedCluster(namespace, memberClusterName, secretKeyRef) }) + ginkgo.AfterEach(func() { + fleerRemoveErr := resources.RemoveFleet(kuratorClient, namespace, fleetname) + gomega.Expect(fleerRemoveErr).ShouldNot(gomega.HaveOccurred()) + + attachedclusterRemoveErr := resources.RemoveAttachedCluster(kuratorClient, namespace, memberClusterName) + gomega.Expect(attachedclusterRemoveErr).ShouldNot(gomega.HaveOccurred()) + + secretRemoveErr := resources.RemoveSecret(kubeClient, namespace, memberClusterName) + gomega.Expect(secretRemoveErr).ShouldNot(gomega.HaveOccurred()) + + namespaceRemoveErr := resources.RemoveNamespace(kubeClient, namespace) + gomega.Expect(namespaceRemoveErr).ShouldNot(gomega.HaveOccurred()) + }) + ginkgo.It("Create Fleet", func() { // step 1.create secrets secretCreateErr := resources.CreateSecret(kubeClient, secret) @@ -71,7 +94,7 @@ var _ = ginkgo.Describe("[AttachedClusters] AttachedClusters testing", func() { attachedCreateErr := resources.CreateAttachedCluster(kuratorClient, attachedcluster) gomega.Expect(attachedCreateErr).ShouldNot(gomega.HaveOccurred()) - time.Sleep(5 * time.Second) + time.Sleep(3 * time.Second) // step 3.create fleet clusters := []*corev1.ObjectReference{ { @@ -82,5 +105,11 @@ var _ = ginkgo.Describe("[AttachedClusters] AttachedClusters testing", func() { fleet := resources.NewFleet(namespace, fleetname, clusters) fleetCreateErr := resources.CreateFleet(kuratorClient, fleet) gomega.Expect(fleetCreateErr).ShouldNot(gomega.HaveOccurred()) + time.Sleep(3 * time.Second) + + // step 4.check fleet status + fleetPresentOnCluster, fleetGetErr := kuratorClient.FleetV1alpha1().Fleets(namespace).Get(context.TODO(), fleetname, metav1.GetOptions{}) + gomega.Expect(fleetGetErr).ShouldNot(gomega.HaveOccurred()) + gomega.Expect(fleetPresentOnCluster.Status.Phase).Should(gomega.Equal(fleetv1a1.ReadyPhase)) }) }) diff --git a/e2e/resources/attachedcluster.go b/e2e/resources/attachedcluster.go index 2ed15d76..cc1bf72b 100644 --- a/e2e/resources/attachedcluster.go +++ b/e2e/resources/attachedcluster.go @@ -63,3 +63,15 @@ func UpdateAttachedCluster(client kurator.Interface, attachedCluster *clusterv1a } return nil } + +func RemoveAttachedCluster(client kurator.Interface, namespace, name string) error { + err := client.ClusterV1alpha1().AttachedClusters(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{}) + if err != nil { + if apierrors.IsNotFound(err) { + return nil + } else { + return err + } + } + return nil +} diff --git a/e2e/resources/fleet.go b/e2e/resources/fleet.go index 9ec314c8..f3fd8fa2 100644 --- a/e2e/resources/fleet.go +++ b/e2e/resources/fleet.go @@ -27,6 +27,7 @@ import ( kurator "kurator.dev/kurator/pkg/client-go/generated/clientset/versioned" ) +// NewFleet will build a Fleet object. func NewFleet(namespace string, name string, clusters []*corev1.ObjectReference) *fleetv1a1.Fleet { return &fleetv1a1.Fleet{ TypeMeta: metav1.TypeMeta{ @@ -64,3 +65,15 @@ func UpdateFleet(client kurator.Interface, fleet *fleetv1a1.Fleet) error { } return nil } + +func RemoveFleet(client kurator.Interface, namespace, name string) error { + err := client.FleetV1alpha1().Fleets(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{}) + if err != nil { + if apierrors.IsNotFound(err) { + return nil + } else { + return err + } + } + return nil +} diff --git a/e2e/resources/namespace.go b/e2e/resources/namespace.go new file mode 100644 index 00000000..ff614f84 --- /dev/null +++ b/e2e/resources/namespace.go @@ -0,0 +1,59 @@ +/* +Copyright Kurator Authors. + +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 resources + +import ( + "context" + + 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" +) + +// NewNamespace will build a Namespace object. +func NewNamespace(namespace string) *corev1.Namespace { + return &corev1.Namespace{ + ObjectMeta: metav1.ObjectMeta{ + Name: namespace, + }, + } +} + +func CreateNamespace(client kubernetes.Interface, namespace *corev1.Namespace) error { + _, err := client.CoreV1().Namespaces().Create(context.TODO(), namespace, metav1.CreateOptions{}) + if err != nil { + if apierrors.IsAlreadyExists(err) { + return nil + } else { + return err + } + } + return nil +} + +func RemoveNamespace(client kubernetes.Interface, name string) error { + err := client.CoreV1().Namespaces().Delete(context.TODO(), name, metav1.DeleteOptions{}) + if err != nil { + if apierrors.IsNotFound(err) { + return nil + } else { + return err + } + } + return nil +} diff --git a/e2e/resources/secret.go b/e2e/resources/secret.go index 742146ba..78a69ee5 100644 --- a/e2e/resources/secret.go +++ b/e2e/resources/secret.go @@ -61,3 +61,15 @@ func UpdateSecret(client kubernetes.Interface, secret *corev1.Secret) error { } return nil } + +func RemoveSecret(client kubernetes.Interface, namespace, name string) error { + err := client.CoreV1().Secrets(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{}) + if err != nil { + if apierrors.IsNotFound(err) { + return nil + } else { + return err + } + } + return nil +}