go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/ifplugin/vppcalls/vpp2202/ip_container_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  	"fmt"
    19  	"testing"
    20  
    21  	. "github.com/onsi/gomega"
    22  
    23  	vpp_ip "go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2202/ip"
    24  	"go.ligato.io/vpp-agent/v3/plugins/vpp/binapi/vpp2202/ip_types"
    25  	"go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/vppcalls/vpp2202"
    26  )
    27  
    28  func ipToAddr(ip string) ip_types.Address {
    29  	addr, err := vpp2202.IPToAddress(ip)
    30  	if err != nil {
    31  		panic(fmt.Sprintf("invalid IP: %s", ip))
    32  	}
    33  	return addr
    34  }
    35  
    36  func TestAddContainerIP(t *testing.T) {
    37  	ctx, ifHandler := ifTestSetup(t)
    38  	defer ctx.TeardownTestCtx()
    39  
    40  	ctx.MockVpp.MockReply(&vpp_ip.IPContainerProxyAddDelReply{})
    41  
    42  	err := ifHandler.AddContainerIP(1, "10.0.0.1/24")
    43  
    44  	Expect(err).To(BeNil())
    45  	vppMsg, ok := ctx.MockChannel.Msg.(*vpp_ip.IPContainerProxyAddDel)
    46  	Expect(ok).To(BeTrue())
    47  	Expect(vppMsg.SwIfIndex).To(BeEquivalentTo(1))
    48  	Expect(vppMsg.Pfx).To(BeEquivalentTo(ip_types.Prefix{
    49  		Address: ipToAddr("10.0.0.1"),
    50  		Len:     24,
    51  	}))
    52  	Expect(vppMsg.IsAdd).To(BeTrue())
    53  }
    54  
    55  func TestAddContainerIPv6(t *testing.T) {
    56  	ctx, ifHandler := ifTestSetup(t)
    57  	defer ctx.TeardownTestCtx()
    58  
    59  	ctx.MockVpp.MockReply(&vpp_ip.IPContainerProxyAddDelReply{})
    60  
    61  	err := ifHandler.AddContainerIP(1, "2001:db8:0:1:1:1:1:1/128")
    62  
    63  	Expect(err).To(BeNil())
    64  	vppMsg, ok := ctx.MockChannel.Msg.(*vpp_ip.IPContainerProxyAddDel)
    65  	Expect(ok).To(BeTrue())
    66  	Expect(vppMsg.SwIfIndex).To(BeEquivalentTo(1))
    67  	Expect(vppMsg.Pfx).To(BeEquivalentTo(ip_types.Prefix{
    68  		Address: ipToAddr("2001:db8:0:1:1:1:1:1"),
    69  		Len:     128,
    70  	}))
    71  	Expect(vppMsg.IsAdd).To(BeTrue())
    72  }
    73  
    74  func TestAddContainerIPInvalidIP(t *testing.T) {
    75  	ctx, ifHandler := ifTestSetup(t)
    76  	defer ctx.TeardownTestCtx()
    77  
    78  	ctx.MockVpp.MockReply(&vpp_ip.IPAddressDetails{})
    79  
    80  	err := ifHandler.AddContainerIP(1, "invalid-ip")
    81  
    82  	Expect(err).ToNot(BeNil())
    83  }
    84  
    85  func TestAddContainerIPError(t *testing.T) {
    86  	ctx, ifHandler := ifTestSetup(t)
    87  	defer ctx.TeardownTestCtx()
    88  
    89  	ctx.MockVpp.MockReply(&vpp_ip.IPAddressDetails{})
    90  
    91  	err := ifHandler.AddContainerIP(1, "10.0.0.1/24")
    92  
    93  	Expect(err).ToNot(BeNil())
    94  }
    95  
    96  func TestAddContainerIPRetval(t *testing.T) {
    97  	ctx, ifHandler := ifTestSetup(t)
    98  	defer ctx.TeardownTestCtx()
    99  
   100  	ctx.MockVpp.MockReply(&vpp_ip.IPContainerProxyAddDelReply{
   101  		Retval: 1,
   102  	})
   103  
   104  	err := ifHandler.AddContainerIP(1, "10.0.0.1/24")
   105  
   106  	Expect(err).ToNot(BeNil())
   107  }
   108  
   109  func TestDelContainerIP(t *testing.T) {
   110  	ctx, ifHandler := ifTestSetup(t)
   111  	defer ctx.TeardownTestCtx()
   112  
   113  	ctx.MockVpp.MockReply(&vpp_ip.IPContainerProxyAddDelReply{})
   114  
   115  	err := ifHandler.DelContainerIP(1, "10.0.0.1/24")
   116  
   117  	Expect(err).To(BeNil())
   118  	vppMsg, ok := ctx.MockChannel.Msg.(*vpp_ip.IPContainerProxyAddDel)
   119  	Expect(ok).To(BeTrue())
   120  	Expect(vppMsg.SwIfIndex).To(BeEquivalentTo(1))
   121  	Expect(vppMsg.Pfx).To(BeEquivalentTo(ip_types.Prefix{
   122  		Address: ipToAddr("10.0.0.1"),
   123  		Len:     24,
   124  	}))
   125  	Expect(vppMsg.IsAdd).To(BeFalse())
   126  }
   127  
   128  func TestDelContainerIPv6(t *testing.T) {
   129  	ctx, ifHandler := ifTestSetup(t)
   130  	defer ctx.TeardownTestCtx()
   131  
   132  	ctx.MockVpp.MockReply(&vpp_ip.IPContainerProxyAddDelReply{})
   133  
   134  	err := ifHandler.DelContainerIP(1, "2001:db8:0:1:1:1:1:1/128")
   135  
   136  	Expect(err).To(BeNil())
   137  	vppMsg, ok := ctx.MockChannel.Msg.(*vpp_ip.IPContainerProxyAddDel)
   138  	Expect(ok).To(BeTrue())
   139  	Expect(vppMsg.SwIfIndex).To(BeEquivalentTo(1))
   140  	Expect(vppMsg.Pfx).To(BeEquivalentTo(ip_types.Prefix{
   141  		Address: ipToAddr("2001:db8:0:1:1:1:1:1"),
   142  		Len:     128,
   143  	}))
   144  	Expect(vppMsg.IsAdd).To(BeFalse())
   145  }
   146  
   147  func TestDelContainerIPError(t *testing.T) {
   148  	ctx, ifHandler := ifTestSetup(t)
   149  	defer ctx.TeardownTestCtx()
   150  
   151  	ctx.MockVpp.MockReply(&vpp_ip.IPAddressDetails{})
   152  
   153  	err := ifHandler.DelContainerIP(1, "10.0.0.1/24")
   154  
   155  	Expect(err).ToNot(BeNil())
   156  }
   157  
   158  func TestDelContainerIPRetval(t *testing.T) {
   159  	ctx, ifHandler := ifTestSetup(t)
   160  	defer ctx.TeardownTestCtx()
   161  
   162  	ctx.MockVpp.MockReply(&vpp_ip.IPContainerProxyAddDelReply{
   163  		Retval: 1,
   164  	})
   165  
   166  	err := ifHandler.DelContainerIP(1, "10.0.0.1/24")
   167  
   168  	Expect(err).ToNot(BeNil())
   169  }