github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/azurerm/resource_arm_loadbalancer_backend_address_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 TestAccAzureRMLoadBalancerBackEndAddressPool_basic(t *testing.T) {
    15  	var lb network.LoadBalancer
    16  	ri := acctest.RandInt()
    17  	addressPoolName := fmt.Sprintf("%d-address-pool", ri)
    18  
    19  	subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID")
    20  	backendAddressPool_id := fmt.Sprintf(
    21  		"/subscriptions/%s/resourceGroups/acctestrg-%d/providers/Microsoft.Network/loadBalancers/arm-test-loadbalancer-%d/backendAddressPools/%s",
    22  		subscriptionID, ri, ri, addressPoolName)
    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: testAccAzureRMLoadBalancerBackEndAddressPool_basic(ri, addressPoolName),
    31  				Check: resource.ComposeTestCheckFunc(
    32  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    33  					testCheckAzureRMLoadBalancerBackEndAddressPoolExists(addressPoolName, &lb),
    34  					resource.TestCheckResourceAttr(
    35  						"azurerm_lb_backend_address_pool.test", "id", backendAddressPool_id),
    36  				),
    37  			},
    38  		},
    39  	})
    40  }
    41  
    42  func TestAccAzureRMLoadBalancerBackEndAddressPool_removal(t *testing.T) {
    43  	var lb network.LoadBalancer
    44  	ri := acctest.RandInt()
    45  	addressPoolName := fmt.Sprintf("%d-address-pool", 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: testAccAzureRMLoadBalancerBackEndAddressPool_removal(ri),
    54  				Check: resource.ComposeTestCheckFunc(
    55  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    56  					testCheckAzureRMLoadBalancerBackEndAddressPoolNotExists(addressPoolName, &lb),
    57  				),
    58  			},
    59  		},
    60  	})
    61  }
    62  
    63  func testCheckAzureRMLoadBalancerBackEndAddressPoolExists(addressPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc {
    64  	return func(s *terraform.State) error {
    65  		_, _, exists := findLoadBalancerBackEndAddressPoolByName(lb, addressPoolName)
    66  		if !exists {
    67  			return fmt.Errorf("A BackEnd Address Pool with name %q cannot be found.", addressPoolName)
    68  		}
    69  
    70  		return nil
    71  	}
    72  }
    73  
    74  func testCheckAzureRMLoadBalancerBackEndAddressPoolNotExists(addressPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc {
    75  	return func(s *terraform.State) error {
    76  		_, _, exists := findLoadBalancerBackEndAddressPoolByName(lb, addressPoolName)
    77  		if exists {
    78  			return fmt.Errorf("A BackEnd Address Pool with name %q has been found.", addressPoolName)
    79  		}
    80  
    81  		return nil
    82  	}
    83  }
    84  
    85  func testAccAzureRMLoadBalancerBackEndAddressPool_basic(rInt int, addressPoolName string) string {
    86  	return fmt.Sprintf(`
    87  resource "azurerm_resource_group" "test" {
    88      name = "acctestrg-%d"
    89      location = "West US"
    90  }
    91  
    92  resource "azurerm_public_ip" "test" {
    93      name = "test-ip-%d"
    94      location = "West US"
    95      resource_group_name = "${azurerm_resource_group.test.name}"
    96      public_ip_address_allocation = "static"
    97  }
    98  
    99  resource "azurerm_lb" "test" {
   100      name = "arm-test-loadbalancer-%d"
   101      location = "West US"
   102      resource_group_name = "${azurerm_resource_group.test.name}"
   103  
   104      frontend_ip_configuration {
   105        name = "one-%d"
   106        public_ip_address_id = "${azurerm_public_ip.test.id}"
   107      }
   108  }
   109  
   110  resource "azurerm_lb_backend_address_pool" "test" {
   111    location = "West US"
   112    resource_group_name = "${azurerm_resource_group.test.name}"
   113    loadbalancer_id = "${azurerm_lb.test.id}"
   114    name = "%s"
   115  }
   116  
   117  `, rInt, rInt, rInt, rInt, addressPoolName)
   118  }
   119  
   120  func testAccAzureRMLoadBalancerBackEndAddressPool_removal(rInt int) string {
   121  	return fmt.Sprintf(`
   122  resource "azurerm_resource_group" "test" {
   123      name = "acctestrg-%d"
   124      location = "West US"
   125  }
   126  
   127  resource "azurerm_public_ip" "test" {
   128      name = "test-ip-%d"
   129      location = "West US"
   130      resource_group_name = "${azurerm_resource_group.test.name}"
   131      public_ip_address_allocation = "static"
   132  }
   133  
   134  resource "azurerm_lb" "test" {
   135      name = "arm-test-loadbalancer-%d"
   136      location = "West US"
   137      resource_group_name = "${azurerm_resource_group.test.name}"
   138  
   139      frontend_ip_configuration {
   140        name = "one-%d"
   141        public_ip_address_id = "${azurerm_public_ip.test.id}"
   142      }
   143  }
   144  `, rInt, rInt, rInt, rInt)
   145  }