github.com/renier/terraform@v0.7.8-0.20161024133817-eb8a9ef5471a/builtin/providers/azurerm/resource_arm_loadbalancer_rule_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 TestResourceAzureRMLoadBalancerRuleNameLabel_validation(t *testing.T) {
    15  	cases := []struct {
    16  		Value    string
    17  		ErrCount int
    18  	}{
    19  		{
    20  			Value:    "-word",
    21  			ErrCount: 1,
    22  		},
    23  		{
    24  			Value:    "testing-",
    25  			ErrCount: 1,
    26  		},
    27  		{
    28  			Value:    "test123test",
    29  			ErrCount: 1,
    30  		},
    31  		{
    32  			Value:    acctest.RandStringFromCharSet(81, "abcdedfed"),
    33  			ErrCount: 1,
    34  		},
    35  		{
    36  			Value:    "test.rule",
    37  			ErrCount: 0,
    38  		},
    39  		{
    40  			Value:    "test_rule",
    41  			ErrCount: 0,
    42  		},
    43  		{
    44  			Value:    "test-rule",
    45  			ErrCount: 0,
    46  		},
    47  		{
    48  			Value:    "TestRule",
    49  			ErrCount: 0,
    50  		},
    51  	}
    52  
    53  	for _, tc := range cases {
    54  		_, errors := validateArmLoadBalancerRuleName(tc.Value, "azurerm_lb_rule")
    55  
    56  		if len(errors) != tc.ErrCount {
    57  			t.Fatalf("Expected the Azure RM LoadBalancer Rule Name Label to trigger a validation error")
    58  		}
    59  	}
    60  }
    61  
    62  func TestAccAzureRMLoadBalancerRule_basic(t *testing.T) {
    63  	var lb network.LoadBalancer
    64  	ri := acctest.RandInt()
    65  	lbRuleName := fmt.Sprintf("LbRule-%s", acctest.RandStringFromCharSet(8, acctest.CharSetAlpha))
    66  
    67  	subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID")
    68  	lbRule_id := fmt.Sprintf(
    69  		"/subscriptions/%s/resourceGroups/acctestrg-%d/providers/Microsoft.Network/loadBalancers/arm-test-loadbalancer-%d/loadBalancingRules/%s",
    70  		subscriptionID, ri, ri, lbRuleName)
    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: testAccAzureRMLoadBalancerRule_basic(ri, lbRuleName),
    79  				Check: resource.ComposeTestCheckFunc(
    80  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    81  					testCheckAzureRMLoadBalancerRuleExists(lbRuleName, &lb),
    82  					resource.TestCheckResourceAttr(
    83  						"azurerm_lb_rule.test", "id", lbRule_id),
    84  				),
    85  			},
    86  		},
    87  	})
    88  }
    89  
    90  func TestAccAzureRMLoadBalancerRule_removal(t *testing.T) {
    91  	var lb network.LoadBalancer
    92  	ri := acctest.RandInt()
    93  	lbRuleName := fmt.Sprintf("LbRule-%s", acctest.RandStringFromCharSet(8, acctest.CharSetAlpha))
    94  
    95  	resource.Test(t, resource.TestCase{
    96  		PreCheck:     func() { testAccPreCheck(t) },
    97  		Providers:    testAccProviders,
    98  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
    99  		Steps: []resource.TestStep{
   100  			{
   101  				Config: testAccAzureRMLoadBalancerRule_basic(ri, lbRuleName),
   102  				Check: resource.ComposeTestCheckFunc(
   103  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
   104  					testCheckAzureRMLoadBalancerRuleExists(lbRuleName, &lb),
   105  				),
   106  			},
   107  			{
   108  				Config: testAccAzureRMLoadBalancerRule_removal(ri),
   109  				Check: resource.ComposeTestCheckFunc(
   110  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
   111  					testCheckAzureRMLoadBalancerRuleNotExists(lbRuleName, &lb),
   112  				),
   113  			},
   114  		},
   115  	})
   116  }
   117  
   118  // https://github.com/hashicorp/terraform/issues/9424
   119  func TestAccAzureRMLoadBalancerRule_inconsistentReads(t *testing.T) {
   120  	var lb network.LoadBalancer
   121  	ri := acctest.RandInt()
   122  	backendPoolName := fmt.Sprintf("LbPool-%s", acctest.RandStringFromCharSet(8, acctest.CharSetAlpha))
   123  	lbRuleName := fmt.Sprintf("LbRule-%s", acctest.RandStringFromCharSet(8, acctest.CharSetAlpha))
   124  	probeName := fmt.Sprintf("LbProbe-%s", acctest.RandStringFromCharSet(8, acctest.CharSetAlpha))
   125  
   126  	resource.Test(t, resource.TestCase{
   127  		PreCheck:     func() { testAccPreCheck(t) },
   128  		Providers:    testAccProviders,
   129  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
   130  		Steps: []resource.TestStep{
   131  			{
   132  				Config: testAccAzureRMLoadBalancerRule_inconsistentRead(ri, backendPoolName, probeName, lbRuleName),
   133  				Check: resource.ComposeTestCheckFunc(
   134  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
   135  					testCheckAzureRMLoadBalancerBackEndAddressPoolExists(backendPoolName, &lb),
   136  					testCheckAzureRMLoadBalancerRuleExists(lbRuleName, &lb),
   137  					testCheckAzureRMLoadBalancerProbeExists(probeName, &lb),
   138  				),
   139  			},
   140  		},
   141  	})
   142  }
   143  
   144  func testCheckAzureRMLoadBalancerRuleExists(lbRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc {
   145  	return func(s *terraform.State) error {
   146  		_, _, exists := findLoadBalancerRuleByName(lb, lbRuleName)
   147  		if !exists {
   148  			return fmt.Errorf("A LoadBalancer Rule with name %q cannot be found.", lbRuleName)
   149  		}
   150  
   151  		return nil
   152  	}
   153  }
   154  
   155  func testCheckAzureRMLoadBalancerRuleNotExists(lbRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc {
   156  	return func(s *terraform.State) error {
   157  		_, _, exists := findLoadBalancerRuleByName(lb, lbRuleName)
   158  		if exists {
   159  			return fmt.Errorf("A LoadBalancer Rule with name %q has been found.", lbRuleName)
   160  		}
   161  
   162  		return nil
   163  	}
   164  }
   165  
   166  func testAccAzureRMLoadBalancerRule_basic(rInt int, lbRuleName string) string {
   167  	return fmt.Sprintf(`
   168  resource "azurerm_resource_group" "test" {
   169      name = "acctestrg-%d"
   170      location = "West US"
   171  }
   172  
   173  resource "azurerm_public_ip" "test" {
   174      name = "test-ip-%d"
   175      location = "West US"
   176      resource_group_name = "${azurerm_resource_group.test.name}"
   177      public_ip_address_allocation = "static"
   178  }
   179  
   180  resource "azurerm_lb" "test" {
   181      name = "arm-test-loadbalancer-%d"
   182      location = "West US"
   183      resource_group_name = "${azurerm_resource_group.test.name}"
   184  
   185      frontend_ip_configuration {
   186        name = "one-%d"
   187        public_ip_address_id = "${azurerm_public_ip.test.id}"
   188      }
   189  }
   190  
   191  resource "azurerm_lb_rule" "test" {
   192    location = "West US"
   193    resource_group_name = "${azurerm_resource_group.test.name}"
   194    loadbalancer_id = "${azurerm_lb.test.id}"
   195    name = "%s"
   196    protocol = "Tcp"
   197    frontend_port = 3389
   198    backend_port = 3389
   199    frontend_ip_configuration_name = "one-%d"
   200  }
   201  
   202  `, rInt, rInt, rInt, rInt, lbRuleName, rInt)
   203  }
   204  
   205  func testAccAzureRMLoadBalancerRule_removal(rInt int) string {
   206  	return fmt.Sprintf(`
   207  resource "azurerm_resource_group" "test" {
   208      name = "acctestrg-%d"
   209      location = "West US"
   210  }
   211  
   212  resource "azurerm_public_ip" "test" {
   213      name = "test-ip-%d"
   214      location = "West US"
   215      resource_group_name = "${azurerm_resource_group.test.name}"
   216      public_ip_address_allocation = "static"
   217  }
   218  
   219  resource "azurerm_lb" "test" {
   220      name = "arm-test-loadbalancer-%d"
   221      location = "West US"
   222      resource_group_name = "${azurerm_resource_group.test.name}"
   223  
   224      frontend_ip_configuration {
   225        name = "one-%d"
   226        public_ip_address_id = "${azurerm_public_ip.test.id}"
   227      }
   228  }
   229  `, rInt, rInt, rInt, rInt)
   230  }
   231  
   232  // https://github.com/hashicorp/terraform/issues/9424
   233  func testAccAzureRMLoadBalancerRule_inconsistentRead(rInt int, backendPoolName, probeName, lbRuleName string) string {
   234  	return fmt.Sprintf(`
   235  resource "azurerm_resource_group" "test" {
   236      name = "acctestrg-%d"
   237      location = "West US"
   238  }
   239  
   240  resource "azurerm_public_ip" "test" {
   241      name = "test-ip-%d"
   242      location = "West US"
   243      resource_group_name = "${azurerm_resource_group.test.name}"
   244      public_ip_address_allocation = "static"
   245  }
   246  
   247  resource "azurerm_lb" "test" {
   248      name = "arm-test-loadbalancer-%d"
   249      location = "West US"
   250      resource_group_name = "${azurerm_resource_group.test.name}"
   251  
   252      frontend_ip_configuration {
   253        name = "one-%d"
   254        public_ip_address_id = "${azurerm_public_ip.test.id}"
   255      }
   256  }
   257  
   258  resource "azurerm_lb_backend_address_pool" "teset" {
   259    name                = "%s"
   260    location            = "West US"
   261    resource_group_name = "${azurerm_resource_group.test.name}"
   262    loadbalancer_id     = "${azurerm_lb.test.id}"
   263  }
   264  
   265  resource "azurerm_lb_probe" "test" {
   266    name                = "%s"
   267    location            = "West US"
   268    resource_group_name = "${azurerm_resource_group.test.name}"
   269    loadbalancer_id     = "${azurerm_lb.test.id}"
   270    protocol            = "Tcp"
   271    port                = 443
   272  }
   273  
   274  resource "azurerm_lb_rule" "test" {
   275    name = "%s"
   276    location = "West US"
   277    resource_group_name = "${azurerm_resource_group.test.name}"
   278    loadbalancer_id = "${azurerm_lb.test.id}"
   279    protocol = "Tcp"
   280    frontend_port = 3389
   281    backend_port = 3389
   282    frontend_ip_configuration_name = "one-%d"
   283  }
   284  `, rInt, rInt, rInt, rInt, backendPoolName, probeName, lbRuleName, rInt)
   285  }