go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/l2plugin/vppcalls/vpp2202/l2fib_vppcalls_test.go (about)

     1  //  Copyright (c) 2022 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 vpp2202_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/onsi/gomega"
    21  	"go.ligato.io/cn-infra/v2/logging/logrus"
    22  
    23  	"go.ligato.io/vpp-agent/v3/pkg/idxvpp"
    24  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2202/ethernet_types"
    25  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2202/interface_types"
    26  	vpp_l2 "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2202/l2"
    27  	"go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/ifaceidx"
    28  	"go.ligato.io/vpp-agent/v3/plugins/vpp/l2plugin/vppcalls"
    29  	vpp2202 "go.ligato.io/vpp-agent/v3/plugins/vpp/l2plugin/vppcalls/vpp2202"
    30  	"go.ligato.io/vpp-agent/v3/plugins/vpp/vppmock"
    31  
    32  	l2 "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/l2"
    33  )
    34  
    35  var testDataInFib = []*l2.FIBEntry{
    36  	{PhysAddress: "FF:FF:FF:FF:FF:FF", BridgeDomain: "bd1", OutgoingInterface: "if1", Action: l2.FIBEntry_FORWARD, StaticConfig: true, BridgedVirtualInterface: true},
    37  	{PhysAddress: "AA:AA:AA:AA:AA:AA", BridgeDomain: "bd1", OutgoingInterface: "if1", Action: l2.FIBEntry_FORWARD, StaticConfig: true},
    38  	{PhysAddress: "BB:BB:BB:BB:BB:BB", BridgeDomain: "bd1", Action: l2.FIBEntry_DROP},
    39  	{PhysAddress: "CC:CC:CC:CC:CC:CC", BridgeDomain: "bd1", OutgoingInterface: "if1", Action: l2.FIBEntry_FORWARD},
    40  }
    41  
    42  var testDatasOutFib = []*vpp_l2.L2fibAddDel{
    43  	{BdID: 5, SwIfIndex: 55, BviMac: true, Mac: ethernet_types.MacAddress{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, StaticMac: true, FilterMac: false},
    44  	{BdID: 5, SwIfIndex: 55, BviMac: false, Mac: ethernet_types.MacAddress{0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}, StaticMac: true, FilterMac: false},
    45  	{BdID: 5, SwIfIndex: ^interface_types.InterfaceIndex(0), BviMac: false, Mac: ethernet_types.MacAddress{0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB}, StaticMac: false, FilterMac: true},
    46  	{BdID: 5, SwIfIndex: 55, BviMac: false, Mac: ethernet_types.MacAddress{0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC}, StaticMac: false, FilterMac: false},
    47  }
    48  
    49  func TestL2FibAdd(t *testing.T) {
    50  	ctx, fibHandler, ifaceIdx, bdIndexes := fibTestSetup(t)
    51  	defer ctx.TeardownTestCtx()
    52  
    53  	ifaceIdx.Put("if1", &ifaceidx.IfaceMetadata{SwIfIndex: 55})
    54  	bdIndexes.Put("bd1", &idxvpp.OnlyIndex{Index: 5})
    55  
    56  	for i := 0; i < len(testDataInFib); i++ {
    57  		ctx.MockVpp.MockReply(&vpp_l2.L2fibAddDelReply{})
    58  		err := fibHandler.AddL2FIB(testDataInFib[i])
    59  		Expect(err).ShouldNot(HaveOccurred())
    60  		testDatasOutFib[i].IsAdd = true
    61  		Expect(ctx.MockChannel.Msg).To(Equal(testDatasOutFib[i]))
    62  	}
    63  }
    64  
    65  func TestL2FibAddError(t *testing.T) {
    66  	ctx, fibHandler, ifaceIdx, bdIndexes := fibTestSetup(t)
    67  	defer ctx.TeardownTestCtx()
    68  
    69  	ifaceIdx.Put("if1", &ifaceidx.IfaceMetadata{SwIfIndex: 55})
    70  	bdIndexes.Put("bd1", &idxvpp.OnlyIndex{Index: 5})
    71  
    72  	err := fibHandler.AddL2FIB(&l2.FIBEntry{PhysAddress: "not:mac:addr", BridgeDomain: "bd1", OutgoingInterface: "if1"})
    73  	Expect(err).Should(HaveOccurred())
    74  
    75  	ctx.MockVpp.MockReply(&vpp_l2.L2fibAddDelReply{Retval: 1})
    76  	err = fibHandler.AddL2FIB(testDataInFib[0])
    77  	Expect(err).Should(HaveOccurred())
    78  
    79  	ctx.MockVpp.MockReply(&vpp_l2.BridgeDomainAddDelReply{})
    80  	err = fibHandler.AddL2FIB(testDataInFib[0])
    81  	Expect(err).Should(HaveOccurred())
    82  
    83  	err = fibHandler.AddL2FIB(&l2.FIBEntry{PhysAddress: "CC:CC:CC:CC:CC:CC", BridgeDomain: "non-existing-bd", OutgoingInterface: "if1"})
    84  	Expect(err).Should(HaveOccurred())
    85  
    86  	err = fibHandler.AddL2FIB(&l2.FIBEntry{PhysAddress: "CC:CC:CC:CC:CC:CC", BridgeDomain: "bd1", OutgoingInterface: "non-existing-iface"})
    87  	Expect(err).Should(HaveOccurred())
    88  }
    89  
    90  func TestL2FibDelete(t *testing.T) {
    91  	ctx, fibHandler, ifaceIdx, bdIndexes := fibTestSetup(t)
    92  	defer ctx.TeardownTestCtx()
    93  
    94  	ifaceIdx.Put("if1", &ifaceidx.IfaceMetadata{SwIfIndex: 55})
    95  	bdIndexes.Put("bd1", &idxvpp.OnlyIndex{Index: 5})
    96  
    97  	for i := 0; i < len(testDataInFib); i++ {
    98  		ctx.MockVpp.MockReply(&vpp_l2.L2fibAddDelReply{})
    99  		err := fibHandler.DeleteL2FIB(testDataInFib[i])
   100  		Expect(err).ShouldNot(HaveOccurred())
   101  		testDatasOutFib[i].IsAdd = false
   102  		Expect(ctx.MockChannel.Msg).To(Equal(testDatasOutFib[i]))
   103  	}
   104  }
   105  
   106  func fibTestSetup(t *testing.T) (*vppmock.TestCtx, vppcalls.FIBVppAPI, ifaceidx.IfaceMetadataIndexRW, idxvpp.NameToIndexRW) {
   107  	ctx := vppmock.SetupTestCtx(t)
   108  	logger := logrus.NewLogger("test-log")
   109  	ifaceIdx := ifaceidx.NewIfaceIndex(logger, "fib-if-idx")
   110  	bdIndexes := idxvpp.NewNameToIndex(logger, "fib-bd-idx", nil)
   111  	fibHandler := vpp2202.NewL2VppHandler(ctx.MockChannel, ifaceIdx, bdIndexes, logger)
   112  	return ctx, fibHandler, ifaceIdx, bdIndexes
   113  }