github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/azurerm/resource_arm_loadbalancer_probe_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 TestAccAzureRMLoadBalancerProbe_basic(t *testing.T) { 15 var lb network.LoadBalancer 16 ri := acctest.RandInt() 17 probeName := fmt.Sprintf("probe-%d", ri) 18 19 subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID") 20 probe_id := fmt.Sprintf( 21 "/subscriptions/%s/resourceGroups/acctestrg-%d/providers/Microsoft.Network/loadBalancers/arm-test-loadbalancer-%d/probes/%s", 22 subscriptionID, ri, ri, probeName) 23 24 resource.Test(t, resource.TestCase{ 25 PreCheck: func() { testAccPreCheck(t) }, 26 Providers: testAccProviders, 27 CheckDestroy: testCheckAzureRMLoadBalancerDestroy, 28 Steps: []resource.TestStep{ 29 { 30 Config: testAccAzureRMLoadBalancerProbe_basic(ri, probeName), 31 Check: resource.ComposeTestCheckFunc( 32 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 33 testCheckAzureRMLoadBalancerProbeExists(probeName, &lb), 34 resource.TestCheckResourceAttr( 35 "azurerm_lb_probe.test", "id", probe_id), 36 ), 37 }, 38 }, 39 }) 40 } 41 42 func TestAccAzureRMLoadBalancerProbe_removal(t *testing.T) { 43 var lb network.LoadBalancer 44 ri := acctest.RandInt() 45 probeName := fmt.Sprintf("probe-%d", ri) 46 47 resource.Test(t, resource.TestCase{ 48 PreCheck: func() { testAccPreCheck(t) }, 49 Providers: testAccProviders, 50 CheckDestroy: testCheckAzureRMLoadBalancerDestroy, 51 Steps: []resource.TestStep{ 52 { 53 Config: testAccAzureRMLoadBalancerProbe_basic(ri, probeName), 54 Check: resource.ComposeTestCheckFunc( 55 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 56 testCheckAzureRMLoadBalancerProbeExists(probeName, &lb), 57 ), 58 }, 59 { 60 Config: testAccAzureRMLoadBalancerProbe_removal(ri), 61 Check: resource.ComposeTestCheckFunc( 62 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 63 testCheckAzureRMLoadBalancerProbeNotExists(probeName, &lb), 64 ), 65 }, 66 }, 67 }) 68 } 69 70 func TestAccAzureRMLoadBalancerProbe_update(t *testing.T) { 71 var lb network.LoadBalancer 72 ri := acctest.RandInt() 73 probeName := fmt.Sprintf("probe-%d", ri) 74 probe2Name := fmt.Sprintf("probe-%d", acctest.RandInt()) 75 76 resource.Test(t, resource.TestCase{ 77 PreCheck: func() { testAccPreCheck(t) }, 78 Providers: testAccProviders, 79 CheckDestroy: testCheckAzureRMLoadBalancerDestroy, 80 Steps: []resource.TestStep{ 81 { 82 Config: testAccAzureRMLoadBalancerProbe_multipleProbes(ri, probeName, probe2Name), 83 Check: resource.ComposeTestCheckFunc( 84 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 85 testCheckAzureRMLoadBalancerProbeExists(probeName, &lb), 86 testCheckAzureRMLoadBalancerProbeExists(probe2Name, &lb), 87 resource.TestCheckResourceAttr("azurerm_lb_probe.test2", "port", "80"), 88 ), 89 }, 90 { 91 Config: testAccAzureRMLoadBalancerProbe_multipleProbesUpdate(ri, probeName, probe2Name), 92 Check: resource.ComposeTestCheckFunc( 93 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 94 testCheckAzureRMLoadBalancerProbeExists(probeName, &lb), 95 testCheckAzureRMLoadBalancerProbeExists(probe2Name, &lb), 96 resource.TestCheckResourceAttr("azurerm_lb_probe.test2", "port", "8080"), 97 ), 98 }, 99 }, 100 }) 101 } 102 103 func TestAccAzureRMLoadBalancerProbe_updateProtocol(t *testing.T) { 104 var lb network.LoadBalancer 105 ri := acctest.RandInt() 106 probeName := fmt.Sprintf("probe-%d", ri) 107 108 resource.Test(t, resource.TestCase{ 109 PreCheck: func() { testAccPreCheck(t) }, 110 Providers: testAccProviders, 111 CheckDestroy: testCheckAzureRMLoadBalancerDestroy, 112 Steps: []resource.TestStep{ 113 { 114 Config: testAccAzureRMLoadBalancerProbe_updateProtocolBefore(ri, probeName), 115 Check: resource.ComposeTestCheckFunc( 116 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 117 testCheckAzureRMLoadBalancerProbeExists(probeName, &lb), 118 resource.TestCheckResourceAttr("azurerm_lb_probe.test", "protocol", "Http"), 119 ), 120 }, 121 { 122 Config: testAccAzureRMLoadBalancerProbe_updateProtocolAfter(ri, probeName), 123 Check: resource.ComposeTestCheckFunc( 124 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 125 testCheckAzureRMLoadBalancerProbeExists(probeName, &lb), 126 resource.TestCheckResourceAttr("azurerm_lb_probe.test", "protocol", "Tcp"), 127 ), 128 }, 129 }, 130 }) 131 } 132 133 func TestAccAzureRMLoadBalancerProbe_reapply(t *testing.T) { 134 var lb network.LoadBalancer 135 ri := acctest.RandInt() 136 probeName := fmt.Sprintf("probe-%d", ri) 137 138 deleteProbeState := func(s *terraform.State) error { 139 return s.Remove("azurerm_lb_probe.test") 140 } 141 142 resource.Test(t, resource.TestCase{ 143 PreCheck: func() { testAccPreCheck(t) }, 144 Providers: testAccProviders, 145 CheckDestroy: testCheckAzureRMLoadBalancerDestroy, 146 Steps: []resource.TestStep{ 147 { 148 Config: testAccAzureRMLoadBalancerProbe_basic(ri, probeName), 149 Check: resource.ComposeTestCheckFunc( 150 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 151 testCheckAzureRMLoadBalancerProbeExists(probeName, &lb), 152 deleteProbeState, 153 ), 154 ExpectNonEmptyPlan: true, 155 }, 156 { 157 Config: testAccAzureRMLoadBalancerProbe_basic(ri, probeName), 158 Check: resource.ComposeTestCheckFunc( 159 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 160 testCheckAzureRMLoadBalancerProbeExists(probeName, &lb), 161 ), 162 }, 163 }, 164 }) 165 } 166 167 func TestAccAzureRMLoadBalancerProbe_disappears(t *testing.T) { 168 var lb network.LoadBalancer 169 ri := acctest.RandInt() 170 probeName := fmt.Sprintf("probe-%d", ri) 171 172 resource.Test(t, resource.TestCase{ 173 PreCheck: func() { testAccPreCheck(t) }, 174 Providers: testAccProviders, 175 CheckDestroy: testCheckAzureRMLoadBalancerDestroy, 176 Steps: []resource.TestStep{ 177 { 178 Config: testAccAzureRMLoadBalancerProbe_basic(ri, probeName), 179 Check: resource.ComposeTestCheckFunc( 180 testCheckAzureRMLoadBalancerExists("azurerm_lb.test", &lb), 181 testCheckAzureRMLoadBalancerProbeExists(probeName, &lb), 182 testCheckAzureRMLoadBalancerProbeDisappears(probeName, &lb), 183 ), 184 ExpectNonEmptyPlan: true, 185 }, 186 }, 187 }) 188 } 189 190 func testCheckAzureRMLoadBalancerProbeExists(natRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc { 191 return func(s *terraform.State) error { 192 _, _, exists := findLoadBalancerProbeByName(lb, natRuleName) 193 if !exists { 194 return fmt.Errorf("A Probe with name %q cannot be found.", natRuleName) 195 } 196 197 return nil 198 } 199 } 200 201 func testCheckAzureRMLoadBalancerProbeNotExists(natRuleName string, lb *network.LoadBalancer) resource.TestCheckFunc { 202 return func(s *terraform.State) error { 203 _, _, exists := findLoadBalancerProbeByName(lb, natRuleName) 204 if exists { 205 return fmt.Errorf("A Probe with name %q has been found.", natRuleName) 206 } 207 208 return nil 209 } 210 } 211 212 func testCheckAzureRMLoadBalancerProbeDisappears(addressPoolName string, lb *network.LoadBalancer) resource.TestCheckFunc { 213 return func(s *terraform.State) error { 214 conn := testAccProvider.Meta().(*ArmClient).loadBalancerClient 215 216 _, i, exists := findLoadBalancerProbeByName(lb, addressPoolName) 217 if !exists { 218 return fmt.Errorf("A Probe with name %q cannot be found.", addressPoolName) 219 } 220 221 currentProbes := *lb.LoadBalancerPropertiesFormat.Probes 222 probes := append(currentProbes[:i], currentProbes[i+1:]...) 223 lb.LoadBalancerPropertiesFormat.Probes = &probes 224 225 id, err := parseAzureResourceID(*lb.ID) 226 if err != nil { 227 return err 228 } 229 230 _, error := conn.CreateOrUpdate(id.ResourceGroup, *lb.Name, *lb, make(chan struct{})) 231 err = <-error 232 if err != nil { 233 return fmt.Errorf("Error Creating/Updating LoadBalancer %s", err) 234 } 235 236 _, err = conn.Get(id.ResourceGroup, *lb.Name, "") 237 return err 238 } 239 } 240 241 func testAccAzureRMLoadBalancerProbe_basic(rInt int, probeName 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_probe" "test" { 267 location = "West US" 268 resource_group_name = "${azurerm_resource_group.test.name}" 269 loadbalancer_id = "${azurerm_lb.test.id}" 270 name = "%s" 271 port = 22 272 } 273 `, rInt, rInt, rInt, rInt, probeName) 274 } 275 276 func testAccAzureRMLoadBalancerProbe_removal(rInt int) string { 277 return fmt.Sprintf(` 278 resource "azurerm_resource_group" "test" { 279 name = "acctestrg-%d" 280 location = "West US" 281 } 282 283 resource "azurerm_public_ip" "test" { 284 name = "test-ip-%d" 285 location = "West US" 286 resource_group_name = "${azurerm_resource_group.test.name}" 287 public_ip_address_allocation = "static" 288 } 289 290 resource "azurerm_lb" "test" { 291 name = "arm-test-loadbalancer-%d" 292 location = "West US" 293 resource_group_name = "${azurerm_resource_group.test.name}" 294 295 frontend_ip_configuration { 296 name = "one-%d" 297 public_ip_address_id = "${azurerm_public_ip.test.id}" 298 } 299 } 300 `, rInt, rInt, rInt, rInt) 301 } 302 303 func testAccAzureRMLoadBalancerProbe_multipleProbes(rInt int, probeName, probe2Name string) string { 304 return fmt.Sprintf(` 305 resource "azurerm_resource_group" "test" { 306 name = "acctestrg-%d" 307 location = "West US" 308 } 309 310 resource "azurerm_public_ip" "test" { 311 name = "test-ip-%d" 312 location = "West US" 313 resource_group_name = "${azurerm_resource_group.test.name}" 314 public_ip_address_allocation = "static" 315 } 316 317 resource "azurerm_lb" "test" { 318 name = "arm-test-loadbalancer-%d" 319 location = "West US" 320 resource_group_name = "${azurerm_resource_group.test.name}" 321 322 frontend_ip_configuration { 323 name = "one-%d" 324 public_ip_address_id = "${azurerm_public_ip.test.id}" 325 } 326 } 327 328 resource "azurerm_lb_probe" "test" { 329 location = "West US" 330 resource_group_name = "${azurerm_resource_group.test.name}" 331 loadbalancer_id = "${azurerm_lb.test.id}" 332 name = "%s" 333 port = 22 334 } 335 336 resource "azurerm_lb_probe" "test2" { 337 location = "West US" 338 resource_group_name = "${azurerm_resource_group.test.name}" 339 loadbalancer_id = "${azurerm_lb.test.id}" 340 name = "%s" 341 port = 80 342 } 343 `, rInt, rInt, rInt, rInt, probeName, probe2Name) 344 } 345 346 func testAccAzureRMLoadBalancerProbe_multipleProbesUpdate(rInt int, probeName, probe2Name string) 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 371 resource "azurerm_lb_probe" "test" { 372 location = "West US" 373 resource_group_name = "${azurerm_resource_group.test.name}" 374 loadbalancer_id = "${azurerm_lb.test.id}" 375 name = "%s" 376 port = 22 377 } 378 379 resource "azurerm_lb_probe" "test2" { 380 location = "West US" 381 resource_group_name = "${azurerm_resource_group.test.name}" 382 loadbalancer_id = "${azurerm_lb.test.id}" 383 name = "%s" 384 port = 8080 385 } 386 `, rInt, rInt, rInt, rInt, probeName, probe2Name) 387 } 388 389 func testAccAzureRMLoadBalancerProbe_updateProtocolBefore(rInt int, probeName string) string { 390 return fmt.Sprintf(` 391 resource "azurerm_resource_group" "test" { 392 name = "acctestrg-%d" 393 location = "West US" 394 } 395 396 resource "azurerm_public_ip" "test" { 397 name = "test-ip-%d" 398 location = "West US" 399 resource_group_name = "${azurerm_resource_group.test.name}" 400 public_ip_address_allocation = "static" 401 } 402 403 resource "azurerm_lb" "test" { 404 name = "arm-test-loadbalancer-%d" 405 location = "West US" 406 resource_group_name = "${azurerm_resource_group.test.name}" 407 408 frontend_ip_configuration { 409 name = "one-%d" 410 public_ip_address_id = "${azurerm_public_ip.test.id}" 411 } 412 } 413 414 resource "azurerm_lb_probe" "test" { 415 location = "West US" 416 resource_group_name = "${azurerm_resource_group.test.name}" 417 loadbalancer_id = "${azurerm_lb.test.id}" 418 name = "%s" 419 protocol = "Http" 420 request_path = "/" 421 port = 80 422 } 423 `, rInt, rInt, rInt, rInt, probeName) 424 } 425 426 func testAccAzureRMLoadBalancerProbe_updateProtocolAfter(rInt int, probeName string) string { 427 return fmt.Sprintf(` 428 resource "azurerm_resource_group" "test" { 429 name = "acctestrg-%d" 430 location = "West US" 431 } 432 433 resource "azurerm_public_ip" "test" { 434 name = "test-ip-%d" 435 location = "West US" 436 resource_group_name = "${azurerm_resource_group.test.name}" 437 public_ip_address_allocation = "static" 438 } 439 440 resource "azurerm_lb" "test" { 441 name = "arm-test-loadbalancer-%d" 442 location = "West US" 443 resource_group_name = "${azurerm_resource_group.test.name}" 444 445 frontend_ip_configuration { 446 name = "one-%d" 447 public_ip_address_id = "${azurerm_public_ip.test.id}" 448 } 449 } 450 451 resource "azurerm_lb_probe" "test" { 452 location = "West US" 453 resource_group_name = "${azurerm_resource_group.test.name}" 454 loadbalancer_id = "${azurerm_lb.test.id}" 455 name = "%s" 456 protocol = "Tcp" 457 port = 80 458 } 459 `, rInt, rInt, rInt, rInt, probeName) 460 }