github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/azurerm/resource_arm_public_ip_test.go (about) 1 package azurerm 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/hashicorp/terraform/helper/acctest" 9 "github.com/hashicorp/terraform/helper/resource" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 func TestResourceAzureRMPublicIpAllocation_validation(t *testing.T) { 14 cases := []struct { 15 Value string 16 ErrCount int 17 }{ 18 { 19 Value: "Random", 20 ErrCount: 1, 21 }, 22 { 23 Value: "Static", 24 ErrCount: 0, 25 }, 26 { 27 Value: "Dynamic", 28 ErrCount: 0, 29 }, 30 { 31 Value: "STATIC", 32 ErrCount: 0, 33 }, 34 { 35 Value: "static", 36 ErrCount: 0, 37 }, 38 } 39 40 for _, tc := range cases { 41 _, errors := validatePublicIpAllocation(tc.Value, "azurerm_public_ip") 42 43 if len(errors) != tc.ErrCount { 44 t.Fatalf("Expected the Azure RM Public IP allocation to trigger a validation error") 45 } 46 } 47 } 48 49 func TestResourceAzureRMPublicIpDomainNameLabel_validation(t *testing.T) { 50 cases := []struct { 51 Value string 52 ErrCount int 53 }{ 54 { 55 Value: "tEsting123", 56 ErrCount: 1, 57 }, 58 { 59 Value: "testing123!", 60 ErrCount: 1, 61 }, 62 { 63 Value: "testing123-", 64 ErrCount: 1, 65 }, 66 { 67 Value: acctest.RandString(80), 68 ErrCount: 1, 69 }, 70 } 71 72 for _, tc := range cases { 73 _, errors := validatePublicIpDomainNameLabel(tc.Value, "azurerm_public_ip") 74 75 if len(errors) != tc.ErrCount { 76 t.Fatalf("Expected the Azure RM Public IP Domain Name Label to trigger a validation error") 77 } 78 } 79 } 80 81 func TestAccAzureRMPublicIpStatic_basic(t *testing.T) { 82 83 ri := acctest.RandInt() 84 config := fmt.Sprintf(testAccAzureRMVPublicIpStatic_basic, ri, ri) 85 86 resource.Test(t, resource.TestCase{ 87 PreCheck: func() { testAccPreCheck(t) }, 88 Providers: testAccProviders, 89 CheckDestroy: testCheckAzureRMPublicIpDestroy, 90 Steps: []resource.TestStep{ 91 resource.TestStep{ 92 Config: config, 93 Check: resource.ComposeTestCheckFunc( 94 testCheckAzureRMPublicIpExists("azurerm_public_ip.test"), 95 ), 96 }, 97 }, 98 }) 99 } 100 101 func TestAccAzureRMPublicIpStatic_disappears(t *testing.T) { 102 103 ri := acctest.RandInt() 104 config := fmt.Sprintf(testAccAzureRMVPublicIpStatic_basic, ri, ri) 105 106 resource.Test(t, resource.TestCase{ 107 PreCheck: func() { testAccPreCheck(t) }, 108 Providers: testAccProviders, 109 CheckDestroy: testCheckAzureRMPublicIpDestroy, 110 Steps: []resource.TestStep{ 111 resource.TestStep{ 112 Config: config, 113 Check: resource.ComposeTestCheckFunc( 114 testCheckAzureRMPublicIpExists("azurerm_public_ip.test"), 115 testCheckAzureRMPublicIpDisappears("azurerm_public_ip.test"), 116 ), 117 ExpectNonEmptyPlan: true, 118 }, 119 }, 120 }) 121 } 122 123 func TestAccAzureRMPublicIpStatic_idleTimeout(t *testing.T) { 124 125 ri := acctest.RandInt() 126 config := fmt.Sprintf(testAccAzureRMVPublicIpStatic_idleTimeout, ri, ri) 127 128 resource.Test(t, resource.TestCase{ 129 PreCheck: func() { testAccPreCheck(t) }, 130 Providers: testAccProviders, 131 CheckDestroy: testCheckAzureRMPublicIpDestroy, 132 Steps: []resource.TestStep{ 133 resource.TestStep{ 134 Config: config, 135 Check: resource.ComposeTestCheckFunc( 136 testCheckAzureRMPublicIpExists("azurerm_public_ip.test"), 137 resource.TestCheckResourceAttr( 138 "azurerm_public_ip.test", 139 "idle_timeout_in_minutes", 140 "30", 141 ), 142 ), 143 }, 144 }, 145 }) 146 } 147 148 func TestAccAzureRMPublicIpStatic_withTags(t *testing.T) { 149 150 ri := acctest.RandInt() 151 preConfig := fmt.Sprintf(testAccAzureRMVPublicIpStatic_withTags, ri, ri) 152 postConfig := fmt.Sprintf(testAccAzureRMVPublicIpStatic_withTagsUpdate, ri, ri) 153 154 resource.Test(t, resource.TestCase{ 155 PreCheck: func() { testAccPreCheck(t) }, 156 Providers: testAccProviders, 157 CheckDestroy: testCheckAzureRMPublicIpDestroy, 158 Steps: []resource.TestStep{ 159 resource.TestStep{ 160 Config: preConfig, 161 Check: resource.ComposeTestCheckFunc( 162 testCheckAzureRMPublicIpExists("azurerm_public_ip.test"), 163 resource.TestCheckResourceAttr( 164 "azurerm_public_ip.test", "tags.%", "2"), 165 resource.TestCheckResourceAttr( 166 "azurerm_public_ip.test", "tags.environment", "Production"), 167 resource.TestCheckResourceAttr( 168 "azurerm_public_ip.test", "tags.cost_center", "MSFT"), 169 ), 170 }, 171 172 resource.TestStep{ 173 Config: postConfig, 174 Check: resource.ComposeTestCheckFunc( 175 testCheckAzureRMPublicIpExists("azurerm_public_ip.test"), 176 resource.TestCheckResourceAttr( 177 "azurerm_public_ip.test", "tags.%", "1"), 178 resource.TestCheckResourceAttr( 179 "azurerm_public_ip.test", "tags.environment", "staging"), 180 ), 181 }, 182 }, 183 }) 184 } 185 186 func TestAccAzureRMPublicIpStatic_update(t *testing.T) { 187 188 ri := acctest.RandInt() 189 preConfig := fmt.Sprintf(testAccAzureRMVPublicIpStatic_basic, ri, ri) 190 postConfig := fmt.Sprintf(testAccAzureRMVPublicIpStatic_update, ri, ri) 191 192 resource.Test(t, resource.TestCase{ 193 PreCheck: func() { testAccPreCheck(t) }, 194 Providers: testAccProviders, 195 CheckDestroy: testCheckAzureRMPublicIpDestroy, 196 Steps: []resource.TestStep{ 197 resource.TestStep{ 198 Config: preConfig, 199 Check: resource.ComposeTestCheckFunc( 200 testCheckAzureRMPublicIpExists("azurerm_public_ip.test"), 201 ), 202 }, 203 204 resource.TestStep{ 205 Config: postConfig, 206 Check: resource.ComposeTestCheckFunc( 207 testCheckAzureRMPublicIpExists("azurerm_public_ip.test"), 208 resource.TestCheckResourceAttr( 209 "azurerm_public_ip.test", "domain_name_label", "mylabel01"), 210 ), 211 }, 212 }, 213 }) 214 } 215 216 func TestAccAzureRMPublicIpDynamic_basic(t *testing.T) { 217 218 ri := acctest.RandInt() 219 config := fmt.Sprintf(testAccAzureRMVPublicIpDynamic_basic, ri, ri) 220 221 resource.Test(t, resource.TestCase{ 222 PreCheck: func() { testAccPreCheck(t) }, 223 Providers: testAccProviders, 224 CheckDestroy: testCheckAzureRMPublicIpDestroy, 225 Steps: []resource.TestStep{ 226 resource.TestStep{ 227 Config: config, 228 Check: resource.ComposeTestCheckFunc( 229 testCheckAzureRMPublicIpExists("azurerm_public_ip.test"), 230 ), 231 }, 232 }, 233 }) 234 } 235 236 func testCheckAzureRMPublicIpExists(name string) resource.TestCheckFunc { 237 return func(s *terraform.State) error { 238 // Ensure we have enough information in state to look up in API 239 rs, ok := s.RootModule().Resources[name] 240 if !ok { 241 return fmt.Errorf("Not found: %s", name) 242 } 243 244 availSetName := rs.Primary.Attributes["name"] 245 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 246 if !hasResourceGroup { 247 return fmt.Errorf("Bad: no resource group found in state for public ip: %s", availSetName) 248 } 249 250 conn := testAccProvider.Meta().(*ArmClient).publicIPClient 251 252 resp, err := conn.Get(resourceGroup, availSetName, "") 253 if err != nil { 254 return fmt.Errorf("Bad: Get on publicIPClient: %s", err) 255 } 256 257 if resp.StatusCode == http.StatusNotFound { 258 return fmt.Errorf("Bad: Public IP %q (resource group: %q) does not exist", name, resourceGroup) 259 } 260 261 return nil 262 } 263 } 264 265 func testCheckAzureRMPublicIpDisappears(name string) resource.TestCheckFunc { 266 return func(s *terraform.State) error { 267 // Ensure we have enough information in state to look up in API 268 rs, ok := s.RootModule().Resources[name] 269 if !ok { 270 return fmt.Errorf("Not found: %s", name) 271 } 272 273 publicIpName := rs.Primary.Attributes["name"] 274 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 275 if !hasResourceGroup { 276 return fmt.Errorf("Bad: no resource group found in state for public ip: %s", publicIpName) 277 } 278 279 conn := testAccProvider.Meta().(*ArmClient).publicIPClient 280 281 _, error := conn.Delete(resourceGroup, publicIpName, make(chan struct{})) 282 err := <-error 283 if err != nil { 284 return fmt.Errorf("Bad: Delete on publicIPClient: %s", err) 285 } 286 287 return nil 288 } 289 } 290 291 func testCheckAzureRMPublicIpDestroy(s *terraform.State) error { 292 conn := testAccProvider.Meta().(*ArmClient).publicIPClient 293 294 for _, rs := range s.RootModule().Resources { 295 if rs.Type != "azurerm_public_ip" { 296 continue 297 } 298 299 name := rs.Primary.Attributes["name"] 300 resourceGroup := rs.Primary.Attributes["resource_group_name"] 301 302 resp, err := conn.Get(resourceGroup, name, "") 303 304 if err != nil { 305 return nil 306 } 307 308 if resp.StatusCode != http.StatusNotFound { 309 return fmt.Errorf("Public IP still exists:\n%#v", resp.PublicIPAddressPropertiesFormat) 310 } 311 } 312 313 return nil 314 } 315 316 var testAccAzureRMVPublicIpStatic_basic = ` 317 resource "azurerm_resource_group" "test" { 318 name = "acctestRG-%d" 319 location = "West US" 320 } 321 resource "azurerm_public_ip" "test" { 322 name = "acctestpublicip-%d" 323 location = "West US" 324 resource_group_name = "${azurerm_resource_group.test.name}" 325 public_ip_address_allocation = "static" 326 } 327 ` 328 329 var testAccAzureRMVPublicIpStatic_update = ` 330 resource "azurerm_resource_group" "test" { 331 name = "acctestRG-%d" 332 location = "West US" 333 } 334 resource "azurerm_public_ip" "test" { 335 name = "acctestpublicip-%d" 336 location = "West US" 337 resource_group_name = "${azurerm_resource_group.test.name}" 338 public_ip_address_allocation = "static" 339 domain_name_label = "mylabel01" 340 } 341 ` 342 343 var testAccAzureRMVPublicIpStatic_idleTimeout = ` 344 resource "azurerm_resource_group" "test" { 345 name = "acctestRG-%d" 346 location = "West US" 347 } 348 resource "azurerm_public_ip" "test" { 349 name = "acctestpublicip-%d" 350 location = "West US" 351 resource_group_name = "${azurerm_resource_group.test.name}" 352 public_ip_address_allocation = "static" 353 idle_timeout_in_minutes = 30 354 } 355 ` 356 357 var testAccAzureRMVPublicIpDynamic_basic = ` 358 resource "azurerm_resource_group" "test" { 359 name = "acctestRG-%d" 360 location = "West US" 361 } 362 resource "azurerm_public_ip" "test" { 363 name = "acctestpublicip-%d" 364 location = "West US" 365 resource_group_name = "${azurerm_resource_group.test.name}" 366 public_ip_address_allocation = "dynamic" 367 } 368 ` 369 370 var testAccAzureRMVPublicIpStatic_withTags = ` 371 resource "azurerm_resource_group" "test" { 372 name = "acctestRG-%d" 373 location = "West US" 374 } 375 resource "azurerm_public_ip" "test" { 376 name = "acctestpublicip-%d" 377 location = "West US" 378 resource_group_name = "${azurerm_resource_group.test.name}" 379 public_ip_address_allocation = "static" 380 381 tags { 382 environment = "Production" 383 cost_center = "MSFT" 384 } 385 } 386 ` 387 388 var testAccAzureRMVPublicIpStatic_withTagsUpdate = ` 389 resource "azurerm_resource_group" "test" { 390 name = "acctestRG-%d" 391 location = "West US" 392 } 393 resource "azurerm_public_ip" "test" { 394 name = "acctestpublicip-%d" 395 location = "West US" 396 resource_group_name = "${azurerm_resource_group.test.name}" 397 public_ip_address_allocation = "static" 398 399 tags { 400 environment = "staging" 401 } 402 } 403 `