Skip to content

Commit

Permalink
test: added integration tests for nat44 covering basic nat configurat…
Browse files Browse the repository at this point in the history
…ion and previously added twiceNAT pool IP feature

Signed-off-by: Filip Gschwandtner <filip.gschwandtner@pantheon.tech>
  • Loading branch information
fgschwan committed Sep 21, 2020
1 parent 5265141 commit 1fad79a
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
154 changes: 154 additions & 0 deletions tests/integration/vpp/150_nat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// Copyright (c) 2019 Pantheon.tech
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package vpp

import (
"net"
"testing"

. "github.com/onsi/gomega"
idxmap_mem "go.ligato.io/cn-infra/v2/idxmap/mem"
"go.ligato.io/cn-infra/v2/logging/logrus"
"go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/ifaceidx"
_ "go.ligato.io/vpp-agent/v3/plugins/vpp/natplugin"
nat_vppcalls "go.ligato.io/vpp-agent/v3/plugins/vpp/natplugin/vppcalls"
nat "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/nat"
"google.golang.org/protobuf/proto"
)

const (
vpp1908 = "19.08"
vpp2001 = "20.01"
vpp2005 = "20.05"
vpp2009 = "20.09"
)

// TestNat44StaticMapping tests Create/Read/Delete operations for NAT44 static mappings
func TestNat44StaticMapping(t *testing.T) {
ctx := setupVPP(t)
defer ctx.teardownVPP()

// nat handler
swIfIndexes := ifaceidx.NewIfaceIndex(logrus.DefaultLogger(), "test-sw_if_indexes")
dhcpIndexes := idxmap_mem.NewNamedMapping(logrus.DefaultLogger(), "test-dhcp_indexes", nil)
natHandler := nat_vppcalls.CompatibleNatVppHandler(ctx.vppClient, swIfIndexes, dhcpIndexes, logrus.NewLogger("test"))
Expect(natHandler).ShouldNot(BeNil(), "Handler should be created.")

// some test constants
const dnatLabel = "DNAT 1"
localIP := net.ParseIP("10.0.0.1").To4()
externalIP := net.ParseIP("10.0.0.2").To4()
startOfIPPool := net.ParseIP("10.0.0.10").To4()
endOfIPPool := net.ParseIP("10.0.0.11").To4()

// setup twice NAT pool
Expect(natHandler.AddNat44AddressPool(0, startOfIPPool.String(), endOfIPPool.String(), true)).Should(Succeed())

tests := []struct {
name string
input *nat.DNat44_StaticMapping
expectedDump *nat.DNat44_StaticMapping
excludeUnsupportedVPPVersions []string
}{
{
name: "simple NAT44 static mapping",
input: &nat.DNat44_StaticMapping{
Protocol: nat.DNat44_TCP,
ExternalIp: externalIP.String(),
LocalIps: []*nat.DNat44_StaticMapping_LocalIP{
{
LocalIp: localIP.String(),
},
},
},
},
{
name: "NAT44 static mapping with twice nat",
input: &nat.DNat44_StaticMapping{
Protocol: nat.DNat44_TCP,
ExternalIp: externalIP.String(),
ExternalPort: 80,
LocalIps: []*nat.DNat44_StaticMapping_LocalIP{
{
LocalIp: localIP.String(),
LocalPort: 8080,
},
},
TwiceNat: nat.DNat44_StaticMapping_ENABLED,
},
},
{
name: "NAT44 static mapping with twice nat and twice NAT pool IP",
excludeUnsupportedVPPVersions: []string{vpp1908, vpp2001, vpp2005},
input: &nat.DNat44_StaticMapping{
Protocol: nat.DNat44_TCP,
ExternalIp: externalIP.String(),
ExternalPort: 80,
LocalIps: []*nat.DNat44_StaticMapping_LocalIP{
{
LocalIp: localIP.String(),
LocalPort: 8080,
},
},
TwiceNat: nat.DNat44_StaticMapping_ENABLED,
TwiceNatPoolIp: endOfIPPool.String(),
},
expectedDump: &nat.DNat44_StaticMapping{
// just missing TwiceNatPoolIp (VPP doesnt dump it)
// TODO: fix test when dump will dump currently missing information
Protocol: nat.DNat44_TCP,
ExternalIp: externalIP.String(),
ExternalPort: 80,
LocalIps: []*nat.DNat44_StaticMapping_LocalIP{
{
LocalIp: localIP.String(),
LocalPort: 8080,
},
},
TwiceNat: nat.DNat44_StaticMapping_ENABLED,
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// exclude test testing feature not supported in currently tested VPP version
for _, excludedVPPVersion := range test.excludeUnsupportedVPPVersions {
if ctx.versionInfo.Release() == excludedVPPVersion {
return
}
}

// Create
Expect(test).ShouldNot(BeNil())
Expect(natHandler.AddNat44StaticMapping(test.input, dnatLabel)).Should(Succeed())

// Read
dnatDump, err := natHandler.DNat44Dump()
t.Logf("received this dnat from dump: %v", dnatDump)
Expect(err).ShouldNot(HaveOccurred())
expected := test.input
if test.expectedDump != nil {
expected = test.expectedDump
}
Expect(dnatDump).To(HaveLen(1))
Expect(dnatDump[0].StMappings).To(HaveLen(1))
Expect(proto.Equal(dnatDump[0].StMappings[0], expected)).To(BeTrue())

// Delete
Expect(natHandler.DelNat44StaticMapping(test.input, dnatLabel)).Should(Succeed())
})
}
}
3 changes: 3 additions & 0 deletions tests/integration/vpp/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ const (
}
plugins {
plugin dpdk_plugin.so { disable }
}
nat {
endpoint-dependent
}`
)

Expand Down

0 comments on commit 1fad79a

Please sign in to comment.