Skip to content

Unit test for NSMNSE-37: removeClientInterface() function #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pkg/universal-cnf/config/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package config
import (
"context"
"fmt"
"net"

"github.com/cisco-app-networking/nsm-nse/pkg/nseconfig"
"github.com/golang/protobuf/ptypes/empty"
"github.com/networkservicemesh/networkservicemesh/controlplane/api/connection"
Expand All @@ -27,7 +29,6 @@ import (
"github.com/sirupsen/logrus"
"go.ligato.io/vpp-agent/v3/proto/ligato/vpp"
l3 "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/l3"
"net"
)

// UniversalCNFEndpoint is a Universal CNF Endpoint composite implementation
Expand Down
96 changes: 96 additions & 0 deletions pkg/universal-cnf/config/mocks/UniversalCNFBackend.go

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

252 changes: 252 additions & 0 deletions pkg/universal-cnf/config/removeClientInterface_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
package config

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"go.ligato.io/vpp-agent/v3/proto/ligato/vpp"
interfaces "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/interfaces"
l3 "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/l3"

"github.com/cisco-app-networking/nsm-nse/pkg/universal-cnf/config/mocks"
"github.com/networkservicemesh/networkservicemesh/controlplane/api/connection"
"github.com/networkservicemesh/networkservicemesh/controlplane/api/connectioncontext"
)

var (
interfaces1 = []*interfaces.Interface{
{
IpAddresses: []string{"192.168.22.2/30"},
},
}

interfaces2 = []*interfaces.Interface{
{
IpAddresses: []string{"192.168.22.1/30", "192.168.22.0/30"},
},
}

interfaces3 = []*interfaces.Interface{
{
IpAddresses: []string{"192.168.22.2/30", "192.168.22.1/30", "192.168.22.0/30"},
},
}

routes1 = []*l3.Route{
{
NextHopAddr: "192.168.22.1",
DstNetwork: "192.168.0.0/16",
},
}

routes2 = []*l3.Route{
{
NextHopAddr: "192.168.22.2",
DstNetwork: "192.168.0.0/16",
},
}

conn1 = &connection.Connection{
Id: "2",
NetworkService: "ucnf",
Mechanism: &connection.Mechanism{
Cls: "LOCAL",
Type: "MEMIF",
Parameters: map[string]string{
"description": "NSM Endpoint",
"name": "nsm2DoJzgpRd",
"netnsInode": "4026534215",
"socketfile": "nsm2DoJzgpRd/memif.sock",
},
},
Context: &connectioncontext.ConnectionContext{
IpContext: &connectioncontext.IPContext{
SrcIpAddr: "192.168.22.1/30",
DstIpAddr: "192.168.22.2/30",
SrcIpRequired: true,
DstIpRequired: true,
DstRoutes: []*connectioncontext.Route{
{Prefix: "192.168.0.0/16"},
},
},
},
Labels: map[string]string{
"namespace": "nsm-system",
"podName": "helloworld-ucnf-6454c88c5f-pw98x",
},
Path: &connection.Path{
PathSegments: []*connection.PathSegment{
{
Name: "kind-2-control-plane",
},
},
},
}

//Changed DstIpAddr
conn2 = &connection.Connection{
Id: "2",
NetworkService: "ucnf",
Mechanism: &connection.Mechanism{
Cls: "LOCAL",
Type: "MEMIF",
Parameters: map[string]string{
"description": "NSM Endpoint",
"name": "nsm2DoJzgpRd",
"netnsInode": "4026534215",
"socketfile": "nsm2DoJzgpRd/memif.sock",
},
},
Context: &connectioncontext.ConnectionContext{
IpContext: &connectioncontext.IPContext{
SrcIpAddr: "192.168.22.1/30",
DstIpAddr: "192.168.22.3/30",
SrcIpRequired: true,
DstIpRequired: true,
DstRoutes: []*connectioncontext.Route{
{Prefix: "192.168.0.0/16"},
},
},
},
Labels: map[string]string{
"namespace": "nsm-system",
"podName": "helloworld-ucnf-6454c88c5f-pw98x",
},
Path: &connection.Path{
PathSegments: []*connection.PathSegment{
{
Name: "kind-2-control-plane",
},
},
},
}

//With SrcRoutes
conn3 = &connection.Connection{
Id: "2",
NetworkService: "ucnf",
Mechanism: &connection.Mechanism{
Cls: "LOCAL",
Type: "MEMIF",
Parameters: map[string]string{
"description": "NSM Endpoint",
"name": "nsm2DoJzgpRd",
"netnsInode": "4026534215",
"socketfile": "nsm2DoJzgpRd/memif.sock",
},
},
Context: &connectioncontext.ConnectionContext{
IpContext: &connectioncontext.IPContext{
SrcIpAddr: "192.168.22.1/30",
DstIpAddr: "192.168.22.2/30",
SrcIpRequired: true,
DstIpRequired: true,
DstRoutes: []*connectioncontext.Route{
{Prefix: "192.168.0.0/16"},
},
SrcRoutes: []*connectioncontext.Route{
{Prefix: "192.168.0.0/16"},
},
},
},
Labels: map[string]string{
"namespace": "nsm-system",
"podName": "helloworld-ucnf-6454c88c5f-pw98x",
},
Path: &connection.Path{
PathSegments: []*connection.PathSegment{
{
Name: "kind-2-control-plane",
},
},
},
}
)

func TestRemoveClientInterface(t *testing.T) {
for testName, c := range map[string]struct {
interfaces []*interfaces.Interface
conn *connection.Connection
routes []*l3.Route
expectedRemoveRoutes []*l3.Route
expectedError error
}{
"Remove from nil interface": {
nil,
conn1,
routes1,
nil,
fmt.Errorf("client interface with dstIpAddr %s not found", conn1.Context.IpContext.DstIpAddr),
},
"Remove from empty interface": {
[]*interfaces.Interface{},
conn1,
routes1,
nil,
fmt.Errorf("client interface with dstIpAddr %s not found", conn1.Context.IpContext.DstIpAddr),
},
"Remove from one interface": {
interfaces1,
conn1,
routes1,
nil,
nil,
},
"Remove from multiple interfaces": {
interfaces3,
conn1,
routes1,
nil,
nil,
},
"Unmatched NextHopAddr": {
interfaces1,
conn1,
routes2,
nil,
nil,
},
"Route prefix matches DstNetwork": {
interfaces1,
conn3,
routes1,
routes1,
nil,
},
"Unmatched connection dstIpAddr": {
interfaces1,
conn2,
routes1,
nil,
fmt.Errorf("client interface with dstIpAddr %s not found", conn1.Context.IpContext.DstIpAddr),
},
"Unfound dstIpAddr in interfaces": {
interfaces2,
conn1,
routes1,
nil,
fmt.Errorf("client interface with dstIpAddr %s not found", conn1.Context.IpContext.DstIpAddr),
},
} {
t.Logf("Running test case: %s", testName)

ucnfBackend := &mocks.UniversalCNFBackend{}
uce := &UniversalCNFEndpoint{
backend: ucnfBackend,
dpConfig: &vpp.ConfigData{Interfaces: c.interfaces, Routes: c.routes},
}

removeConfig, err := uce.removeClientInterface(c.conn)
if c.expectedError != nil {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
assert.NotNil(t, removeConfig)
//check removed interface
assert.Equal(t, 1, len(removeConfig.Interfaces))
//check removed route
assert.Equal(t, removeConfig.Routes, c.expectedRemoveRoutes)
}
}
}