Skip to content

Commit

Permalink
test: fix nodepool e2e test (#2283)
Browse files Browse the repository at this point in the history
Co-authored-by: Simon Tien <[email protected]>
  • Loading branch information
tnsimon and Simon Tien authored Feb 3, 2025
1 parent d690808 commit 887a02e
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 109 deletions.
28 changes: 21 additions & 7 deletions test/e2e/util/nodepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,29 @@ import (
"github.com/openyurtio/openyurt/pkg/projectinfo"
)

func CleanupNodePool(ctx context.Context, k8sClient client.Client) error {
nps := &v1beta2.NodePoolList{}
if err := k8sClient.List(ctx, nps); err != nil {
return err
// GetNodepool will get the nodepool with the given name
func GetNodepool(ctx context.Context, k8sClient client.Client, name string) (*v1beta2.NodePool, error) {
pool := &v1beta2.NodePool{}
if err := k8sClient.Get(ctx, client.ObjectKey{Name: name}, pool); err != nil {
return nil, err
}
for _, tmp := range nps.Items {
if err := k8sClient.Delete(ctx, &tmp); err != nil {
return err
return pool, nil
}

// DeleteNodePool will delete the nodepool with the given name
func DeleteNodePool(ctx context.Context, k8sClient client.Client, name string) error {
pool, err := GetNodepool(ctx, k8sClient, name)
if err != nil {
if errors.IsNotFound(err) {
return nil
}
return err
}

if err := k8sClient.Delete(ctx, pool); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -68,6 +81,7 @@ func CleanupNodePoolLabel(ctx context.Context, k8sClient client.Client) error {
return nil
}

// InitNodeAndNodePool will create nodepools and add labels to nodes according to the poolToNodesMap
func InitNodeAndNodePool(
ctx context.Context,
k8sClient client.Client,
Expand Down
196 changes: 94 additions & 102 deletions test/e2e/yurt/nodepool.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2022 The OpenYurt Authors.
Copyright 2025 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.
Expand All @@ -16,104 +16,96 @@ limitations under the License.

package yurt

//import (
// "context"
// "errors"
// "fmt"
// "time"
//
// . "github.com/onsi/ginkgo/v2"
// . "github.com/onsi/gomega"
// "k8s.io/apimachinery/pkg/util/rand"
// "k8s.io/apimachinery/pkg/util/sets"
// runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
//
// "github.com/openyurtio/openyurt/pkg/apis/apps/v1beta2"
// "github.com/openyurtio/openyurt/test/e2e/util"
// ycfg "github.com/openyurtio/openyurt/test/e2e/yurtconfig"
//)
//
//var _ = Describe("nodepool test", func() {
// ctx := context.Background()
// var k8sClient runtimeclient.Client
// poolToNodesMap := make(map[string]sets.String)
//
// checkNodePoolStatus := func(poolToNodesMap map[string]sets.String) error {
// nps := &v1beta2.NodePoolList{}
// if err := k8sClient.List(ctx, nps); err != nil {
// return err
// }
// for _, tmp := range nps.Items {
// if int(tmp.Status.ReadyNodeNum) != poolToNodesMap[tmp.Name].Len() {
// return errors.New("nodepool size not match")
// }
// }
// return nil
// }
//
// BeforeEach(func() {
// By("Start to run nodepool test, cleanup previous resources")
// k8sClient = ycfg.YurtE2eCfg.RuntimeClient
// poolToNodesMap = map[string]sets.String{}
//
// util.CleanupNodePoolLabel(ctx, k8sClient)
// util.CleanupNodePool(ctx, k8sClient)
// })
//
// AfterEach(func() {
// By("Cleanup resources after test")
// util.CleanupNodePoolLabel(ctx, k8sClient)
// util.CleanupNodePool(ctx, k8sClient)
// })
//
// It("Test NodePool empty", func() {
// By("Run noolpool empty")
// Eventually(
// func() error {
// return util.InitNodeAndNodePool(ctx, k8sClient, poolToNodesMap)
// },
// time.Second*5, time.Millisecond*500).Should(SatisfyAny(BeNil()))
//
// Eventually(
// func() error {
// return checkNodePoolStatus(poolToNodesMap)
// },
// time.Second*5, time.Millisecond*500).Should(SatisfyAny(BeNil()))
// })
//
// It("Test NodePool create", func() {
// By("Run nodepool create")
//
// npName := fmt.Sprintf("test-%s", rand.String(4))
// poolToNodesMap[npName] = sets.NewString("openyurt-e2e-test-worker", "openyurt-e2e-test-worker2")
// Eventually(
// func() error {
// return util.InitNodeAndNodePool(ctx, k8sClient, poolToNodesMap)
// },
// time.Second*5, time.Millisecond*500).Should(SatisfyAny(BeNil()))
//
// Eventually(
// func() error {
// return checkNodePoolStatus(poolToNodesMap)
// },
// time.Second*5, time.Millisecond*500).Should(SatisfyAny(BeNil()))
// })
//
// It(" Test Multiple NodePools With Nodes", func() {
// poolToNodesMap["beijing"] = sets.NewString("openyurt-e2e-test-worker")
// poolToNodesMap["hangzhou"] = sets.NewString("openyurt-e2e-test-worker2")
//
// Eventually(
// func() error {
// return util.InitNodeAndNodePool(ctx, k8sClient, poolToNodesMap)
// },
// time.Second*5, time.Millisecond*500).Should(SatisfyAny(BeNil()))
//
// Eventually(
// func() error {
// return checkNodePoolStatus(poolToNodesMap)
// },
// time.Second*5, time.Millisecond*500).Should(SatisfyAny(BeNil()))
// })
//
//})
import (
"context"
"errors"
"fmt"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/util/sets"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"

"github.com/openyurtio/openyurt/test/e2e/util"
ycfg "github.com/openyurtio/openyurt/test/e2e/yurtconfig"
)

var _ = Describe("nodepool test", func() {
ctx := context.Background()
var k8sClient runtimeclient.Client

// checkNodePoolStatus checks the status of the nodepool in poolToNodesMap
// The nodepool is fetched from the k8sClient and the ready node number is checked
// with the number of nodes expected in the pool
checkNodePoolStatus := func(poolToNodesMap map[string]sets.Set[string]) error {
for npName, nodes := range poolToNodesMap {
// Get the node pool
pool, err := util.GetNodepool(ctx, k8sClient, npName)
if err != nil {
return err
}

// Compare length with the number of nodes in map
if int(pool.Status.ReadyNodeNum) != nodes.Len() {
return errors.New("nodepool size not match")
}
}
return nil
}

BeforeEach(func() {
By("Start to run nodepool test, cleanup previous resources")
k8sClient = ycfg.YurtE2eCfg.RuntimeClient
})

AfterEach(func() {})

It("Test Nodepool lifecycle", func() {
By("Run creating an empty nodepool and then deleting it")
// We can delete an empty nodepool
npName := fmt.Sprintf("test-%d", time.Now().Unix())
poolToNodesMap := map[string]sets.Set[string]{
npName: {},
}

Eventually(
func() error {
return util.InitNodeAndNodePool(ctx, k8sClient, poolToNodesMap)
},
time.Second*5, time.Millisecond*500).Should(BeNil())

Eventually(
func() error {
return checkNodePoolStatus(poolToNodesMap)
},
time.Second*5, time.Millisecond*500).Should(BeNil())

Eventually(
func() error {
return util.DeleteNodePool(ctx, k8sClient, npName)
},
time.Second*5, time.Millisecond*500).Should(BeNil())
})

It("Test NodePool create not empty", func() {
By("Run nodepool create with worker 2") // worker 1 is already mapped to a pool
npName := fmt.Sprintf("test-%d", time.Now().Unix())
poolToNodesMap := map[string]sets.Set[string]{
npName: sets.New("openyurt-e2e-test-worker2"), // we will use this worker in the nodepool
}

Eventually(
func() error {
return util.InitNodeAndNodePool(ctx, k8sClient, poolToNodesMap)
},
time.Second*5, time.Millisecond*500).Should(BeNil())

Eventually(
func() error {
return checkNodePoolStatus(poolToNodesMap)
},
time.Second*5, time.Millisecond*500).Should(BeNil())
})

})

0 comments on commit 887a02e

Please sign in to comment.