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

     1  //  Copyright (c) 2020 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 vpp
    16  
    17  import (
    18  	"math"
    19  	"net"
    20  	"testing"
    21  
    22  	"go.ligato.io/cn-infra/v2/logging/logrus"
    23  
    24  	netalloc_mock "go.ligato.io/vpp-agent/v3/plugins/netalloc/mock"
    25  	"go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/ifaceidx"
    26  	ifplugin_vppcalls "go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/vppcalls"
    27  	l3plugin_vppcalls "go.ligato.io/vpp-agent/v3/plugins/vpp/l3plugin/vppcalls"
    28  	"go.ligato.io/vpp-agent/v3/plugins/vpp/l3plugin/vrfidx"
    29  	vpp_l3 "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/l3"
    30  
    31  	_ "go.ligato.io/vpp-agent/v3/plugins/vpp/l3plugin"
    32  )
    33  
    34  func TestL3XC(t *testing.T) {
    35  	test := setupVPP(t)
    36  	defer test.teardownVPP()
    37  
    38  	// Setup indexers
    39  	ifIndexes := ifaceidx.NewIfaceIndex(logrus.NewLogger("test-if"), "test-if")
    40  	vrfIndexes := vrfidx.NewVRFIndex(logrus.NewLogger("test-vrf"), "test-vrf")
    41  	vrfIndexes.Put("vrf1-ipv4", &vrfidx.VRFMetadata{Index: 0, Protocol: vpp_l3.VrfTable_IPV4})
    42  	vrfIndexes.Put("vrf1-ipv6", &vrfidx.VRFMetadata{Index: 0, Protocol: vpp_l3.VrfTable_IPV6})
    43  
    44  	ih := ifplugin_vppcalls.CompatibleInterfaceVppHandler(test.vppClient, logrus.NewLogger("test"))
    45  
    46  	// Create interfaces
    47  	const iface1 = "loop1"
    48  	ifIdx1, err := ih.AddLoopbackInterface(iface1)
    49  	if err != nil {
    50  		t.Fatalf("creating interface failed: %v", err)
    51  	}
    52  	ifIndexes.Put(iface1, &ifaceidx.IfaceMetadata{SwIfIndex: ifIdx1})
    53  	ipNet1 := net.IPNet{
    54  		IP:   net.IPv4(10, 10, 0, 1),
    55  		Mask: net.IPv4Mask(255, 255, 255, 0),
    56  	}
    57  	if err := ih.AddInterfaceIP(ifIdx1, &ipNet1); err != nil {
    58  		t.Fatalf("adding interface IP failed: %v", err)
    59  	}
    60  	if err := ih.InterfaceAdminUp(test.Ctx, ifIdx1); err != nil {
    61  		t.Fatalf("setting interface admin up failed: %v", err)
    62  	}
    63  
    64  	const iface2 = "loop2"
    65  	ifIdx2, err := ih.AddLoopbackInterface(iface2)
    66  	if err != nil {
    67  		t.Fatalf("creating interface failed: %v", err)
    68  	}
    69  	ifIndexes.Put(iface2, &ifaceidx.IfaceMetadata{SwIfIndex: ifIdx2})
    70  	ipNet2 := net.IPNet{
    71  		IP:   net.IPv4(10, 20, 0, 1),
    72  		Mask: net.IPv4Mask(255, 255, 255, 0),
    73  	}
    74  	if err := ih.AddInterfaceIP(ifIdx2, &ipNet2); err != nil {
    75  		t.Fatalf("adding interface IP failed: %v", err)
    76  	}
    77  	if err := ih.InterfaceAdminUp(test.Ctx, ifIdx2); err != nil {
    78  		t.Fatalf("setting interface admin up failed: %v", err)
    79  	}
    80  
    81  	// Run test cases
    82  	l3Handler := l3plugin_vppcalls.CompatibleL3VppHandler(test.vppClient, ifIndexes, vrfIndexes,
    83  		netalloc_mock.NewMockNetAlloc(), logrus.NewLogger("test"))
    84  
    85  	l3xcs, err := l3Handler.DumpL3XC(test.Ctx, math.MaxUint32)
    86  	if err != nil {
    87  		t.Fatalf("dumping l3xcs failed: %v", err)
    88  	} else if len(l3xcs) != 0 {
    89  		t.Fatalf("expected empty dump, but got: %+v", l3xcs)
    90  	}
    91  
    92  	err = l3Handler.UpdateL3XC(test.Ctx, &l3plugin_vppcalls.L3XC{
    93  		SwIfIndex: ifIdx1,
    94  		IsIPv6:    false,
    95  		Paths: []l3plugin_vppcalls.Path{
    96  			{
    97  				SwIfIndex: ifIdx2,
    98  				NextHop:   ipNet2.IP,
    99  			},
   100  		},
   101  	})
   102  	if err != nil {
   103  		t.Fatalf("unexpected error on updating l3xcs: %v", err)
   104  	}
   105  
   106  	l3xcs, err = l3Handler.DumpL3XC(test.Ctx, math.MaxUint32)
   107  	if err != nil {
   108  		t.Fatalf("dumping l3xcs failed: %v", err)
   109  	} else if n := len(l3xcs); n != 1 {
   110  		t.Fatalf("expected 1 l3xc, but got %d", n)
   111  	}
   112  
   113  	if l3xcs[0].SwIfIndex != ifIdx1 {
   114  		t.Fatalf("expected SwIfIndex to be %d, but got %d", ifIdx1, l3xcs[0].SwIfIndex)
   115  	} else if l3xcs[0].IsIPv6 != false {
   116  		t.Fatalf("expected IsIPv6 to be false")
   117  	} else if len(l3xcs[0].Paths) != 1 {
   118  		t.Fatalf("expected 1 path, but got %d", len(l3xcs[0].Paths))
   119  	}
   120  	path := l3xcs[0].Paths[0]
   121  	if path.SwIfIndex != ifIdx2 {
   122  		t.Fatalf("expected path SwIfIndex to be %d, but got %d", ifIdx2, path.SwIfIndex)
   123  	} else if !path.NextHop.Equal(ipNet2.IP) {
   124  		t.Fatalf("expected path Nh to be %v, but got %v", ipNet2.IP, path.NextHop)
   125  	}
   126  
   127  	err = l3Handler.DeleteL3XC(test.Ctx, ifIdx1, false)
   128  	if err != nil {
   129  		t.Fatalf("deleting l3xc failed: %v", err)
   130  	}
   131  
   132  	l3xcs, err = l3Handler.DumpL3XC(test.Ctx, math.MaxUint32)
   133  	if err != nil {
   134  		t.Fatalf("dumping l3xcs failed: %v", err)
   135  	} else if len(l3xcs) != 0 {
   136  		t.Fatalf("expected empty dump, but got: %+v", l3xcs)
   137  	}
   138  }