Skip to content

Commit

Permalink
Add staticroute e2e test
Browse files Browse the repository at this point in the history
Add staticroute e2e test
  • Loading branch information
TaoZou1 committed Jan 6, 2025
1 parent 57d792c commit 8705209
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 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 @@ -145,6 +145,7 @@ var (
ResourceTypeVirtualMachine = "VirtualMachine"
ResourceTypeLBService = "LBService"
ResourceTypeVpcAttachment = "VpcAttachment"
ResourceTypeStaticRoute = "StaticRoute"
ResourceTypeShare = "Share"
ResourceTypeSharedResource = "SharedResource"
ResourceTypeChildSharedResource = "ChildSharedResource"
Expand Down
8 changes: 4 additions & 4 deletions pkg/nsx/services/staticroute/staticroute.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,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 +173,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
81 changes: 81 additions & 0 deletions test/e2e/nsx_staticroute_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// This file is for e2e StaticRoute tests.

package e2e

import (
"context"
"fmt"
"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) {
deadlineCtx, deadlineCancel := context.WithTimeout(context.Background(), 2*defaultTimeout)
defer deadlineCancel()
err := wait.PollUntilContextTimeout(deadlineCtx, 1*time.Second, 2*defaultTimeout, false, func(ctx context.Context) (done bool, err error) {
res, err = testData.crdClientset.CrdV1alpha1().StaticRoutes(TestNamespace).Get(context.Background(), staticRouteName, v1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
return false, nil
}
log.Error(err, "Error fetching StaticRoute", "StaticRoute", res, "namespace", ns, "name", staticRouteName)
return false, fmt.Errorf("error when waiting for StaticRoute %s", staticRouteName)
}
log.V(1).Info("StaticRoute status", "status", res.Status)
for _, con := range res.Status.Conditions {
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},
}}
_, 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, staticRoute.Name, subnetTestNamespace)
err = testData.waitForResourceExistOrNot(TestNamespace, common.ResourceTypeStaticRoute, staticRoute.Name, true)
require.NoError(t, err)
}

func DeleteStaticRoute(t *testing.T) {
err := testData.crdClientset.CrdV1alpha1().Subnets(subnetTestNamespace).Delete(context.TODO(), StaticRouteName, v1.DeleteOptions{})
require.NoError(t, err)

waitForStaticRouteCRReady(t, StaticRouteName, subnetTestNamespace)
err = testData.waitForResourceExistOrNot(TestNamespace, common.ResourceTypeStaticRoute, StaticRouteName, false)
require.NoError(t, err)
}

0 comments on commit 8705209

Please sign in to comment.