go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/l2plugin/vppcalls/vpp2202/interface_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 vpp2202_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/onsi/gomega"
    21  
    22  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2202/interface_types"
    23  	vpp_l2 "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2202/l2"
    24  	"go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/ifaceidx"
    25  	l2 "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/l2"
    26  )
    27  
    28  func TestAddInterfaceToBridgeDomain(t *testing.T) {
    29  	ctx, bdHandler, ifaceIdx := bdTestSetup(t)
    30  	defer ctx.TeardownTestCtx()
    31  
    32  	ifaceIdx.Put("if1", &ifaceidx.IfaceMetadata{SwIfIndex: 1})
    33  
    34  	ctx.MockVpp.MockReply(&vpp_l2.SwInterfaceSetL2BridgeReply{})
    35  	err := bdHandler.AddInterfaceToBridgeDomain(1, &l2.BridgeDomain_Interface{
    36  		Name:                    "if1",
    37  		BridgedVirtualInterface: true,
    38  		SplitHorizonGroup:       0,
    39  	})
    40  
    41  	Expect(err).To(BeNil())
    42  	Expect(ctx.MockChannel.Msgs).To(HaveLen(1))
    43  	msg := ctx.MockChannel.Msgs[0]
    44  	Expect(msg).To(BeEquivalentTo(&vpp_l2.SwInterfaceSetL2Bridge{
    45  		RxSwIfIndex: interface_types.InterfaceIndex(1),
    46  		BdID:        1,
    47  		Shg:         uint8(0),
    48  		PortType:    vpp_l2.L2_API_PORT_TYPE_BVI,
    49  		Enable:      true,
    50  	}))
    51  }
    52  
    53  func TestAddMissingInterfaceToBridgeDomain(t *testing.T) {
    54  	ctx, bdHandler, _ := bdTestSetup(t)
    55  	defer ctx.TeardownTestCtx()
    56  
    57  	// missing: ifaceIdx.Put("if1", &ifaceidx.IfaceMetadata{SwIfIndex: 1})
    58  
    59  	ctx.MockVpp.MockReply(&vpp_l2.SwInterfaceSetL2BridgeReply{})
    60  	err := bdHandler.AddInterfaceToBridgeDomain(1, &l2.BridgeDomain_Interface{
    61  		Name:                    "if1",
    62  		BridgedVirtualInterface: true,
    63  		SplitHorizonGroup:       0,
    64  	})
    65  
    66  	Expect(err).ToNot(BeNil())
    67  	Expect(ctx.MockChannel.Msgs).To(BeEmpty())
    68  }
    69  
    70  func TestAddInterfaceToBridgeDomainWithError(t *testing.T) {
    71  	ctx, bdHandler, ifaceIdx := bdTestSetup(t)
    72  	defer ctx.TeardownTestCtx()
    73  
    74  	ifaceIdx.Put("if1", &ifaceidx.IfaceMetadata{SwIfIndex: 1})
    75  
    76  	ctx.MockVpp.MockReply(&vpp_l2.SwInterfaceSetL2Bridge{}) // wrong reply message type
    77  	err := bdHandler.AddInterfaceToBridgeDomain(1, &l2.BridgeDomain_Interface{
    78  		Name:                    "if1",
    79  		BridgedVirtualInterface: true,
    80  		SplitHorizonGroup:       0,
    81  	})
    82  
    83  	Expect(err).ToNot(BeNil())
    84  	Expect(ctx.MockChannel.Msgs).To(HaveLen(1))
    85  }
    86  
    87  func TestAddInterfaceToBridgeDomainWithNonZeroRetval(t *testing.T) {
    88  	ctx, bdHandler, ifaceIdx := bdTestSetup(t)
    89  	defer ctx.TeardownTestCtx()
    90  
    91  	ifaceIdx.Put("if1", &ifaceidx.IfaceMetadata{SwIfIndex: 1})
    92  
    93  	ctx.MockVpp.MockReply(&vpp_l2.SwInterfaceSetL2BridgeReply{
    94  		Retval: 1,
    95  	})
    96  	err := bdHandler.AddInterfaceToBridgeDomain(1, &l2.BridgeDomain_Interface{
    97  		Name:                    "if1",
    98  		BridgedVirtualInterface: true,
    99  		SplitHorizonGroup:       0,
   100  	})
   101  
   102  	Expect(err).ToNot(BeNil())
   103  	Expect(ctx.MockChannel.Msgs).To(HaveLen(1))
   104  }
   105  
   106  func TestDeleteInterfaceFromBridgeDomain(t *testing.T) {
   107  	ctx, bdHandler, ifaceIdx := bdTestSetup(t)
   108  	defer ctx.TeardownTestCtx()
   109  
   110  	ifaceIdx.Put("if1", &ifaceidx.IfaceMetadata{SwIfIndex: 10})
   111  
   112  	ctx.MockVpp.MockReply(&vpp_l2.SwInterfaceSetL2BridgeReply{})
   113  	err := bdHandler.DeleteInterfaceFromBridgeDomain(4, &l2.BridgeDomain_Interface{
   114  		Name:              "if1",
   115  		SplitHorizonGroup: 12,
   116  	})
   117  
   118  	Expect(err).To(BeNil())
   119  	Expect(ctx.MockChannel.Msgs).To(HaveLen(1))
   120  	msg := ctx.MockChannel.Msgs[0]
   121  	Expect(msg).To(BeEquivalentTo(&vpp_l2.SwInterfaceSetL2Bridge{
   122  		RxSwIfIndex: interface_types.InterfaceIndex(10),
   123  		BdID:        4,
   124  		Shg:         uint8(12),
   125  		PortType:    vpp_l2.L2_API_PORT_TYPE_NORMAL,
   126  		Enable:      false,
   127  	}))
   128  }
   129  
   130  func TestDeleteMissingInterfaceFromBridgeDomain(t *testing.T) {
   131  	ctx, bdHandler, _ := bdTestSetup(t)
   132  	defer ctx.TeardownTestCtx()
   133  
   134  	// missing: ifaceIdx.Put("if1", &ifaceidx.IfaceMetadata{SwIfIndex: 10})
   135  
   136  	ctx.MockVpp.MockReply(&vpp_l2.SwInterfaceSetL2BridgeReply{})
   137  	err := bdHandler.DeleteInterfaceFromBridgeDomain(4, &l2.BridgeDomain_Interface{
   138  		Name:              "if1",
   139  		SplitHorizonGroup: 12,
   140  	})
   141  
   142  	Expect(err).ToNot(BeNil())
   143  	Expect(ctx.MockChannel.Msgs).To(BeEmpty())
   144  }