Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: added e2e test for iot systems #1750

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,20 @@ func (r *ReconcilePlatformAdmin) Reconcile(ctx context.Context, request reconcil
// resource are patched back to the API server.
defer func(isDeleted *bool) {
if !*isDeleted {
platformAdmin.Status = *platformAdminStatus
// Finally check whether PlatformAdmin is Ready
platformAdminStatus.Ready = true
if cond := util.GetPlatformAdminCondition(*platformAdminStatus, iotv1alpha2.ConfigmapAvailableCondition); cond.Status == corev1.ConditionFalse {
platformAdminStatus.Ready = false
}
if cond := util.GetPlatformAdminCondition(*platformAdminStatus, iotv1alpha2.ComponentAvailableCondition); cond.Status == corev1.ConditionFalse {
platformAdminStatus.Ready = false
}
if platformAdminStatus.UnreadyComponentNum != 0 {
platformAdminStatus.Ready = false
}

// Finally update the status of PlatformAdmin
platformAdmin.Status = *platformAdminStatus
if err := r.Status().Update(ctx, platformAdmin); err != nil {
klog.Errorf(Format("Update the status of PlatformAdmin %s/%s failed", platformAdmin.Namespace, platformAdmin.Name))
reterr = kerrors.NewAggregate([]error{reterr, err})
Expand Down Expand Up @@ -257,8 +269,6 @@ func (r *ReconcilePlatformAdmin) reconcileDelete(ctx context.Context, platformAd
}
desiredComponents = append(desiredComponents, additionalComponents...)

//TODO: handle PlatformAdmin.Spec.Components

for _, dc := range desiredComponents {
if err := r.Get(
ctx,
Expand Down Expand Up @@ -332,7 +342,6 @@ func (r *ReconcilePlatformAdmin) reconcileNormal(ctx context.Context, platformAd
util.SetPlatformAdminCondition(platformAdminStatus, util.NewPlatformAdminCondition(iotv1alpha2.ComponentAvailableCondition, corev1.ConditionTrue, "", ""))

// Update the metadata of PlatformAdmin
platformAdminStatus.Ready = true
if err := r.Client.Update(ctx, platformAdmin); err != nil {
klog.Errorf(Format("Update PlatformAdmin %s error %v", klog.KObj(platformAdmin), err))
return reconcile.Result{}, err
Expand Down
5 changes: 5 additions & 0 deletions test/e2e/cmd/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ var (
yurtHubImageFormat = "openyurt/yurthub:%s"
yurtManagerImageFormat = "openyurt/yurt-manager:%s"
nodeServantImageFormat = "openyurt/node-servant:%s"
yurtIotDockImageFormat = "openyurt/yurt-iot-dock:%s"
)

func NewInitCMD(out io.Writer) *cobra.Command {
Expand Down Expand Up @@ -192,6 +193,7 @@ func (o *kindOptions) Config() *initializerConfig {
YurtHubImage: fmt.Sprintf(yurtHubImageFormat, o.OpenYurtVersion),
YurtManagerImage: fmt.Sprintf(yurtManagerImageFormat, o.OpenYurtVersion),
NodeServantImage: fmt.Sprintf(nodeServantImageFormat, o.OpenYurtVersion),
yurtIotDockImage: fmt.Sprintf(yurtIotDockImageFormat, o.OpenYurtVersion),
EnableDummyIf: o.EnableDummyIf,
DisableDefaultCNI: o.DisableDefaultCNI,
}
Expand Down Expand Up @@ -237,6 +239,7 @@ type initializerConfig struct {
YurtHubImage string
YurtManagerImage string
NodeServantImage string
yurtIotDockImage string
EnableDummyIf bool
DisableDefaultCNI bool
}
Expand Down Expand Up @@ -313,6 +316,7 @@ func (ki *Initializer) prepareImages() error {
ki.YurtHubImage,
ki.YurtManagerImage,
ki.NodeServantImage,
ki.yurtIotDockImage,
}, ki.CloudNodes); err != nil {
return err
}
Expand All @@ -321,6 +325,7 @@ func (ki *Initializer) prepareImages() error {
if err := ki.loadImagesToKindNodes([]string{
ki.YurtHubImage,
ki.NodeServantImage,
ki.yurtIotDockImage,
}, ki.EdgeNodes); err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ var _ = ginkgo.BeforeSuite(func() {
_, err = ns.CreateNameSpace(c, constants.YurtE2ENamespaceName)
gomega.Expect(err).NotTo(gomega.HaveOccurred(), "fail to create namespace")

err = util.PrepareNodePoolWithNode(context.TODO(), yurtconfig.YurtE2eCfg.RuntimeClient, "openyurt-e2e-test-worker")
LavenderQAQ marked this conversation as resolved.
Show resolved Hide resolved
gomega.Expect(err).NotTo(gomega.HaveOccurred(), "fail to create a nodepool with node")

if labelFilter([]string{"edge-autonomy"}) {
// get nginx podIP on edge node worker2
cs := c
Expand Down
38 changes: 38 additions & 0 deletions test/e2e/util/nodepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -111,3 +112,40 @@ func InitNodeAndNodePool(ctx context.Context, k8sClient client.Client, poolToNod
}
return nil
}

const (
NodePoolName = "nodepool-with-node"
)

// PrepareNodePoolWithNode will create a edge nodepool named "nodepool-with-node" and add the "openyurt-e2e-test-worker" node to this nodepool.
// In order for Pods to be successfully deployed in e2e tests, a nodepool with nodes needs to be created
func PrepareNodePoolWithNode(ctx context.Context, k8sClient client.Client, nodeName string) error {
if err := k8sClient.Get(ctx, client.ObjectKey{Name: NodePoolName}, &v1beta1.NodePool{}); err == nil {
return nil
} else if !errors.IsNotFound(err) {
return err
}

if err := k8sClient.Create(ctx, &v1beta1.NodePool{
ObjectMeta: metav1.ObjectMeta{
Name: NodePoolName,
},
Spec: v1beta1.NodePoolSpec{
Type: v1beta1.Edge,
}}); err != nil {
return err
}

node := &corev1.Node{}
if err := k8sClient.Get(ctx, client.ObjectKey{Name: nodeName}, node); err != nil {
return err
}

patchObj := client.MergeFrom(node.DeepCopy())
node.Labels[projectinfo.GetNodePoolLabel()] = NodePoolName

if err := k8sClient.Patch(ctx, node, patchObj); err != nil {
return err
}
return nil
}
4 changes: 4 additions & 0 deletions test/e2e/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import (

appsv1alpha1 "github.com/openyurtio/openyurt/pkg/apis/apps/v1alpha1"
appsv1beta1 "github.com/openyurtio/openyurt/pkg/apis/apps/v1beta1"
iotv1alpha1 "github.com/openyurtio/openyurt/pkg/apis/iot/v1alpha1"
iotv1alpha2 "github.com/openyurtio/openyurt/pkg/apis/iot/v1alpha2"
"github.com/openyurtio/openyurt/test/e2e/yurtconfig"
)

Expand All @@ -46,6 +48,8 @@ func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(appsv1alpha1.AddToScheme(scheme))
utilruntime.Must(appsv1beta1.AddToScheme(scheme))
utilruntime.Must(iotv1alpha1.AddToScheme(scheme))
utilruntime.Must(iotv1alpha2.AddToScheme(scheme))
}

const (
Expand Down
148 changes: 148 additions & 0 deletions test/e2e/yurt/iot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
Copyright 2023 The OpenYurt 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 yurt

import (
"context"
"fmt"
"time"

"github.com/google/uuid"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

iotv1alpha2 "github.com/openyurtio/openyurt/pkg/apis/iot/v1alpha2"
"github.com/openyurtio/openyurt/test/e2e/util"
ycfg "github.com/openyurtio/openyurt/test/e2e/yurtconfig"
)

func generateTestVersions() []string {
return []string{"levski", "jakarta", "kamakura", "ireland", "minnesota"}
}

var _ = Describe("OpenYurt IoT Test", Ordered, func() {
var (
platformAdminName string
namespaceName string
)

ctx := context.Background()
k8sClient := ycfg.YurtE2eCfg.RuntimeClient
timeout := 60 * time.Second
platformadminTimeout := 5 * time.Minute
testVersions := generateTestVersions()
nodePoolName := util.NodePoolName

createNamespace := func() {
By(fmt.Sprintf("create the Namespace named %s for iot e2e test", namespaceName))
Eventually(
func() error {
return k8sClient.Delete(ctx, &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespaceName,
},
})
}, timeout, 500*time.Millisecond).Should(SatisfyAny(BeNil(), &util.NotFoundMatcher{}))

ns := corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespaceName,
},
}

Eventually(
func() error {
return k8sClient.Create(ctx, &ns)
}, timeout, 500*time.Millisecond).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{}))
}

createPlatformAdmin := func(version string) {
By(fmt.Sprintf("create the PlatformAdmin named %s for iot e2e test", platformAdminName))
Eventually(func() error {
return k8sClient.Delete(ctx, &iotv1alpha2.PlatformAdmin{
ObjectMeta: metav1.ObjectMeta{
Name: platformAdminName,
Namespace: namespaceName,
},
})
}, timeout, 500*time.Millisecond).Should(SatisfyAny(BeNil(), &util.NotFoundMatcher{}))

testPlatformAdmin := iotv1alpha2.PlatformAdmin{
ObjectMeta: metav1.ObjectMeta{
Name: platformAdminName,
Namespace: namespaceName,
},
Spec: iotv1alpha2.PlatformAdminSpec{
Version: version,
PoolName: nodePoolName,
},
}
Eventually(func() error {
return k8sClient.Create(ctx, &testPlatformAdmin)
}, timeout, 500*time.Millisecond).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{}))
}

BeforeEach(func() {
By("Start to run iot test, clean up previous resources")
k8sClient = ycfg.YurtE2eCfg.RuntimeClient

longUUID := uuid.New()
shortUUID := longUUID.String()[:8]
namespaceName = "iot-test-" + shortUUID

createNamespace()
})

AfterEach(func() {
By("Cleanup resources after test")
By(fmt.Sprintf("Delete the entire namespace named %s", namespaceName))
Expect(k8sClient.Delete(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespaceName}})).Should(SatisfyAny(BeNil(), &util.NotFoundMatcher{}))
})

for _, testVersion := range testVersions {
version := testVersion
Describe(fmt.Sprintf("Test the %s version of PlatformAdmin", version), func() {
BeforeEach(func() {
platformAdminName = "test-platform-admin-" + version
createPlatformAdmin(version)
})

AfterEach(func() {
By(fmt.Sprintf("Delete the platformAdmin %s", platformAdminName))
Expect(k8sClient.Delete(ctx, &iotv1alpha2.PlatformAdmin{ObjectMeta: metav1.ObjectMeta{Name: platformAdminName, Namespace: namespaceName}})).Should(BeNil())
})

It(fmt.Sprintf("The %s version of PlatformAdmin should be stable in ready state after it is created", version), func() {
By("verify the status of platformadmin")
Eventually(func() error {
testPlatfromAdmin := &iotv1alpha2.PlatformAdmin{}
if err := k8sClient.Get(ctx, types.NamespacedName{Name: platformAdminName, Namespace: namespaceName}, testPlatfromAdmin); err != nil {
return err
}
if testPlatfromAdmin.Status.Ready == true {
LavenderQAQ marked this conversation as resolved.
Show resolved Hide resolved
return nil
} else {
return fmt.Errorf("The %s version of PlatformAdmin is not ready", version)
}
}, platformadminTimeout, 5*time.Second).Should(Succeed())
})
})
}
})
Loading