go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/stnplugin/vppcalls/vpp2106/stn_vppcalls_test.go (about)

     1  //  Copyright (c) 2021 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 vpp2106_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/onsi/gomega"
    21  	"go.ligato.io/cn-infra/v2/logging/logrus"
    22  
    23  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2106/ip_types"
    24  	vpp_stn "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2106/stn"
    25  	"go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/ifaceidx"
    26  	"go.ligato.io/vpp-agent/v3/plugins/vpp/stnplugin/vppcalls"
    27  	vpp2106 "go.ligato.io/vpp-agent/v3/plugins/vpp/stnplugin/vppcalls/vpp2106"
    28  	"go.ligato.io/vpp-agent/v3/plugins/vpp/vppmock"
    29  	stn "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/stn"
    30  )
    31  
    32  func TestAddStnRule(t *testing.T) {
    33  	ctx, stnHandler, ifIndexes := stnTestSetup(t)
    34  	defer ctx.TeardownTestCtx()
    35  
    36  	ifIndexes.Put("if1", &ifaceidx.IfaceMetadata{SwIfIndex: 1})
    37  
    38  	ctx.MockVpp.MockReply(&vpp_stn.StnAddDelRuleReply{})
    39  
    40  	err := stnHandler.AddSTNRule(&stn.Rule{
    41  		Interface: "if1",
    42  		IpAddress: "10.0.0.1",
    43  	})
    44  
    45  	Expect(err).To(BeNil())
    46  	vppMsg, ok := ctx.MockChannel.Msg.(*vpp_stn.StnAddDelRule)
    47  	Expect(ok).To(BeTrue())
    48  	Expect(vppMsg.SwIfIndex).To(BeEquivalentTo(1))
    49  	Expect(vppMsg.IPAddress.Un.GetIP4()).To(BeEquivalentTo(ip_types.IP4Address{10, 0, 0, 1}))
    50  	Expect(vppMsg.IPAddress.Af).To(Equal(ip_types.ADDRESS_IP4))
    51  	Expect(vppMsg.IsAdd).To(BeTrue())
    52  }
    53  
    54  func TestAddStnRuleIPv6(t *testing.T) {
    55  	ctx, stnHandler, ifIndexes := stnTestSetup(t)
    56  	defer ctx.TeardownTestCtx()
    57  
    58  	ifIndexes.Put("if1", &ifaceidx.IfaceMetadata{SwIfIndex: 1})
    59  
    60  	ctx.MockVpp.MockReply(&vpp_stn.StnAddDelRuleReply{})
    61  
    62  	err := stnHandler.AddSTNRule(&stn.Rule{
    63  		Interface: "if1",
    64  		IpAddress: "2001:db8:0:1:1:1:1:1",
    65  	})
    66  
    67  	Expect(err).To(BeNil())
    68  	vppMsg, ok := ctx.MockChannel.Msg.(*vpp_stn.StnAddDelRule)
    69  	Expect(ok).To(BeTrue())
    70  	Expect(vppMsg.SwIfIndex).To(BeEquivalentTo(1))
    71  	Expect(vppMsg.IPAddress.Un.GetIP6()).To(BeEquivalentTo(ip_types.IP6Address{32, 1, 13, 184, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1}))
    72  	Expect(vppMsg.IPAddress.Af).To(Equal(ip_types.ADDRESS_IP6))
    73  	Expect(vppMsg.IsAdd).To(BeTrue())
    74  }
    75  
    76  func TestAddStnRuleInvalidIP(t *testing.T) {
    77  	ctx, stnHandler, ifIndexes := stnTestSetup(t)
    78  	defer ctx.TeardownTestCtx()
    79  
    80  	ifIndexes.Put("if1", &ifaceidx.IfaceMetadata{SwIfIndex: 1})
    81  
    82  	ctx.MockVpp.MockReply(&vpp_stn.StnAddDelRuleReply{})
    83  
    84  	err := stnHandler.AddSTNRule(&stn.Rule{
    85  		Interface: "if1",
    86  		IpAddress: "invalid-ip",
    87  	})
    88  
    89  	Expect(err).ToNot(BeNil())
    90  }
    91  
    92  func TestAddStnRuleError(t *testing.T) {
    93  	ctx, stnHandler, ifIndexes := stnTestSetup(t)
    94  	defer ctx.TeardownTestCtx()
    95  
    96  	ifIndexes.Put("if1", &ifaceidx.IfaceMetadata{SwIfIndex: 1})
    97  
    98  	ctx.MockVpp.MockReply(&vpp_stn.StnAddDelRule{})
    99  
   100  	err := stnHandler.AddSTNRule(&stn.Rule{
   101  		Interface: "if1",
   102  		IpAddress: "10.0.0.1",
   103  	})
   104  
   105  	Expect(err).ToNot(BeNil())
   106  }
   107  
   108  func TestAddStnRuleRetval(t *testing.T) {
   109  	ctx, stnHandler, ifIndexes := stnTestSetup(t)
   110  	defer ctx.TeardownTestCtx()
   111  
   112  	ifIndexes.Put("if1", &ifaceidx.IfaceMetadata{SwIfIndex: 1})
   113  
   114  	ctx.MockVpp.MockReply(&vpp_stn.StnAddDelRuleReply{
   115  		Retval: 1,
   116  	})
   117  
   118  	err := stnHandler.AddSTNRule(&stn.Rule{
   119  		Interface: "if1",
   120  		IpAddress: "10.0.0.1",
   121  	})
   122  
   123  	Expect(err).ToNot(BeNil())
   124  }
   125  
   126  func TestDelStnRule(t *testing.T) {
   127  	ctx, stnHandler, ifIndexes := stnTestSetup(t)
   128  	defer ctx.TeardownTestCtx()
   129  
   130  	ifIndexes.Put("if1", &ifaceidx.IfaceMetadata{SwIfIndex: 1})
   131  
   132  	ctx.MockVpp.MockReply(&vpp_stn.StnAddDelRuleReply{})
   133  
   134  	err := stnHandler.DeleteSTNRule(&stn.Rule{
   135  		Interface: "if1",
   136  		IpAddress: "10.0.0.1",
   137  	})
   138  
   139  	Expect(err).To(BeNil())
   140  	vppMsg, ok := ctx.MockChannel.Msg.(*vpp_stn.StnAddDelRule)
   141  	Expect(ok).To(BeTrue())
   142  	Expect(vppMsg.IsAdd).To(BeFalse())
   143  }
   144  
   145  func stnTestSetup(t *testing.T) (*vppmock.TestCtx, vppcalls.StnVppAPI, ifaceidx.IfaceMetadataIndexRW) {
   146  	ctx := vppmock.SetupTestCtx(t)
   147  	logger := logrus.NewLogger("test-log")
   148  	ifIndexes := ifaceidx.NewIfaceIndex(logger, "stn-if-idx")
   149  	stnHandler := vpp2106.NewStnVppHandler(ctx.MockChannel, ifIndexes, logrus.DefaultLogger())
   150  	return ctx, stnHandler, ifIndexes
   151  }