go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/l3plugin/vppcalls/vpp2101/route_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  	"go.ligato.io/vpp-agent/v3/plugins/vpp/l3plugin/vrfidx"
    21  
    22  	. "github.com/onsi/gomega"
    23  	"go.ligato.io/cn-infra/v2/logging/logrus"
    24  
    25  	netallock_mock "go.ligato.io/vpp-agent/v3/plugins/netalloc/mock"
    26  	vpp_ip "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2101/ip"
    27  	"go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/ifaceidx"
    28  	ifvppcalls "go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/vppcalls"
    29  	ifvpp2101_379 "go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/vppcalls/vpp2101"
    30  	"go.ligato.io/vpp-agent/v3/plugins/vpp/l3plugin/vppcalls"
    31  	vpp2101 "go.ligato.io/vpp-agent/v3/plugins/vpp/l3plugin/vppcalls/vpp2101"
    32  	"go.ligato.io/vpp-agent/v3/plugins/vpp/vppmock"
    33  	l3 "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/l3"
    34  )
    35  
    36  var routes = []*l3.Route{
    37  	{
    38  		VrfId:             1,
    39  		DstNetwork:        "192.168.10.21/24",
    40  		NextHopAddr:       "192.168.30.1",
    41  		OutgoingInterface: "iface1",
    42  	},
    43  	{
    44  		VrfId:       2,
    45  		DstNetwork:  "10.0.0.1/24",
    46  		NextHopAddr: "192.168.30.1",
    47  	},
    48  	{
    49  		VrfId:             2,
    50  		DstNetwork:        "10.11.0.1/16",
    51  		NextHopAddr:       "192.168.30.1",
    52  		OutgoingInterface: "iface3",
    53  	},
    54  }
    55  
    56  // Test adding routes
    57  func TestAddRoute(t *testing.T) {
    58  	ctx, _, rtHandler := routeTestSetup(t)
    59  	defer ctx.TeardownTestCtx()
    60  
    61  	ctx.MockVpp.MockReply(&vpp_ip.IPRouteAddDelReply{})
    62  	err := rtHandler.VppAddRoute(ctx.Context, routes[0])
    63  	Expect(err).To(Succeed())
    64  
    65  	ctx.MockVpp.MockReply(&vpp_ip.IPRouteAddDelReply{})
    66  	err = rtHandler.VppAddRoute(ctx.Context, routes[2])
    67  	Expect(err).To(Not(BeNil())) // unknown interface
    68  }
    69  
    70  // Test deleting routes
    71  func TestDeleteRoute(t *testing.T) {
    72  	ctx, _, rtHandler := routeTestSetup(t)
    73  	defer ctx.TeardownTestCtx()
    74  
    75  	ctx.MockVpp.MockReply(&vpp_ip.IPRouteAddDelReply{})
    76  	err := rtHandler.VppDelRoute(ctx.Context, routes[0])
    77  	Expect(err).To(Succeed())
    78  
    79  	ctx.MockVpp.MockReply(&vpp_ip.IPRouteAddDelReply{})
    80  	err = rtHandler.VppDelRoute(ctx.Context, routes[1])
    81  	Expect(err).To(Succeed())
    82  
    83  	ctx.MockVpp.MockReply(&vpp_ip.IPRouteAddDelReply{Retval: 1})
    84  	err = rtHandler.VppDelRoute(ctx.Context, routes[0])
    85  	Expect(err).To(Not(BeNil()))
    86  }
    87  
    88  func routeTestSetup(t *testing.T) (*vppmock.TestCtx, ifvppcalls.InterfaceVppAPI, vppcalls.RouteVppAPI) {
    89  	ctx := vppmock.SetupTestCtx(t)
    90  	log := logrus.NewLogger("test-log")
    91  	ifHandler := ifvpp2101_379.NewInterfaceVppHandler(ctx.MockVPPClient, log)
    92  	ifIndexes := ifaceidx.NewIfaceIndex(logrus.NewLogger("test-if"), "test-if")
    93  	vrfIndexes := vrfidx.NewVRFIndex(logrus.NewLogger("test-vrf"), "test-vrf")
    94  	ifIndexes.Put("iface1", &ifaceidx.IfaceMetadata{
    95  		SwIfIndex: 1,
    96  	})
    97  	rtHandler := vpp2101.NewRouteVppHandler(ctx.MockChannel, ifIndexes, vrfIndexes, netallock_mock.NewMockNetAlloc(), log)
    98  	return ctx, ifHandler, rtHandler
    99  }