Skip to content

Commit 386cab0

Browse files
The operations of creating the secret and attachedcluster are moved to the suite_test (#599)
Signed-off-by: LiZhenCheng9527 <lizhencheng6@huawei.com>
1 parent fbb0ef7 commit 386cab0

File tree

6 files changed

+137
-122
lines changed

6 files changed

+137
-122
lines changed

e2e/attachedcluster_test.go

Lines changed: 0 additions & 117 deletions
This file was deleted.

e2e/fleet_attachedcluster_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Copyright Kurator Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package e2e
18+
19+
import (
20+
"github.com/onsi/ginkgo/v2"
21+
"github.com/onsi/gomega"
22+
corev1 "k8s.io/api/core/v1"
23+
24+
"kurator.dev/kurator/e2e/resources"
25+
fleetv1a1 "kurator.dev/kurator/pkg/apis/fleet/v1alpha1"
26+
)
27+
28+
var _ = ginkgo.Describe("[AttachedClusters] AttachedClusters testing", func() {
29+
var (
30+
fleetname string
31+
fleet *fleetv1a1.Fleet
32+
)
33+
34+
ginkgo.BeforeEach(func() {
35+
fleetname = "e2e"
36+
// build fleet
37+
clusters := []*corev1.ObjectReference{
38+
{
39+
Name: memberClusterName,
40+
Kind: "AttachedCluster",
41+
},
42+
}
43+
fleet = resources.NewFleet(namespace, fleetname, clusters)
44+
})
45+
46+
ginkgo.AfterEach(func() {
47+
fleerRemoveErr := resources.RemoveFleet(kuratorClient, namespace, fleetname)
48+
gomega.Expect(fleerRemoveErr).ShouldNot(gomega.HaveOccurred())
49+
50+
attachedclusterRemoveErr := resources.RemoveAttachedCluster(kuratorClient, namespace, memberClusterName)
51+
gomega.Expect(attachedclusterRemoveErr).ShouldNot(gomega.HaveOccurred())
52+
53+
secretRemoveErr := resources.RemoveSecret(kubeClient, namespace, memberClusterName)
54+
gomega.Expect(secretRemoveErr).ShouldNot(gomega.HaveOccurred())
55+
56+
namespaceRemoveErr := resources.RemoveNamespace(kubeClient, namespace)
57+
gomega.Expect(namespaceRemoveErr).ShouldNot(gomega.HaveOccurred())
58+
})
59+
60+
ginkgo.It("Create Fleet", func() {
61+
// create fleet and checkout fleet status
62+
fleetCreateErr := resources.CreateFleet(kuratorClient, fleet)
63+
gomega.Expect(fleetCreateErr).ShouldNot(gomega.HaveOccurred())
64+
resources.WaitFleetFitWith(kuratorClient, namespace, fleetname, func(fleet *fleetv1a1.Fleet) bool {
65+
return fleet.Status.Phase == fleetv1a1.ReadyPhase
66+
})
67+
})
68+
})

e2e/resources/attachedcluster.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,13 @@ func CreateAttachedCluster(client kurator.Interface, attachedCluster *clusterv1a
5858

5959
// UpdateAttachedCluster update AttachedCluster
6060
func UpdateAttachedCluster(client kurator.Interface, attachedCluster *clusterv1a1.AttachedCluster) error {
61-
_, err := client.ClusterV1alpha1().AttachedClusters(attachedCluster.Namespace).Update(context.TODO(), attachedCluster, metav1.UpdateOptions{})
61+
attachedClusterPresentOnCluster, attacattachedClusterGetErr := client.ClusterV1alpha1().AttachedClusters(attachedCluster.Namespace).Get(context.TODO(), attachedCluster.Name, metav1.GetOptions{})
62+
if attacattachedClusterGetErr != nil {
63+
return attacattachedClusterGetErr
64+
}
65+
DCattachedcluster := attachedClusterPresentOnCluster.DeepCopy()
66+
DCattachedcluster.Spec = attachedCluster.Spec
67+
_, err := client.ClusterV1alpha1().AttachedClusters(DCattachedcluster.Namespace).Update(context.TODO(), DCattachedcluster, metav1.UpdateOptions{})
6268
if err != nil {
6369
return err
6470
}
@@ -85,5 +91,5 @@ func WaitAttachedClusterFitWith(client kurator.Interface, namespace, name string
8591
return false
8692
}
8793
return fit(attachedClusterPresentOnCluster)
88-
}, pollTimeout, pollIntervalInHostCluster).Should(gomega.Equal(true))
94+
}, pollTimeoutInHostCluster, pollIntervalInHostCluster).Should(gomega.Equal(true))
8995
}

e2e/resources/constant.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ package resources
1919
import "time"
2020

