go.ligato.io/vpp-agent/v3@v3.5.0/tests/integration/vpp/120_ipip_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 vpp
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"go.ligato.io/cn-infra/v2/logging/logrus"
    22  
    23  	ifplugin_vppcalls "go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/vppcalls"
    24  	interfaces "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/interfaces"
    25  
    26  	_ "go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin"
    27  )
    28  
    29  func TestIPIP(t *testing.T) {
    30  	ctx := setupVPP(t)
    31  	defer ctx.teardownVPP()
    32  
    33  	h := ifplugin_vppcalls.CompatibleInterfaceVppHandler(ctx.vppClient, logrus.NewLogger("test"))
    34  
    35  	tests := []struct {
    36  		name       string
    37  		ipip       *interfaces.IPIPLink
    38  		ipip2      *interfaces.IPIPLink
    39  		shouldFail bool
    40  	}{
    41  		{
    42  			name: "Create IPIP tunnel (IPv4)",
    43  			ipip: &interfaces.IPIPLink{
    44  				SrcAddr: "20.30.40.50",
    45  				DstAddr: "50.40.30.20",
    46  			},
    47  			shouldFail: false,
    48  		},
    49  		{
    50  			name: "Create IPIP tunnel (IPv6)",
    51  			ipip: &interfaces.IPIPLink{
    52  				SrcAddr: "2001:db8:0:1:1:1:1:1",
    53  				DstAddr: "2002:db8:0:1:1:1:1:1",
    54  			},
    55  			shouldFail: false,
    56  		},
    57  		{
    58  			name: "Create IPIP tunnel with same src and dst address",
    59  			ipip: &interfaces.IPIPLink{
    60  				SrcAddr: "20.30.40.50",
    61  				DstAddr: "20.30.40.50",
    62  			},
    63  			shouldFail: true,
    64  		},
    65  		{
    66  			name: "Create IPIP tunnel with missing src address",
    67  			ipip: &interfaces.IPIPLink{
    68  				DstAddr: "20.30.40.50",
    69  			},
    70  			shouldFail: true,
    71  		},
    72  		{
    73  			name: "Create p2p IPIP tunnel with missing dst address",
    74  			ipip: &interfaces.IPIPLink{
    75  				SrcAddr:    "20.30.40.50",
    76  				TunnelMode: interfaces.IPIPLink_POINT_TO_POINT,
    77  			},
    78  			shouldFail: true,
    79  		},
    80  		{
    81  			name: "Create p2mp IPIP tunnel (dst address not specified)",
    82  			ipip: &interfaces.IPIPLink{
    83  				SrcAddr:    "20.30.40.50",
    84  				TunnelMode: interfaces.IPIPLink_POINT_TO_MULTIPOINT,
    85  			},
    86  			shouldFail: false,
    87  		},
    88  		{
    89  			name: "Create 2 IPIP tunnels (IPv4)",
    90  			ipip: &interfaces.IPIPLink{
    91  				SrcAddr: "20.30.40.50",
    92  				DstAddr: "50.40.30.20",
    93  			},
    94  			ipip2: &interfaces.IPIPLink{
    95  				SrcAddr: "20.30.40.50",
    96  				DstAddr: "50.40.30.21",
    97  			},
    98  			shouldFail: false,
    99  		},
   100  		{
   101  			name: "Create 2 IPIP tunnels with same src & dst addresses (IPv4)",
   102  			ipip: &interfaces.IPIPLink{
   103  				SrcAddr: "20.30.40.50",
   104  				DstAddr: "50.40.30.20",
   105  			},
   106  			ipip2: &interfaces.IPIPLink{
   107  				SrcAddr: "20.30.40.50",
   108  				DstAddr: "50.40.30.20",
   109  			},
   110  			shouldFail: true,
   111  		},
   112  	}
   113  	for i, test := range tests {
   114  		t.Run(test.name, func(t *testing.T) {
   115  			ifName := fmt.Sprintf("ipip%d", i)
   116  			ifIdx, err := h.AddIpipTunnel(ifName, 0, test.ipip)
   117  
   118  			if err != nil {
   119  				if test.shouldFail {
   120  					return
   121  				}
   122  				t.Fatalf("create IPIP tunnel failed: %v\n", err)
   123  			} else {
   124  				if test.shouldFail && test.ipip2 == nil {
   125  					t.Fatal("create IPIP tunnel must fail, but it's not")
   126  				}
   127  			}
   128  
   129  			var (
   130  				ifName2 string
   131  				ifIdx2  uint32
   132  			)
   133  			if test.ipip2 != nil {
   134  				ifName2 := fmt.Sprintf("ipip%d-2", i)
   135  				ifIdx2, err = h.AddIpipTunnel(ifName2, 0, test.ipip2)
   136  
   137  				if err != nil {
   138  					if test.shouldFail {
   139  						return
   140  					}
   141  					t.Fatalf("create IPIP tunnel failed: %v\n", err)
   142  				} else {
   143  					if test.shouldFail {
   144  						t.Fatal("create IPIP tunnel must fail, but it's not")
   145  					}
   146  				}
   147  			}
   148  
   149  			ifaces, err := h.DumpInterfaces(ctx.Ctx)
   150  			if err != nil {
   151  				t.Fatalf("dumping interfaces failed: %v", err)
   152  			}
   153  			iface, ok := ifaces[ifIdx]
   154  			if !ok {
   155  				t.Fatalf("IPIP interface was not found in dump")
   156  			}
   157  			if test.ipip2 != nil {
   158  				_, ok := ifaces[ifIdx2]
   159  				if !ok {
   160  					t.Fatalf("IPIP interface2 was not found in dump")
   161  				}
   162  			}
   163  
   164  			if iface.Interface.GetType() != interfaces.Interface_IPIP_TUNNEL {
   165  				t.Fatalf("Interface is not an IPIP tunnel")
   166  			}
   167  
   168  			ipip := iface.Interface.GetIpip()
   169  			if test.ipip.TunnelMode != ipip.TunnelMode {
   170  				t.Fatalf("expected tunnel mode <%v>, got: <%v>", test.ipip.TunnelMode, ipip.TunnelMode)
   171  			}
   172  			if test.ipip.SrcAddr != ipip.SrcAddr {
   173  				t.Fatalf("expected source address <%s>, got: <%s>", test.ipip.SrcAddr, ipip.SrcAddr)
   174  			}
   175  			if test.ipip.DstAddr != ipip.DstAddr {
   176  				t.Fatalf("expected destination address <%s>, got: <%s>", test.ipip.DstAddr, ipip.DstAddr)
   177  			}
   178  
   179  			err = h.DelIpipTunnel(ifName, ifIdx)
   180  			if err != nil {
   181  				t.Fatalf("delete IPIP tunnel failed: %v\n", err)
   182  			}
   183  			if test.ipip2 != nil {
   184  				err = h.DelIpipTunnel(ifName2, ifIdx2)
   185  				if err != nil {
   186  					t.Fatalf("delete IPIP tunnel failed: %v\n", err)
   187  				}
   188  			}
   189  
   190  			ifaces, err = h.DumpInterfaces(ctx.Ctx)
   191  			if err != nil {
   192  				t.Fatalf("dumping interfaces failed: %v", err)
   193  			}
   194  
   195  			if _, ok := ifaces[ifIdx]; ok {
   196  				t.Fatalf("IPIP interface was found in dump after removing")
   197  			}
   198  			if test.ipip2 != nil {
   199  				if _, ok := ifaces[ifIdx2]; ok {
   200  					t.Fatalf("IPIP interface2 was found in dump after removing")
   201  				}
   202  			}
   203  		})
   204  	}
   205  }