Skip to content

Commit

Permalink
Merge pull request #990 from TaoZou1/cliente2e
Browse files Browse the repository at this point in the history
Add staticroute e2e test
  • Loading branch information
TaoZou1 authored Jan 14, 2025
2 parents acc0d70 + 1752630 commit 309c616
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 8 deletions.
1 change: 1 addition & 0 deletions pkg/nsx/services/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ var (
ResourceTypeVirtualMachine = "VirtualMachine"
ResourceTypeLBService = "LBService"
ResourceTypeVpcAttachment = "VpcAttachment"
ResourceTypeStaticRoute = "StaticRoutes"
ResourceTypeShare = "Share"
ResourceTypeSharedResource = "SharedResource"
ResourceTypeChildSharedResource = "ChildSharedResource"
Expand Down
15 changes: 7 additions & 8 deletions pkg/nsx/services/staticroute/staticroute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
}
Expand Down
78 changes: 78 additions & 0 deletions test/e2e/nsx_staticroute_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 309c616

Please sign in to comment.