go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/l3plugin/vppcalls/vpp2101/proxyarp_vppcalls_test.go (about)

     1  //  Copyright (c) 2019 Cisco and/or its affiliates.
     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 vpp2101_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/onsi/gomega"
    21  	"go.ligato.io/cn-infra/v2/logging/logrus"
    22  
    23  	vpp_arp "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2101/arp"
    24  	"go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/ifaceidx"
    25  	"go.ligato.io/vpp-agent/v3/plugins/vpp/l3plugin/vppcalls"
    26  	vpp2101 "go.ligato.io/vpp-agent/v3/plugins/vpp/l3plugin/vppcalls/vpp2101"
    27  	"go.ligato.io/vpp-agent/v3/plugins/vpp/vppmock"
    28  )
    29  
    30  // Test enable/disable proxy arp
    31  func TestProxyArp(t *testing.T) {
    32  	ctx, ifIndexes, pArpHandler := pArpTestSetup(t)
    33  	defer ctx.TeardownTestCtx()
    34  
    35  	ifIndexes.Put("if1", &ifaceidx.IfaceMetadata{SwIfIndex: 1})
    36  
    37  	ctx.MockVpp.MockReply(&vpp_arp.ProxyArpIntfcEnableDisableReply{})
    38  	err := pArpHandler.EnableProxyArpInterface("if1")
    39  	Expect(err).To(Succeed())
    40  
    41  	ctx.MockVpp.MockReply(&vpp_arp.ProxyArpIntfcEnableDisableReply{})
    42  	err = pArpHandler.DisableProxyArpInterface("if1")
    43  	Expect(err).To(Succeed())
    44  
    45  	ctx.MockVpp.MockReply(&vpp_arp.ProxyArpIntfcEnableDisableReply{Retval: 1})
    46  	err = pArpHandler.DisableProxyArpInterface("if1")
    47  	Expect(err).NotTo(BeNil())
    48  }
    49  
    50  func TestProxyArpRange(t *testing.T) {
    51  	ctx, _, pArpHandler := pArpTestSetup(t)
    52  	defer ctx.TeardownTestCtx()
    53  
    54  	t.Run("Add: success case", func(t *testing.T) {
    55  		ctx.MockVpp.MockReply(&vpp_arp.ProxyArpAddDelReply{Retval: 0})
    56  
    57  		Expect(pArpHandler.AddProxyArpRange(
    58  			[]byte{192, 168, 10, 20}, []byte{192, 168, 10, 30}, 0,
    59  		)).To(Succeed())
    60  	})
    61  
    62  	testAddProxyARPRangeError := func(firstIP, lastIP []byte, vrf uint32) {
    63  		t.Helper()
    64  
    65  		ctx.MockVpp.MockReply(&vpp_arp.ProxyArpAddDelReply{Retval: 0})
    66  		Expect(pArpHandler.AddProxyArpRange(firstIP, lastIP, vrf)).ToNot(Succeed())
    67  
    68  		//Get mocked reply, since VPP call should not happen before.
    69  		Expect(
    70  			ctx.MockVPPClient.SendRequest(&vpp_arp.ProxyArpAddDel{}).ReceiveReply(&vpp_arp.ProxyArpAddDelReply{}),
    71  		).To(Succeed())
    72  	}
    73  	t.Run("Add: error cases", func(t *testing.T) {
    74  		// Bad first IP address.
    75  		testAddProxyARPRangeError([]byte{192, 168, 20}, []byte{192, 168, 10, 30}, 0)
    76  		// Bad last IP address.
    77  		testAddProxyARPRangeError([]byte{192, 168, 10, 20}, []byte{192, 168, 30}, 0)
    78  		// Bad both IP addresses.
    79  		testAddProxyARPRangeError([]byte{192, 168, 20}, []byte{192, 168, 30}, 0)
    80  	})
    81  
    82  	t.Run("Delete: success case", func(t *testing.T) {
    83  		ctx.MockVpp.MockReply(&vpp_arp.ProxyArpAddDelReply{Retval: 0})
    84  
    85  		Expect(pArpHandler.DeleteProxyArpRange(
    86  			[]byte{192, 168, 10, 20}, []byte{192, 168, 10, 30}, 0,
    87  		)).To(Succeed())
    88  	})
    89  
    90  	testDelProxyARPRangeError := func(firstIP, lastIP []byte, vrf uint32) {
    91  		t.Helper()
    92  
    93  		ctx.MockVpp.MockReply(&vpp_arp.ProxyArpAddDelReply{Retval: 0})
    94  		Expect(pArpHandler.DeleteProxyArpRange(firstIP, lastIP, vrf)).ToNot(Succeed())
    95  
    96  		//Get mocked reply, since VPP call should not happen before.
    97  		Expect(
    98  			ctx.MockVPPClient.SendRequest(&vpp_arp.ProxyArpAddDel{}).ReceiveReply(&vpp_arp.ProxyArpAddDelReply{}),
    99  		).To(Succeed())
   100  	}
   101  	t.Run("Delete: error cases", func(t *testing.T) {
   102  		// Bad first IP address.
   103  		testDelProxyARPRangeError([]byte{192, 168, 20}, []byte{192, 168, 10, 30}, 0)
   104  		// Bad last IP address.
   105  		testDelProxyARPRangeError([]byte{192, 168, 10, 20}, []byte{192, 168, 30}, 0)
   106  		// Bad both IP addresses.
   107  		testDelProxyARPRangeError([]byte{192, 168, 20}, []byte{192, 168, 30}, 0)
   108  	})
   109  
   110  	// Test retval in "add" scenario.
   111  	ctx.MockVpp.MockReply(&vpp_arp.ProxyArpAddDelReply{Retval: 1})
   112  	Expect(pArpHandler.AddProxyArpRange(
   113  		[]byte{192, 168, 10, 20}, []byte{192, 168, 10, 30}, 0,
   114  	)).ToNot(Succeed())
   115  
   116  	// Test retval in "delete" scenario.
   117  	ctx.MockVpp.MockReply(&vpp_arp.ProxyArpAddDelReply{Retval: 1})
   118  	Expect(pArpHandler.DeleteProxyArpRange(
   119  		[]byte{192, 168, 10, 20}, []byte{192, 168, 10, 30}, 0,
   120  	)).ToNot(Succeed())
   121  }
   122  
   123  func pArpTestSetup(t *testing.T) (*vppmock.TestCtx, ifaceidx.IfaceMetadataIndexRW, vppcalls.ProxyArpVppAPI) {
   124  	ctx := vppmock.SetupTestCtx(t)
   125  	log := logrus.NewLogger("test-log")
   126  	ifIndexes := ifaceidx.NewIfaceIndex(logrus.NewLogger("test"), "test")
   127  	pArpHandler := vpp2101.NewProxyArpVppHandler(ctx.MockChannel, ifIndexes, log)
   128  	return ctx, ifIndexes, pArpHandler
   129  }