github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/azurerm/resource_arm_loadbalancer_nat_pool_test.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/Azure/azure-sdk-for-go/arm/network"
     9  	"github.com/hashicorp/terraform/helper/acctest"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAzureRMLoadBalancerNatPool_basic(t *testing.T) {
    15  	var lb network.LoadBalancer
    16  	ri := acctest.RandInt()
    17  	natPoolName := fmt.Sprintf("NatPool-%d", ri)
    18  
    19  	subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID")
    20  	natPool_id := fmt.Sprintf(
    21  		"/subscriptions/%s/resourceGroups/acctestrg-%d/providers/Microsoft.Network/loadBalancers/arm-test-loadbalancer-%d/inboundNatPools/%s",
    22  		subscriptionID, ri, ri, natPoolName)
    23  
    24  	resource.Test(t, resource.TestCase{
    25  		PreCheck:     func() { testAccPreCheck(t) },
    26  		Providers:    testAccProviders,
    27  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
    28  		Steps: []resource.TestStep{
    29  			{
    30  				Config: testAccAzureRMLoadBalancerNatPool_basic(ri, natPoolName),
    31  				Check: resource.ComposeTestCheckFunc(
    32  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    33  					testCheckAzureRMLoadBalancerNatPoolExists(natPoolName, &lb),
    34  					resource.TestCheckResourceAttr(
    35  						"azurerm_lb_nat_pool.test", "id", natPool_id),
    36  				),
    37  			},
    38  		},
    39  	})
    40  }
    41  
    42  func TestAccAzureRMLoadBalancerNatPool_removal(t *testing.T) {
    43  	var lb network.LoadBalancer
    44  	ri := acctest.RandInt()
    45  	natPoolName := fmt.Sprintf("NatPool-%d", ri)
    46  
    47  	resource.Test(t, resource.TestCase{
    48  		PreCheck:     func() { testAccPreCheck(t) },
    49  		Providers:    testAccProviders,
    50  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
    51  		Steps: []resource.TestStep{
    52  			{
    53  				Config: testAccAzureRMLoadBalancerNatPool_basic(ri, natPoolName),
    54  				Check: resource.ComposeTestCheckFunc(
    55  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    56  					testCheckAzureRMLoadBalancerNatPoolExists(natPoolName, &lb),
    57  				),
    58  			},
    59  			{
    60  				Config: testAccAzureRMLoadBalancerNatPool_removal(ri),
    61  				Check: resource.ComposeTestCheckFunc(
    62  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    63  					testCheckAzureRMLoadBalancerNatPoolNotExists(natPoolName, &lb),
    64  				),
    65  			},
    66  		},
    67  	})
    68  }
    69  
    70  func testCheckAzureRMLoadBalancerNatPoolExists(natPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc {
    71  	return func(s *terraform.State) error {
    72  		_, _, exists := findLoadBalancerNatPoolByName(lb, natPoolName)
    73  		if !exists {
    74  			return fmt.Errorf("A NAT Pool with name %q cannot be found.", natPoolName)
    75  		}
    76  
    77  		return nil
    78  	}
    79  }
    80  
    81  func testCheckAzureRMLoadBalancerNatPoolNotExists(natPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc {
    82  	return func(s *terraform.State) error {
    83  		_, _, exists := findLoadBalancerNatPoolByName(lb, natPoolName)
    84  		if exists {
    85  			return fmt.Errorf("A NAT Pool with name %q has been found.", natPoolName)
    86  		}
    87  
    88  		return nil
    89  	}
    90  }
    91  
    92  func testAccAzureRMLoadBalancerNatPool_basic(rInt int, natPoolName string) string {
    93  	return fmt.Sprintf(`
    94  resource "azurerm_resource_group" "test" {
    95      name = "acctestrg-%d"
    96      location = "West US"
    97  }
    98  
    99  resource "azurerm_public_ip" "test" {
   100      name = "test-ip-%d"
   101      location = "West US"
   102      resource_group_name = "${azurerm_resource_group.test.name}"
   103      public_ip_address_allocation = "static"
   104  }
   105  
   106  resource "azurerm_lb" "test" {
   107      name = "arm-test-loadbalancer-%d"
   108      location = "West US"
   109      resource_group_name = "${azurerm_resource_group.test.name}"
   110  
   111      frontend_ip_configuration {
   112        name = "one-%d"
   113        public_ip_address_id = "${azurerm_public_ip.test.id}"
   114      }
   115  }
   116  
   117  resource "azurerm_lb_nat_pool" "test" {
   118    location = "West US"
   119    resource_group_name = "${azurerm_resource_group.test.name}"
   120    loadbalancer_id = "${azurerm_lb.test.id}"
   121    name = "%s"
   122    protocol = "Tcp"
   123    frontend_port_start = 80
   124    frontend_port_end = 81
   125    backend_port = 3389
   126    frontend_ip_configuration_name = "one-%d"
   127  }
   128  
   129  `, rInt, rInt, rInt, rInt, natPoolName, rInt)
   130  }
   131  
   132  func testAccAzureRMLoadBalancerNatPool_removal(rInt int) string {
   133  	return fmt.Sprintf(`
   134  resource "azurerm_resource_group" "test" {
   135      name = "acctestrg-%d"
   136      location = "West US"
   137  }
   138  
   139  resource "azurerm_public_ip" "test" {
   140      name = "test-ip-%d"
   141      location = "West US"
   142      resource_group_name = "${azurerm_resource_group.test.name}"
   143      public_ip_address_allocation = "static"
   144  }
   145  
   146  resource "azurerm_lb" "test" {
   147      name = "arm-test-loadbalancer-%d"
   148      location = "West US"
   149      resource_group_name = "${azurerm_resource_group.test.name}"
   150  
   151      frontend_ip_configuration {
   152        name = "one-%d"
   153        public_ip_address_id = "${azurerm_public_ip.test.id}"
   154      }
   155  }
   156  `, rInt, rInt, rInt, rInt)
   157  }