Skip to content

Commit

Permalink
E2E: Add subports to trunked port to test subports deletion
Browse files Browse the repository at this point in the history
Signed-off-by: Huy Mai <huy.mai@est.tech>
  • Loading branch information
mquhuy committed Jul 19, 2024
1 parent 82d7acf commit 941da20
Showing 1 changed file with 83 additions and 8 deletions.
91 changes: 83 additions & 8 deletions test/e2e/suites/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"strings"
"time"

"github.com/gophercloud/gophercloud/v2"
"github.com/gophercloud/gophercloud/v2/openstack"
"github.com/gophercloud/gophercloud/v2/openstack/blockstorage/v3/volumes"
"github.com/gophercloud/gophercloud/v2/openstack/compute/v2/servers"
"github.com/gophercloud/gophercloud/v2/openstack/networking/v2/extensions/layer3/routers"
Expand All @@ -39,9 +41,11 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gstruct"
"go.uber.org/mock/gomock"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
apimachinerytypes "k8s.io/apimachinery/pkg/types"
utilrand "k8s.io/apimachinery/pkg/util/rand"
"k8s.io/apimachinery/pkg/util/sets"
Expand All @@ -54,6 +58,8 @@ import (
crclient "sigs.k8s.io/controller-runtime/pkg/client"

infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-openstack/pkg/cloud/services/networking"
"sigs.k8s.io/cluster-api-provider-openstack/pkg/scope"
"sigs.k8s.io/cluster-api-provider-openstack/test/e2e/shared"
)

Expand All @@ -67,6 +73,8 @@ var _ = Describe("e2e tests [PR-Blocking]", func() {

// Cleanup functions which cannot run until after the cluster has been deleted
postClusterCleanup []func(context.Context)
mockCtrl *gomock.Controller
mockScopeFactory *scope.MockScopeFactory
)

BeforeEach(func() {
Expand All @@ -78,6 +86,8 @@ var _ = Describe("e2e tests [PR-Blocking]", func() {
Expect(e2eCtx.E2EConfig).ToNot(BeNil(), "Invalid argument. e2eConfig can't be nil when calling %s spec", specName)
Expect(e2eCtx.E2EConfig.Variables).To(HaveKey(shared.KubernetesVersion))
postClusterCleanup = nil
mockCtrl = gomock.NewController(GinkgoT())
mockScopeFactory = scope.NewMockScopeFactory(mockCtrl, "")
})

Describe("Workload cluster (default)", func() {
Expand Down Expand Up @@ -445,30 +455,95 @@ var _ = Describe("e2e tests [PR-Blocking]", func() {
return len(plist)
}, e2eCtx.E2EConfig.GetIntervals(specName, "wait-worker-nodes")...).Should(Equal(1))

port := plist[0]
Expect(port.Description).To(Equal("primary"))
Expect(port.Tags).To(ContainElement(testTag))
primaryPort := plist[0]
Expect(primaryPort.Description).To(Equal("primary"))
Expect(primaryPort.Tags).To(ContainElement(testTag))

// assert trunked port is created.
Eventually(func() int {
plist, err = shared.DumpOpenStackPorts(e2eCtx, ports.ListOpts{Description: "trunked", Tags: testTag})
Expect(err).To(BeNil())
return len(plist)
}, e2eCtx.E2EConfig.GetIntervals(specName, "wait-worker-nodes")...).Should(Equal(1))
port = plist[0]
Expect(port.Description).To(Equal("trunked"))
Expect(port.Tags).To(ContainElement(testTag))
trunkedPort := plist[0]
Expect(trunkedPort.Description).To(Equal("trunked"))
Expect(trunkedPort.Tags).To(ContainElement(testTag))

// assert trunk data.
var trunk *trunks.Trunk
Eventually(func() int {
trunk, err = shared.DumpOpenStackTrunks(e2eCtx, port.ID)
trunk, err = shared.DumpOpenStackTrunks(e2eCtx, trunkedPort.ID)
Expect(err).To(BeNil())
Expect(trunk).NotTo(BeNil())
return 1
}, e2eCtx.E2EConfig.GetIntervals(specName, "wait-worker-nodes")...).Should(Equal(1))
Expect(trunk.PortID).To(Equal(port.ID))
Expect(trunk.PortID).To(Equal(trunkedPort.ID))

// Testing subports
shared.Logf("Create a new port and add a subport to it")

providerClient, clientOpts, _, err := shared.GetTenantProviderClient(e2eCtx)
Expect(err).To(BeNil(), "Cannot create providerClient")

networkClient, err := openstack.NewNetworkV2(providerClient, gophercloud.EndpointOpts{
Region: clientOpts.RegionName,
})
Expect(err).To(BeNil(), "Cannot create network client")

networksList, err := shared.DumpOpenStackNetworks(e2eCtx, networks.ListOpts{})
Expect(err).To(BeNil(), "Cannot get network List")
createOpts := ports.CreateOpts{
NetworkID: networksList[0].ID,
}

subPort, err := ports.Create(ctx, networkClient, createOpts).Extract()
Expect(err).To(BeNil(), "Cannot create subPort")

addSubportsOpts := trunks.AddSubportsOpts{
Subports: []trunks.Subport{
{
SegmentationID: 1,
SegmentationType: "vlan",
PortID: subPort.ID,
},
},
}
_, err = trunks.AddSubports(ctx, networkClient, trunk.ID, addSubportsOpts).Extract()
Expect(err).To(BeNil(), "Cannot add subports")

subports, err := trunks.GetSubports(ctx, networkClient, trunk.ID).Extract()
Expect(err).To(BeNil())
Expect(subports).To(HaveLen(1))

shared.Logf("Deleting the trunk")
log := GinkgoLogr
k8sClient := e2eCtx.Environment.BootstrapClusterProxy.GetClient()

clientScope, err := mockScopeFactory.NewClientScopeFromObject(ctx, k8sClient, nil, log, nil)
Expect(err).To(BeNil())
scope := scope.NewWithLogger(clientScope, log)
networkService, err := networking.NewService(scope)
Expect(err).To(BeNil())

var obj runtime.Object = &corev1.Pod{}
trunksList := []trunks.Trunk{
*trunk,
}
defer GinkgoRecover()
mockScopeFactory.NetworkClient.EXPECT().ListTrunk(gomock.Any()).Return(trunksList, nil)
mockScopeFactory.NetworkClient.EXPECT().ListTrunkSubports(gomock.Any()).Return(subports, nil)
mockScopeFactory.NetworkClient.EXPECT().RemoveSubports(gomock.Any(), gomock.Any()).Return(nil)
mockScopeFactory.NetworkClient.EXPECT().DeletePort(gomock.Any()).Return(nil)
mockScopeFactory.NetworkClient.EXPECT().DeleteTrunk(gomock.Any()).Return(nil)
err = networkService.DeleteTrunk(obj, trunkedPort.ID)
Expect(err).To(BeNil(), "Failed to remove trunk subports")

// Verify that subPort is deleted
_, err = ports.Get(ctx, networkClient, subPort.ID).Extract()
Expect(err).To(BeNil())

// assert port level security group is created by name using SecurityGroupFilters

securityGroupsList, err := shared.DumpOpenStackSecurityGroups(e2eCtx, groups.ListOpts{Name: testSecurityGroupName})
Expect(err).NotTo(HaveOccurred())
Expect(securityGroupsList).To(HaveLen(1))
Expand Down

0 comments on commit 941da20

Please sign in to comment.