go.ligato.io/vpp-agent/v3@v3.5.0/tests/integration/vpp/150_nat_test.go (about)

     1  // Copyright (c) 2021 Pantheon.tech
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at:
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package vpp
    16  
    17  import (
    18  	"net"
    19  	"testing"
    20  
    21  	. "github.com/onsi/gomega"
    22  	idxmap_mem "go.ligato.io/cn-infra/v2/idxmap/mem"
    23  	"go.ligato.io/cn-infra/v2/logging/logrus"
    24  	"go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/ifaceidx"
    25  	_ "go.ligato.io/vpp-agent/v3/plugins/vpp/natplugin"
    26  	nat_vppcalls "go.ligato.io/vpp-agent/v3/plugins/vpp/natplugin/vppcalls"
    27  	nat "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/nat"
    28  	"google.golang.org/protobuf/proto"
    29  )
    30  
    31  const (
    32  	vpp2005 = "20.05"
    33  	vpp2009 = "20.09"
    34  	vpp2101 = "21.01"
    35  	vpp2106 = "21.06"
    36  	vpp2202 = "22.02"
    37  )
    38  
    39  func TestNat44Global(t *testing.T) {
    40  	ctx := setupVPP(t)
    41  	defer ctx.teardownVPP()
    42  
    43  	unsupportedVPPVersions := []string{vpp2005, vpp2009}
    44  	// in older versions is used VPP startup config
    45  	// exclude test testing feature not supported in currently tested VPP version
    46  	for _, excludedVPPVersion := range unsupportedVPPVersions {
    47  		if ctx.versionInfo.Release() == excludedVPPVersion {
    48  			return
    49  		}
    50  	}
    51  
    52  	// nat handler
    53  	swIfIndexes := ifaceidx.NewIfaceIndex(logrus.DefaultLogger(), "test-sw_if_indexes")
    54  	dhcpIndexes := idxmap_mem.NewNamedMapping(logrus.DefaultLogger(), "test-dhcp_indexes", nil)
    55  	natHandler := nat_vppcalls.CompatibleNatVppHandler(ctx.vppClient, swIfIndexes, dhcpIndexes, logrus.NewLogger("test"))
    56  
    57  	Expect(natHandler).ShouldNot(BeNil(), "Nat handler should be created.")
    58  
    59  	dumpBeforeEnable, err := natHandler.Nat44GlobalConfigDump(false)
    60  	Expect(err).To(Succeed())
    61  	t.Logf("dump before enable: %#v", dumpBeforeEnable)
    62  
    63  	if !natHandler.WithLegacyStartupConf() {
    64  		Expect(natHandler.EnableNAT44Plugin(nat_vppcalls.Nat44InitOpts{EndpointDependent: true})).Should(Succeed())
    65  	}
    66  	dumpAfterEnable, err := natHandler.Nat44GlobalConfigDump(false)
    67  	Expect(err).To(Succeed())
    68  	t.Logf("dump after enable: %#v", dumpAfterEnable)
    69  
    70  	Expect(natHandler.DisableNAT44Plugin()).Should(Succeed())
    71  	dumpAfterDisable, err := natHandler.Nat44GlobalConfigDump(false)
    72  	Expect(err).To(Succeed())
    73  	Expect(dumpAfterDisable).To(Equal(natHandler.DefaultNat44GlobalConfig()))
    74  	t.Logf("dump after disable: %#v", dumpAfterDisable)
    75  
    76  	if !natHandler.WithLegacyStartupConf() {
    77  		Expect(natHandler.EnableNAT44Plugin(nat_vppcalls.Nat44InitOpts{EndpointDependent: true})).Should(Succeed())
    78  	}
    79  	dumpAfterSecondEnable, err := natHandler.Nat44GlobalConfigDump(false)
    80  	Expect(err).To(Succeed())
    81  	t.Logf("dump after second enable: %#v", dumpAfterSecondEnable)
    82  }
    83  
    84  // TestNat44StaticMapping tests Create/Read/Delete operations for NAT44 static mappings
    85  func TestNat44StaticMapping(t *testing.T) {
    86  	ctx := setupVPP(t)
    87  	defer ctx.teardownVPP()
    88  
    89  	// nat handler
    90  	swIfIndexes := ifaceidx.NewIfaceIndex(logrus.DefaultLogger(), "test-sw_if_indexes")
    91  	dhcpIndexes := idxmap_mem.NewNamedMapping(logrus.DefaultLogger(), "test-dhcp_indexes", nil)
    92  	natHandler := nat_vppcalls.CompatibleNatVppHandler(ctx.vppClient, swIfIndexes, dhcpIndexes, logrus.NewLogger("test"))
    93  	Expect(natHandler).ShouldNot(BeNil(), "Handler should be created.")
    94  
    95  	// some test constants
    96  	const dnatLabel = "DNAT 1"
    97  	localIP := net.ParseIP("10.0.0.1").To4()
    98  	externalIP := net.ParseIP("10.0.0.2").To4()
    99  	startOfIPPool := net.ParseIP("10.0.0.10").To4()
   100  	endOfIPPool := net.ParseIP("10.0.0.11").To4()
   101  
   102  	// setup twice NAT pool
   103  	if !natHandler.WithLegacyStartupConf() {
   104  		Expect(natHandler.EnableNAT44Plugin(nat_vppcalls.Nat44InitOpts{EndpointDependent: true})).Should(Succeed())
   105  	}
   106  	Expect(natHandler.AddNat44AddressPool(0, startOfIPPool.String(), endOfIPPool.String(), true)).Should(Succeed())
   107  
   108  	tests := []struct {
   109  		name                          string
   110  		input                         *nat.DNat44_StaticMapping
   111  		expectedDump                  *nat.DNat44_StaticMapping
   112  		excludeUnsupportedVPPVersions []string
   113  	}{
   114  		{
   115  			name: "simple NAT44 static mapping",
   116  			input: &nat.DNat44_StaticMapping{
   117  				Protocol:   nat.DNat44_TCP,
   118  				ExternalIp: externalIP.String(),
   119  				LocalIps: []*nat.DNat44_StaticMapping_LocalIP{
   120  					{
   121  						LocalIp: localIP.String(),
   122  					},
   123  				},
   124  			},
   125  		},
   126  		{
   127  			name: "NAT44 static mapping with twice nat",
   128  			input: &nat.DNat44_StaticMapping{
   129  				Protocol:     nat.DNat44_TCP,
   130  				ExternalIp:   externalIP.String(),
   131  				ExternalPort: 80,
   132  				LocalIps: []*nat.DNat44_StaticMapping_LocalIP{
   133  					{
   134  						LocalIp:   localIP.String(),
   135  						LocalPort: 8080,
   136  					},
   137  				},
   138  				TwiceNat: nat.DNat44_StaticMapping_ENABLED,
   139  			},
   140  		},
   141  		{
   142  			name:                          "NAT44 static mapping with twice nat and twice NAT pool IP",
   143  			excludeUnsupportedVPPVersions: []string{vpp2005},
   144  			input: &nat.DNat44_StaticMapping{
   145  				Protocol:     nat.DNat44_TCP,
   146  				ExternalIp:   externalIP.String(),
   147  				ExternalPort: 80,
   148  				LocalIps: []*nat.DNat44_StaticMapping_LocalIP{
   149  					{
   150  						LocalIp:   localIP.String(),
   151  						LocalPort: 8080,
   152  					},
   153  				},
   154  				TwiceNat:       nat.DNat44_StaticMapping_ENABLED,
   155  				TwiceNatPoolIp: endOfIPPool.String(),
   156  			},
   157  			expectedDump: &nat.DNat44_StaticMapping{
   158  				// just missing TwiceNatPoolIp (VPP doesnt dump it)
   159  				// TODO: fix test when dump will dump currently missing information
   160  				Protocol:     nat.DNat44_TCP,
   161  				ExternalIp:   externalIP.String(),
   162  				ExternalPort: 80,
   163  				LocalIps: []*nat.DNat44_StaticMapping_LocalIP{
   164  					{
   165  						LocalIp:   localIP.String(),
   166  						LocalPort: 8080,
   167  					},
   168  				},
   169  				TwiceNat: nat.DNat44_StaticMapping_ENABLED,
   170  			},
   171  		},
   172  	}
   173  
   174  	for _, test := range tests {
   175  		t.Run(test.name, func(t *testing.T) {
   176  			// exclude test testing feature not supported in currently tested VPP version
   177  			for _, excludedVPPVersion := range test.excludeUnsupportedVPPVersions {
   178  				if ctx.versionInfo.Release() == excludedVPPVersion {
   179  					return
   180  				}
   181  			}
   182  
   183  			// Create
   184  			Expect(test).ShouldNot(BeNil())
   185  			Expect(natHandler.AddNat44StaticMapping(test.input, dnatLabel)).Should(Succeed())
   186  
   187  			// Read
   188  			dnatDump, err := natHandler.DNat44Dump()
   189  			t.Logf("received this dnat from dump: %v", dnatDump)
   190  			Expect(err).ShouldNot(HaveOccurred())
   191  			expected := test.input
   192  			if test.expectedDump != nil {
   193  				expected = test.expectedDump
   194  			}
   195  			Expect(dnatDump).To(HaveLen(1))
   196  			Expect(dnatDump[0].StMappings).To(HaveLen(1))
   197  			Expect(proto.Equal(dnatDump[0].StMappings[0], expected)).To(BeTrue())
   198  
   199  			// Delete
   200  			Expect(natHandler.DelNat44StaticMapping(test.input, dnatLabel)).Should(Succeed())
   201  		})
   202  	}
   203  }