go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/ifplugin/vppcalls/vpp2101/afpacket_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  	. "github.com/onsi/gomega"
    21  
    22  	vpp_afpacket "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2101/af_packet"
    23  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2101/ethernet_types"
    24  	vpp_ifs "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2101/interface"
    25  	"go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/vppcalls/vpp2101"
    26  )
    27  
    28  func TestAddAfPacketInterface(t *testing.T) {
    29  	ctx, ifHandler := ifTestSetup(t)
    30  	defer ctx.TeardownTestCtx()
    31  
    32  	ctx.MockVpp.MockReply(&vpp_afpacket.AfPacketCreateReply{})
    33  	ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceTagAddDelReply{})
    34  
    35  	ifIndex, err := ifHandler.AddAfPacketInterface("if1", "", "host1")
    36  
    37  	Expect(err).To(BeNil())
    38  	Expect(ifIndex).ToNot(BeNil())
    39  	Expect(len(ctx.MockChannel.Msgs)).To(BeEquivalentTo(2))
    40  	for i, msg := range ctx.MockChannel.Msgs {
    41  		if i == 0 {
    42  			vppMsg, ok := msg.(*vpp_afpacket.AfPacketCreate)
    43  			Expect(ok).To(BeTrue())
    44  			Expect(vppMsg).To(Equal(&vpp_afpacket.AfPacketCreate{
    45  				HostIfName:      "host1",
    46  				HwAddr:          ethernet_types.MacAddress{},
    47  				UseRandomHwAddr: true,
    48  			}))
    49  		}
    50  	}
    51  }
    52  
    53  func TestAddAfPacketInterfaceError(t *testing.T) {
    54  	ctx, ifHandler := ifTestSetup(t)
    55  	defer ctx.TeardownTestCtx()
    56  
    57  	ctx.MockVpp.MockReply(&vpp_afpacket.AfPacketDeleteReply{})
    58  
    59  	_, err := ifHandler.AddAfPacketInterface("if1", "", "host1")
    60  
    61  	Expect(err).ToNot(BeNil())
    62  }
    63  
    64  func TestAddAfPacketInterfaceRetval(t *testing.T) {
    65  	ctx, ifHandler := ifTestSetup(t)
    66  	defer ctx.TeardownTestCtx()
    67  
    68  	ctx.MockVpp.MockReply(&vpp_afpacket.AfPacketCreateReply{
    69  		Retval: 1,
    70  	})
    71  	ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceTagAddDelReply{})
    72  
    73  	_, err := ifHandler.AddAfPacketInterface("if1", "", "host1")
    74  
    75  	Expect(err).ToNot(BeNil())
    76  }
    77  
    78  func TestDeleteAfPacketInterface(t *testing.T) {
    79  	ctx, ifHandler := ifTestSetup(t)
    80  	defer ctx.TeardownTestCtx()
    81  
    82  	ctx.MockVpp.MockReply(&vpp_afpacket.AfPacketDeleteReply{})
    83  	ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceTagAddDelReply{})
    84  
    85  	err := ifHandler.DeleteAfPacketInterface("if1", 0, "host1")
    86  
    87  	Expect(err).To(BeNil())
    88  	Expect(len(ctx.MockChannel.Msgs)).To(BeEquivalentTo(2))
    89  	for i, msg := range ctx.MockChannel.Msgs {
    90  		if i == 0 {
    91  			vppMsg, ok := msg.(*vpp_afpacket.AfPacketDelete)
    92  			Expect(ok).To(BeTrue())
    93  			Expect(vppMsg).To(Equal(&vpp_afpacket.AfPacketDelete{
    94  				HostIfName: "host1",
    95  			}))
    96  		}
    97  	}
    98  }
    99  
   100  func TestDeleteAfPacketInterfaceError(t *testing.T) {
   101  	ctx, ifHandler := ifTestSetup(t)
   102  	defer ctx.TeardownTestCtx()
   103  
   104  	ctx.MockVpp.MockReply(&vpp_afpacket.AfPacketCreateReply{})
   105  
   106  	err := ifHandler.DeleteAfPacketInterface("if1", 0, "host1")
   107  
   108  	Expect(err).ToNot(BeNil())
   109  }
   110  
   111  func TestDeleteAfPacketInterfaceRetval(t *testing.T) {
   112  	ctx, ifHandler := ifTestSetup(t)
   113  	defer ctx.TeardownTestCtx()
   114  
   115  	ctx.MockVpp.MockReply(&vpp_afpacket.AfPacketDeleteReply{
   116  		Retval: 1,
   117  	})
   118  	ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceTagAddDelReply{})
   119  
   120  	err := ifHandler.DeleteAfPacketInterface("if1", 0, "host1")
   121  
   122  	Expect(err).ToNot(BeNil())
   123  }
   124  
   125  func TestAddAfPacketInterfaceMac(t *testing.T) {
   126  	ctx, ifHandler := ifTestSetup(t)
   127  	defer ctx.TeardownTestCtx()
   128  
   129  	ctx.MockVpp.MockReply(&vpp_afpacket.AfPacketCreateReply{})
   130  	ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceTagAddDelReply{})
   131  
   132  	ifIndex, err := ifHandler.AddAfPacketInterface("if1", "a2:01:01:01:01:01", "host1")
   133  
   134  	Expect(err).To(BeNil())
   135  	Expect(ifIndex).ToNot(BeNil())
   136  	Expect(len(ctx.MockChannel.Msgs)).To(BeEquivalentTo(2))
   137  
   138  	mac, err := vpp2101.ParseMAC("a2:01:01:01:01:01")
   139  	Expect(err).To(BeNil())
   140  
   141  	for i, msg := range ctx.MockChannel.Msgs {
   142  		if i == 0 {
   143  			vppMsg, ok := msg.(*vpp_afpacket.AfPacketCreate)
   144  			Expect(ok).To(BeTrue())
   145  			Expect(vppMsg).To(Equal(&vpp_afpacket.AfPacketCreate{
   146  				HostIfName:      "host1",
   147  				HwAddr:          mac,
   148  				UseRandomHwAddr: false,
   149  			}))
   150  		}
   151  	}
   152  }