go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/ifplugin/vppcalls/vpp2210/watch_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 vpp2210_test
    16  
    17  import (
    18  	"net"
    19  	"testing"
    20  
    21  	. "github.com/onsi/gomega"
    22  
    23  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2210/dhcp"
    24  	interfaces "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2210/interface"
    25  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2210/ip_types"
    26  	"go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/vppcalls"
    27  )
    28  
    29  func TestWatchInterfaceEvents(t *testing.T) {
    30  	ctx, ifHandler := ifTestSetup(t)
    31  	defer ctx.TeardownTestCtx()
    32  	ctx.MockVpp.MockReply(&interfaces.WantInterfaceEventsReply{})
    33  	eventsChan := make(chan *vppcalls.InterfaceEvent)
    34  	err := ifHandler.WatchInterfaceEvents(ctx.Context, eventsChan)
    35  	notifChan := ctx.MockChannel.GetChannel()
    36  	Expect(notifChan).ToNot(BeNil())
    37  	Expect(err).To(BeNil())
    38  
    39  	notifChan <- &interfaces.SwInterfaceEvent{
    40  		SwIfIndex: 1,
    41  		Flags:     3,
    42  		Deleted:   true,
    43  	}
    44  	var result *vppcalls.InterfaceEvent
    45  	Eventually(eventsChan, 2).Should(Receive(&result))
    46  	Expect(result).To(Equal(&vppcalls.InterfaceEvent{
    47  		SwIfIndex:  1,
    48  		AdminState: 1,
    49  		LinkState:  1,
    50  		Deleted:    true,
    51  	}))
    52  
    53  	notifChan <- &interfaces.SwInterfaceEvent{
    54  		SwIfIndex: 2,
    55  		Flags:     1,
    56  		Deleted:   true,
    57  	}
    58  	result = &vppcalls.InterfaceEvent{}
    59  	Eventually(eventsChan, 2).Should(Receive(&result))
    60  	Expect(result).To(Equal(&vppcalls.InterfaceEvent{SwIfIndex: 2, AdminState: 1, LinkState: 0, Deleted: true}))
    61  
    62  	notifChan <- &interfaces.SwInterfaceEvent{
    63  		SwIfIndex: 3,
    64  		Flags:     3,
    65  		Deleted:   false,
    66  	}
    67  	result = &vppcalls.InterfaceEvent{}
    68  	Eventually(eventsChan, 2).Should(Receive(&result))
    69  	Expect(result).To(Equal(&vppcalls.InterfaceEvent{
    70  		SwIfIndex:  3,
    71  		AdminState: 1,
    72  		LinkState:  1,
    73  		Deleted:    false,
    74  	}))
    75  
    76  	close(notifChan)
    77  }
    78  
    79  func TestWatchDHCPLeases(t *testing.T) {
    80  	ctx, ifHandler := ifTestSetup(t)
    81  	defer ctx.TeardownTestCtx()
    82  	leasesChChan := make(chan *vppcalls.Lease)
    83  	err := ifHandler.WatchDHCPLeases(ctx.Context, leasesChChan)
    84  	notifChan := ctx.MockChannel.GetChannel()
    85  	Expect(notifChan).ToNot(BeNil())
    86  	Expect(err).To(BeNil())
    87  
    88  	var hostAddr, routerAddr [16]byte
    89  	copy(hostAddr[:], net.ParseIP("10.10.10.5").To4())
    90  	copy(routerAddr[:], net.ParseIP("10.10.10.1").To4())
    91  
    92  	notifChan <- &dhcp.DHCPComplEvent{
    93  		PID: 50,
    94  		Lease: dhcp.DHCPLease{
    95  			SwIfIndex:     1,
    96  			State:         1,
    97  			Hostname:      "host1",
    98  			IsIPv6:        false,
    99  			MaskWidth:     24,
   100  			HostAddress:   ip_types.Address{Un: ip_types.AddressUnion{XXX_UnionData: hostAddr}},
   101  			RouterAddress: ip_types.Address{Un: ip_types.AddressUnion{XXX_UnionData: routerAddr}},
   102  			HostMac:       [6]byte{16, 16, 32, 32, 48, 48},
   103  		},
   104  	}
   105  	var result *vppcalls.Lease
   106  	Eventually(leasesChChan, 50).Should(Receive(&result))
   107  	Expect(result).To(Equal(&vppcalls.Lease{
   108  		SwIfIndex:     1,
   109  		State:         1,
   110  		Hostname:      "host1",
   111  		HostAddress:   "10.10.10.5/24",
   112  		RouterAddress: "10.10.10.1/24",
   113  		HostMac:       "10:10:20:20:30:30",
   114  	}))
   115  
   116  	copy(hostAddr[:], net.ParseIP("1234::").To16())
   117  	copy(routerAddr[:], net.ParseIP("abcd::").To16())
   118  
   119  	notifChan <- &dhcp.DHCPComplEvent{
   120  		PID: 50,
   121  		Lease: dhcp.DHCPLease{
   122  			SwIfIndex:     2,
   123  			State:         0,
   124  			Hostname:      "host2",
   125  			IsIPv6:        true,
   126  			MaskWidth:     64,
   127  			HostAddress:   ip_types.Address{Un: ip_types.AddressUnion{XXX_UnionData: hostAddr}},
   128  			RouterAddress: ip_types.Address{Un: ip_types.AddressUnion{XXX_UnionData: routerAddr}},
   129  			HostMac:       [6]byte{16, 16, 32, 32, 64, 64},
   130  		},
   131  	}
   132  	Eventually(leasesChChan, 2).Should(Receive(&result))
   133  	Expect(result).To(Equal(&vppcalls.Lease{
   134  		SwIfIndex:     2,
   135  		Hostname:      "host2",
   136  		IsIPv6:        true,
   137  		HostAddress:   "1234::/64",
   138  		RouterAddress: "abcd::/64",
   139  		HostMac:       "10:10:20:20:40:40",
   140  	}))
   141  
   142  	close(leasesChChan)
   143  }