github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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_basicExample, 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_withParams(t *testing.T) { 33 ri := acctest.RandInt() 34 config := fmt.Sprintf(testAccAzureRMTemplateDeployment_withParams, 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 resource.TestCheckResourceAttr("azurerm_template_deployment.test", "outputs.testOutput", "Output Value"), 45 ), 46 }, 47 }, 48 }) 49 } 50 51 func TestAccAzureRMTemplateDeployment_withError(t *testing.T) { 52 ri := acctest.RandInt() 53 config := fmt.Sprintf(testAccAzureRMTemplateDeployment_withError, ri, ri) 54 resource.Test(t, resource.TestCase{ 55 PreCheck: func() { testAccPreCheck(t) }, 56 Providers: testAccProviders, 57 CheckDestroy: testCheckAzureRMTemplateDeploymentDestroy, 58 Steps: []resource.TestStep{ 59 { 60 Config: config, 61 ExpectError: regexp.MustCompile("The deployment operation failed"), 62 }, 63 }, 64 }) 65 } 66 67 func testCheckAzureRMTemplateDeploymentExists(name string) resource.TestCheckFunc { 68 return func(s *terraform.State) error { 69 // Ensure we have enough information in state to look up in API 70 rs, ok := s.RootModule().Resources[name] 71 if !ok { 72 return fmt.Errorf("Not found: %s", name) 73 } 74 75 name := rs.Primary.Attributes["name"] 76 resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] 77 if !hasResourceGroup { 78 return fmt.Errorf("Bad: no resource group found in state for template deployment: %s", name) 79 } 80 81 conn := testAccProvider.Meta().(*ArmClient).deploymentsClient 82 83 resp, err := conn.Get(resourceGroup, name) 84 if err != nil { 85 return fmt.Errorf("Bad: Get on deploymentsClient: %s", err) 86 } 87 88 if resp.StatusCode == http.StatusNotFound { 89 return fmt.Errorf("Bad: TemplateDeployment %q (resource group: %q) does not exist", name, resourceGroup) 90 } 91 92 return nil 93 } 94 } 95 96 func testCheckAzureRMTemplateDeploymentDestroy(s *terraform.State) error { 97 conn := testAccProvider.Meta().(*ArmClient).vmClient 98 99 for _, rs := range s.RootModule().Resources { 100 if rs.Type != "azurerm_template_deployment" { 101 continue 102 } 103 104 name := rs.Primary.Attributes["name"] 105 resourceGroup := rs.Primary.Attributes["resource_group_name"] 106 107 resp, err := conn.Get(resourceGroup, name, "") 108 109 if err != nil { 110 return nil 111 } 112 113 if resp.StatusCode != http.StatusNotFound { 114 return fmt.Errorf("Template Deployment still exists:\n%#v", resp.Properties) 115 } 116 } 117 118 return nil 119 } 120 121 var testAccAzureRMTemplateDeployment_basicExample = ` 122 resource "azurerm_resource_group" "test" { 123 name = "acctestrg-%d" 124 location = "West US" 125 } 126 127 resource "azurerm_template_deployment" "test" { 128 name = "acctesttemplate-%d" 129 resource_group_name = "${azurerm_resource_group.test.name}" 130 template_body = <<DEPLOY 131 { 132 "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", 133 "contentVersion": "1.0.0.0", 134 "parameters": { 135 "storageAccountType": { 136 "type": "string", 137 "defaultValue": "Standard_LRS", 138 "allowedValues": [ 139 "Standard_LRS", 140 "Standard_GRS", 141 "Standard_ZRS" 142 ], 143 "metadata": { 144 "description": "Storage Account type" 145 } 146 } 147 }, 148 "variables": { 149 "location": "[resourceGroup().location]", 150 "storageAccountName": "[concat(uniquestring(resourceGroup().id), 'storage')]", 151 "publicIPAddressName": "[concat('myPublicIp', uniquestring(resourceGroup().id))]", 152 "publicIPAddressType": "Dynamic", 153 "apiVersion": "2015-06-15", 154 "dnsLabelPrefix": "terraform-tdacctest" 155 }, 156 "resources": [ 157 { 158 "type": "Microsoft.Storage/storageAccounts", 159 "name": "[variables('storageAccountName')]", 160 "apiVersion": "[variables('apiVersion')]", 161 "location": "[variables('location')]", 162 "properties": { 163 "accountType": "[parameters('storageAccountType')]" 164 } 165 }, 166 { 167 "type": "Microsoft.Network/publicIPAddresses", 168 "apiVersion": "[variables('apiVersion')]", 169 "name": "[variables('publicIPAddressName')]", 170 "location": "[variables('location')]", 171 "properties": { 172 "publicIPAllocationMethod": "[variables('publicIPAddressType')]", 173 "dnsSettings": { 174 "domainNameLabel": "[variables('dnsLabelPrefix')]" 175 } 176 } 177 } 178 ] 179 } 180 DEPLOY 181 deployment_mode = "Complete" 182 } 183 184 ` 185 186 var testAccAzureRMTemplateDeployment_withParams = ` 187 resource "azurerm_resource_group" "test" { 188 name = "acctestrg-%d" 189 location = "West US" 190 } 191 192 output "test" { 193 value = "${azurerm_template_deployment.test.outputs.testOutput}" 194 } 195 196 resource "azurerm_template_deployment" "test" { 197 name = "acctesttemplate-%d" 198 resource_group_name = "${azurerm_resource_group.test.name}" 199 template_body = <<DEPLOY 200 { 201 "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", 202 "contentVersion": "1.0.0.0", 203 "parameters": { 204 "storageAccountType": { 205 "type": "string", 206 "defaultValue": "Standard_LRS", 207 "allowedValues": [ 208 "Standard_LRS", 209 "Standard_GRS", 210 "Standard_ZRS" 211 ], 212 "metadata": { 213 "description": "Storage Account type" 214 } 215 }, 216 "dnsLabelPrefix": { 217 "type": "string", 218 "metadata": { 219 "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." 220 } 221 } 222 }, 223 "variables": { 224 "location": "[resourceGroup().location]", 225 "storageAccountName": "[concat(uniquestring(resourceGroup().id), 'storage')]", 226 "publicIPAddressName": "[concat('myPublicIp', uniquestring(resourceGroup().id))]", 227 "publicIPAddressType": "Dynamic", 228 "apiVersion": "2015-06-15" 229 }, 230 "resources": [ 231 { 232 "type": "Microsoft.Storage/storageAccounts", 233 "name": "[variables('storageAccountName')]", 234 "apiVersion": "[variables('apiVersion')]", 235 "location": "[variables('location')]", 236 "properties": { 237 "accountType": "[parameters('storageAccountType')]" 238 } 239 }, 240 { 241 "type": "Microsoft.Network/publicIPAddresses", 242 "apiVersion": "[variables('apiVersion')]", 243 "name": "[variables('publicIPAddressName')]", 244 "location": "[variables('location')]", 245 "properties": { 246 "publicIPAllocationMethod": "[variables('publicIPAddressType')]", 247 "dnsSettings": { 248 "domainNameLabel": "[parameters('dnsLabelPrefix')]" 249 } 250 } 251 } 252 ], 253 "outputs": { 254 "testOutput": { 255 "type": "string", 256 "value": "Output Value" 257 } 258 } 259 } 260 DEPLOY 261 parameters { 262 dnsLabelPrefix = "terraform-test-%d" 263 storageAccountType = "Standard_GRS" 264 } 265 deployment_mode = "Complete" 266 } 267 268 ` 269 270 // StorageAccount name is too long, forces error 271 var testAccAzureRMTemplateDeployment_withError = ` 272 resource "azurerm_resource_group" "test" { 273 name = "acctestrg-%d" 274 location = "West US" 275 } 276 277 output "test" { 278 value = "${azurerm_template_deployment.test.outputs.testOutput}" 279 } 280 281 resource "azurerm_template_deployment" "test" { 282 name = "acctesttemplate-%d" 283 resource_group_name = "${azurerm_resource_group.test.name}" 284 template_body = <<DEPLOY 285 { 286 "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", 287 "contentVersion": "1.0.0.0", 288 "parameters": { 289 "storageAccountType": { 290 "type": "string", 291 "defaultValue": "Standard_LRS", 292 "allowedValues": [ 293 "Standard_LRS", 294 "Standard_GRS", 295 "Standard_ZRS" 296 ], 297 "metadata": { 298 "description": "Storage Account type" 299 } 300 } 301 }, 302 "variables": { 303 "location": "[resourceGroup().location]", 304 "storageAccountName": "badStorageAccountNameTooLong", 305 "apiVersion": "2015-06-15" 306 }, 307 "resources": [ 308 { 309 "type": "Microsoft.Storage/storageAccounts", 310 "name": "[variables('storageAccountName')]", 311 "apiVersion": "[variables('apiVersion')]", 312 "location": "[variables('location')]", 313 "properties": { 314 "accountType": "[parameters('storageAccountType')]" 315 } 316 } 317 ], 318 "outputs": { 319 "testOutput": { 320 "type": "string", 321 "value": "Output Value" 322 } 323 } 324 } 325 DEPLOY 326 parameters { 327 storageAccountType = "Standard_GRS" 328 } 329 deployment_mode = "Complete" 330 } 331 `