From 1fad79add2075d249733c72e3a99ca57b906200e Mon Sep 17 00:00:00 2001 From: Filip Gschwandtner Date: Mon, 21 Sep 2020 20:11:50 +0200 Subject: [PATCH] test: added integration tests for nat44 covering basic nat configuration and previously added twiceNAT pool IP feature Signed-off-by: Filip Gschwandtner --- tests/integration/vpp/150_nat_test.go | 154 ++++++++++++++++++++++ tests/integration/vpp/integration_test.go | 3 + 2 files changed, 157 insertions(+) create mode 100644 tests/integration/vpp/150_nat_test.go diff --git a/tests/integration/vpp/150_nat_test.go b/tests/integration/vpp/150_nat_test.go new file mode 100644 index 0000000000..705b7a6e63 --- /dev/null +++ b/tests/integration/vpp/150_nat_test.go @@ -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()) + }) + } +} diff --git a/tests/integration/vpp/integration_test.go b/tests/integration/vpp/integration_test.go index a8542e6382..207ec01d30 100644 --- a/tests/integration/vpp/integration_test.go +++ b/tests/integration/vpp/integration_test.go @@ -65,6 +65,9 @@ const ( } plugins { plugin dpdk_plugin.so { disable } + } + nat { + endpoint-dependent }` )