From 375e8e74cb7ef7b29c8372d37e5b7aab1a616436 Mon Sep 17 00:00:00 2001 From: LiZhenCheng9527 Date: Mon, 5 Feb 2024 09:49:46 +0800 Subject: [PATCH] The operations of creating the secret and attachedcluster are moved to the suite_test Signed-off-by: LiZhenCheng9527 --- e2e/attachedcluster_test.go | 117 ------------------------------ e2e/fleet_attachedcluster_test.go | 68 +++++++++++++++++ e2e/resources/attachedcluster.go | 10 ++- e2e/resources/constant.go | 6 +- e2e/resources/fleet.go | 12 +++ e2e/suite_test.go | 46 ++++++++++++ 6 files changed, 137 insertions(+), 122 deletions(-) delete mode 100644 e2e/attachedcluster_test.go create mode 100644 e2e/fleet_attachedcluster_test.go diff --git a/e2e/attachedcluster_test.go b/e2e/attachedcluster_test.go deleted file mode 100644 index 34606591..00000000 --- a/e2e/attachedcluster_test.go +++ /dev/null @@ -1,117 +0,0 @@ -/* -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 e2e - -import ( - "context" - "os" - "path/filepath" - "time" - - "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() { - var ( - namespace string - fleetname string - memberClusterName string - kubeconfigPath string - secret *corev1.Secret - attachedcluster *clusterv1a1.AttachedCluster - ) - - ginkgo.BeforeEach(func() { - 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()) - data := make(map[string][]byte) - data[memberClusterName] = kubeconfig - secret = resources.NewSecret(namespace, memberClusterName, data) - - // build two attachedclusters - secretKeyRef := clusterv1a1.SecretKeyRef{ - Name: memberClusterName, - Key: memberClusterName, - } - 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) - gomega.Expect(secretCreateErr).ShouldNot(gomega.HaveOccurred()) - - // step 2.create attachedclusters - attachedCreateErr := resources.CreateAttachedCluster(kuratorClient, attachedcluster) - gomega.Expect(attachedCreateErr).ShouldNot(gomega.HaveOccurred()) - resources.WaitAttachedClusterFitWith(kuratorClient, namespace, memberClusterName, func(attachedCluster *clusterv1a1.AttachedCluster) bool { - return attachedCluster.Status.Ready - }) - - // step 3.create fleet - clusters := []*corev1.ObjectReference{ - { - Name: memberClusterName, - Kind: "AttachedCluster", - }, - } - 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/fleet_attachedcluster_test.go b/e2e/fleet_attachedcluster_test.go new file mode 100644 index 00000000..4d06c223 --- /dev/null +++ b/e2e/fleet_attachedcluster_test.go @@ -0,0 +1,68 @@ +/* +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 e2e + +import ( + "github.com/onsi/ginkgo/v2" + "github.com/onsi/gomega" + corev1 "k8s.io/api/core/v1" + + "kurator.dev/kurator/e2e/resources" + fleetv1a1 "kurator.dev/kurator/pkg/apis/fleet/v1alpha1" +) + +var _ = ginkgo.Describe("[AttachedClusters] AttachedClusters testing", func() { + var ( + fleetname string + fleet *fleetv1a1.Fleet + ) + + ginkgo.BeforeEach(func() { + fleetname = "e2e" + // build fleet + clusters := []*corev1.ObjectReference{ + { + Name: memberClusterName, + Kind: "AttachedCluster", + }, + } + fleet = resources.NewFleet(namespace, fleetname, clusters) + }) + + 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() { + // 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 { + return fleet.Status.Phase == fleetv1a1.ReadyPhase + }) + }) +}) diff --git a/e2e/resources/attachedcluster.go b/e2e/resources/attachedcluster.go index 7eadd4a7..3a02eeda 100644 --- a/e2e/resources/attachedcluster.go +++ b/e2e/resources/attachedcluster.go @@ -58,7 +58,13 @@ func CreateAttachedCluster(client kurator.Interface, attachedCluster *clusterv1a // UpdateAttachedCluster update AttachedCluster func UpdateAttachedCluster(client kurator.Interface, attachedCluster *clusterv1a1.AttachedCluster) error { - _, err := client.ClusterV1alpha1().AttachedClusters(attachedCluster.Namespace).Update(context.TODO(), attachedCluster, metav1.UpdateOptions{}) + attachedClusterPresentOnCluster, attacattachedClusterGetErr := client.ClusterV1alpha1().AttachedClusters(attachedCluster.Namespace).Get(context.TODO(), attachedCluster.Name, metav1.GetOptions{}) + if attacattachedClusterGetErr != nil { + return attacattachedClusterGetErr + } + DCattachedcluster := attachedClusterPresentOnCluster.DeepCopy() + DCattachedcluster.Spec = attachedCluster.Spec + _, err := client.ClusterV1alpha1().AttachedClusters(DCattachedcluster.Namespace).Update(context.TODO(), DCattachedcluster, metav1.UpdateOptions{}) if err != nil { return err } @@ -85,5 +91,5 @@ func WaitAttachedClusterFitWith(client kurator.Interface, namespace, name string return false } return fit(attachedClusterPresentOnCluster) - }, pollTimeout, pollIntervalInHostCluster).Should(gomega.Equal(true)) + }, pollTimeoutInHostCluster, pollIntervalInHostCluster).Should(gomega.Equal(true)) } diff --git a/e2e/resources/constant.go b/e2e/resources/constant.go index 1faeb185..5590fd38 100644 --- a/e2e/resources/constant.go +++ b/e2e/resources/constant.go @@ -19,8 +19,8 @@ package resources import "time" const ( - // pollIntervalInHostCluster defines the interval time for a poll operation. + // pollIntervalInHostCluster defines the interval time for a poll operation in host cluster. pollIntervalInHostCluster = 3 * time.Second - // pollTimeout defines the time after which the poll operation times out. - pollTimeout = 420 * time.Second + // pollTimeoutInHostCluster defines the time after which the poll operation times out in host cluster. + pollTimeoutInHostCluster = 90 * time.Second ) diff --git a/e2e/resources/fleet.go b/e2e/resources/fleet.go index f3fd8fa2..85cbbee2 100644 --- a/e2e/resources/fleet.go +++ b/e2e/resources/fleet.go @@ -19,6 +19,7 @@ package resources import ( "context" + "github.com/onsi/gomega" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -77,3 +78,14 @@ func RemoveFleet(client kurator.Interface, namespace, name string) error { } return nil } + +// WaitAttachedClusterFitWith wait fleet sync with fit func. +func WaitFleetFitWith(client kurator.Interface, namespace, name string, fit func(fleeet *fleetv1a1.Fleet) bool) { + gomega.Eventually(func() bool { + fleetPresentOnCluster, err := client.FleetV1alpha1().Fleets(namespace).Get(context.TODO(), name, metav1.GetOptions{}) + if err != nil { + return false + } + return fit(fleetPresentOnCluster) + }, pollTimeoutInHostCluster, pollIntervalInHostCluster).Should(gomega.Equal(true)) +} diff --git a/e2e/suite_test.go b/e2e/suite_test.go index d085943a..a1644087 100644 --- a/e2e/suite_test.go +++ b/e2e/suite_test.go @@ -18,13 +18,18 @@ package e2e import ( "os" + "path/filepath" "testing" + "time" "github.com/onsi/ginkgo/v2" "github.com/onsi/gomega" + corev1 "k8s.io/api/core/v1" "k8s.io/client-go/kubernetes" "kurator.dev/kurator/e2e/framework" + "kurator.dev/kurator/e2e/resources" + clusterv1a1 "kurator.dev/kurator/pkg/apis/cluster/v1alpha1" kurator "kurator.dev/kurator/pkg/client-go/generated/clientset/versioned" ) @@ -33,6 +38,12 @@ var ( kubeClient kubernetes.Interface kuratorClient kurator.Interface kuratorContext string + + namespace string + memberClusterName string + kubeconfigPath string + secret *corev1.Secret + attachedcluster *clusterv1a1.AttachedCluster ) func TestE2E(t *testing.T) { @@ -54,4 +65,39 @@ var _ = ginkgo.SynchronizedBeforeSuite(func() []byte { kuratorClient, err = kurator.NewForConfig(rest) gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) + + namespace = "e2e-test" + 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()) + data := make(map[string][]byte) + data[memberClusterName] = kubeconfig + secret = resources.NewSecret(namespace, memberClusterName, data) + + // build attachedclusters for fleet + secretKeyRef := clusterv1a1.SecretKeyRef{ + Name: memberClusterName, + Key: memberClusterName, + } + attachedcluster = resources.NewAttachedCluster(namespace, memberClusterName, secretKeyRef) + + secretCreateErr := resources.CreateSecret(kubeClient, secret) + gomega.Expect(secretCreateErr).ShouldNot(gomega.HaveOccurred()) + + attachedCreateErr := resources.CreateAttachedCluster(kuratorClient, attachedcluster) + gomega.Expect(attachedCreateErr).ShouldNot(gomega.HaveOccurred()) + resources.WaitAttachedClusterFitWith(kuratorClient, namespace, memberClusterName, func(attachedCluster *clusterv1a1.AttachedCluster) bool { + return attachedCluster.Status.Ready + }) })