From 3b70be82b2e28ecbd98266ec5d1f80af29564e05 Mon Sep 17 00:00:00 2001 From: Tao Zou Date: Tue, 5 Nov 2024 16:11:31 +0800 Subject: [PATCH] Add staticroute e2e test Add staticroute e2e test --- pkg/nsx/services/common/types.go | 1 + pkg/nsx/services/staticroute/staticroute.go | 15 ++-- test/e2e/nsx_staticroute_test.go | 78 +++++++++++++++++++++ 3 files changed, 86 insertions(+), 8 deletions(-) create mode 100644 test/e2e/nsx_staticroute_test.go diff --git a/pkg/nsx/services/common/types.go b/pkg/nsx/services/common/types.go index 6f09432ac..6f075260b 100644 --- a/pkg/nsx/services/common/types.go +++ b/pkg/nsx/services/common/types.go @@ -146,6 +146,7 @@ var ( ResourceTypeVirtualMachine = "VirtualMachine" ResourceTypeLBService = "LBService" ResourceTypeVpcAttachment = "VpcAttachment" + ResourceTypeStaticRoute = "StaticRoutes" ResourceTypeShare = "Share" ResourceTypeSharedResource = "SharedResource" ResourceTypeChildSharedResource = "ChildSharedResource" diff --git a/pkg/nsx/services/staticroute/staticroute.go b/pkg/nsx/services/staticroute/staticroute.go index 4a007b538..7144761ad 100644 --- a/pkg/nsx/services/staticroute/staticroute.go +++ b/pkg/nsx/services/staticroute/staticroute.go @@ -24,9 +24,8 @@ type StaticRouteService struct { } var ( - log = &logger.Log - resourceTypeStaticRoute = "StaticRoutes" - String = common.String + log = &logger.Log + String = common.String ) // InitializeStaticRoute sync NSX resources @@ -47,7 +46,7 @@ func InitializeStaticRoute(commonService common.Service, vpcService common.VPCSe staticRouteService.NSXConfig = commonService.NSXConfig staticRouteService.VPCService = vpcService - go staticRouteService.InitializeResourceStore(&wg, fatalErrors, resourceTypeStaticRoute, nil, staticRouteService.StaticRouteStore) + go staticRouteService.InitializeResourceStore(&wg, fatalErrors, common.ResourceTypeStaticRoute, nil, staticRouteService.StaticRouteStore) go func() { wg.Wait() @@ -120,7 +119,7 @@ func (service *StaticRouteService) DeleteStaticRouteByPath(orgId string, project return err } - log.Info("successfully deleted NSX StaticRoute", "nsxStaticRoute", *staticroute.Id) + log.Info("Successfully deleted NSX StaticRoute", "nsxStaticRoute", *staticroute.Id) return nil } func (service *StaticRouteService) GetUID(staticroute *model.StaticRoutes) *string { @@ -173,17 +172,17 @@ func (service *StaticRouteService) ListStaticRoute() []*model.StaticRoutes { func (service *StaticRouteService) Cleanup(ctx context.Context) error { staticRouteSet := service.ListStaticRoute() - log.Info("cleanup staticroute", "count", len(staticRouteSet)) + log.Info("Cleanup staticroute", "count", len(staticRouteSet)) for _, staticRoute := range staticRouteSet { path := strings.Split(*staticRoute.Path, "/") - log.Info("removing staticroute", "staticroute path", *staticRoute.Path) + log.Info("Deleting staticroute", "staticroute path", *staticRoute.Path) select { case <-ctx.Done(): return errors.Join(nsxutil.TimeoutFailed, ctx.Err()) default: err := service.DeleteStaticRouteByPath(path[2], path[4], path[6], *staticRoute.Id) if err != nil { - log.Error(err, "remove staticroute failed", "staticroute id", *staticRoute.Id) + log.Error(err, "Delete staticroute failed", "staticroute id", *staticRoute.Id) return err } } diff --git a/test/e2e/nsx_staticroute_test.go b/test/e2e/nsx_staticroute_test.go new file mode 100644 index 000000000..b65337c53 --- /dev/null +++ b/test/e2e/nsx_staticroute_test.go @@ -0,0 +1,78 @@ +// This file is for e2e StaticRoute tests. + +package e2e + +import ( + "context" + "testing" + "time" + + "github.com/stretchr/testify/require" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/errors" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/wait" + + "github.com/vmware-tanzu/nsx-operator/pkg/apis/vpc/v1alpha1" + "github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/common" +) + +const ( + StaticRoute = "StaticRoute" + TestNamespace = "sc-a" + StaticRouteName = "guestcluster-staticroute-2" +) + +// TestStaticRouteBasic verifies that it could successfully realize StaticRoute. +func TestStaticRouteBasic(t *testing.T) { + setupTest(t, TestNamespace) + defer teardownTest(t, TestNamespace, defaultTimeout) + t.Run("case=CreateStaticRoute", CreateStaticRoute) + t.Run("case=DeleteStaticRoute", DeleteStaticRoute) +} + +func waitForStaticRouteCRReady(t *testing.T, ns, staticRouteName string) (res *v1alpha1.StaticRoute) { + log.Info("Waiting for StaticRoute CR to be ready", "ns", ns, "staticRouteName", staticRouteName) + err := wait.PollUntilContextTimeout(context.TODO(), 3*time.Second, 60*time.Second, true, func(ctx context.Context) (done bool, err error) { + res, err = testData.crdClientset.CrdV1alpha1().StaticRoutes(ns).Get(context.TODO(), staticRouteName, v1.GetOptions{}) + if err != nil { + log.Error(err, "Error fetching StaticRoute", "namespace", ns, "name", staticRouteName) + return false, nil + } + log.Info("StaticRoute status", "status", res.Status) + for _, con := range res.Status.Conditions { + log.Info("Checking condition", "type", con.Type, "status", con.Status) + if con.Type == v1alpha1.Ready && con.Status == corev1.ConditionTrue { + return true, nil + } + } + return false, nil + }) + require.NoError(t, err) + return +} +func CreateStaticRoute(t *testing.T) { + nextHop := v1alpha1.NextHop{IPAddress: "192.168.0.1"} + staticRoute := &v1alpha1.StaticRoute{ + Spec: v1alpha1.StaticRouteSpec{ + Network: "45.1.2.0/24", + NextHops: []v1alpha1.NextHop{nextHop}, + }} + staticRoute.Name = StaticRouteName + _, err := testData.crdClientset.CrdV1alpha1().StaticRoutes(TestNamespace).Create(context.TODO(), staticRoute, v1.CreateOptions{}) + if err != nil && errors.IsAlreadyExists(err) { + err = nil + } + require.NoError(t, err) + waitForStaticRouteCRReady(t, TestNamespace, staticRoute.Name) + err = testData.waitForResourceExistOrNot(TestNamespace, common.ResourceTypeStaticRoute, staticRoute.Name, true) + require.NoError(t, err) +} + +func DeleteStaticRoute(t *testing.T) { + err := testData.crdClientset.CrdV1alpha1().StaticRoutes(TestNamespace).Delete(context.TODO(), StaticRouteName, v1.DeleteOptions{}) + require.NoError(t, err) + + err = testData.waitForResourceExistOrNot(TestNamespace, common.ResourceTypeStaticRoute, StaticRouteName, false) + require.NoError(t, err) +}