github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/nsxv_nat_test.go (about)

     1  //go:build edge || nat || nsxv || functional || ALL
     2  
     3  /*
     4   * Copyright 2019 VMware, Inc.  All rights reserved.  Licensed under the Apache v2 License.
     5   */
     6  
     7  package govcd
     8  
     9  import (
    10  	"fmt"
    11  
    12  	"github.com/vmware/go-vcloud-director/v2/types/v56"
    13  	. "gopkg.in/check.v1"
    14  )
    15  
    16  func (vcd *TestVCD) Test_NsxvSnatRule(check *C) {
    17  	if vcd.config.VCD.EdgeGateway == "" {
    18  		check.Skip("Skipping test because no edge gateway given")
    19  	}
    20  	edge, err := vcd.vdc.GetEdgeGatewayByName(vcd.config.VCD.EdgeGateway, false)
    21  	check.Assert(err, IsNil)
    22  	check.Assert(edge.EdgeGateway.Name, Equals, vcd.config.VCD.EdgeGateway)
    23  
    24  	vnicIndex, err := edge.GetVnicIndexByNetworkNameAndType(vcd.config.VCD.Network.Net1, "internal")
    25  	check.Assert(err, IsNil)
    26  
    27  	natRule := &types.EdgeNatRule{
    28  		Action:            "snat",
    29  		Vnic:              vnicIndex,
    30  		OriginalAddress:   vcd.config.VCD.InternalIp,
    31  		TranslatedAddress: vcd.config.VCD.ExternalIp,
    32  		Enabled:           true,
    33  		LoggingEnabled:    true,
    34  		Description:       "my description",
    35  	}
    36  	if testVerbose {
    37  		fmt.Printf("# %s %s %s -> %s\n", natRule.Action, natRule.Protocol, natRule.OriginalAddress,
    38  			natRule.TranslatedAddress)
    39  	}
    40  	testNsxvNat(natRule, vcd, check, *edge)
    41  }
    42  func (vcd *TestVCD) Test_NsxvDnatRule(check *C) {
    43  	if vcd.config.VCD.EdgeGateway == "" {
    44  		check.Skip("Skipping test because no edge gateway given")
    45  	}
    46  	edge, err := vcd.vdc.GetEdgeGatewayByName(vcd.config.VCD.EdgeGateway, false)
    47  	check.Assert(err, IsNil)
    48  	check.Assert(edge.EdgeGateway.Name, Equals, vcd.config.VCD.EdgeGateway)
    49  
    50  	vnicIndex, err := edge.GetVnicIndexByNetworkNameAndType(vcd.config.VCD.ExternalNetwork, "uplink")
    51  	check.Assert(err, IsNil)
    52  
    53  	natRule := &types.EdgeNatRule{
    54  		Action:            "dnat",
    55  		Vnic:              vnicIndex,
    56  		Protocol:          "tcp",
    57  		OriginalAddress:   vcd.config.VCD.ExternalIp,
    58  		OriginalPort:      "443",
    59  		TranslatedAddress: vcd.config.VCD.InternalIp,
    60  		TranslatedPort:    "8443",
    61  		Enabled:           true,
    62  		LoggingEnabled:    true,
    63  		Description:       "my description",
    64  	}
    65  	if testVerbose {
    66  		fmt.Printf("# %s %s %s:%s -> %s:%s\n", natRule.Action, natRule.Protocol, natRule.OriginalAddress,
    67  			natRule.OriginalPort, natRule.TranslatedAddress, natRule.TranslatedPort)
    68  	}
    69  
    70  	testNsxvNat(natRule, vcd, check, *edge)
    71  
    72  	natRule = &types.EdgeNatRule{
    73  		Action:            "dnat",
    74  		Vnic:              vnicIndex,
    75  		Protocol:          "icmp",
    76  		IcmpType:          "router-advertisement",
    77  		OriginalAddress:   vcd.config.VCD.ExternalIp,
    78  		TranslatedAddress: vcd.config.VCD.InternalIp,
    79  		Enabled:           true,
    80  		LoggingEnabled:    true,
    81  		Description:       "my description",
    82  	}
    83  	if testVerbose {
    84  		fmt.Printf("# %s %s:%s %s -> %s\n", natRule.Action, natRule.Protocol, natRule.IcmpType,
    85  			natRule.OriginalAddress, natRule.TranslatedAddress)
    86  	}
    87  	testNsxvNat(natRule, vcd, check, *edge)
    88  
    89  	natRule = &types.EdgeNatRule{
    90  		Action:            "dnat",
    91  		Vnic:              vnicIndex,
    92  		Protocol:          "any",
    93  		OriginalAddress:   vcd.config.VCD.ExternalIp,
    94  		TranslatedAddress: vcd.config.VCD.InternalIp,
    95  		Enabled:           true,
    96  		LoggingEnabled:    true,
    97  		Description:       "my description",
    98  	}
    99  	if testVerbose {
   100  		fmt.Printf("# %s %s %s -> %s\n", natRule.Action, natRule.Protocol, natRule.OriginalAddress,
   101  			natRule.TranslatedAddress)
   102  	}
   103  	testNsxvNat(natRule, vcd, check, *edge)
   104  }
   105  
   106  // testNsxvNat is a helper to test multiple configurations of NAT rules. It does the following
   107  // 1. Creates NAT rule with provided config
   108  // 2. Checks that it can be retrieve and verifies if IDs match
   109  // 3. Tries to update description field and validates that nothing else except description changes
   110  // 4. Deletes the rule
   111  // 5. Validates that the rule was deleted
   112  func testNsxvNat(natRule *types.EdgeNatRule, vcd *TestVCD, check *C, edge EdgeGateway) {
   113  	createdNatRule, err := edge.CreateNsxvNatRule(natRule)
   114  	check.Assert(err, IsNil)
   115  
   116  	parentEntity := vcd.org.Org.Name + "|" + vcd.vdc.Vdc.Name + "|" + vcd.config.VCD.EdgeGateway
   117  	AddToCleanupList(createdNatRule.ID, "nsxvNatRule", parentEntity, check.TestName())
   118  
   119  	gotNatRule, err := edge.GetNsxvNatRuleById(createdNatRule.ID)
   120  	check.Assert(err, IsNil)
   121  	check.Assert(gotNatRule, NotNil)
   122  	check.Assert(gotNatRule, DeepEquals, createdNatRule)
   123  	check.Assert(gotNatRule.ID, Equals, createdNatRule.ID)
   124  
   125  	// Set ID and update nat rule with description
   126  	natRule.ID = gotNatRule.ID
   127  	natRule.Description = "Description for NAT rule"
   128  	updatedNatRule, err := edge.UpdateNsxvNatRule(natRule)
   129  	check.Assert(err, IsNil)
   130  	check.Assert(updatedNatRule, NotNil)
   131  
   132  	check.Assert(updatedNatRule.Description, Equals, natRule.Description)
   133  
   134  	// Test that we can extract a list of NSXV NAT rules, and that one of them is the rule we have got when searching by ID
   135  	natRules, err := edge.GetNsxvNatRules()
   136  	check.Assert(err, IsNil)
   137  	check.Assert(natRules, NotNil)
   138  	foundRule := false
   139  	for _, rule := range natRules {
   140  		if rule.ID == natRule.ID {
   141  			foundRule = true
   142  		}
   143  	}
   144  	check.Assert(foundRule, Equals, true)
   145  
   146  	// Check if the objects are deeply equal (except updated 'Description' field)
   147  	createdNatRule.Description = natRule.Description
   148  	check.Assert(updatedNatRule, DeepEquals, createdNatRule)
   149  
   150  	err = edge.DeleteNsxvNatRuleById(gotNatRule.ID)
   151  	check.Assert(err, IsNil)
   152  
   153  	// Ensure the rule does not exist anymore
   154  	_, err = edge.GetNsxvNatRuleById(createdNatRule.ID)
   155  	check.Assert(IsNotFound(err), Equals, true)
   156  }