github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/resource_arm_loadbalancer_test.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     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 TestResourceAzureRMLoadBalancerPrivateIpAddressAllocation_validation(t *testing.T) {
    15  	cases := []struct {
    16  		Value    string
    17  		ErrCount int
    18  	}{
    19  		{
    20  			Value:    "Random",
    21  			ErrCount: 1,
    22  		},
    23  		{
    24  			Value:    "Static",
    25  			ErrCount: 0,
    26  		},
    27  		{
    28  			Value:    "Dynamic",
    29  			ErrCount: 0,
    30  		},
    31  		{
    32  			Value:    "STATIC",
    33  			ErrCount: 0,
    34  		},
    35  		{
    36  			Value:    "static",
    37  			ErrCount: 0,
    38  		},
    39  	}
    40  
    41  	for _, tc := range cases {
    42  		_, errors := validateLoadBalancerPrivateIpAddressAllocation(tc.Value, "azurerm_lb")
    43  
    44  		if len(errors) != tc.ErrCount {
    45  			t.Fatalf("Expected the Azure RM LoadBalancer private_ip_address_allocation to trigger a validation error")
    46  		}
    47  	}
    48  }
    49  
    50  func TestAccAzureRMLoadBalancer_basic(t *testing.T) {
    51  	var lb network.LoadBalancer
    52  	ri := acctest.RandInt()
    53  
    54  	resource.Test(t, resource.TestCase{
    55  		PreCheck:     func() { testAccPreCheck(t) },
    56  		Providers:    testAccProviders,
    57  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
    58  		Steps: []resource.TestStep{
    59  			{
    60  				Config: testAccAzureRMLoadBalancer_basic(ri),
    61  				Check: resource.ComposeTestCheckFunc(
    62  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    63  				),
    64  			},
    65  		},
    66  	})
    67  }
    68  
    69  func TestAccAzureRMLoadBalancer_frontEndConfig(t *testing.T) {
    70  	var lb network.LoadBalancer
    71  	ri := acctest.RandInt()
    72  
    73  	resource.Test(t, resource.TestCase{
    74  		PreCheck:     func() { testAccPreCheck(t) },
    75  		Providers:    testAccProviders,
    76  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
    77  		Steps: []resource.TestStep{
    78  			{
    79  				Config: testAccAzureRMLoadBalancer_frontEndConfig(ri),
    80  				Check: resource.ComposeTestCheckFunc(
    81  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    82  					resource.TestCheckResourceAttr(
    83  						"azurerm_lb.test", "frontend_ip_configuration.#", "2"),
    84  				),
    85  			},
    86  			{
    87  				Config: testAccAzureRMLoadBalancer_frontEndConfigRemoval(ri),
    88  				Check: resource.ComposeTestCheckFunc(
    89  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    90  					resource.TestCheckResourceAttr(
    91  						"azurerm_lb.test", "frontend_ip_configuration.#", "1"),
    92  				),
    93  			},
    94  		},
    95  	})
    96  }
    97  
    98  func TestAccAzureRMLoadBalancer_tags(t *testing.T) {
    99  	var lb network.LoadBalancer
   100  	ri := acctest.RandInt()
   101  
   102  	resource.Test(t, resource.TestCase{
   103  		PreCheck:     func() { testAccPreCheck(t) },
   104  		Providers:    testAccProviders,
   105  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
   106  		Steps: []resource.TestStep{
   107  			{
   108  				Config: testAccAzureRMLoadBalancer_basic(ri),
   109  				Check: resource.ComposeTestCheckFunc(
   110  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
   111  					resource.TestCheckResourceAttr(
   112  						"azurerm_lb.test", "tags.%", "2"),
   113  					resource.TestCheckResourceAttr(
   114  						"azurerm_lb.test", "tags.Environment", "production"),
   115  					resource.TestCheckResourceAttr(
   116  						"azurerm_lb.test", "tags.Purpose", "AcceptanceTests"),
   117  				),
   118  			},
   119  			{
   120  				Config: testAccAzureRMLoadBalancer_updatedTags(ri),
   121  				Check: resource.ComposeTestCheckFunc(
   122  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
   123  					resource.TestCheckResourceAttr(
   124  						"azurerm_lb.test", "tags.%", "1"),
   125  					resource.TestCheckResourceAttr(
   126  						"azurerm_lb.test", "tags.Purpose", "AcceptanceTests"),
   127  				),
   128  			},
   129  		},
   130  	})
   131  }
   132  
   133  func testCheckAzureRMLoadBalancerExists(name string, lb *network.LoadBalancer) resource.TestCheckFunc {
   134  	return func(s *terraform.State) error {
   135  		rs, ok := s.RootModule().Resources[name]
   136  		if !ok {
   137  			return fmt.Errorf("Not found: %s", name)
   138  		}
   139  
   140  		loadbalancerName := rs.Primary.Attributes["name"]
   141  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   142  		if !hasResourceGroup {
   143  			return fmt.Errorf("Bad: no resource group found in state for loadbalancer: %s", loadbalancerName)
   144  		}
   145  
   146  		conn := testAccProvider.Meta().(*ArmClient).loadBalancerClient
   147  
   148  		resp, err := conn.Get(resourceGroup, loadbalancerName, "")
   149  		if err != nil {
   150  			if resp.StatusCode == http.StatusNotFound {
   151  				return fmt.Errorf("Bad: LoadBalancer %q (resource group: %q) does not exist", loadbalancerName, resourceGroup)
   152  			}
   153  
   154  			return fmt.Errorf("Bad: Get on loadBalancerClient: %s", err)
   155  		}
   156  
   157  		*lb = resp
   158  
   159  		return nil
   160  	}
   161  }
   162  
   163  func testCheckAzureRMLoadBalancerDestroy(s *terraform.State) error {
   164  	conn := testAccProvider.Meta().(*ArmClient).loadBalancerClient
   165  
   166  	for _, rs := range s.RootModule().Resources {
   167  		if rs.Type != "azurerm_lb" {
   168  			continue
   169  		}
   170  
   171  		name := rs.Primary.Attributes["name"]
   172  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   173  
   174  		resp, err := conn.Get(resourceGroup, name, "")
   175  
   176  		if err != nil {
   177  			return nil
   178  		}
   179  
   180  		if resp.StatusCode != http.StatusNotFound {
   181  			return fmt.Errorf("LoadBalancer still exists:\n%#v", resp.LoadBalancerPropertiesFormat)
   182  		}
   183  	}
   184  
   185  	return nil
   186  }
   187  
   188  func testAccAzureRMLoadBalancer_basic(rInt int) string {
   189  	return fmt.Sprintf(`
   190  
   191  resource "azurerm_resource_group" "test" {
   192      name = "acctestrg-%d"
   193      location = "West US"
   194  }
   195  
   196  resource "azurerm_lb" "test" {
   197      name = "arm-test-loadbalancer-%d"
   198      location = "West US"
   199      resource_group_name = "${azurerm_resource_group.test.name}"
   200  
   201      tags {
   202      	Environment = "production"
   203      	Purpose = "AcceptanceTests"
   204      }
   205  
   206  }`, rInt, rInt)
   207  }
   208  
   209  func testAccAzureRMLoadBalancer_updatedTags(rInt int) string {
   210  	return fmt.Sprintf(`
   211  
   212  resource "azurerm_resource_group" "test" {
   213      name = "acctestrg-%d"
   214      location = "West US"
   215  }
   216  
   217  resource "azurerm_lb" "test" {
   218      name = "arm-test-loadbalancer-%d"
   219      location = "West US"
   220      resource_group_name = "${azurerm_resource_group.test.name}"
   221  
   222      tags {
   223      	Purpose = "AcceptanceTests"
   224      }
   225  
   226  }`, rInt, rInt)
   227  }
   228  
   229  func testAccAzureRMLoadBalancer_frontEndConfig(rInt int) string {
   230  	return fmt.Sprintf(`
   231  
   232  resource "azurerm_resource_group" "test" {
   233      name = "acctestrg-%d"
   234      location = "West US"
   235  }
   236  
   237  resource "azurerm_public_ip" "test" {
   238      name = "test-ip-%d"
   239      location = "West US"
   240      resource_group_name = "${azurerm_resource_group.test.name}"
   241      public_ip_address_allocation = "static"
   242  }
   243  
   244  resource "azurerm_public_ip" "test1" {
   245      name = "another-test-ip-%d"
   246      location = "West US"
   247      resource_group_name = "${azurerm_resource_group.test.name}"
   248      public_ip_address_allocation = "static"
   249  }
   250  
   251  resource "azurerm_lb" "test" {
   252      name = "arm-test-loadbalancer-%d"
   253      location = "West US"
   254      resource_group_name = "${azurerm_resource_group.test.name}"
   255  
   256      frontend_ip_configuration {
   257        name = "one-%d"
   258        public_ip_address_id = "${azurerm_public_ip.test.id}"
   259      }
   260  
   261      frontend_ip_configuration {
   262        name = "two-%d"
   263        public_ip_address_id = "${azurerm_public_ip.test1.id}"
   264      }
   265  }`, rInt, rInt, rInt, rInt, rInt, rInt)
   266  }
   267  
   268  func testAccAzureRMLoadBalancer_frontEndConfigRemoval(rInt int) string {
   269  	return fmt.Sprintf(`
   270  
   271  resource "azurerm_resource_group" "test" {
   272      name = "acctestrg-%d"
   273      location = "West US"
   274  }
   275  
   276  resource "azurerm_public_ip" "test" {
   277      name = "test-ip-%d"
   278      location = "West US"
   279      resource_group_name = "${azurerm_resource_group.test.name}"
   280      public_ip_address_allocation = "static"
   281  }
   282  
   283  resource "azurerm_lb" "test" {
   284      name = "arm-test-loadbalancer-%d"
   285      location = "West US"
   286      resource_group_name = "${azurerm_resource_group.test.name}"
   287  
   288      frontend_ip_configuration {
   289        name = "one-%d"
   290        public_ip_address_id = "${azurerm_public_ip.test.id}"
   291      }
   292  }`, rInt, rInt, rInt, rInt)
   293  }