2121
const (
22-
// pollIntervalInHostCluster defines the interval time for a poll operation.
22+
// pollIntervalInHostCluster defines the interval time for a poll operation in host cluster.
2323
pollIntervalInHostCluster = 3 * time.Second
24-
// pollTimeout defines the time after which the poll operation times out.
25-
pollTimeout = 420 * time.Second
24+
// pollTimeoutInHostCluster defines the time after which the poll operation times out in host cluster.
25+
pollTimeoutInHostCluster = 90 * time.Second
2626
)

e2e/resources/fleet.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package resources
1919
import (
2020
"context"
2121

22+
"github.com/onsi/gomega"
2223
corev1 "k8s.io/api/core/v1"
2324
apierrors "k8s.io/apimachinery/pkg/api/errors"
2425
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -77,3 +78,14 @@ func RemoveFleet(client kurator.Interface, namespace, name string) error {
7778
}
7879
return nil
7980
}
81+
82+
// WaitAttachedClusterFitWith wait fleet sync with fit func.
83+
func WaitFleetFitWith(client kurator.Interface, namespace, name string, fit func(fleeet *fleetv1a1.Fleet) bool) {
84+
gomega.Eventually(func() bool {
85+
fleetPresentOnCluster, err := client.FleetV1alpha1().Fleets(namespace).Get(context.TODO(), name, metav1.GetOptions{})
86+
if err != nil {
87+
return false
88+
}
89+
return fit(fleetPresentOnCluster)
90+
}, pollTimeoutInHostCluster, pollIntervalInHostCluster).Should(gomega.Equal(true))
91+
}

e2e/suite_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@ package e2e
1818

1919
import (
2020
"os"
21+
"path/filepath"
2122
"testing"
23+
"time"
2224

2325
"github.com/onsi/ginkgo/v2"
2426
"github.com/onsi/gomega"
27+
corev1 "k8s.io/api/core/v1"
2528
"k8s.io/client-go/kubernetes"
2629

2730
"kurator.dev/kurator/e2e/framework"
31+
"kurator.dev/kurator/e2e/resources"
32+
clusterv1a1 "kurator.dev/kurator/pkg/apis/cluster/v1alpha1"
2833
kurator "kurator.dev/kurator/pkg/client-go/generated/clientset/versioned"
2934
)
3035

@@ -33,6 +38,12 @@ var (
3338
kubeClient kubernetes.Interface
3439
kuratorClient kurator.Interface
3540
kuratorContext string
41+
42+
namespace string
43+
memberClusterName string
44+
kubeconfigPath string
45+
secret *corev1.Secret
46+
attachedcluster *clusterv1a1.AttachedCluster
3647
)
3748

3849
func TestE2E(t *testing.T) {
@@ -54,4 +65,39 @@ var _ = ginkgo.SynchronizedBeforeSuite(func() []byte {
5465

5566
kuratorClient, err = kurator.NewForConfig(rest)
5667
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
68+
69+
namespace = "e2e-test"
70+
memberClusterName = "kurator-member"
71+
homeDir, err := os.UserHomeDir()
72+
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
73+
kubeconfigPath = filepath.Join(homeDir, ".kube/kurator-member.config")
74+
75+
// create namespace for e2e test
76+
e2eNamespace := resources.NewNamespace(namespace)
77+
createNSErr := resources.CreateNamespace(kubeClient, e2eNamespace)
78+
gomega.Expect(createNSErr).ShouldNot(gomega.HaveOccurred())
79+
time.Sleep(3 * time.Second)
80+
81+
// build secrets use member cluster kubeconfig
82+
kubeconfig, readfileErr := os.ReadFile(kubeconfigPath)
83+
gomega.Expect(readfileErr).ShouldNot(gomega.HaveOccurred())
84+
data := make(map[string][]byte)
85+
data[memberClusterName] = kubeconfig
86+
secret = resources.NewSecret(namespace, memberClusterName, data)
87+
88+
// build attachedclusters for fleet
89+
secretKeyRef := clusterv1a1.SecretKeyRef{
90+
Name: memberClusterName,
91+
Key: memberClusterName,
92+
}
93+
attachedcluster = resources.NewAttachedCluster(namespace, memberClusterName, secretKeyRef)
94+
95+
secretCreateErr := resources.CreateSecret(kubeClient, secret)
96+
gomega.Expect(secretCreateErr).ShouldNot(gomega.HaveOccurred())
97+
98+
attachedCreateErr := resources.CreateAttachedCluster(kuratorClient, attachedcluster)
99+
gomega.Expect(attachedCreateErr).ShouldNot(gomega.HaveOccurred())
100+
resources.WaitAttachedClusterFitWith(kuratorClient, namespace, memberClusterName, func(attachedCluster *clusterv1a1.AttachedCluster) bool {
101+
return attachedCluster.Status.Ready
102+
})
57103
})

0 commit comments

Comments
 (0)