Skip to content

Commit

Permalink
use go code to write kurator 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 1, 2024
1 parent 320a72f commit 9d15200
Show file tree
Hide file tree
Showing 13 changed files with 473 additions and 94 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ jobs:
hack/e2e-test/install.sh
- name: fleet-clusters e2e test
run: |
hack/e2e-test/fleet-clusters/e2e-run.sh
hack/e2e-test/run-e2e.sh
108 changes: 108 additions & 0 deletions e2e/attachedcluster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
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 (
"os"
"time"

"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"

"kurator.dev/kurator/e2e/resources"
clusterv1a1 "kurator.dev/kurator/pkg/apis/cluster/v1alpha1"
)

var _ = ginkgo.Describe("[AttachedClusters] AttachedClusters testing", func() {
var (
namespace string
fleetname string
memberClusterName1 string
memberClusterName2 string
kubeconfig1Path string
kubeconfig2Path string
secret1 *corev1.Secret
secret2 *corev1.Secret
attachedcluster1 *clusterv1a1.AttachedCluster
attachedcluster2 *clusterv1a1.AttachedCluster
)

ginkgo.BeforeEach(func() {
namespace = "default"
fleetname = "e2etest"
memberClusterName1 = "kurator-member1"
memberClusterName2 = "kurator-member2"
kubeconfig1Path = "/root/.kube/kurator-member1.config"
kubeconfig2Path = "/root/.kube/kurator-member2.config"

// build two secrets
kubeconfig1, readfileErr1 := os.ReadFile(kubeconfig1Path)
gomega.Expect(readfileErr1).ShouldNot(gomega.HaveOccurred())
data1 := make(map[string][]byte)
data1[memberClusterName1] = kubeconfig1
secret1 = resources.NewSecret(namespace, memberClusterName1, data1)

kubeconfig2, readfileErr2 := os.ReadFile(kubeconfig2Path)
gomega.Expect(readfileErr2).ShouldNot(gomega.HaveOccurred())
data2 := make(map[string][]byte)
data2[memberClusterName2] = kubeconfig2
secret2 = resources.NewSecret(namespace, memberClusterName2, data2)

// build two attachedclusters
secretKeyRef1 := clusterv1a1.SecretKeyRef{
Name: memberClusterName1,
Key: memberClusterName1,
}
secretKeyRef2 := clusterv1a1.SecretKeyRef{
Name: memberClusterName2,
Key: memberClusterName2,
}
attachedcluster1 = resources.NewAttachedCluster(namespace, memberClusterName1, secretKeyRef1)
attachedcluster2 = resources.NewAttachedCluster(namespace, memberClusterName2, secretKeyRef2)
})

ginkgo.It("Create Fleet", func() {
// step 1.create secrets
secretCreateErr1 := resources.CreateSecret(kubeClient, secret1)
secretCreateErr2 := resources.CreateSecret(kubeClient, secret2)
gomega.Expect(secretCreateErr1).ShouldNot(gomega.HaveOccurred())
gomega.Expect(secretCreateErr2).ShouldNot(gomega.HaveOccurred())

// step 2.create attachedclusters
attachedCreateErr1 := resources.CreateAttachedCluster(kuratorClient, attachedcluster1)
attachedCreateErr2 := resources.CreateAttachedCluster(kuratorClient, attachedcluster2)
gomega.Expect(attachedCreateErr1).ShouldNot(gomega.HaveOccurred())
gomega.Expect(attachedCreateErr2).ShouldNot(gomega.HaveOccurred())

time.Sleep(5 * time.Second)
// step 3.create fleet
clusters := []*corev1.ObjectReference{
{
Name: memberClusterName1,
Kind: "AttachedCluster",
},
{
Name: memberClusterName2,
Kind: "AttachedCluster",
},
}
fleet := resources.NewFleet(namespace, fleetname, clusters)
fleetCreateErr := resources.CreateFleet(kuratorClient, fleet)
gomega.Expect(fleetCreateErr).ShouldNot(gomega.HaveOccurred())
})
})
44 changes: 44 additions & 0 deletions e2e/framework/cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
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 framework

import (
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
)

// LoadRESTClientConfig creates a rest.Config using the passed kubeconfig. If context is empty, current context in kubeconfig will be used.
func LoadRESTClientConfig(kubeconfig string, context string) (*rest.Config, error) {
loader := &clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig}
loadedConfig, err := loader.Load()
if err != nil {
return nil, err
}

if context == "" {
context = loadedConfig.CurrentContext
}
klog.Infof("Use context %v", context)

