diff --git a/test/e2e/util/nodepool.go b/test/e2e/util/nodepool.go index 25a56c9464b..47b8bfe1a4d 100644 --- a/test/e2e/util/nodepool.go +++ b/test/e2e/util/nodepool.go @@ -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 } @@ -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, diff --git a/test/e2e/yurt/nodepool.go b/test/e2e/yurt/nodepool.go index 109beec7483..5e84d4c79a1 100644 --- a/test/e2e/yurt/nodepool.go +++ b/test/e2e/yurt/nodepool.go @@ -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. @@ -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()) + }) + +})