github.com/mapuri/terraform@v0.7.6-0.20161012203214-7e0408293f97/builtin/providers/azurerm/resource_arm_loadbalancer_backend_address_pool_test.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/Azure/azure-sdk-for-go/arm/network"
     8  	"github.com/hashicorp/terraform/helper/acctest"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAzureRMLoadBalancerBackEndAddressPool_basic(t *testing.T) {
    14  	var lb network.LoadBalancer
    15  	ri := acctest.RandInt()
    16  	addressPoolName := fmt.Sprintf("%d-address-pool", ri)
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
    22  		Steps: []resource.TestStep{
    23  			{
    24  				Config: testAccAzureRMLoadBalancerBackEndAddressPool_basic(ri, addressPoolName),
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    27  					testCheckAzureRMLoadBalancerBackEndAddressPoolExists(addressPoolName, &lb),
    28  				),
    29  			},
    30  		},
    31  	})
    32  }
    33  
    34  func TestAccAzureRMLoadBalancerBackEndAddressPool_removal(t *testing.T) {
    35  	var lb network.LoadBalancer
    36  	ri := acctest.RandInt()
    37  	addressPoolName := fmt.Sprintf("%d-address-pool", ri)
    38  
    39  	resource.Test(t, resource.TestCase{
    40  		PreCheck:     func() { testAccPreCheck(t) },
    41  		Providers:    testAccProviders,
    42  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
    43  		Steps: []resource.TestStep{
    44  			{
    45  				Config: testAccAzureRMLoadBalancerBackEndAddressPool_removal(ri),
    46  				Check: resource.ComposeTestCheckFunc(
    47  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    48  					testCheckAzureRMLoadBalancerBackEndAddressPoolNotExists(addressPoolName, &lb),
    49  				),
    50  			},
    51  		},
    52  	})
    53  }
    54  
    55  func testCheckAzureRMLoadBalancerBackEndAddressPoolExists(addressPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc {
    56  	return func(s *terraform.State) error {
    57  		_, _, exists := findLoadBalancerBackEndAddressPoolByName(lb, addressPoolName)
    58  		if !exists {
    59  			return fmt.Errorf("A BackEnd Address Pool with name %q cannot be found.", addressPoolName)
    60  		}
    61  
    62  		return nil
    63  	}
    64  }
    65  
    66  func testCheckAzureRMLoadBalancerBackEndAddressPoolNotExists(addressPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc {
    67  	return func(s *terraform.State) error {
    68  		_, _, exists := findLoadBalancerBackEndAddressPoolByName(lb, addressPoolName)
    69  		if exists {
    70  			return fmt.Errorf("A BackEnd Address Pool with name %q has been found.", addressPoolName)
    71  		}
    72  
    73  		return nil
    74  	}
    75  }
    76  
    77  func testAccAzureRMLoadBalancerBackEndAddressPool_basic(rInt int, addressPoolName string) string {
    78  	return fmt.Sprintf(`
    79  resource "azurerm_resource_group" "test" {
    80      name = "acctestrg-%d"
    81      location = "West US"
    82  }
    83  
    84  resource "azurerm_public_ip" "test" {
    85      name = "test-ip-%d"
    86      location = "West US"
    87      resource_group_name = "${azurerm_resource_group.test.name}"
    88      public_ip_address_allocation = "static"
    89  }
    90  
    91  resource "azurerm_lb" "test" {
    92      name = "arm-test-loadbalancer-%d"
    93      location = "West US"
    94      resource_group_name = "${azurerm_resource_group.test.name}"
    95  
    96      frontend_ip_configuration {
    97        name = "one-%d"
    98        public_ip_address_id = "${azurerm_public_ip.test.id}"
    99      }
   100  }
   101  
   102  resource "azurerm_lb_backend_address_pool" "test" {
   103    location = "West US"
   104    resource_group_name = "${azurerm_resource_group.test.name}"
   105    loadbalancer_id = "${azurerm_lb.test.id}"
   106    name = "%s"
   107  }
   108  
   109  `, rInt, rInt, rInt, rInt, addressPoolName)
   110  }
   111  
   112  func testAccAzureRMLoadBalancerBackEndAddressPool_removal(rInt int) string {
   113  	return fmt.Sprintf(`
   114  resource "azurerm_resource_group" "test" {
   115      name = "acctestrg-%d"
   116      location = "West US"
   117  }
   118  
   119  resource "azurerm_public_ip" "test" {
   120      name = "test-ip-%d"
   121      location = "West US"
   122      resource_group_name = "${azurerm_resource_group.test.name}"
   123      public_ip_address_allocation = "static"
   124  }
   125  
   126  resource "azurerm_lb" "test" {
   127      name = "arm-test-loadbalancer-%d"
   128      location = "West US"
   129      resource_group_name = "${azurerm_resource_group.test.name}"
   130  
   131      frontend_ip_configuration {
   132        name = "one-%d"
   133        public_ip_address_id = "${azurerm_public_ip.test.id}"
   134      }
   135  }
   136  `, rInt, rInt, rInt, rInt)
   137  }