go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/l2plugin/vppcalls/vpp2101/bridge_domain_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  	"go.ligato.io/cn-infra/v2/logging/logrus"
    22  
    23  	vpp_l2 "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2101/l2"
    24  	"go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/ifaceidx"
    25  	"go.ligato.io/vpp-agent/v3/plugins/vpp/l2plugin/vppcalls"
    26  	vpp2101 "go.ligato.io/vpp-agent/v3/plugins/vpp/l2plugin/vppcalls/vpp2101"
    27  	"go.ligato.io/vpp-agent/v3/plugins/vpp/vppmock"
    28  	l2 "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/l2"
    29  )
    30  
    31  const (
    32  	dummyBridgeDomain     = 4
    33  	dummyBridgeDomainName = "bridge_domain"
    34  )
    35  
    36  // Input test data for creating bridge domain
    37  var createTestDataInBD = &l2.BridgeDomain{
    38  	Name:                dummyBridgeDomainName,
    39  	Flood:               true,
    40  	UnknownUnicastFlood: true,
    41  	Forward:             true,
    42  	Learn:               true,
    43  	ArpTermination:      true,
    44  	MacAge:              45,
    45  }
    46  
    47  // Output test data for creating bridge domain
    48  var createTestDataOutBD = &vpp_l2.BridgeDomainAddDel{
    49  	BdID:    dummyBridgeDomain,
    50  	Flood:   true,
    51  	UuFlood: true,
    52  	Forward: true,
    53  	Learn:   true,
    54  	ArpTerm: true,
    55  	MacAge:  45,
    56  	BdTag:   dummyBridgeDomainName,
    57  	IsAdd:   true,
    58  }
    59  
    60  // Output test data for deleting bridge domain
    61  var deleteTestDataOutBd = &vpp_l2.BridgeDomainAddDel{
    62  	BdID:  dummyBridgeDomain,
    63  	IsAdd: false,
    64  }
    65  
    66  func TestVppAddBridgeDomain(t *testing.T) {
    67  	ctx, bdHandler, _ := bdTestSetup(t)
    68  	defer ctx.TeardownTestCtx()
    69  
    70  	ctx.MockVpp.MockReply(&vpp_l2.BridgeDomainAddDelReply{})
    71  	err := bdHandler.AddBridgeDomain(dummyBridgeDomain, createTestDataInBD)
    72  
    73  	Expect(err).ShouldNot(HaveOccurred())
    74  	Expect(ctx.MockChannel.Msg).To(Equal(createTestDataOutBD))
    75  }
    76  
    77  func TestVppAddBridgeDomainError(t *testing.T) {
    78  	ctx, bdHandler, _ := bdTestSetup(t)
    79  	defer ctx.TeardownTestCtx()
    80  
    81  	ctx.MockVpp.MockReply(&vpp_l2.BridgeDomainAddDelReply{Retval: 1})
    82  	ctx.MockVpp.MockReply(&vpp_l2.SwInterfaceSetL2Bridge{})
    83  
    84  	err := bdHandler.AddBridgeDomain(dummyBridgeDomain, createTestDataInBD)
    85  	Expect(err).Should(HaveOccurred())
    86  
    87  	err = bdHandler.AddBridgeDomain(dummyBridgeDomain, createTestDataInBD)
    88  	Expect(err).Should(HaveOccurred())
    89  }
    90  
    91  func TestVppDeleteBridgeDomain(t *testing.T) {
    92  	ctx, bdHandler, _ := bdTestSetup(t)
    93  	defer ctx.TeardownTestCtx()
    94  
    95  	ctx.MockVpp.MockReply(&vpp_l2.BridgeDomainAddDelReply{})
    96  	err := bdHandler.DeleteBridgeDomain(dummyBridgeDomain)
    97  
    98  	Expect(err).ShouldNot(HaveOccurred())
    99  	Expect(ctx.MockChannel.Msg).To(Equal(deleteTestDataOutBd))
   100  }
   101  
   102  func TestVppDeleteBridgeDomainError(t *testing.T) {
   103  	ctx, bdHandler, _ := bdTestSetup(t)
   104  	defer ctx.TeardownTestCtx()
   105  
   106  	ctx.MockVpp.MockReply(&vpp_l2.BridgeDomainAddDelReply{Retval: 1})
   107  	ctx.MockVpp.MockReply(&vpp_l2.SwInterfaceSetL2Bridge{})
   108  
   109  	err := bdHandler.DeleteBridgeDomain(dummyBridgeDomain)
   110  	Expect(err).Should(HaveOccurred())
   111  
   112  	err = bdHandler.DeleteBridgeDomain(dummyBridgeDomain)
   113  	Expect(err).Should(HaveOccurred())
   114  }
   115  
   116  func bdTestSetup(t *testing.T) (*vppmock.TestCtx, vppcalls.BridgeDomainVppAPI, ifaceidx.IfaceMetadataIndexRW) {
   117  	ctx := vppmock.SetupTestCtx(t)
   118  	log := logrus.NewLogger("test-log")
   119  	ifIndex := ifaceidx.NewIfaceIndex(log, "bd-test-ifidx")
   120  	bdHandler := vpp2101.NewL2VppHandler(ctx.MockChannel, ifIndex, nil, log)
   121  	return ctx, bdHandler, ifIndex
   122  }