go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/puntplugin/vppcalls/vpp2106/punt_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  	vpp_ip "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2106/ip"
    24  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2106/ip_types"
    25  	vpp_punt "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2106/punt"
    26  	"go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/ifaceidx"
    27  	"go.ligato.io/vpp-agent/v3/plugins/vpp/puntplugin/vppcalls"
    28  	vpp2106 "go.ligato.io/vpp-agent/v3/plugins/vpp/puntplugin/vppcalls/vpp2106"
    29  	"go.ligato.io/vpp-agent/v3/plugins/vpp/vppmock"
    30  	punt "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/punt"
    31  )
    32  
    33  // TODO test below temporary disabled (re-enable with set_punt)
    34  /*
    35  func TestAddPunt(t *testing.T) {
    36  	ctx, puntHandler, _ := puntTestSetup(t)
    37  	defer ctx.TeardownTestCtx()
    38  
    39  	ctx.MockVpp.MockReply(&ba_punt.SetPuntReply{})
    40  
    41  	err := puntHandler.AddPunt(&punt.ToHost{
    42  		L3Protocol: punt.L3Protocol_IPv4,
    43  		L4Protocol: punt.L4Protocol_UDP,
    44  		Port:       9000,
    45  	})
    46  
    47  	Expect(err).To(BeNil())
    48  	vppMsg, ok := ctx.MockChannel.Msg.(*ba_punt.SetPunt)
    49  	Expect(ok).To(BeTrue())
    50  	Expect(vppMsg.IsAdd).To(Equal(uint8(1)))
    51  	Expect(vppMsg.Punt.IPv).To(Equal(uint8(4)))
    52  	Expect(vppMsg.Punt.L4Protocol).To(Equal(uint8(17)))
    53  	Expect(vppMsg.Punt.L4Port).To(Equal(uint16(9000)))
    54  }
    55  
    56  func TestDeletePunt(t *testing.T) {
    57  	ctx, puntHandler, _ := puntTestSetup(t)
    58  	defer ctx.TeardownTestCtx()
    59  
    60  	ctx.MockVpp.MockReply(&ba_punt.SetPuntReply{})
    61  
    62  	err := puntHandler.DeletePunt(&punt.ToHost{
    63  		L3Protocol: punt.L3Protocol_IPv4,
    64  		L4Protocol: punt.L4Protocol_UDP,
    65  		Port:       9000,
    66  	})
    67  
    68  	Expect(err).To(BeNil())
    69  	vppMsg, ok := ctx.MockChannel.Msg.(*ba_punt.SetPunt)
    70  	Expect(ok).To(BeTrue())
    71  	Expect(vppMsg.IsAdd).To(Equal(uint8(0)))
    72  	Expect(vppMsg.Punt.IPv).To(Equal(uint8(4)))
    73  	Expect(vppMsg.Punt.L4Protocol).To(Equal(uint8(17)))
    74  	Expect(vppMsg.Punt.L4Port).To(Equal(uint16(9000)))
    75  }
    76  */
    77  
    78  func TestRegisterPuntSocket(t *testing.T) {
    79  	ctx, puntHandler, _ := puntTestSetup(t)
    80  	defer ctx.TeardownTestCtx()
    81  
    82  	ctx.MockVpp.MockReply(&vpp_punt.PuntSocketRegisterReply{
    83  		Pathname: "/othersock",
    84  	})
    85  
    86  	path, err := puntHandler.RegisterPuntSocket(&punt.ToHost{
    87  		L3Protocol: punt.L3Protocol_IPV4,
    88  		L4Protocol: punt.L4Protocol_UDP,
    89  		Port:       9000,
    90  		SocketPath: "/test/path/socket",
    91  	})
    92  
    93  	Expect(err).To(BeNil())
    94  	vppMsg, ok := ctx.MockChannel.Msg.(*vpp_punt.PuntSocketRegister)
    95  	Expect(ok).To(BeTrue())
    96  	Expect(vppMsg.HeaderVersion).To(Equal(uint32(1)))
    97  	Expect(vppMsg.Punt.Punt.GetL4().Af).To(Equal(ip_types.ADDRESS_IP4))
    98  	Expect(vppMsg.Punt.Punt.GetL4().Protocol).To(Equal(ip_types.IP_API_PROTO_UDP))
    99  	Expect(vppMsg.Punt.Punt.GetL4().Port).To(Equal(uint16(9000)))
   100  	Expect(path).To(Equal("/othersock"))
   101  }
   102  
   103  func TestRegisterPuntSocketAllIPv4(t *testing.T) {
   104  	ctx, puntHandler, _ := puntTestSetup(t)
   105  	defer ctx.TeardownTestCtx()
   106  
   107  	ctx.MockVpp.MockReply(&vpp_punt.PuntSocketRegisterReply{
   108  		Pathname: "/othersock",
   109  	})
   110  	ctx.MockVpp.MockReply(&vpp_punt.PuntSocketRegisterReply{
   111  		Pathname: "/othersock",
   112  	})
   113  
   114  	path, err := puntHandler.RegisterPuntSocket(&punt.ToHost{
   115  		L3Protocol: punt.L3Protocol_ALL,
   116  		L4Protocol: punt.L4Protocol_UDP,
   117  		Port:       9000,
   118  		SocketPath: "/test/path/socket",
   119  	})
   120  
   121  	Expect(err).To(BeNil())
   122  	for _, msg := range ctx.MockChannel.Msgs {
   123  		vppMsg, ok := msg.(*vpp_punt.PuntSocketRegister)
   124  		Expect(ok).To(BeTrue())
   125  
   126  		if vppMsg.Punt.Punt.GetL4().Af == ip_types.ADDRESS_IP4 {
   127  			Expect(vppMsg.HeaderVersion).To(Equal(uint32(1)))
   128  			Expect(vppMsg.Punt.Punt.GetL4().Protocol).To(Equal(ip_types.IP_API_PROTO_UDP))
   129  			Expect(vppMsg.Punt.Punt.GetL4().Port).To(Equal(uint16(9000)))
   130  			Expect(path).To(Equal("/othersock"))
   131  		}
   132  		if vppMsg.Punt.Punt.GetL4().Af == ip_types.ADDRESS_IP6 {
   133  			Expect(vppMsg.HeaderVersion).To(Equal(uint32(1)))
   134  			Expect(vppMsg.Punt.Punt.GetL4().Protocol).To(Equal(ip_types.IP_API_PROTO_UDP))
   135  			Expect(vppMsg.Punt.Punt.GetL4().Port).To(Equal(uint16(9000)))
   136  			Expect(path).To(Equal("/othersock"))
   137  		}
   138  	}
   139  }
   140  
   141  func TestAddIPRedirect(t *testing.T) {
   142  	ctx, puntHandler, ifIndexes := puntTestSetup(t)
   143  	defer ctx.TeardownTestCtx()
   144  
   145  	ctx.MockVpp.MockReply(&vpp_ip.IPPuntRedirectReply{})
   146  
   147  	ifIndexes.Put("if1", &ifaceidx.IfaceMetadata{SwIfIndex: 1})
   148  	ifIndexes.Put("if2", &ifaceidx.IfaceMetadata{SwIfIndex: 2})
   149  
   150  	err := puntHandler.AddPuntRedirect(&punt.IPRedirect{
   151  		L3Protocol:  punt.L3Protocol_IPV4,
   152  		RxInterface: "if1",
   153  		TxInterface: "if2",
   154  		NextHop:     "10.0.0.1",
   155  	})
   156  
   157  	Expect(err).To(BeNil())
   158  	vppMsg, ok := ctx.MockChannel.Msg.(*vpp_ip.IPPuntRedirect)
   159  	Expect(ok).To(BeTrue())
   160  	Expect(vppMsg.IsAdd).To(BeTrue())
   161  	Expect(vppMsg.Punt.Nh.Af).To(Equal(ip_types.ADDRESS_IP4))
   162  	Expect(vppMsg.Punt.RxSwIfIndex).To(BeEquivalentTo(uint32(1)))
   163  	Expect(vppMsg.Punt.TxSwIfIndex).To(BeEquivalentTo(uint32(2)))
   164  	//Expect(vppMsg.Nh).To(Equal([]uint8(net.ParseIP("10.0.0.1").To4())))
   165  }
   166  
   167  func TestAddIPRedirectAll(t *testing.T) {
   168  	ctx, puntHandler, ifIndexes := puntTestSetup(t)
   169  	defer ctx.TeardownTestCtx()
   170  
   171  	ctx.MockVpp.MockReply(&vpp_ip.IPPuntRedirectReply{})
   172  
   173  	ifIndexes.Put("if1", &ifaceidx.IfaceMetadata{SwIfIndex: 1})
   174  
   175  	err := puntHandler.AddPuntRedirect(&punt.IPRedirect{
   176  		L3Protocol:  punt.L3Protocol_IPV4,
   177  		TxInterface: "if1",
   178  		NextHop:     "30.0.0.1",
   179  	})
   180  
   181  	Expect(err).To(BeNil())
   182  	vppMsg, ok := ctx.MockChannel.Msg.(*vpp_ip.IPPuntRedirect)
   183  	Expect(ok).To(BeTrue())
   184  	Expect(vppMsg.IsAdd).To(BeTrue())
   185  	Expect(vppMsg.Punt.Nh.Af).To(Equal(ip_types.ADDRESS_IP4))
   186  	Expect(vppMsg.Punt.RxSwIfIndex).To(BeEquivalentTo(^uint32(0)))
   187  	Expect(vppMsg.Punt.TxSwIfIndex).To(BeEquivalentTo(uint32(1)))
   188  	//Expect(vppMsg.Nh).To(Equal([]uint8(net.ParseIP("30.0.0.1").To4())))
   189  }
   190  
   191  func TestDeleteIPRedirect(t *testing.T) {
   192  	ctx, puntHandler, ifIndexes := puntTestSetup(t)
   193  	defer ctx.TeardownTestCtx()
   194  
   195  	ctx.MockVpp.MockReply(&vpp_ip.IPPuntRedirectReply{})
   196  
   197  	ifIndexes.Put("if1", &ifaceidx.IfaceMetadata{SwIfIndex: 1})
   198  	ifIndexes.Put("if2", &ifaceidx.IfaceMetadata{SwIfIndex: 2})
   199  
   200  	err := puntHandler.DeletePuntRedirect(&punt.IPRedirect{
   201  		L3Protocol:  punt.L3Protocol_IPV4,
   202  		RxInterface: "if1",
   203  		TxInterface: "if2",
   204  		NextHop:     "10.0.0.1",
   205  	})
   206  
   207  	Expect(err).To(BeNil())
   208  	vppMsg, ok := ctx.MockChannel.Msg.(*vpp_ip.IPPuntRedirect)
   209  	Expect(ok).To(BeTrue())
   210  	Expect(vppMsg.IsAdd).To(BeFalse())
   211  	//Expect(vppMsg.IsIP6).To(Equal(uint8(0)))
   212  	Expect(vppMsg.Punt.Nh.Af).To(Equal(ip_types.ADDRESS_IP4))
   213  	Expect(vppMsg.Punt.RxSwIfIndex).To(BeEquivalentTo(uint32(1)))
   214  	Expect(vppMsg.Punt.TxSwIfIndex).To(BeEquivalentTo(uint32(2)))
   215  	//Expect(vppMsg.Nh).To(Equal([]uint8(net.ParseIP("10.0.0.1").To4())))
   216  }
   217  
   218  func puntTestSetup(t *testing.T) (*vppmock.TestCtx, vppcalls.PuntVppAPI, ifaceidx.IfaceMetadataIndexRW) {
   219  	ctx := vppmock.SetupTestCtx(t)
   220  	logger := logrus.NewLogger("test-log")
   221  	ifIndexes := ifaceidx.NewIfaceIndex(logger, "punt-if-idx")
   222  	puntHandler := vpp2106.NewPuntVppHandler(ctx.MockChannel, ifIndexes, logrus.DefaultLogger())
   223  	return ctx, puntHandler, ifIndexes
   224  }