github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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 _, err := conn.Delete(resourceGroup, publicIpName, make(chan struct{})) 282 if err != nil { 283 return fmt.Errorf("Bad: Delete on publicIPClient: %s", err) 284 } 285 286 return nil 287 } 288 } 289 290 func testCheckAzureRMPublicIpDestroy(s *terraform.State) error { 291 conn := testAccProvider.Meta().(*ArmClient).publicIPClient 292 293 for _, rs := range s.RootModule().Resources { 294 if rs.Type != "azurerm_public_ip" { 295 continue 296 } 297 298 name := rs.Primary.Attributes["name"] 299 resourceGroup := rs.Primary.Attributes["resource_group_name"] 300 301 resp, err := conn.Get(resourceGroup, name, "") 302 303 if err != nil { 304 return nil 305 } 306 307 if resp.StatusCode != http.StatusNotFound { 308 return fmt.Errorf("Public IP still exists:\n%#v", resp.PublicIPAddressPropertiesFormat) 309 } 310 } 311 312 return nil 313 } 314 315 var testAccAzureRMVPublicIpStatic_basic = ` 316 resource "azurerm_resource_group" "test" { 317 name = "acctestRG-%d" 318 location = "West US" 319 } 320 resource "azurerm_public_ip" "test" { 321 name = "acctestpublicip-%d" 322 location = "West US" 323 resource_group_name = "${azurerm_resource_group.test.name}" 324 public_ip_address_allocation = "static" 325 } 326 ` 327 328 var testAccAzureRMVPublicIpStatic_update = ` 329 resource "azurerm_resource_group" "test" { 330 name = "acctestRG-%d" 331 location = "West US" 332 } 333 resource "azurerm_public_ip" "test" { 334 name = "acctestpublicip-%d" 335 location = "West US" 336 resource_group_name = "${azurerm_resource_group.test.name}" 337 public_ip_address_allocation = "static" 338 domain_name_label = "mylabel01" 339 } 340 ` 341 342 var testAccAzureRMVPublicIpStatic_idleTimeout = ` 343 resource "azurerm_resource_group" "test" { 344 name = "acctestRG-%d" 345 location = "West US" 346 } 347 resource "azurerm_public_ip" "test" { 348 name = "acctestpublicip-%d" 349 location = "West US" 350 resource_group_name = "${azurerm_resource_group.test.name}" 351 public_ip_address_allocation = "static" 352 idle_timeout_in_minutes = 30 353 } 354 ` 355 356 var testAccAzureRMVPublicIpDynamic_basic = ` 357 resource "azurerm_resource_group" "test" { 358 name = "acctestRG-%d" 359 location = "West US" 360 } 361 resource "azurerm_public_ip" "test" { 362 name = "acctestpublicip-%d" 363 location = "West US" 364 resource_group_name = "${azurerm_resource_group.test.name}" 365 public_ip_address_allocation = "dynamic" 366 } 367 ` 368 369 var testAccAzureRMVPublicIpStatic_withTags = ` 370 resource "azurerm_resource_group" "test" { 371 name = "acctestRG-%d" 372 location = "West US" 373 } 374 resource "azurerm_public_ip" "test" { 375 name = "acctestpublicip-%d" 376 location = "West US" 377 resource_group_name = "${azurerm_resource_group.test.name}" 378 public_ip_address_allocation = "static" 379 380 tags { 381 environment = "Production" 382 cost_center = "MSFT" 383 } 384 } 385 ` 386 387 var testAccAzureRMVPublicIpStatic_withTagsUpdate = ` 388 resource "azurerm_resource_group" "test" { 389 name = "acctestRG-%d" 390 location = "West US" 391 } 392 resource "azurerm_public_ip" "test" { 393 name = "acctestpublicip-%d" 394 location = "West US" 395 resource_group_name = "${azurerm_resource_group.test.name}" 396 public_ip_address_allocation = "static" 397 398 tags { 399 environment = "staging" 400 } 401 } 402 `