go.ligato.io/vpp-agent/v3@v3.5.0/tests/e2e/130_dhcp_proxy_test.go (about)

     1  // Copyright (c) 2020 Pantheon.tech
     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 e2e
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"testing"
    21  
    22  	. "github.com/onsi/gomega"
    23  
    24  	"go.ligato.io/vpp-agent/v3/proto/ligato/kvscheduler"
    25  	linux_interfaces "go.ligato.io/vpp-agent/v3/proto/ligato/linux/interfaces"
    26  	linux_namespace "go.ligato.io/vpp-agent/v3/proto/ligato/linux/namespace"
    27  	vpp_interfaces "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/interfaces"
    28  	vpp_l3 "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/l3"
    29  	. "go.ligato.io/vpp-agent/v3/tests/e2e/e2etest"
    30  )
    31  
    32  func TestDhcpProxy(t *testing.T) {
    33  	if !SupportsLinuxVRF() {
    34  		t.Skip("Linux VRFs are not supported")
    35  	}
    36  
    37  	ctx := Setup(t)
    38  	defer ctx.Teardown()
    39  
    40  	const (
    41  		vrf1ID            = 1
    42  		vrf2ID            = 2
    43  		vrf1Label         = "vrf-1"
    44  		vrf2Label         = "vrf-2"
    45  		vppTap1Name       = "vpp-tap1"
    46  		vppTap2Name       = "vpp-tap2"
    47  		linuxTap1Name     = "linux-tap1"
    48  		linuxTap2Name     = "linux-tap2"
    49  		linuxTap1Hostname = "tap1"
    50  		linuxTap2Hostname = "tap2"
    51  		vppTapIP          = "192.168.1.1"
    52  		linuxTapIP        = "192.168.1.2"
    53  		netMask           = "/30"
    54  		msName            = "microservice1"
    55  	)
    56  
    57  	vppVrf1 := &vpp_l3.VrfTable{
    58  		Id:       vrf1ID,
    59  		Label:    vrf1Label,
    60  		Protocol: vpp_l3.VrfTable_IPV4,
    61  	}
    62  	vppVrf2 := &vpp_l3.VrfTable{
    63  		Id:       vrf2ID,
    64  		Label:    vrf2Label,
    65  		Protocol: vpp_l3.VrfTable_IPV4,
    66  	}
    67  	linuxVrf1 := &linux_interfaces.Interface{
    68  		Name:    vrf1Label,
    69  		Type:    linux_interfaces.Interface_VRF_DEVICE,
    70  		Enabled: true,
    71  		Link: &linux_interfaces.Interface_VrfDev{
    72  			VrfDev: &linux_interfaces.VrfDevLink{
    73  				RoutingTable: vrf1ID,
    74  			},
    75  		},
    76  		Namespace: &linux_namespace.NetNamespace{
    77  			Type:      linux_namespace.NetNamespace_MICROSERVICE,
    78  			Reference: MsNamePrefix + msName,
    79  		},
    80  	}
    81  	linuxVrf2 := &linux_interfaces.Interface{
    82  		Name:    vrf2Label,
    83  		Type:    linux_interfaces.Interface_VRF_DEVICE,
    84  		Enabled: true,
    85  		Link: &linux_interfaces.Interface_VrfDev{
    86  			VrfDev: &linux_interfaces.VrfDevLink{
    87  				RoutingTable: vrf2ID,
    88  			},
    89  		},
    90  		Namespace: &linux_namespace.NetNamespace{
    91  			Type:      linux_namespace.NetNamespace_MICROSERVICE,
    92  			Reference: MsNamePrefix + msName,
    93  		},
    94  	}
    95  	vppTap1 := &vpp_interfaces.Interface{
    96  		Name:        vppTap1Name,
    97  		Type:        vpp_interfaces.Interface_TAP,
    98  		Enabled:     true,
    99  		Vrf:         vrf1ID,
   100  		IpAddresses: []string{vppTapIP + netMask},
   101  		Link: &vpp_interfaces.Interface_Tap{
   102  			Tap: &vpp_interfaces.TapLink{
   103  				Version:        2,
   104  				ToMicroservice: MsNamePrefix + msName,
   105  			},
   106  		},
   107  	}
   108  	linuxTap1 := &linux_interfaces.Interface{
   109  		Name:               linuxTap1Name,
   110  		Type:               linux_interfaces.Interface_TAP_TO_VPP,
   111  		Enabled:            true,
   112  		IpAddresses:        []string{linuxTapIP + netMask},
   113  		HostIfName:         linuxTap1Hostname,
   114  		VrfMasterInterface: vrf1Label,
   115  		Link: &linux_interfaces.Interface_Tap{
   116  			Tap: &linux_interfaces.TapLink{
   117  				VppTapIfName: vppTap1Name,
   118  			},
   119  		},
   120  		Namespace: &linux_namespace.NetNamespace{
   121  			Type:      linux_namespace.NetNamespace_MICROSERVICE,
   122  			Reference: MsNamePrefix + msName,
   123  		},
   124  	}
   125  	vppTap2 := &vpp_interfaces.Interface{
   126  		Name:        vppTap2Name,
   127  		Type:        vpp_interfaces.Interface_TAP,
   128  		Enabled:     true,
   129  		Vrf:         vrf2ID,
   130  		IpAddresses: []string{vppTapIP + netMask},
   131  		Link: &vpp_interfaces.Interface_Tap{
   132  			Tap: &vpp_interfaces.TapLink{
   133  				Version:        2,
   134  				ToMicroservice: MsNamePrefix + msName,
   135  			},
   136  		},
   137  	}
   138  	linuxTap2 := &linux_interfaces.Interface{
   139  		Name:               linuxTap2Name,
   140  		Type:               linux_interfaces.Interface_TAP_TO_VPP,
   141  		Enabled:            true,
   142  		IpAddresses:        []string{linuxTapIP + netMask},
   143  		HostIfName:         linuxTap2Hostname,
   144  		VrfMasterInterface: vrf2Label,
   145  		Link: &linux_interfaces.Interface_Tap{
   146  			Tap: &linux_interfaces.TapLink{
   147  				VppTapIfName: vppTap2Name,
   148  			},
   149  		},
   150  		Namespace: &linux_namespace.NetNamespace{
   151  			Type:      linux_namespace.NetNamespace_MICROSERVICE,
   152  			Reference: MsNamePrefix + msName,
   153  		},
   154  	}
   155  
   156  	dhcpProxy1 := &vpp_l3.DHCPProxy{
   157  		SourceIpAddress: vppTapIP,
   158  		RxVrfId:         vrf1ID,
   159  		Servers: []*vpp_l3.DHCPProxy_DHCPServer{
   160  			{
   161  				VrfId:     vrf1ID,
   162  				IpAddress: linuxTapIP,
   163  			},
   164  		},
   165  	}
   166  	dhcpProxy2 := &vpp_l3.DHCPProxy{
   167  		SourceIpAddress: vppTapIP,
   168  		RxVrfId:         vrf2ID,
   169  		Servers: []*vpp_l3.DHCPProxy_DHCPServer{
   170  			{
   171  				VrfId:     vrf2ID,
   172  				IpAddress: linuxTapIP,
   173  			},
   174  		},
   175  	}
   176  
   177  	ctx.StartMicroservice(msName)
   178  
   179  	dhcpProxies := func() string {
   180  		output, err := ctx.ExecVppctl("show", "dhcp", "proxy")
   181  		ctx.Expect(err).ShouldNot(HaveOccurred())
   182  		return output
   183  	}
   184  	dhcpProxyRegexp := func(vrf int) string {
   185  		return fmt.Sprintf("%d[ ]+%s[ ]+%d,%s", vrf, vppTapIP, vrf, linuxTapIP)
   186  	}
   187  
   188  	err := ctx.GenericClient().ChangeRequest().Update(
   189  		vppVrf1,
   190  		vppVrf2,
   191  		linuxVrf1,
   192  		linuxVrf2,
   193  		vppTap1,
   194  		vppTap2,
   195  		linuxTap1,
   196  		linuxTap2,
   197  		dhcpProxy1,
   198  		dhcpProxy2,
   199  	).Send(context.Background())
   200  	ctx.Expect(err).ToNot(HaveOccurred())
   201  
   202  	ctx.Eventually(ctx.GetValueStateClb(vppTap1)).Should(Equal(kvscheduler.ValueState_CONFIGURED))
   203  	ctx.Eventually(ctx.GetValueStateClb(vppTap2)).Should(Equal(kvscheduler.ValueState_CONFIGURED))
   204  	ctx.Eventually(ctx.GetValueStateClb(linuxTap1)).Should(Equal(kvscheduler.ValueState_CONFIGURED))
   205  	ctx.Eventually(ctx.GetValueStateClb(linuxTap2)).Should(Equal(kvscheduler.ValueState_CONFIGURED))
   206  	ctx.Eventually(ctx.GetValueStateClb(dhcpProxy1)).Should(Equal(kvscheduler.ValueState_CONFIGURED))
   207  	ctx.Eventually(ctx.GetValueStateClb(dhcpProxy2)).Should(Equal(kvscheduler.ValueState_CONFIGURED))
   208  	ctx.Expect(ctx.PingFromMs(msName, vppTapIP, PingWithSourceInterface(linuxTap1Hostname))).To(Succeed())
   209  	ctx.Expect(ctx.PingFromMs(msName, vppTapIP, PingWithSourceInterface(linuxTap2Hostname))).To(Succeed())
   210  	ctx.Expect(ctx.AgentInSync()).To(BeTrue())
   211  
   212  	ctx.Expect(dhcpProxies()).Should(MatchRegexp(dhcpProxyRegexp(vrf1ID)))
   213  	ctx.Expect(dhcpProxies()).Should(MatchRegexp(dhcpProxyRegexp(vrf2ID)))
   214  
   215  	err = ctx.GenericClient().ChangeRequest().Delete(
   216  		dhcpProxy1,
   217  	).Send(context.Background())
   218  	ctx.Expect(err).ToNot(HaveOccurred())
   219  
   220  	ctx.Expect(ctx.AgentInSync()).To(BeTrue())
   221  	ctx.Expect(dhcpProxies()).ShouldNot(MatchRegexp(dhcpProxyRegexp(vrf1ID)))
   222  	ctx.Expect(dhcpProxies()).Should(MatchRegexp(dhcpProxyRegexp(vrf2ID)))
   223  
   224  }