github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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 testCheckAzureRMLoadBalancerRuleExists(lbRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc { 153 return func(s *terraform.State) error { 154 _, _, exists := findLoadBalancerRuleByName(lb, lbRuleName) 155 if !exists { 156 return fmt.Errorf("A LoadBalancer Rule with name %q cannot be found.", lbRuleName) 157 } 158 159 return nil 160 } 161 } 162 163 func testCheckAzureRMLoadBalancerRuleNotExists(lbRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc { 164 return func(s *terraform.State) error { 165 _, _, exists := findLoadBalancerRuleByName(lb, lbRuleName) 166 if exists { 167 return fmt.Errorf("A LoadBalancer Rule with name %q has been found.", lbRuleName) 168 } 169 170 return nil 171 } 172 } 173 174 func testAccAzureRMLoadBalancerRule_basic(rInt int, lbRuleName string) string { 175 return fmt.Sprintf(` 176 resource "azurerm_resource_group" "test" { 177 name = "acctestrg-%d" 178 location = "West US" 179 } 180 181 resource "azurerm_public_ip" "test" { 182 name = "test-ip-%d" 183 location = "West US" 184 resource_group_name = "${azurerm_resource_group.test.name}" 185 public_ip_address_allocation = "static" 186 } 187 188 resource "azurerm_lb" "test" { 189 name = "arm-test-loadbalancer-%d" 190 location = "West US" 191 resource_group_name = "${azurerm_resource_group.test.name}" 192 193 frontend_ip_configuration { 194 name = "one-%d" 195 public_ip_address_id = "${azurerm_public_ip.test.id}" 196 } 197 } 198 199 resource "azurerm_lb_rule" "test" { 200 location = "West US" 201 resource_group_name = "${azurerm_resource_group.test.name}" 202 loadbalancer_id = "${azurerm_lb.test.id}" 203 name = "%s" 204 protocol = "Tcp" 205 frontend_port = 3389 206 backend_port = 3389 207 frontend_ip_configuration_name = "one-%d" 208 } 209 210 `, rInt, rInt, rInt, rInt, lbRuleName, rInt) 211 } 212 213 func testAccAzureRMLoadBalancerRule_removal(rInt int) string { 214 return fmt.Sprintf(` 215 resource "azurerm_resource_group" "test" { 216 name = "acctestrg-%d" 217 location = "West US" 218 } 219 220 resource "azurerm_public_ip" "test" { 221 name = "test-ip-%d" 222 location = "West US" 223 resource_group_name = "${azurerm_resource_group.test.name}" 224 public_ip_address_allocation = "static" 225 } 226 227 resource "azurerm_lb" "test" { 228 name = "arm-test-loadbalancer-%d" 229 location = "West US" 230 resource_group_name = "${azurerm_resource_group.test.name}" 231 232 frontend_ip_configuration { 233 name = "one-%d" 234 public_ip_address_id = "${azurerm_public_ip.test.id}" 235 } 236 } 237 `, rInt, rInt, rInt, rInt) 238 } 239 240 // https://github.com/hashicorp/terraform/issues/9424 241 func testAccAzureRMLoadBalancerRule_inconsistentRead(rInt int, backendPoolName, probeName, lbRuleName string) string { 242 return fmt.Sprintf(` 243 resource "azurerm_resource_group" "test" { 244 name = "acctestrg-%d" 245 location = "West US" 246 } 247 248 resource "azurerm_public_ip" "test" { 249 name = "test-ip-%d" 250 location = "West US" 251 resource_group_name = "${azurerm_resource_group.test.name}" 252 public_ip_address_allocation = "static" 253 } 254 255 resource "azurerm_lb" "test" { 256 name = "arm-test-loadbalancer-%d" 257 location = "West US" 258 resource_group_name = "${azurerm_resource_group.test.name}" 259 260 frontend_ip_configuration { 261 name = "one-%d" 262 public_ip_address_id = "${azurerm_public_ip.test.id}" 263 } 264 } 265 266 resource "azurerm_lb_backend_address_pool" "teset" { 267 name = "%s" 268 location = "West US" 269 resource_group_name = "${azurerm_resource_group.test.name}" 270 loadbalancer_id = "${azurerm_lb.test.id}" 271 } 272 273 resource "azurerm_lb_probe" "test" { 274 name = "%s" 275 location = "West US" 276 resource_group_name = "${azurerm_resource_group.test.name}" 277 loadbalancer_id = "${azurerm_lb.test.id}" 278 protocol = "Tcp" 279 port = 443 280 } 281 282 resource "azurerm_lb_rule" "test" { 283 name = "%s" 284 location = "West US" 285 resource_group_name = "${azurerm_resource_group.test.name}" 286 loadbalancer_id = "${azurerm_lb.test.id}" 287 protocol = "Tcp" 288 frontend_port = 3389 289 backend_port = 3389 290 frontend_ip_configuration_name = "one-%d" 291 } 292 `, rInt, rInt, rInt, rInt, backendPoolName, probeName, lbRuleName, rInt) 293 }