github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/azurerm/resource_arm_template_deployment_test.go (about) 1 package azurerm 2 3 import ( 4 "fmt" 5 "net/http" 6 "regexp" 7 "testing" 8 9 "github.com/hashicorp/terraform/helper/acctest" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAzureRMTemplateDeployment_basic(t *testing.T) { 15 ri := acctest.RandInt() 16 config := fmt.Sprintf(testAccAzureRMTemplateDeployment_basicMultiple, ri, ri) 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testCheckAzureRMTemplateDeploymentDestroy, 21 Steps: []resource.TestStep{ 22 { 23 Config: config, 24 Check: resource.ComposeTestCheckFunc( 25 testCheckAzureRMTemplateDeploymentExists("azurerm_template_deployment.test"), 26 ), 27 }, 28 }, 29 }) 30 } 31 32 func TestAccAzureRMTemplateDeployment_disappears(t *testing.T) { 33 ri := acctest.RandInt() 34 config := fmt.Sprintf(testAccAzureRMTemplateDeployment_basicSingle, ri, ri, ri) 35 resource.Test(t, resource.TestCase{ 36 PreCheck: func() { testAccPreCheck(t) }, 37 Providers: testAccProviders, 38 CheckDestroy: testCheckAzureRMTemplateDeploymentDestroy, 39 Steps: []resource.TestStep{ 40 { 41 Config: config, 42 Check: resource.ComposeTestCheckFunc( 43 testCheckAzureRMTemplateDeploymentExists("azurerm_template_deployment.test"), 44 testCheckAzureRMTemplateDeploymentDisappears("azurerm_template_deployment.test"), 45 ), 46 ExpectNonEmptyPlan: true, 47 }, 48 }, 49 }) 50 } 51 52 func TestAccAzureRMTemplateDeployment_withParams(t *testing.T) { 53 ri := acctest.RandInt() 54 config := fmt.Sprintf(testAccAzureRMTemplateDeployment_withParams, ri, ri, ri) 55 resource.Test(t, resource.TestCase{ 56 PreCheck: func() { testAccPreCheck(t) }, 57 Providers: testAccProviders, 58 CheckDestroy: testCheckAzureRMTemplateDeploymentDestroy, 59 Steps: []resource.TestStep{ 60 { 61 Config: config, 62 Check: resource.ComposeTestCheckFunc( 63 testCheckAzureRMTemplateDeploymentExists("azurerm_template_deployment.test"), 64 resource.TestCheckResourceAttr("azurerm_template_deployment.test", "outputs.testOutput", "Output Value"), 65 ), 66 }, 67 }, 68 }) 69 } 70 71 func TestAccAzureRMTemplateDeployment_withOutputs(t *testing.T) { 72 ri := acctest.RandInt() 73 config := fmt.Sprintf(testAccAzureRMTemplateDeployment_withOutputs, ri, ri, ri) 74 resource.Test(t, resource.TestCase{ 75 PreCheck: func() { testAccPreCheck(t) }, 76 Providers: testAccProviders, 77 CheckDestroy: testCheckAzureRMTemplateDeploymentDestroy, 78 Steps: []resource.TestStep{ 79 { 80 Config: config, 81 Check: resource.ComposeTestCheckFunc( 82 testCheckAzureRMTemplateDeploymentExists("azurerm_template_deployment.test"), 83 resource.TestCheckOutput("tfIntOutput", "-123"), 84 resource.TestCheckOutput("tfStringOutput", "Standard_GRS"), 85 resource.TestCheckOutput("tfFalseOutput", "false"), 86 resource.TestCheckOutput("tfTrueOutput", "true"), 87 resource.TestCheckResourceAttr("azurerm_template_deployment.test", "outputs.stringOutput", "Standard_GRS"), 88 ), 89 }, 90 }, 91 }) 92 } 93 94 func TestAccAzureRMTemplateDeployment_withError(t *testing.T) { 95 ri := acctest.RandInt() 96 config := fmt.Sprintf(testAccAzureRMTemplateDeployment_withError, ri, ri) 97 resource.Test(t, resource.TestCase{ 98 PreCheck: func() { testAccPreCheck(t) }, 99 Providers: testAccProviders, 100 CheckDestroy: testCheckAzureRMTemplateDeploymentDestroy, 101 Steps: []resource.TestStep{ 102 { 103 Config: config, 104 ExpectError: regexp.MustCompile("The deployment operation failed"), 105 }, 106 }, 107 }) 108 } 109 110 func testCheckAzureRMTemplateDeploymentExists(name string) resource.TestCheckFunc { 111 return func(s *terraform.State) error { 112 // Ensure we have enough information in state to look up in API 113 rs, ok := s.RootModule().Resources[name] 114 if !ok { 115 return fmt.Errorf("Not found: %s", name) 116 } 117 118 name := rs.Primary.Attributes["name"] 119 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 120 if !hasResourceGroup { 121 return fmt.Errorf("Bad: no resource group found in state for template deployment: %s", name) 122 } 123 124 conn := testAccProvider.Meta().(*ArmClient).deploymentsClient 125 126 resp, err := conn.Get(resourceGroup, name) 127 if err != nil { 128 return fmt.Errorf("Bad: Get on deploymentsClient: %s", err) 129 } 130 131 if resp.StatusCode == http.StatusNotFound { 132 return fmt.Errorf("Bad: TemplateDeployment %q (resource group: %q) does not exist", name, resourceGroup) 133 } 134 135 return nil 136 } 137 } 138 139 func testCheckAzureRMTemplateDeploymentDisappears(name string) resource.TestCheckFunc { 140 return func(s *terraform.State) error { 141 // Ensure we have enough information in state to look up in API 142 rs, ok := s.RootModule().Resources[name] 143 if !ok { 144 return fmt.Errorf("Not found: %s", name) 145 } 146 147 name := rs.Primary.Attributes["name"] 148 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 149 if !hasResourceGroup { 150 return fmt.Errorf("Bad: no resource group found in state for template deployment: %s", name) 151 } 152 153 conn := testAccProvider.Meta().(*ArmClient).deploymentsClient 154 155 _, err := conn.Delete(resourceGroup, name, make(chan struct{})) 156 if err != nil { 157 return fmt.Errorf("Bad: Delete on deploymentsClient: %s", err) 158 } 159 160 return nil 161 } 162 } 163 164 func testCheckAzureRMTemplateDeploymentDestroy(s *terraform.State) error { 165 conn := testAccProvider.Meta().(*ArmClient).vmClient 166 167 for _, rs := range s.RootModule().Resources { 168 if rs.Type != "azurerm_template_deployment" { 169 continue 170 } 171 172 name := rs.Primary.Attributes["name"] 173 resourceGroup := rs.Primary.Attributes["resource_group_name"] 174 175 resp, err := conn.Get(resourceGroup, name, "") 176 177 if err != nil { 178 return nil 179 } 180 181 if resp.StatusCode != http.StatusNotFound { 182 return fmt.Errorf("Template Deployment still exists:\n%#v", resp.VirtualMachineProperties) 183 } 184 } 185 186 return nil 187 } 188 189 var testAccAzureRMTemplateDeployment_basicSingle = ` 190 resource "azurerm_resource_group" "test" { 191 name = "acctestRG-%d" 192 location = "West US" 193 } 194 195 resource "azurerm_template_deployment" "test" { 196 name = "acctesttemplate-%d" 197 resource_group_name = "${azurerm_resource_group.test.name}" 198 template_body = <<DEPLOY 199 { 200 "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", 201 "contentVersion": "1.0.0.0", 202 "variables": { 203 "location": "[resourceGroup().location]", 204 "publicIPAddressType": "Dynamic", 205 "apiVersion": "2015-06-15", 206 "dnsLabelPrefix": "[concat('terraform-tdacctest', uniquestring(resourceGroup().id))]" 207 }, 208 "resources": [ 209 { 210 "type": "Microsoft.Network/publicIPAddresses", 211 "apiVersion": "[variables('apiVersion')]", 212 "name": "acctestpip-%d", 213 "location": "[variables('location')]", 214 "properties": { 215 "publicIPAllocationMethod": "[variables('publicIPAddressType')]", 216 "dnsSettings": { 217 "domainNameLabel": "[variables('dnsLabelPrefix')]" 218 } 219 } 220 } 221 ] 222 } 223 DEPLOY 224 deployment_mode = "Complete" 225 } 226 227 ` 228 229 var testAccAzureRMTemplateDeployment_basicMultiple = ` 230 resource "azurerm_resource_group" "test" { 231 name = "acctestRG-%d" 232 location = "West US" 233 } 234 235 resource "azurerm_template_deployment" "test" { 236 name = "acctesttemplate-%d" 237 resource_group_name = "${azurerm_resource_group.test.name}" 238 template_body = <<DEPLOY 239 { 240 "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", 241 "contentVersion": "1.0.0.0", 242 "parameters": { 243 "storageAccountType": { 244 "type": "string", 245 "defaultValue": "Standard_LRS", 246 "allowedValues": [ 247 "Standard_LRS", 248 "Standard_GRS", 249 "Standard_ZRS" 250 ], 251 "metadata": { 252 "description": "Storage Account type" 253 } 254 } 255 }, 256 "variables": { 257 "location": "[resourceGroup().location]", 258 "storageAccountName": "[concat(uniquestring(resourceGroup().id), 'storage')]", 259 "publicIPAddressName": "[concat('myPublicIp', uniquestring(resourceGroup().id))]", 260 "publicIPAddressType": "Dynamic", 261 "apiVersion": "2015-06-15", 262 "dnsLabelPrefix": "[concat('terraform-tdacctest', uniquestring(resourceGroup().id))]" 263 }, 264 "resources": [ 265 { 266 "type": "Microsoft.Storage/storageAccounts", 267 "name": "[variables('storageAccountName')]", 268 "apiVersion": "[variables('apiVersion')]", 269 "location": "[variables('location')]", 270 "properties": { 271 "accountType": "[parameters('storageAccountType')]" 272 } 273 }, 274 { 275 "type": "Microsoft.Network/publicIPAddresses", 276 "apiVersion": "[variables('apiVersion')]", 277 "name": "[variables('publicIPAddressName')]", 278 "location": "[variables('location')]", 279 "properties": { 280 "publicIPAllocationMethod": "[variables('publicIPAddressType')]", 281 "dnsSettings": { 282 "domainNameLabel": "[variables('dnsLabelPrefix')]" 283 } 284 } 285 } 286 ] 287 } 288 DEPLOY 289 deployment_mode = "Complete" 290 } 291 292 ` 293 294 var testAccAzureRMTemplateDeployment_withParams = ` 295 resource "azurerm_resource_group" "test" { 296 name = "acctestRG-%d" 297 location = "West US" 298 } 299 300 output "test" { 301 value = "${azurerm_template_deployment.test.outputs["testOutput"]}" 302 } 303 304 resource "azurerm_storage_container" "using-outputs" { 305 name = "vhds" 306 resource_group_name = "${azurerm_resource_group.test.name}" 307 storage_account_name = "${azurerm_template_deployment.test.outputs["accountName"]}" 308 container_access_type = "private" 309 } 310 311 resource "azurerm_template_deployment" "test" { 312 name = "acctesttemplate-%d" 313 resource_group_name = "${azurerm_resource_group.test.name}" 314 template_body = <<DEPLOY 315 { 316 "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", 317 "contentVersion": "1.0.0.0", 318 "parameters": { 319 "storageAccountType": { 320 "type": "string", 321 "defaultValue": "Standard_LRS", 322 "allowedValues": [ 323 "Standard_LRS", 324 "Standard_GRS", 325 "Standard_ZRS" 326 ], 327 "metadata": { 328 "description": "Storage Account type" 329 } 330 }, 331 "dnsLabelPrefix": { 332 "type": "string", 333 "metadata": { 334 "description": "DNS Label for the Public IP. Must be lowercase. It should match with the following regular expression: ^[a-z][a-z0-9-]{1,61}[a-z0-9]$ or it will raise an error." 335 } 336 } 337 }, 338 "variables": { 339 "location": "[resourceGroup().location]", 340 "storageAccountName": "[concat(uniquestring(resourceGroup().id), 'storage')]", 341 "publicIPAddressName": "[concat('myPublicIp', uniquestring(resourceGroup().id))]", 342 "publicIPAddressType": "Dynamic", 343 "apiVersion": "2015-06-15" 344 }, 345 "resources": [ 346 { 347 "type": "Microsoft.Storage/storageAccounts", 348 "name": "[variables('storageAccountName')]", 349 "apiVersion": "[variables('apiVersion')]", 350 "location": "[variables('location')]", 351 "properties": { 352 "accountType": "[parameters('storageAccountType')]" 353 } 354 }, 355 { 356 "type": "Microsoft.Network/publicIPAddresses", 357 "apiVersion": "[variables('apiVersion')]", 358 "name": "[variables('publicIPAddressName')]", 359 "location": "[variables('location')]", 360 "properties": { 361 "publicIPAllocationMethod": "[variables('publicIPAddressType')]", 362 "dnsSettings": { 363 "domainNameLabel": "[parameters('dnsLabelPrefix')]" 364 } 365 } 366 } 367 ], 368 "outputs": { 369 "testOutput": { 370 "type": "string", 371 "value": "Output Value" 372 }, 373 "accountName": { 374 "type": "string", 375 "value": "[variables('storageAccountName')]" 376 } 377 } 378 } 379 DEPLOY 380 parameters { 381 dnsLabelPrefix = "terraform-test-%d" 382 storageAccountType = "Standard_GRS" 383 } 384 deployment_mode = "Complete" 385 } 386 387 ` 388 389 var testAccAzureRMTemplateDeployment_withOutputs = ` 390 resource "azurerm_resource_group" "test" { 391 name = "acctestRG-%d" 392 location = "West US" 393 } 394 395 output "tfStringOutput" { 396 value = "${azurerm_template_deployment.test.outputs.stringOutput}" 397 } 398 399 output "tfIntOutput" { 400 value = "${azurerm_template_deployment.test.outputs.intOutput}" 401 } 402 403 output "tfFalseOutput" { 404 value = "${azurerm_template_deployment.test.outputs.falseOutput}" 405 } 406 407 output "tfTrueOutput" { 408 value = "${azurerm_template_deployment.test.outputs.trueOutput}" 409 } 410 411 resource "azurerm_template_deployment" "test" { 412 name = "acctesttemplate-%d" 413 resource_group_name = "${azurerm_resource_group.test.name}" 414 template_body = <<DEPLOY 415 { 416 "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", 417 "contentVersion": "1.0.0.0", 418 "parameters": { 419 "storageAccountType": { 420 "type": "string", 421 "defaultValue": "Standard_LRS", 422 "allowedValues": [ 423 "Standard_LRS", 424 "Standard_GRS", 425 "Standard_ZRS" 426 ], 427 "metadata": { 428 "description": "Storage Account type" 429 } 430 }, 431 "dnsLabelPrefix": { 432 "type": "string", 433 "metadata": { 434 "description": "DNS Label for the Public IP. Must be lowercase. It should match with the following regular expression: ^[a-z][a-z0-9-]{1,61}[a-z0-9]$ or it will raise an error." 435 } 436 }, 437 "intParameter": { 438 "type": "int", 439 "defaultValue": -123 440 }, 441 "falseParameter": { 442 "type": "bool", 443 "defaultValue": false 444 }, 445 "trueParameter": { 446 "type": "bool", 447 "defaultValue": true 448 } 449 }, 450 "variables": { 451 "location": "[resourceGroup().location]", 452 "storageAccountName": "[concat(uniquestring(resourceGroup().id), 'storage')]", 453 "publicIPAddressName": "[concat('myPublicIp', uniquestring(resourceGroup().id))]", 454 "publicIPAddressType": "Dynamic", 455 "apiVersion": "2015-06-15" 456 }, 457 "resources": [ 458 { 459 "type": "Microsoft.Storage/storageAccounts", 460 "name": "[variables('storageAccountName')]", 461 "apiVersion": "[variables('apiVersion')]", 462 "location": "[variables('location')]", 463 "properties": { 464 "accountType": "[parameters('storageAccountType')]" 465 } 466 }, 467 { 468 "type": "Microsoft.Network/publicIPAddresses", 469 "apiVersion": "[variables('apiVersion')]", 470 "name": "[variables('publicIPAddressName')]", 471 "location": "[variables('location')]", 472 "properties": { 473 "publicIPAllocationMethod": "[variables('publicIPAddressType')]", 474 "dnsSettings": { 475 "domainNameLabel": "[parameters('dnsLabelPrefix')]" 476 } 477 } 478 } 479 ], 480 "outputs": { 481 "stringOutput": { 482 "type": "string", 483 "value": "[parameters('storageAccountType')]" 484 }, 485 "intOutput": { 486 "type": "int", 487 "value": "[parameters('intParameter')]" 488 }, 489 "falseOutput": { 490 "type": "bool", 491 "value": "[parameters('falseParameter')]" 492 }, 493 "trueOutput": { 494 "type": "bool", 495 "value": "[parameters('trueParameter')]" 496 } 497 } 498 } 499 DEPLOY 500 parameters { 501 dnsLabelPrefix = "terraform-test-%d" 502 storageAccountType = "Standard_GRS" 503 } 504 deployment_mode = "Incremental" 505 } 506 507 ` 508 509 // StorageAccount name is too long, forces error 510 var testAccAzureRMTemplateDeployment_withError = ` 511 resource "azurerm_resource_group" "test" { 512 name = "acctestRG-%d" 513 location = "West US" 514 } 515 516 output "test" { 517 value = "${azurerm_template_deployment.test.outputs.testOutput}" 518 } 519 520 resource "azurerm_template_deployment" "test" { 521 name = "acctesttemplate-%d" 522 resource_group_name = "${azurerm_resource_group.test.name}" 523 template_body = <<DEPLOY 524 { 525 "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", 526 "contentVersion": "1.0.0.0", 527 "parameters": { 528 "storageAccountType": { 529 "type": "string", 530 "defaultValue": "Standard_LRS", 531 "allowedValues": [ 532 "Standard_LRS", 533 "Standard_GRS", 534 "Standard_ZRS" 535 ], 536 "metadata": { 537 "description": "Storage Account type" 538 } 539 } 540 }, 541 "variables": { 542 "location": "[resourceGroup().location]", 543 "storageAccountName": "badStorageAccountNameTooLong", 544 "apiVersion": "2015-06-15" 545 }, 546 "resources": [ 547 { 548 "type": "Microsoft.Storage/storageAccounts", 549 "name": "[variables('storageAccountName')]", 550 "apiVersion": "[variables('apiVersion')]", 551 "location": "[variables('location')]", 552 "properties": { 553 "accountType": "[parameters('storageAccountType')]" 554 } 555 } 556 ], 557 "outputs": { 558 "testOutput": { 559 "type": "string", 560 "value": "Output Value" 561 } 562 } 563 } 564 DEPLOY 565 parameters { 566 storageAccountType = "Standard_GRS" 567 } 568 deployment_mode = "Complete" 569 } 570 `