github.com/greysond/terraform@v0.8.5-0.20170124173113-439b5507bbe9/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 TestAccAzureRMLoadBalancerBackEndAddressPool_reapply(t *testing.T) {
    64  	var lb network.LoadBalancer
    65  	ri := acctest.RandInt()
    66  	addressPoolName := fmt.Sprintf("%d-address-pool", ri)
    67  
    68  	deleteAddressPoolState := func(s *terraform.State) error {
    69  		return s.Remove("azurerm_lb_backend_address_pool.test")
    70  	}
    71  
    72  	resource.Test(t, resource.TestCase{
    73  		PreCheck:     func() { testAccPreCheck(t) },
    74  		Providers:    testAccProviders,
    75  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
    76  		Steps: []resource.TestStep{
    77  			{
    78  				Config: testAccAzureRMLoadBalancerBackEndAddressPool_basic(ri, addressPoolName),
    79  				Check: resource.ComposeTestCheckFunc(
    80  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    81  					testCheckAzureRMLoadBalancerBackEndAddressPoolExists(addressPoolName, &lb),
    82  					deleteAddressPoolState,
    83  				),
    84  				ExpectNonEmptyPlan: true,
    85  			},
    86  			{
    87  				Config: testAccAzureRMLoadBalancerBackEndAddressPool_basic(ri, addressPoolName),
    88  				Check: resource.ComposeTestCheckFunc(
    89  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    90  					testCheckAzureRMLoadBalancerBackEndAddressPoolExists(addressPoolName, &lb),
    91  				),
    92  			},
    93  		},
    94  	})
    95  }
    96  
    97  func testCheckAzureRMLoadBalancerBackEndAddressPoolExists(addressPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc {
    98  	return func(s *terraform.State) error {
    99  		_, _, exists := findLoadBalancerBackEndAddressPoolByName(lb, addressPoolName)
   100  		if !exists {
   101  			return fmt.Errorf("A BackEnd Address Pool with name %q cannot be found.", addressPoolName)
   102  		}
   103  
   104  		return nil
   105  	}
   106  }
   107  
   108  func testCheckAzureRMLoadBalancerBackEndAddressPoolNotExists(addressPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc {
   109  	return func(s *terraform.State) error {
   110  		_, _, exists := findLoadBalancerBackEndAddressPoolByName(lb, addressPoolName)
   111  		if exists {
   112  			return fmt.Errorf("A BackEnd Address Pool with name %q has been found.", addressPoolName)
   113  		}
   114  
   115  		return nil
   116  	}
   117  }
   118  
   119  func testAccAzureRMLoadBalancerBackEndAddressPool_basic(rInt int, addressPoolName string) string {
   120  	return fmt.Sprintf(`
   121  resource "azurerm_resource_group" "test" {
   122      name = "acctestrg-%d"
   123      location = "West US"
   124  }
   125  
   126  resource "azurerm_public_ip" "test" {
   127      name = "test-ip-%d"
   128      location = "West US"
   129      resource_group_name = "${azurerm_resource_group.test.name}"
   130      public_ip_address_allocation = "static"
   131  }
   132  
   133  resource "azurerm_lb" "test" {
   134      name = "arm-test-loadbalancer-%d"
   135      location = "West US"
   136      resource_group_name = "${azurerm_resource_group.test.name}"
   137  
   138      frontend_ip_configuration {
   139        name = "one-%d"
   140        public_ip_address_id = "${azurerm_public_ip.test.id}"
   141      }
   142  }
   143  
   144  resource "azurerm_lb_backend_address_pool" "test" {
   145    location = "West US"
   146    resource_group_name = "${azurerm_resource_group.test.name}"
   147    loadbalancer_id = "${azurerm_lb.test.id}"
   148    name = "%s"
   149  }
   150  
   151  `, rInt, rInt, rInt, rInt, addressPoolName)
   152  }
   153  
   154  func testAccAzureRMLoadBalancerBackEndAddressPool_removal(rInt int) string {
   155  	return fmt.Sprintf(`
   156  resource "azurerm_resource_group" "test" {
   157      name = "acctestrg-%d"
   158      location = "West US"
   159  }
   160  
   161  resource "azurerm_public_ip" "test" {
   162      name = "test-ip-%d"
   163      location = "West US"
   164      resource_group_name = "${azurerm_resource_group.test.name}"
   165      public_ip_address_allocation = "static"
   166  }
   167  
   168  resource "azurerm_lb" "test" {
   169      name = "arm-test-loadbalancer-%d"
   170      location = "West US"
   171      resource_group_name = "${azurerm_resource_group.test.name}"
   172  
   173      frontend_ip_configuration {
   174        name = "one-%d"
   175        public_ip_address_id = "${azurerm_public_ip.test.id}"
   176      }
   177  }
   178  `, rInt, rInt, rInt, rInt)
   179  }