Skip to content

Commit

Permalink
add fleet status check and delete resource when finish e2e test
Browse files Browse the repository at this point in the history
Signed-off-by: LiZhenCheng9527 <lizhencheng6@huawei.com>
  • Loading branch information
LiZhenCheng9527 committed Feb 2, 2024
1 parent 31f126f commit fe94542
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 2 deletions.
33 changes: 31 additions & 2 deletions e2e/attachedcluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ 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() {
Expand All @@ -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())
Expand All @@ -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)
Expand All @@ -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{
{
Expand All @@ -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))
})
})
12 changes: 12 additions & 0 deletions e2e/resources/attachedcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
13 changes: 13 additions & 0 deletions e2e/resources/fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
}
59 changes: 59 additions & 0 deletions e2e/resources/namespace.go
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 12 additions & 0 deletions e2e/resources/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit fe94542

Please sign in to comment.