go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/ifplugin/ifaceidx/ifaceidx_test.go (about)

     1  // Copyright (c) 2018 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 ifaceidx
    16  
    17  import (
    18  	"testing"
    19  
    20  	"fmt"
    21  
    22  	. "github.com/onsi/gomega"
    23  	"go.ligato.io/cn-infra/v2/logging/logrus"
    24  )
    25  
    26  // Constants
    27  const (
    28  	ifName0          = "if0"
    29  	ifName1          = "if1"
    30  	ifName2          = "if2"
    31  	idx0      uint32 = 0
    32  	idx1      uint32 = 1
    33  	idx2      uint32 = 2
    34  	ipAddr0          = "192.168.0.1/24"
    35  	ipAddr1          = "192.168.1.1/24"
    36  	ipAddr2          = "192.168.2.1/24"
    37  	ipAddr3          = "192.168.3.1/24"
    38  	watchName        = "watchName"
    39  )
    40  
    41  func testInitialization(t *testing.T) IfaceMetadataIndexRW {
    42  	RegisterTestingT(t)
    43  	return NewIfaceIndex(logrus.DefaultLogger(), "iface-meta-index")
    44  }
    45  
    46  // TestIndexMetadata tests whether func IndexMetadata return map filled with correct values
    47  func TestIndexMetadata(t *testing.T) {
    48  	const (
    49  		ipAddr0 = "192.168.0.1/24"
    50  		ipAddr1 = "192.168.1.1/24"
    51  	)
    52  
    53  	testInitialization(t)
    54  	iface := &IfaceMetadata{IPAddresses: []string{ipAddr0, ipAddr1}}
    55  
    56  	result := indexMetadata(nil)
    57  	Expect(result).To(HaveLen(0))
    58  
    59  	result = indexMetadata(iface)
    60  	Expect(result).To(HaveLen(1))
    61  
    62  	ipAddrs := result[ipAddressIndexKey]
    63  	Expect(ipAddrs).To(HaveLen(2))
    64  	Expect(ipAddrs).To(ContainElement(ipAddr0))
    65  	Expect(ipAddrs).To(ContainElement(ipAddr1))
    66  }
    67  
    68  // Tests registering and unregistering name to index
    69  func TestRegisterAndUnregisterName(t *testing.T) {
    70  	index := testInitialization(t)
    71  	iface := &IfaceMetadata{SwIfIndex: idx0, IPAddresses: []string{ipAddr0, ipAddr1}}
    72  
    73  	// Register iface
    74  	index.Put(ifName0, iface)
    75  	names := index.ListAllInterfaces()
    76  	Expect(names).To(HaveLen(1))
    77  	Expect(names).To(ContainElement(ifName0))
    78  
    79  	// Unregister iface
    80  	index.Delete(ifName0)
    81  	names = index.ListAllInterfaces()
    82  	Expect(names).To(BeEmpty())
    83  }
    84  
    85  // Tests index mapping clear
    86  func TestClearInterfaces(t *testing.T) {
    87  	index := testInitialization(t)
    88  
    89  	// Register entries
    90  	index.Put("if1", &IfaceMetadata{SwIfIndex: 0})
    91  	index.Put("if2", &IfaceMetadata{SwIfIndex: 1})
    92  	index.Put("if3", &IfaceMetadata{SwIfIndex: 2})
    93  	names := index.ListAllInterfaces()
    94  	Expect(names).To(HaveLen(3))
    95  
    96  	// Clear
    97  	index.Clear()
    98  	names = index.ListAllInterfaces()
    99  	Expect(names).To(BeEmpty())
   100  }
   101  
   102  // Tests updating of metadata
   103  func TestUpdateMetadata(t *testing.T) {
   104  	index := testInitialization(t)
   105  	iface := &IfaceMetadata{IPAddresses: []string{ipAddr0, ipAddr1}}
   106  
   107  	ifUpdate1 := &IfaceMetadata{
   108  		IPAddresses: []string{ipAddr2},
   109  	}
   110  
   111  	ifUpdate2 := &IfaceMetadata{
   112  		IPAddresses: []string{ipAddr3},
   113  	}
   114  
   115  	// Update before registration (no entry created)
   116  	success := index.Update(ifName0, iface)
   117  	Expect(success).To(BeFalse())
   118  
   119  	metadata, found := index.LookupByName(ifName0)
   120  	Expect(found).To(BeFalse())
   121  	Expect(metadata).To(BeNil())
   122  
   123  	// Add interface
   124  	index.Put(ifName0, iface)
   125  	names := index.ListAllInterfaces()
   126  	Expect(names).To(HaveLen(1))
   127  	Expect(names).To(ContainElement(ifName0))
   128  
   129  	// Evaluate entry metadata
   130  	metadata, found = index.LookupByName(ifName0)
   131  	Expect(found).To(BeTrue())
   132  	Expect(metadata).ToNot(BeNil())
   133  	Expect(metadata.IPAddresses).To(HaveLen(2))
   134  
   135  	ipaddrs := metadata.IPAddresses
   136  	Expect(ipaddrs).To(ContainElement(ipAddr0))
   137  	Expect(ipaddrs).To(ContainElement(ipAddr1))
   138  
   139  	// Update metadata (same name, different data)
   140  	success = index.Update(ifName0, ifUpdate1)
   141  	Expect(success).To(BeTrue())
   142  
   143  	// Evaluate updated metadata
   144  	metadata, found = index.LookupByName(ifName0)
   145  	Expect(found).To(BeTrue())
   146  	Expect(metadata).ToNot(BeNil())
   147  	Expect(metadata.IPAddresses).To(HaveLen(1))
   148  
   149  	ipaddrs = metadata.IPAddresses
   150  	Expect(ipaddrs).To(ContainElement(ipAddr2))
   151  
   152  	// Update metadata again
   153  	success = index.Update(ifName0, ifUpdate2)
   154  	Expect(success).To(BeTrue())
   155  
   156  	// Evaluate updated metadata
   157  	metadata, found = index.LookupByName(ifName0)
   158  	Expect(found).To(BeTrue())
   159  	Expect(metadata).ToNot(BeNil())
   160  	Expect(metadata.IPAddresses).To(HaveLen(1))
   161  
   162  	ipaddrs = metadata.IPAddresses
   163  	Expect(ipaddrs).To(ContainElement(ipAddr3))
   164  
   165  	// Remove interface
   166  	index.Delete(ifName0)
   167  
   168  	// Check removal
   169  	names = index.ListAllInterfaces()
   170  	Expect(names).To(BeEmpty())
   171  }
   172  
   173  // Tests lookup by name
   174  func TestLookupByName(t *testing.T) {
   175  	index := testInitialization(t)
   176  	iface := &IfaceMetadata{SwIfIndex: idx0, IPAddresses: []string{ipAddr0, ipAddr1}}
   177  
   178  	index.Put(ifName0, iface)
   179  
   180  	metadata, exist := index.LookupByName(ifName0)
   181  	Expect(exist).To(BeTrue())
   182  	Expect(metadata.GetIndex()).To(Equal(idx0))
   183  	Expect(metadata).To(Equal(iface))
   184  }
   185  
   186  // Tests lookup by index
   187  func TestLookupByIndex(t *testing.T) {
   188  	index := testInitialization(t)
   189  	iface := &IfaceMetadata{SwIfIndex: idx0, IPAddresses: []string{ipAddr0, ipAddr1}}
   190  
   191  	index.Put(ifName0, iface)
   192  
   193  	foundName, metadata, exist := index.LookupBySwIfIndex(idx0)
   194  	Expect(exist).To(BeTrue())
   195  	Expect(foundName).To(Equal(ifName0))
   196  	Expect(metadata).To(Equal(iface))
   197  }
   198  
   199  // Tests lookup by ip address
   200  func TestLookupByIP(t *testing.T) {
   201  	index := testInitialization(t)
   202  
   203  	// defines 3 interfaces
   204  	iface1 := &IfaceMetadata{SwIfIndex: idx0, IPAddresses: []string{ipAddr0, ipAddr1}}
   205  	iface2 := &IfaceMetadata{SwIfIndex: idx1, IPAddresses: []string{ipAddr0, ipAddr2}}
   206  	iface3 := &IfaceMetadata{SwIfIndex: idx2, IPAddresses: []string{ipAddr3}}
   207  
   208  	// register all interfaces
   209  	index.Put(ifName0, iface1)
   210  	index.Put(ifName1, iface2)
   211  	index.Put(ifName2, iface3)
   212  
   213  	// try to lookup each interface by each ip adress
   214  	ifaces := index.LookupByIP(ipAddr0)
   215  	Expect(ifaces).To(ContainElement(ifName0))
   216  	Expect(ifaces).To(ContainElement(ifName1))
   217  	Expect(ifaces).To(HaveLen(2))
   218  
   219  	ifaces = index.LookupByIP(ipAddr1)
   220  	Expect(ifaces).To(ContainElement(ifName0))
   221  	Expect(ifaces).To(HaveLen(1))
   222  
   223  	ifaces = index.LookupByIP(ipAddr2)
   224  	Expect(ifaces).To(ContainElement(ifName1))
   225  	Expect(ifaces).To(HaveLen(1))
   226  
   227  	ifaces = index.LookupByIP(ipAddr3)
   228  	Expect(ifaces).To(ContainElement(ifName2))
   229  	Expect(ifaces).To(HaveLen(1))
   230  
   231  	// try empty lookup, should return nothing
   232  	ifaces = index.LookupByIP("")
   233  	Expect(ifaces).To(BeEmpty())
   234  }
   235  
   236  // Tests watch interfaces
   237  func TestWatchNameToIdx(t *testing.T) {
   238  	fmt.Println("TestWatchNameToIdx")
   239  	index := testInitialization(t)
   240  	iface := &IfaceMetadata{SwIfIndex: idx0, IPAddresses: []string{ipAddr0, ipAddr1}}
   241  
   242  	c := make(chan IfaceMetadataDto, 10)
   243  	index.WatchInterfaces(watchName, c)
   244  
   245  	index.Put(ifName0, iface)
   246  
   247  	var dto IfaceMetadataDto
   248  	Eventually(c).Should(Receive(&dto))
   249  
   250  	Expect(dto.Name).To(Equal(ifName0))
   251  	Expect(dto.Metadata.GetIndex()).To(Equal(idx0))
   252  }