github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/acceptance/openstack/networking/v2/extensions/dnatrulest_test.go (about)

     1  package extensions
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/opentelekomcloud/gophertelekomcloud/acceptance/clients"
     8  	"github.com/opentelekomcloud/gophertelekomcloud/acceptance/openstack"
     9  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/networking/v2/extensions/dnatrules"
    10  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/networking/v2/extensions/portsecurity"
    11  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/networking/v2/ports"
    12  	th "github.com/opentelekomcloud/gophertelekomcloud/testhelper"
    13  )
    14  
    15  func TestDnatRuleLifeCycle(t *testing.T) {
    16  	dnatLong := os.Getenv("OS_DNAT_RULES_LONG")
    17  
    18  	client, err := clients.NewNatV2Client()
    19  	th.AssertNoErr(t, err)
    20  
    21  	natGateway := createNatGateway(t, client)
    22  	t.Cleanup(func() {
    23  		deleteNatGateway(t, client, natGateway.ID)
    24  	})
    25  
    26  	elasticIp := createEip(t)
    27  	t.Cleanup(func() {
    28  		deleteEip(t, elasticIp.ID)
    29  	})
    30  
    31  	t.Logf("Attempting to create DNAT rule, Direct Connect scenario")
    32  	allServicePorts := 0
    33  	createDCOpts := dnatrules.CreateOpts{
    34  		NatGatewayID:        natGateway.ID,
    35  		InternalServicePort: &allServicePorts,
    36  		PrivateIp:           "192.168.1.100",
    37  		FloatingIpID:        elasticIp.ID,
    38  		ExternalServicePort: &allServicePorts,
    39  		Protocol:            "any",
    40  	}
    41  	dnatRule, err := dnatrules.Create(client, createDCOpts)
    42  	th.AssertNoErr(t, err)
    43  	t.Logf("Created DNAT DC rule: %s", dnatRule.ID)
    44  
    45  	t.Cleanup(func() {
    46  		t.Logf("Attempting to delete DC DNAT rule: %s", dnatRule.ID)
    47  		err = dnatrules.Delete(client, dnatRule.ID)
    48  		th.AssertNoErr(t, err)
    49  		t.Logf("Deleted DC DNAT rule: %s", dnatRule.ID)
    50  	})
    51  
    52  	if dnatLong != "" {
    53  		clientNetwork, err := clients.NewNetworkV2Client()
    54  		th.AssertNoErr(t, err)
    55  
    56  		clientCompute, err := clients.NewComputeV1Client()
    57  		th.AssertNoErr(t, err)
    58  
    59  		// Get ECSv1 createOpts
    60  		createEcsOpts := openstack.GetCloudServerCreateOpts(t)
    61  
    62  		// Create ECSv1 instance
    63  		ecs := openstack.CreateCloudServer(t, clientCompute, createEcsOpts)
    64  		t.Cleanup(func() {
    65  			openstack.DeleteCloudServer(t, clientCompute, ecs.ID)
    66  		})
    67  
    68  		type portWithExt struct {
    69  			ports.Port
    70  			portsecurity.PortSecurityExt
    71  		}
    72  
    73  		var allPorts []portWithExt
    74  
    75  		allPages, err := ports.List(clientNetwork, ports.ListOpts{DeviceID: ecs.ID}).AllPages()
    76  		th.AssertNoErr(t, err)
    77  
    78  		err = ports.ExtractPortsInto(allPages, &allPorts)
    79  		th.AssertNoErr(t, err)
    80  
    81  		elasticIpSecond := createEip(t)
    82  		t.Cleanup(func() {
    83  			deleteEip(t, elasticIpSecond.ID)
    84  		})
    85  
    86  		t.Logf("Attempting to create DNAT rule, VPC scenario")
    87  		createVPCOpts := dnatrules.CreateOpts{
    88  			NatGatewayID:        natGateway.ID,
    89  			InternalServicePort: &allServicePorts,
    90  			FloatingIpID:        elasticIpSecond.ID,
    91  			ExternalServicePort: &allServicePorts,
    92  			Protocol:            "any",
    93  			PortID:              allPorts[0].ID,
    94  		}
    95  		dnatRuleVpc, err := dnatrules.Create(client, createVPCOpts)
    96  		th.AssertNoErr(t, err)
    97  		t.Logf("Created DNAT VPC rule: %s", dnatRuleVpc.ID)
    98  
    99  		t.Cleanup(func() {
   100  			t.Logf("Attempting to delete VPC DNAT rule: %s", dnatRuleVpc.ID)
   101  			err = dnatrules.Delete(client, dnatRuleVpc.ID)
   102  			th.AssertNoErr(t, err)
   103  			t.Logf("Deleted VPC DNAT rule: %s", dnatRuleVpc.ID)
   104  		})
   105  	}
   106  	t.Logf("Attempting to get DNAT rule: %s", dnatRule.ID)
   107  	newDnatRule, err := dnatrules.Get(client, dnatRule.ID)
   108  	th.AssertNoErr(t, err)
   109  	th.AssertEquals(t, createDCOpts.NatGatewayID, newDnatRule.NatGatewayId)
   110  
   111  	t.Logf("Attempting to list DNAT rules")
   112  	listRules, err := dnatrules.List(client, dnatrules.ListOpts{})
   113  	th.AssertNoErr(t, err)
   114  	if dnatLong != "" {
   115  		th.AssertEquals(t, len(listRules), 2)
   116  	} else {
   117  		th.AssertEquals(t, len(listRules), 1)
   118  	}
   119  
   120  }