go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/ifplugin/vppcalls/vpp2202/admin_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  	"path"
    19  	"reflect"
    20  	"testing"
    21  
    22  	. "github.com/onsi/gomega"
    23  	"go.fd.io/govpp/api"
    24  	"go.fd.io/govpp/core"
    25  	"go.ligato.io/cn-infra/v2/logging/logrus"
    26  
    27  	vpp_ifs "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2202/interface"
    28  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2202/interface_types"
    29  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2202/memclnt"
    30  	"go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/vppcalls"
    31  	vpp2202 "go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/vppcalls/vpp2202"
    32  	"go.ligato.io/vpp-agent/v3/plugins/vpp/vppmock"
    33  )
    34  
    35  func TestInterfaceAdminDown(t *testing.T) {
    36  	ctx, ifHandler := ifTestSetup(t)
    37  	defer ctx.TeardownTestCtx()
    38  
    39  	ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceSetFlagsReply{})
    40  	err := ifHandler.InterfaceAdminDown(ctx.Context, 1)
    41  
    42  	Expect(err).To(BeNil())
    43  	vppMsg, ok := ctx.MockChannel.Msg.(*vpp_ifs.SwInterfaceSetFlags)
    44  	Expect(ok).To(BeTrue())
    45  	Expect(vppMsg).NotTo(BeNil())
    46  	Expect(vppMsg.SwIfIndex).To(Equal(interface_types.InterfaceIndex(1)))
    47  	Expect(vppMsg.Flags).To(Equal(interface_types.IfStatusFlags(0)))
    48  }
    49  
    50  func TestInterfaceAdminDownError(t *testing.T) {
    51  	ctx, ifHandler := ifTestSetup(t)
    52  	defer ctx.TeardownTestCtx()
    53  
    54  	ctx.MockVpp.MockReply(&vpp_ifs.HwInterfaceSetMtuReply{})
    55  	err := ifHandler.InterfaceAdminDown(ctx.Context, 1)
    56  
    57  	Expect(err).ToNot(BeNil())
    58  }
    59  
    60  func TestInterfaceAdminDownRetval(t *testing.T) {
    61  	ctx, ifHandler := ifTestSetup(t)
    62  	defer ctx.TeardownTestCtx()
    63  
    64  	ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceSetFlagsReply{
    65  		Retval: 1,
    66  	})
    67  	err := ifHandler.InterfaceAdminDown(ctx.Context, 1)
    68  
    69  	Expect(err).ToNot(BeNil())
    70  }
    71  
    72  func TestInterfaceAdminUp(t *testing.T) {
    73  	ctx, ifHandler := ifTestSetup(t)
    74  	defer ctx.TeardownTestCtx()
    75  
    76  	ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceSetFlagsReply{})
    77  	err := ifHandler.InterfaceAdminUp(ctx.Context, 1)
    78  
    79  	Expect(err).ShouldNot(HaveOccurred())
    80  	vppMsg, ok := ctx.MockChannel.Msg.(*vpp_ifs.SwInterfaceSetFlags)
    81  	Expect(ok).To(BeTrue())
    82  	Expect(vppMsg).NotTo(BeNil())
    83  	Expect(vppMsg.SwIfIndex).To(Equal(interface_types.InterfaceIndex(1)))
    84  	Expect(vppMsg.Flags).To(Equal(interface_types.IF_STATUS_API_FLAG_ADMIN_UP))
    85  }
    86  
    87  func TestInterfaceAdminUpError(t *testing.T) {
    88  	ctx, ifHandler := ifTestSetup(t)
    89  	defer ctx.TeardownTestCtx()
    90  
    91  	ctx.MockVpp.MockReply(&vpp_ifs.HwInterfaceSetMtuReply{})
    92  	err := ifHandler.InterfaceAdminDown(ctx.Context, 1)
    93  
    94  	Expect(err).ToNot(BeNil())
    95  }
    96  
    97  func TestInterfaceAdminUpRetval(t *testing.T) {
    98  	ctx, ifHandler := ifTestSetup(t)
    99  	defer ctx.TeardownTestCtx()
   100  
   101  	ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceSetFlagsReply{
   102  		Retval: 1,
   103  	})
   104  	err := ifHandler.InterfaceAdminDown(ctx.Context, 1)
   105  
   106  	Expect(err).ToNot(BeNil())
   107  }
   108  
   109  func TestInterfaceSetTag(t *testing.T) {
   110  	ctx, ifHandler := ifTestSetup(t)
   111  	defer ctx.TeardownTestCtx()
   112  
   113  	ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceTagAddDelReply{})
   114  	err := ifHandler.SetInterfaceTag("tag", 1)
   115  
   116  	Expect(err).To(BeNil())
   117  	vppMsg, ok := ctx.MockChannel.Msg.(*vpp_ifs.SwInterfaceTagAddDel)
   118  	Expect(ok).To(BeTrue())
   119  	Expect(vppMsg).NotTo(BeNil())
   120  	Expect(vppMsg.Tag).To(BeEquivalentTo("tag"))
   121  	Expect(vppMsg.SwIfIndex).To(Equal(interface_types.InterfaceIndex(1)))
   122  	Expect(vppMsg.IsAdd).To(BeTrue())
   123  }
   124  
   125  func TestInterfaceSetTagError(t *testing.T) {
   126  	ctx, ifHandler := ifTestSetup(t)
   127  	defer ctx.TeardownTestCtx()
   128  
   129  	ctx.MockVpp.MockReply(&vpp_ifs.HwInterfaceSetMtuReply{})
   130  	err := ifHandler.SetInterfaceTag("tag", 1)
   131  
   132  	Expect(err).ToNot(BeNil())
   133  }
   134  
   135  func TestInterfaceSetTagRetval(t *testing.T) {
   136  	ctx, ifHandler := ifTestSetup(t)
   137  	defer ctx.TeardownTestCtx()
   138  
   139  	ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceTagAddDelReply{
   140  		Retval: 1,
   141  	})
   142  	err := ifHandler.SetInterfaceTag("tag", 1)
   143  
   144  	Expect(err).ToNot(BeNil())
   145  }
   146  
   147  func TestInterfaceRemoveTag(t *testing.T) {
   148  	ctx, ifHandler := ifTestSetup(t)
   149  	defer ctx.TeardownTestCtx()
   150  
   151  	ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceTagAddDelReply{})
   152  	err := ifHandler.RemoveInterfaceTag("tag", 1)
   153  
   154  	Expect(err).To(BeNil())
   155  	vppMsg, ok := ctx.MockChannel.Msg.(*vpp_ifs.SwInterfaceTagAddDel)
   156  	Expect(ok).To(BeTrue())
   157  	Expect(vppMsg).NotTo(BeNil())
   158  	Expect(vppMsg.Tag).To(BeEquivalentTo("tag"))
   159  	Expect(vppMsg.IsAdd).To(BeFalse())
   160  }
   161  
   162  func TestInterfaceRemoveTagError(t *testing.T) {
   163  	ctx, ifHandler := ifTestSetup(t)
   164  	defer ctx.TeardownTestCtx()
   165  
   166  	ctx.MockVpp.MockReply(&vpp_ifs.HwInterfaceSetMtuReply{})
   167  	err := ifHandler.RemoveInterfaceTag("tag", 1)
   168  
   169  	Expect(err).ToNot(BeNil())
   170  }
   171  
   172  func TestInterfaceRemoveTagRetval(t *testing.T) {
   173  	ctx, ifHandler := ifTestSetup(t)
   174  	defer ctx.TeardownTestCtx()
   175  
   176  	ctx.MockVpp.MockReply(&vpp_ifs.SwInterfaceTagAddDelReply{
   177  		Retval: 1,
   178  	})
   179  	err := ifHandler.RemoveInterfaceTag("tag", 1)
   180  
   181  	Expect(err).ToNot(BeNil())
   182  }
   183  
   184  // registeredMessageTypes = make(map[string]map[reflect.Type]string)
   185  
   186  func ifTestSetup(t *testing.T) (*vppmock.TestCtx, vppcalls.InterfaceVppAPI) {
   187  	// FIXME: this control pings below are hacked to avoid issues in tests
   188  	// that do not properly handle all replies, affecting tests that run after
   189  	// causing failures, because of unexpected ControlPingReply type from core
   190  	controlPingMsg := &memclnt.ControlPingReply{}
   191  	binapiPath := path.Dir(reflect.TypeOf(controlPingMsg).Elem().PkgPath())
   192  	api.GetRegisteredMessages()[binapiPath]["control_ping_reply"] = controlPingMsg
   193  	api.GetRegisteredMessages()[binapiPath]["control_ping_reply_f6b0b8ca"] = controlPingMsg
   194  	core.SetControlPingReply(controlPingMsg)
   195  	ctx := vppmock.SetupTestCtx(t)
   196  	core.SetControlPingReply(controlPingMsg)
   197  	ctx.PingReplyMsg = controlPingMsg
   198  	log := logrus.NewLogger("test-log")
   199  	ifHandler := vpp2202.NewInterfaceVppHandler(ctx.MockVPPClient, log)
   200  	return ctx, ifHandler
   201  }