Skip to content

Commit

Permalink
Add vppinit af_packet test
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Glazychev <artem.glazychev@xored.com>
  • Loading branch information
glazychev-art committed Nov 28, 2023
1 parent 0eda46f commit 89ba40b
Show file tree
Hide file tree
Showing 7 changed files with 282 additions and 17 deletions.
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ issues:
linters:
- funlen
text: "Function 'TestCombinations'"
- path: internal/tests/vppinit_test.go
linters:
- funlen
text: "Function 'Test_VppInit_AfPacket'"
- path: internal/tests/suite_setup_test.go
linters:
- funlen
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/networkservicemesh/govpp v0.0.0-20231108193829-2484aa7cd5d9
github.com/networkservicemesh/sdk v0.5.1-0.20231124092826-4d02ed414eed
github.com/networkservicemesh/sdk-k8s v0.0.0-20231123112133-3a307c41ee71
github.com/networkservicemesh/sdk-kernel v0.0.0-20231124093522-404b33c4a701
github.com/networkservicemesh/sdk-sriov v0.0.0-20231124094207-86a8285ad1ef
github.com/networkservicemesh/sdk-vpp v0.0.0-20231124094126-ac31c7ae5a1d
github.com/networkservicemesh/vpphelper v0.0.0-20230901145133-a14aecebd1cb
Expand Down Expand Up @@ -58,7 +59,6 @@ require (
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/lunixbochs/struc v0.0.0-20200521075829-a4cb8d33dbbe // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/networkservicemesh/sdk-kernel v0.0.0-20231124093522-404b33c4a701 // indirect
github.com/open-policy-agent/opa v0.44.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.17.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions internal/imports/imports_linux.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions internal/tests/suite_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,15 @@ func (f *ForwarderTestSuite) SetupSuite() {
// ********************************************************************************
log.FromContext(f.ctx).Infof("Creating test bridge (time since start: %s)", time.Since(starttime))
// ********************************************************************************
f.Require().NoError(SetupBridge())
bridgeCancel, err := SetupBridge()
f.Require().NoError(err)
f.bridgeCancel = bridgeCancel

// ********************************************************************************
log.FromContext(f.ctx).Infof("Creating test vpp Server (time since start: %s)", time.Since(starttime))
// ********************************************************************************
f.vppServerConn, f.vppServerRoot, f.vppServerErrCh = f.createVpp(f.ctx, "vpp-server")
_, err := vppinit.LinkToSocket(f.ctx, f.vppServerConn, net.ParseIP(serverIP), vppinit.AfPacket)
_, err = vppinit.LinkToSocket(f.ctx, f.vppServerConn, net.ParseIP(serverIP), vppinit.AfPacket)
f.Require().NoError(err)

// ********************************************************************************
Expand Down Expand Up @@ -117,8 +119,7 @@ func (f *ForwarderTestSuite) SetupSuite() {
// ********************************************************************************
log.FromContext(f.ctx).Infof("Running system under test (SUT) (time since start: %s)", time.Since(starttime))
// ********************************************************************************
cmdStr := "forwarder"
f.sutErrCh = exechelper.Start(cmdStr,
f.sutErrCh = exechelper.Start(forwarderName,
exechelper.WithContext(f.ctx),
exechelper.WithEnvirons(append(os.Environ(), "NSM_REGISTRY_CLIENT_POLICIES=\"\"")...),
exechelper.WithStdout(os.Stdout),
Expand Down Expand Up @@ -221,6 +222,7 @@ func (f *ForwarderTestSuite) createVpp(ctx context.Context, name string) (vppCon

func (f *ForwarderTestSuite) TearDownSuite() {
f.cancel()
f.bridgeCancel()
for {
_, ok := <-f.sutErrCh
if !ok {
Expand Down
2 changes: 2 additions & 0 deletions internal/tests/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type ForwarderTestSuite struct {
cancel context.CancelFunc
config config.Config

bridgeCancel func()

sutErrCh <-chan error
sutCC grpc.ClientConnInterface

Expand Down
34 changes: 22 additions & 12 deletions internal/tests/suite_util_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020-2022 Cisco and/or its affiliates.
// Copyright (c) 2020-2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -36,6 +36,8 @@ const (
forwarderIP = "10.0.2.1"
clientIP = "10.0.2.2"
serverIP = "10.0.2.3"

forwarderName = "forwarder"
)

func (f *ForwarderTestSuite) ListenAndServe(ctx context.Context, listenOn *url.URL, server *grpc.Server) <-chan error {
Expand All @@ -58,23 +60,25 @@ func (f *ForwarderTestSuite) ListenAndServe(ctx context.Context, listenOn *url.U
return returnErrCh
}

func SetupBridge() error {
func SetupBridge() (cancelFn func(), err error) {
la := netlink.NewLinkAttrs()
la.Name = "bridge"
bridge := &netlink.Bridge{LinkAttrs: la}
err := netlink.LinkAdd(bridge)
err = netlink.LinkAdd(bridge)
if err != nil {
return errors.Wrapf(err, "could not add %s: %v", la.Name, err)
return nil, errors.Wrapf(err, "could not add %s: %v", la.Name, err)
}
if err := netlink.LinkSetUp(bridge); err != nil {
return errors.Wrapf(err, "failure creating bridge")
return nil, errors.Wrapf(err, "failure creating bridge")
}

ifaceMap := map[string]*net.IPNet{
"fowarder": {IP: net.ParseIP(forwarderIP), Mask: net.CIDRMask(24, 32)},
"client": {IP: net.ParseIP(clientIP), Mask: net.CIDRMask(24, 32)},
"server": {IP: net.ParseIP(serverIP), Mask: net.CIDRMask(24, 32)},
}

var linkList []netlink.Link
for ifaceName, netIP := range ifaceMap {
la := netlink.NewLinkAttrs()
la.Name = ifaceName
Expand All @@ -83,25 +87,31 @@ func SetupBridge() error {
PeerName: la.Name + "-veth",
}
if err := netlink.LinkAdd(l); err != nil {
return errors.Wrapf(err, "unable to create link %s", l.PeerName)
return nil, errors.Wrapf(err, "unable to create link %s", l.PeerName)
}
peer, err := netlink.LinkByName(l.PeerName)
if err != nil {
return errors.Wrapf(err, "unable to get link %s", l.PeerName)
return nil, errors.Wrapf(err, "unable to get link %s", l.PeerName)
}
if err := netlink.LinkSetUp(l); err != nil {
return errors.Wrapf(err, "unable to up link %s", l.Attrs().Name)
return nil, errors.Wrapf(err, "unable to up link %s", l.Attrs().Name)
}
if err := netlink.LinkSetUp(peer); err != nil {
return errors.Wrapf(err, "unable to up link %s", peer.Attrs().Name)
return nil, errors.Wrapf(err, "unable to up link %s", peer.Attrs().Name)
}
if err := netlink.AddrAdd(l, &netlink.Addr{IPNet: netIP}); err != nil {
return errors.Wrapf(err, "unable to add address %s to link %s", netIP, l.Attrs().Name)
return nil, errors.Wrapf(err, "unable to add address %s to link %s", netIP, l.Attrs().Name)
}

if err := netlink.LinkSetMaster(peer, bridge); err != nil {
return errors.Wrapf(err, "unable to add link %s to bridge %s", peer.Attrs().Name, bridge.LinkAttrs.Name)
return nil, errors.Wrapf(err, "unable to add link %s to bridge %s", peer.Attrs().Name, bridge.LinkAttrs.Name)
}
linkList = append(linkList, l)
}
return nil
return func() {
for _, l := range linkList {
_ = netlink.LinkDel(l)
}
_ = netlink.LinkDel(bridge)
}, nil
}
Loading

0 comments on commit 89ba40b

Please sign in to comment.