return clientcmd.NewNonInteractiveClientConfig(
*loadedConfig,
context,
&clientcmd.ConfigOverrides{},
loader,
).ClientConfig()
}
65 changes: 65 additions & 0 deletions e2e/resources/attachedcluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
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"

apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

clusterv1a1 "kurator.dev/kurator/pkg/apis/cluster/v1alpha1"
kurator "kurator.dev/kurator/pkg/client-go/generated/clientset/versioned"
)

func NewAttachedCluster(namespace string, name string, config clusterv1a1.SecretKeyRef) *clusterv1a1.AttachedCluster {
return &clusterv1a1.AttachedCluster{
TypeMeta: metav1.TypeMeta{
APIVersion: "cluster.kurator.dev/v1alpha1",
Kind: "AttachedCluster",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
},
Spec: clusterv1a1.AttachedClusterSpec{
Kubeconfig: config,
},
}
}

// CreateAttachedCluster create AttachedCluster.
func CreateAttachedCluster(client kurator.Interface, attachedCluster *clusterv1a1.AttachedCluster) error {
_, err := client.ClusterV1alpha1().AttachedClusters(attachedCluster.Namespace).Create(context.TODO(), attachedCluster, metav1.CreateOptions{})
if err != nil {
if apierrors.IsAlreadyExists(err) {
return UpdateAttachedCluster(client, attachedCluster)
} else {
return err
}
}
return nil
}

// UpdateAttachedCluster update AttachedCluster
func UpdateAttachedCluster(client kurator.Interface, attachedCluster *clusterv1a1.AttachedCluster) error {
_, err := client.ClusterV1alpha1().AttachedClusters(attachedCluster.Namespace).Update(context.TODO(), attachedCluster, metav1.UpdateOptions{})
if err != nil {
return err
}
return nil
}
28 changes: 28 additions & 0 deletions e2e/resources/constant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
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 "time"

const (
// pollInterval defines the interval time for a poll operation.
pollInterval = 5 * time.Second
// pollTimeout defines the time after which the poll operation times out.
pollTimeout = 420 * time.Second
// metricsCreationDelay defines the maximum time metrics not yet available for pod.
metricsCreationDelay = 2 * time.Minute
)
66 changes: 66 additions & 0 deletions e2e/resources/fleet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
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"

fleetv1a1 "kurator.dev/kurator/pkg/apis/fleet/v1alpha1"
kurator "kurator.dev/kurator/pkg/client-go/generated/clientset/versioned"
)

func NewFleet(namespace string, name string, clusters []*corev1.ObjectReference) *fleetv1a1.Fleet {
return &fleetv1a1.Fleet{
TypeMeta: metav1.TypeMeta{
APIVersion: "fleet.kurator.dev/v1alpha1",
Kind: "Fleet",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
},
Spec: fleetv1a1.FleetSpec{
Clusters: clusters,
},
}
}

// CreateAttachedCluster create AttachedCluster.
func CreateFleet(client kurator.Interface, fleet *fleetv1a1.Fleet) error {
_, err := client.FleetV1alpha1().Fleets(fleet.Namespace).Create(context.TODO(), fleet, metav1.CreateOptions{})
if err != nil {
if apierrors.IsAlreadyExists(err) {
return UpdateFleet(client, fleet)
} else {
return err
}
}
return nil
}

// UpdateAttachedCluster update AttachedCluster
func UpdateFleet(client kurator.Interface, fleet *fleetv1a1.Fleet) error {
_, err := client.FleetV1alpha1().Fleets(fleet.Namespace).Update(context.TODO(), fleet, metav1.UpdateOptions{})
if err != nil {
return err
}
return nil
}
63 changes: 63 additions & 0 deletions e2e/resources/secret.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
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"
)

// NewSecret will build a secret object.
func NewSecret(namespace string, name string, data map[string][]byte) *corev1.Secret {
return &corev1.Secret{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Secret",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
},
Data: data,
}
}

// CreateSecret create Secret.
func CreateSecret(client kubernetes.Interface, secret *corev1.Secret) error {
_, err := client.CoreV1().Secrets(secret.Namespace).Create(context.TODO(), secret, metav1.CreateOptions{})
if err != nil {
if apierrors.IsAlreadyExists(err) {
return UpdateSecret(client, secret)
} else {
return err
}
}
return nil
}

// UpdateSecret update Secret
func UpdateSecret(client kubernetes.Interface, secret *corev1.Secret) error {
_, err := client.CoreV1().Secrets(secret.Namespace).Update(context.TODO(), secret, metav1.UpdateOptions{})
if err != nil {
return err
}
return nil
}
Loading

0 comments on commit 9d15200

Please sign in to comment.