go.ligato.io/vpp-agent/v3@v3.5.0/tests/integration/vpp/030_arp_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 vpp
    16  
    17  import (
    18  	"strings"
    19  	"testing"
    20  
    21  	"go.ligato.io/cn-infra/v2/logging/logrus"
    22  
    23  	netalloc_mock "go.ligato.io/vpp-agent/v3/plugins/netalloc/mock"
    24  	"go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/ifaceidx"
    25  	ifplugin_vppcalls "go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/vppcalls"
    26  	l3plugin_vppcalls "go.ligato.io/vpp-agent/v3/plugins/vpp/l3plugin/vppcalls"
    27  	"go.ligato.io/vpp-agent/v3/plugins/vpp/l3plugin/vrfidx"
    28  	vpp_l3 "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/l3"
    29  
    30  	_ "go.ligato.io/vpp-agent/v3/plugins/vpp/l3plugin"
    31  )
    32  
    33  func TestArp(t *testing.T) {
    34  	ctx := setupVPP(t)
    35  	defer ctx.teardownVPP()
    36  
    37  	ih := ifplugin_vppcalls.CompatibleInterfaceVppHandler(ctx.vppClient, logrus.NewLogger("test"))
    38  	const ifName = "loop1"
    39  	ifIdx, err := ih.AddLoopbackInterface(ifName)
    40  	if err != nil {
    41  		t.Fatalf("creating interface failed: %v", err)
    42  	}
    43  	t.Logf("interface created %v", ifIdx)
    44  
    45  	ifIndexes := ifaceidx.NewIfaceIndex(logrus.NewLogger("test-if"), "test-if")
    46  	ifIndexes.Put(ifName, &ifaceidx.IfaceMetadata{SwIfIndex: ifIdx})
    47  	vrfIndexes := vrfidx.NewVRFIndex(logrus.NewLogger("test-vrf"), "test-vrf")
    48  	vrfIndexes.Put("vrf1-ipv4", &vrfidx.VRFMetadata{Index: 0, Protocol: vpp_l3.VrfTable_IPV4})
    49  	vrfIndexes.Put("vrf1-ipv6", &vrfidx.VRFMetadata{Index: 0, Protocol: vpp_l3.VrfTable_IPV6})
    50  
    51  	h := l3plugin_vppcalls.CompatibleL3VppHandler(ctx.vppClient, ifIndexes, vrfIndexes,
    52  		netalloc_mock.NewMockNetAlloc(), logrus.NewLogger("test"))
    53  
    54  	tests := []*struct {
    55  		name        string
    56  		newArpEntry vpp_l3.ARPEntry
    57  	}{
    58  		{"static arp for ipv4", vpp_l3.ARPEntry{
    59  			Interface:   ifName,
    60  			IpAddress:   "192.168.10.21",
    61  			PhysAddress: "59:6C:45:59:8E:BD",
    62  			Static:      true,
    63  		}},
    64  		{"nonstatic arp for ipv4", vpp_l3.ARPEntry{
    65  			Interface:   ifName,
    66  			IpAddress:   "192.168.10.22",
    67  			PhysAddress: "6C:45:59:59:8E:BD",
    68  			Static:      false,
    69  		}},
    70  		{"nonstatic arp for ipv6", vpp_l3.ARPEntry{
    71  			Interface:   ifName,
    72  			IpAddress:   "dead::1",
    73  			PhysAddress: "8E:BD:6C:45:59:59",
    74  			Static:      false,
    75  		}},
    76  	}
    77  
    78  	for _, test := range tests {
    79  		t.Run(test.name, func(t *testing.T) {
    80  
    81  			arpentries, err := h.DumpArpEntries()
    82  			if err != nil {
    83  				t.Fatalf("dumping arpentries failed: %v", err)
    84  			}
    85  			arpentriescnt := len(arpentries)
    86  			t.Logf("%d arpentries dumped", arpentriescnt)
    87  
    88  			err = h.VppAddArp(&test.newArpEntry)
    89  			if err != nil {
    90  				t.Fatalf("adding arpentry failed: %v", err)
    91  			}
    92  			t.Logf("arpentry added %+v", &test.newArpEntry)
    93  
    94  			arpentries, err = h.DumpArpEntries()
    95  			if err != nil {
    96  				t.Fatalf("dumping arpentries failed: %v", err)
    97  			}
    98  			arpentriescnt2 := len(arpentries)
    99  			t.Logf("%d arpentries dumped", arpentriescnt2)
   100  
   101  			if arpentriescnt+1 != arpentriescnt2 {
   102  				t.Errorf("Number of arp entries after adding of one arp entry is not incremented by 1")
   103  			}
   104  
   105  			newArpEntryIsPresent := false
   106  			for _, arpentry := range arpentries {
   107  				if (arpentry.Arp.Interface == test.newArpEntry.Interface) && (arpentry.Arp.IpAddress == test.newArpEntry.IpAddress) && strings.EqualFold(arpentry.Arp.PhysAddress, test.newArpEntry.PhysAddress) {
   108  					t.Logf("dumped arpentry %+v", arpentry)
   109  					newArpEntryIsPresent = true
   110  					break
   111  				}
   112  			}
   113  
   114  			if !newArpEntryIsPresent {
   115  				t.Error("Added arp entry is not present in arp dump")
   116  			}
   117  
   118  			err = h.VppDelArp(&test.newArpEntry)
   119  			if err != nil {
   120  				t.Fatalf("deleting arpentry failed: %v", err)
   121  			}
   122  			t.Logf("arpentry deleted")
   123  
   124  			arpentries, err = h.DumpArpEntries()
   125  			if err != nil {
   126  				t.Fatalf("dumping arpentries failed: %v", err)
   127  			}
   128  			arpentriescnt3 := len(arpentries)
   129  			t.Logf("%d arpentries dumped", arpentriescnt3)
   130  
   131  			if arpentriescnt2-1 != arpentriescnt3 {
   132  				t.Errorf("Number of arp entries after deleting of one arp entry is not decremented by 1")
   133  			}
   134  
   135  			for _, arpentry := range arpentries {
   136  				if (arpentry.Arp.Interface == test.newArpEntry.Interface) && (arpentry.Arp.IpAddress == test.newArpEntry.IpAddress) && strings.EqualFold(arpentry.Arp.PhysAddress, test.newArpEntry.PhysAddress) {
   137  					t.Error("Added arp entry is still present in arp dump - should be deleted")
   138  				}
   139  			}
   140  		})
   141  	}
   142  }