github.com/mapuri/terraform@v0.7.6-0.20161012203214-7e0408293f97/builtin/providers/azurerm/resource_arm_loadbalancer_rule_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 TestResourceAzureRMLoadBalancerRuleNameLabel_validation(t *testing.T) {
    14  	cases := []struct {
    15  		Value    string
    16  		ErrCount int
    17  	}{
    18  		{
    19  			Value:    "-word",
    20  			ErrCount: 1,
    21  		},
    22  		{
    23  			Value:    "testing-",
    24  			ErrCount: 1,
    25  		},
    26  		{
    27  			Value:    "test123test",
    28  			ErrCount: 1,
    29  		},
    30  		{
    31  			Value:    acctest.RandStringFromCharSet(81, "abcdedfed"),
    32  			ErrCount: 1,
    33  		},
    34  		{
    35  			Value:    "test.rule",
    36  			ErrCount: 0,
    37  		},
    38  		{
    39  			Value:    "test_rule",
    40  			ErrCount: 0,
    41  		},
    42  		{
    43  			Value:    "test-rule",
    44  			ErrCount: 0,
    45  		},
    46  		{
    47  			Value:    "TestRule",
    48  			ErrCount: 0,
    49  		},
    50  	}
    51  
    52  	for _, tc := range cases {
    53  		_, errors := validateArmLoadBalancerRuleName(tc.Value, "azurerm_lb_rule")
    54  
    55  		if len(errors) != tc.ErrCount {
    56  			t.Fatalf("Expected the Azure RM LoadBalancer Rule Name Label to trigger a validation error")
    57  		}
    58  	}
    59  }
    60  
    61  func TestAccAzureRMLoadBalancerRule_basic(t *testing.T) {
    62  	var lb network.LoadBalancer
    63  	ri := acctest.RandInt()
    64  	lbRuleName := fmt.Sprintf("LbRule-%s", acctest.RandStringFromCharSet(8, acctest.CharSetAlpha))
    65  
    66  	resource.Test(t, resource.TestCase{
    67  		PreCheck:     func() { testAccPreCheck(t) },
    68  		Providers:    testAccProviders,
    69  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
    70  		Steps: []resource.TestStep{
    71  			{
    72  				Config: testAccAzureRMLoadBalancerRule_basic(ri, lbRuleName),
    73  				Check: resource.ComposeTestCheckFunc(
    74  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    75  					testCheckAzureRMLoadBalancerRuleExists(lbRuleName, &lb),
    76  				),
    77  			},
    78  		},
    79  	})
    80  }
    81  
    82  func TestAccAzureRMLoadBalancerRule_removal(t *testing.T) {
    83  	var lb network.LoadBalancer
    84  	ri := acctest.RandInt()
    85  	lbRuleName := fmt.Sprintf("LbRule-%s", acctest.RandStringFromCharSet(8, acctest.CharSetAlpha))
    86  
    87  	resource.Test(t, resource.TestCase{
    88  		PreCheck:     func() { testAccPreCheck(t) },
    89  		Providers:    testAccProviders,
    90  		CheckDestroy: testCheckAzureRMLoadBalancerDestroy,
    91  		Steps: []resource.TestStep{
    92  			{
    93  				Config: testAccAzureRMLoadBalancerRule_basic(ri, lbRuleName),
    94  				Check: resource.ComposeTestCheckFunc(
    95  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
    96  					testCheckAzureRMLoadBalancerRuleExists(lbRuleName, &lb),
    97  				),
    98  			},
    99  			{
   100  				Config: testAccAzureRMLoadBalancerRule_removal(ri),
   101  				Check: resource.ComposeTestCheckFunc(
   102  					testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb),
   103  					testCheckAzureRMLoadBalancerRuleNotExists(lbRuleName, &lb),
   104  				),
   105  			},
   106  		},
   107  	})
   108  }
   109  
   110  func testCheckAzureRMLoadBalancerRuleExists(lbRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc {
   111  	return func(s *terraform.State) error {
   112  		_, _, exists := findLoadBalancerRuleByName(lb, lbRuleName)
   113  		if !exists {
   114  			return fmt.Errorf("A LoadBalancer Rule with name %q cannot be found.", lbRuleName)
   115  		}
   116  
   117  		return nil
   118  	}
   119  }
   120  
   121  func testCheckAzureRMLoadBalancerRuleNotExists(lbRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc {
   122  	return func(s *terraform.State) error {
   123  		_, _, exists := findLoadBalancerRuleByName(lb, lbRuleName)
   124  		if exists {
   125  			return fmt.Errorf("A LoadBalancer Rule with name %q has been found.", lbRuleName)
   126  		}
   127  
   128  		return nil
   129  	}
   130  }
   131  
   132  func testAccAzureRMLoadBalancerRule_basic(rInt int, lbRuleName string) string {
   133  	return fmt.Sprintf(`
   134  resource "azurerm_resource_group" "test" {
   135      name = "acctestrg-%d"
   136      location = "West US"
   137  }
   138  
   139  resource "azurerm_public_ip" "test" {
   140      name = "test-ip-%d"
   141      location = "West US"
   142      resource_group_name = "${azurerm_resource_group.test.name}"
   143      public_ip_address_allocation = "static"
   144  }
   145  
   146  resource "azurerm_lb" "test" {
   147      name = "arm-test-loadbalancer-%d"
   148      location = "West US"
   149      resource_group_name = "${azurerm_resource_group.test.name}"
   150  
   151      frontend_ip_configuration {
   152        name = "one-%d"
   153        public_ip_address_id = "${azurerm_public_ip.test.id}"
   154      }
   155  }
   156  
   157  resource "azurerm_lb_rule" "test" {
   158    location = "West US"
   159    resource_group_name = "${azurerm_resource_group.test.name}"
   160    loadbalancer_id = "${azurerm_lb.test.id}"
   161    name = "%s"
   162    protocol = "Tcp"
   163    frontend_port = 3389
   164    backend_port = 3389
   165    frontend_ip_configuration_name = "one-%d"
   166  }
   167  
   168  `, rInt, rInt, rInt, rInt, lbRuleName, rInt)
   169  }
   170  
   171  func testAccAzureRMLoadBalancerRule_removal(rInt int) string {
   172  	return fmt.Sprintf(`
   173  resource "azurerm_resource_group" "test" {
   174      name = "acctestrg-%d"
   175      location = "West US"
   176  }
   177  
   178  resource "azurerm_public_ip" "test" {
   179      name = "test-ip-%d"
   180      location = "West US"
   181      resource_group_name = "${azurerm_resource_group.test.name}"
   182      public_ip_address_allocation = "static"
   183  }
   184  
   185  resource "azurerm_lb" "test" {
   186      name = "arm-test-loadbalancer-%d"
   187      location = "West US"
   188      resource_group_name = "${azurerm_resource_group.test.name}"
   189  
   190      frontend_ip_configuration {
   191        name = "one-%d"
   192        public_ip_address_id = "${azurerm_public_ip.test.id}"
   193      }
   194  }
   195  `, rInt, rInt, rInt, rInt)
   196  }