github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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:    "test#test",
    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  			Value:    "Test123Rule",
    53  			ErrCount: 0,
    54  		},
    55  		{
    56  			Value:    "TestRule",
    57  			ErrCount: 0,
    58  		},
    59  	}
    60  
    61  	for _, tc := range cases {
    62  		_, errors := validateArmLoadBalancerRuleName(tc.Value, "azurerm_lb_rule")
    63  
    64  		if len(errors) != tc.ErrCount {
    65  			t.Fatalf("Expected the Azure RM LoadBalancer Rule Name Label to trigger a validation error")
    66  		}
    67  	}
    68  }
    69  
    70  func TestAccAzureRMLoadBalancerRule_basic(t *testing.T) {
    71  	var lb network.LoadBalancer
    72  	ri := acctest.RandInt()
    73  	lbRuleName := fmt.Sprintf("LbRule-%s", acctest.RandStringFromCharSet(8, acctest.CharSetAlpha))
    74  
    75  	subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID")
    76  	lbRule_id := fmt.Sprintf(
    77  		"/subscriptions/%s/resourceGroups/acctestrg-%d/providers/Microsoft.Network/loadBalancers/arm-test-loadbalancer-%d/loadBalancingRules/%s",
    78  		subscriptionID, ri, ri, lbRuleName)
    79  
    80  	resource.Test(t, resource.TestCase{
    81  		PreCheck:     func() { testAccPreCheck(t) },
    82  		Providers:    testAccProviders,
    83  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
    84  		Steps: []resource.TestStep{
    85  			{
    86  				Config: testAccAzureRMLoadBalancerRule_basic(ri, lbRuleName),
    87  				Check: resource.ComposeTestCheckFunc(
    88  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    89  					testCheckAzureRMLoadBalancerRuleExists(lbRuleName, &lb),
    90  					resource.TestCheckResourceAttr(
    91  						"azurerm_lb_rule.test", "id", lbRule_id),
    92  				),
    93  			},
    94  		},
    95  	})
    96  }
    97  
    98  func TestAccAzureRMLoadBalancerRule_removal(t *testing.T) {
    99  	var lb network.LoadBalancer
   100  	ri := acctest.RandInt()
   101  	lbRuleName := fmt.Sprintf("LbRule-%s", acctest.RandStringFromCharSet(8, acctest.CharSetAlpha))
   102  
   103  	resource.Test(t, resource.TestCase{
   104  		PreCheck:     func() { testAccPreCheck(t) },
   105  		Providers:    testAccProviders,
   106  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
   107  		Steps: []resource.TestStep{
   108  			{
   109  				Config: testAccAzureRMLoadBalancerRule_basic(ri, lbRuleName),
   110  				Check: resource.ComposeTestCheckFunc(
   111  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
   112  					testCheckAzureRMLoadBalancerRuleExists(lbRuleName, &lb),
   113  				),
   114  			},
   115  			{
   116  				Config: testAccAzureRMLoadBalancerRule_removal(ri),
   117  				Check: resource.ComposeTestCheckFunc(
   118  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
   119  					testCheckAzureRMLoadBalancerRuleNotExists(lbRuleName, &lb),
   120  				),
   121  			},
   122  		},
   123  	})
   124  }
   125  
   126  // https://github.com/hashicorp/terraform/issues/9424
   127  func TestAccAzureRMLoadBalancerRule_inconsistentReads(t *testing.T) {
   128  	var lb network.LoadBalancer
   129  	ri := acctest.RandInt()
   130  	backendPoolName := fmt.Sprintf("LbPool-%s", acctest.RandStringFromCharSet(8, acctest.CharSetAlpha))
   131  	lbRuleName := fmt.Sprintf("LbRule-%s", acctest.RandStringFromCharSet(8, acctest.CharSetAlpha))
   132  	probeName := fmt.Sprintf("LbProbe-%s", acctest.RandStringFromCharSet(8, acctest.CharSetAlpha))
   133  
   134  	resource.Test(t, resource.TestCase{
   135  		PreCheck:     func() { testAccPreCheck(t) },
   136  		Providers:    testAccProviders,
   137  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
   138  		Steps: []resource.TestStep{
   139  			{
   140  				Config: testAccAzureRMLoadBalancerRule_inconsistentRead(ri, backendPoolName, probeName, lbRuleName),
   141  				Check: resource.ComposeTestCheckFunc(
   142  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
   143  					testCheckAzureRMLoadBalancerBackEndAddressPoolExists(backendPoolName, &lb),
   144  					testCheckAzureRMLoadBalancerRuleExists(lbRuleName, &lb),
   145  					testCheckAzureRMLoadBalancerProbeExists(probeName, &lb),
   146  				),
   147  			},
   148  		},
   149  	})
   150  }
   151  
   152  func TestAccAzureRMLoadBalancerRule_update(t *testing.T) {
   153  	var lb network.LoadBalancer
   154  	ri := acctest.RandInt()
   155  	lbRuleName := fmt.Sprintf("LbRule-%s", acctest.RandStringFromCharSet(8, acctest.CharSetAlpha))
   156  	lbRule2Name := fmt.Sprintf("LbRule-%s", acctest.RandStringFromCharSet(8, acctest.CharSetAlpha))
   157  
   158  	subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID")
   159  	lbRuleID := fmt.Sprintf(
   160  		"/subscriptions/%s/resourceGroups/acctestrg-%d/providers/Microsoft.Network/loadBalancers/arm-test-loadbalancer-%d/loadBalancingRules/%s",
   161  		subscriptionID, ri, ri, lbRuleName)
   162  
   163  	lbRule2ID := fmt.Sprintf(
   164  		"/subscriptions/%s/resourceGroups/acctestrg-%d/providers/Microsoft.Network/loadBalancers/arm-test-loadbalancer-%d/loadBalancingRules/%s",
   165  		subscriptionID, ri, ri, lbRule2Name)
   166  
   167  	resource.Test(t, resource.TestCase{
   168  		PreCheck:     func() { testAccPreCheck(t) },
   169  		Providers:    testAccProviders,
   170  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
   171  		Steps: []resource.TestStep{
   172  			{
   173  				Config: testAccAzureRMLoadBalancerRule_multipleRules(ri, lbRuleName, lbRule2Name),
   174  				Check: resource.ComposeTestCheckFunc(
   175  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
   176  					testCheckAzureRMLoadBalancerRuleExists(lbRuleName, &lb),
   177  					testCheckAzureRMLoadBalancerRuleExists(lbRule2Name, &lb),
   178  					resource.TestCheckResourceAttr("azurerm_lb_rule.test", "id", lbRuleID),
   179  					resource.TestCheckResourceAttr("azurerm_lb_rule.test2", "id", lbRule2ID),
   180  					resource.TestCheckResourceAttr("azurerm_lb_rule.test2", "frontend_port", "3390"),
   181  					resource.TestCheckResourceAttr("azurerm_lb_rule.test2", "backend_port", "3390"),
   182  				),
   183  			},
   184  			{
   185  				Config: testAccAzureRMLoadBalancerRule_multipleRulesUpdate(ri, lbRuleName, lbRule2Name),
   186  				Check: resource.ComposeTestCheckFunc(
   187  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
   188  					testCheckAzureRMLoadBalancerRuleExists(lbRuleName, &lb),
   189  					testCheckAzureRMLoadBalancerRuleExists(lbRule2Name, &lb),
   190  					resource.TestCheckResourceAttr("azurerm_lb_rule.test", "id", lbRuleID),
   191  					resource.TestCheckResourceAttr("azurerm_lb_rule.test2", "id", lbRule2ID),
   192  					resource.TestCheckResourceAttr("azurerm_lb_rule.test2", "frontend_port", "3391"),
   193  					resource.TestCheckResourceAttr("azurerm_lb_rule.test2", "backend_port", "3391"),
   194  				),
   195  			},
   196  		},
   197  	})
   198  }
   199  
   200  func TestAccAzureRMLoadBalancerRule_reapply(t *testing.T) {
   201  	var lb network.LoadBalancer
   202  	ri := acctest.RandInt()
   203  	lbRuleName := fmt.Sprintf("LbRule-%s", acctest.RandStringFromCharSet(8, acctest.CharSetAlpha))
   204  
   205  	deleteRuleState := func(s *terraform.State) error {
   206  		return s.Remove("azurerm_lb_rule.test")
   207  	}
   208  
   209  	resource.Test(t, resource.TestCase{
   210  		PreCheck:     func() { testAccPreCheck(t) },
   211  		Providers:    testAccProviders,
   212  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
   213  		Steps: []resource.TestStep{
   214  			{
   215  				Config: testAccAzureRMLoadBalancerRule_basic(ri, lbRuleName),
   216  				Check: resource.ComposeTestCheckFunc(
   217  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
   218  					testCheckAzureRMLoadBalancerRuleExists(lbRuleName, &lb),
   219  					deleteRuleState,
   220  				),
   221  				ExpectNonEmptyPlan: true,
   222  			},
   223  			{
   224  				Config: testAccAzureRMLoadBalancerRule_basic(ri, lbRuleName),
   225  				Check: resource.ComposeTestCheckFunc(
   226  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
   227  					testCheckAzureRMLoadBalancerRuleExists(lbRuleName, &lb),
   228  				),
   229  			},
   230  		},
   231  	})
   232  }
   233  
   234  func TestAccAzureRMLoadBalancerRule_disappears(t *testing.T) {
   235  	var lb network.LoadBalancer
   236  	ri := acctest.RandInt()
   237  	lbRuleName := fmt.Sprintf("LbRule-%s", acctest.RandStringFromCharSet(8, acctest.CharSetAlpha))
   238  
   239  	resource.Test(t, resource.TestCase{
   240  		PreCheck:     func() { testAccPreCheck(t) },
   241  		Providers:    testAccProviders,
   242  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
   243  		Steps: []resource.TestStep{
   244  			{
   245  				Config: testAccAzureRMLoadBalancerRule_basic(ri, lbRuleName),
   246  				Check: resource.ComposeTestCheckFunc(
   247  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
   248  					testCheckAzureRMLoadBalancerRuleExists(lbRuleName, &lb),
   249  					testCheckAzureRMLoadBalancerRuleDisappears(lbRuleName, &lb),
   250  				),
   251  				ExpectNonEmptyPlan: true,
   252  			},
   253  		},
   254  	})
   255  }
   256  
   257  func testCheckAzureRMLoadBalancerRuleExists(lbRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc {
   258  	return func(s *terraform.State) error {
   259  		_, _, exists := findLoadBalancerRuleByName(lb, lbRuleName)
   260  		if !exists {
   261  			return fmt.Errorf("A LoadBalancer Rule with name %q cannot be found.", lbRuleName)
   262  		}
   263  
   264  		return nil
   265  	}
   266  }
   267  
   268  func testCheckAzureRMLoadBalancerRuleNotExists(lbRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc {
   269  	return func(s *terraform.State) error {
   270  		_, _, exists := findLoadBalancerRuleByName(lb, lbRuleName)
   271  		if exists {
   272  			return fmt.Errorf("A LoadBalancer Rule with name %q has been found.", lbRuleName)
   273  		}
   274  
   275  		return nil
   276  	}
   277  }
   278  
   279  func testCheckAzureRMLoadBalancerRuleDisappears(ruleName string, lb *network.LoadBalancer) resource.TestCheckFunc {
   280  	return func(s *terraform.State) error {
   281  		conn := testAccProvider.Meta().(*ArmClient).loadBalancerClient
   282  
   283  		_, i, exists := findLoadBalancerRuleByName(lb, ruleName)
   284  		if !exists {
   285  			return fmt.Errorf("A Rule with name %q cannot be found.", ruleName)
   286  		}
   287  
   288  		currentRules := *lb.LoadBalancerPropertiesFormat.LoadBalancingRules
   289  		rules := append(currentRules[:i], currentRules[i+1:]...)
   290  		lb.LoadBalancerPropertiesFormat.LoadBalancingRules = &rules
   291  
   292  		id, err := parseAzureResourceID(*lb.ID)
   293  		if err != nil {
   294  			return err
   295  		}
   296  
   297  		_, err = conn.CreateOrUpdate(id.ResourceGroup, *lb.Name, *lb, make(chan struct{}))
   298  		if err != nil {
   299  			return fmt.Errorf("Error Creating/Updating LoadBalancer %s", err)
   300  		}
   301  
   302  		_, err = conn.Get(id.ResourceGroup, *lb.Name, "")
   303  		return err
   304  	}
   305  }
   306  
   307  func testAccAzureRMLoadBalancerRule_basic(rInt int, lbRuleName string) string {
   308  	return fmt.Sprintf(`
   309  resource "azurerm_resource_group" "test" {
   310      name = "acctestrg-%d"
   311      location = "West US"
   312  }
   313  
   314  resource "azurerm_public_ip" "test" {
   315      name = "test-ip-%d"
   316      location = "West US"
   317      resource_group_name = "${azurerm_resource_group.test.name}"
   318      public_ip_address_allocation = "static"
   319  }
   320  
   321  resource "azurerm_lb" "test" {
   322      name = "arm-test-loadbalancer-%d"
   323      location = "West US"
   324      resource_group_name = "${azurerm_resource_group.test.name}"
   325  
   326      frontend_ip_configuration {
   327        name = "one-%d"
   328        public_ip_address_id = "${azurerm_public_ip.test.id}"
   329      }
   330  }
   331  
   332  resource "azurerm_lb_rule" "test" {
   333    location = "West US"
   334    resource_group_name = "${azurerm_resource_group.test.name}"
   335    loadbalancer_id = "${azurerm_lb.test.id}"
   336    name = "%s"
   337    protocol = "Tcp"
   338    frontend_port = 3389
   339    backend_port = 3389
   340    frontend_ip_configuration_name = "one-%d"
   341  }
   342  
   343  `, rInt, rInt, rInt, rInt, lbRuleName, rInt)
   344  }
   345  
   346  func testAccAzureRMLoadBalancerRule_removal(rInt int) string {
   347  	return fmt.Sprintf(`
   348  resource "azurerm_resource_group" "test" {
   349      name = "acctestrg-%d"
   350      location = "West US"
   351  }
   352  
   353  resource "azurerm_public_ip" "test" {
   354      name = "test-ip-%d"
   355      location = "West US"
   356      resource_group_name = "${azurerm_resource_group.test.name}"
   357      public_ip_address_allocation = "static"
   358  }
   359  
   360  resource "azurerm_lb" "test" {
   361      name = "arm-test-loadbalancer-%d"
   362      location = "West US"
   363      resource_group_name = "${azurerm_resource_group.test.name}"
   364  
   365      frontend_ip_configuration {
   366        name = "one-%d"
   367        public_ip_address_id = "${azurerm_public_ip.test.id}"
   368      }
   369  }
   370  `, rInt, rInt, rInt, rInt)
   371  }
   372  
   373  // https://github.com/hashicorp/terraform/issues/9424
   374  func testAccAzureRMLoadBalancerRule_inconsistentRead(rInt int, backendPoolName, probeName, lbRuleName string) string {
   375  	return fmt.Sprintf(`
   376  resource "azurerm_resource_group" "test" {
   377      name = "acctestrg-%d"
   378      location = "West US"
   379  }
   380  
   381  resource "azurerm_public_ip" "test" {
   382      name = "test-ip-%d"
   383      location = "West US"
   384      resource_group_name = "${azurerm_resource_group.test.name}"
   385      public_ip_address_allocation = "static"
   386  }
   387  
   388  resource "azurerm_lb" "test" {
   389      name = "arm-test-loadbalancer-%d"
   390      location = "West US"
   391      resource_group_name = "${azurerm_resource_group.test.name}"
   392  
   393      frontend_ip_configuration {
   394        name = "one-%d"
   395        public_ip_address_id = "${azurerm_public_ip.test.id}"
   396      }
   397  }
   398  
   399  resource "azurerm_lb_backend_address_pool" "teset" {
   400    name                = "%s"
   401    location            = "West US"
   402    resource_group_name = "${azurerm_resource_group.test.name}"
   403    loadbalancer_id     = "${azurerm_lb.test.id}"
   404  }
   405  
   406  resource "azurerm_lb_probe" "test" {
   407    name                = "%s"
   408    location            = "West US"
   409    resource_group_name = "${azurerm_resource_group.test.name}"
   410    loadbalancer_id     = "${azurerm_lb.test.id}"
   411    protocol            = "Tcp"
   412    port                = 443
   413  }
   414  
   415  resource "azurerm_lb_rule" "test" {
   416    name = "%s"
   417    location = "West US"
   418    resource_group_name = "${azurerm_resource_group.test.name}"
   419    loadbalancer_id = "${azurerm_lb.test.id}"
   420    protocol = "Tcp"
   421    frontend_port = 3389
   422    backend_port = 3389
   423    frontend_ip_configuration_name = "one-%d"
   424  }
   425  `, rInt, rInt, rInt, rInt, backendPoolName, probeName, lbRuleName, rInt)
   426  }
   427  
   428  func testAccAzureRMLoadBalancerRule_multipleRules(rInt int, lbRuleName, lbRule2Name string) string {
   429  	return fmt.Sprintf(`
   430  resource "azurerm_resource_group" "test" {
   431      name = "acctestrg-%d"
   432      location = "West US"
   433  }
   434  
   435  resource "azurerm_public_ip" "test" {
   436      name = "test-ip-%d"
   437      location = "West US"
   438      resource_group_name = "${azurerm_resource_group.test.name}"
   439      public_ip_address_allocation = "static"
   440  }
   441  
   442  resource "azurerm_lb" "test" {
   443      name = "arm-test-loadbalancer-%d"
   444      location = "West US"
   445      resource_group_name = "${azurerm_resource_group.test.name}"
   446  
   447      frontend_ip_configuration {
   448        name = "one-%d"
   449        public_ip_address_id = "${azurerm_public_ip.test.id}"
   450      }
   451  }
   452  
   453  resource "azurerm_lb_rule" "test" {
   454    location = "West US"
   455    resource_group_name = "${azurerm_resource_group.test.name}"
   456    loadbalancer_id = "${azurerm_lb.test.id}"
   457    name = "%s"
   458    protocol = "Udp"
   459    frontend_port = 3389
   460    backend_port = 3389
   461    frontend_ip_configuration_name = "one-%d"
   462  }
   463  
   464  resource "azurerm_lb_rule" "test2" {
   465    location = "West US"
   466    resource_group_name = "${azurerm_resource_group.test.name}"
   467    loadbalancer_id = "${azurerm_lb.test.id}"
   468    name = "%s"
   469    protocol = "Udp"
   470    frontend_port = 3390
   471    backend_port = 3390
   472    frontend_ip_configuration_name = "one-%d"
   473  }
   474  
   475  `, rInt, rInt, rInt, rInt, lbRuleName, rInt, lbRule2Name, rInt)
   476  }
   477  
   478  func testAccAzureRMLoadBalancerRule_multipleRulesUpdate(rInt int, lbRuleName, lbRule2Name string) string {
   479  	return fmt.Sprintf(`
   480  resource "azurerm_resource_group" "test" {
   481      name = "acctestrg-%d"
   482      location = "West US"
   483  }
   484  
   485  resource "azurerm_public_ip" "test" {
   486      name = "test-ip-%d"
   487      location = "West US"
   488      resource_group_name = "${azurerm_resource_group.test.name}"
   489      public_ip_address_allocation = "static"
   490  }
   491  
   492  resource "azurerm_lb" "test" {
   493      name = "arm-test-loadbalancer-%d"
   494      location = "West US"
   495      resource_group_name = "${azurerm_resource_group.test.name}"
   496  
   497      frontend_ip_configuration {
   498        name = "one-%d"
   499        public_ip_address_id = "${azurerm_public_ip.test.id}"
   500      }
   501  }
   502  
   503  resource "azurerm_lb_rule" "test" {
   504    location = "West US"
   505    resource_group_name = "${azurerm_resource_group.test.name}"
   506    loadbalancer_id = "${azurerm_lb.test.id}"
   507    name = "%s"
   508    protocol = "Udp"
   509    frontend_port = 3389
   510    backend_port = 3389
   511    frontend_ip_configuration_name = "one-%d"
   512  }
   513  
   514  resource "azurerm_lb_rule" "test2" {
   515    location = "West US"
   516    resource_group_name = "${azurerm_resource_group.test.name}"
   517    loadbalancer_id = "${azurerm_lb.test.id}"
   518    name = "%s"
   519    protocol = "Udp"
   520    frontend_port = 3391
   521    backend_port = 3391
   522    frontend_ip_configuration_name = "one-%d"
   523  }
   524  
   525  `, rInt, rInt, rInt, rInt, lbRuleName, rInt, lbRule2Name, rInt)
   526  }