Skip to content

Commit

Permalink
The operations of creating the secret and attachedcluster are moved t…
Browse files Browse the repository at this point in the history
…o the suite_test

Signed-off-by: LiZhenCheng9527 <[email protected]>
  • Loading branch information
LiZhenCheng9527 committed Feb 5, 2024
1 parent fbb0ef7 commit 870682e
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 122 deletions.
117 changes: 0 additions & 117 deletions e2e/attachedcluster_test.go

This file was deleted.

68 changes: 68 additions & 0 deletions e2e/fleet_attachedcluster_test.go
Original file line number Diff line number Diff line change
@@ -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
})
})
})
10 changes: 8 additions & 2 deletions e2e/resources/attachedcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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))
}
6 changes: 3 additions & 3 deletions e2e/resources/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
12 changes: 12 additions & 0 deletions e2e/resources/fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
}
46 changes: 46 additions & 0 deletions e2e/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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) {
Expand All @@ -54,4 +65,39 @@ var _ = ginkgo.SynchronizedBeforeSuite(func() []byte {

kuratorClient, err = kurator.NewForConfig(rest)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())

namespace = "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 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
})
})

0 comments on commit 870682e

Please sign in to comment.