github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/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  		_, error := conn.Delete(resourceGroup, name, make(chan struct{}))
   156  		err := <-error
   157  		if err != nil {
   158  			return fmt.Errorf("Bad: Delete on deploymentsClient: %s", err)
   159  		}
   160  
   161  		return nil
   162  	}
   163  }
   164  
   165  func testCheckAzureRMTemplateDeploymentDestroy(s *terraform.State) error {
   166  	conn := testAccProvider.Meta().(*ArmClient).vmClient
   167  
   168  	for _, rs := range s.RootModule().Resources {
   169  		if rs.Type != "azurerm_template_deployment" {
   170  			continue
   171  		}
   172  
   173  		name := rs.Primary.Attributes["name"]
   174  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   175  
   176  		resp, err := conn.Get(resourceGroup, name, "")
   177  
   178  		if err != nil {
   179  			return nil
   180  		}
   181  
   182  		if resp.StatusCode != http.StatusNotFound {
   183  			return fmt.Errorf("Template Deployment still exists:\n%#v", resp.VirtualMachineProperties)
   184  		}
   185  	}
   186  
   187  	return nil
   188  }
   189  
   190  var testAccAzureRMTemplateDeployment_basicSingle = `
   191    resource "azurerm_resource_group" "test" {
   192      name = "acctestRG-%d"
   193      location = "West US"
   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    "variables": {
   204      "location": "[resourceGroup().location]",
   205      "publicIPAddressType": "Dynamic",
   206      "apiVersion": "2015-06-15",
   207      "dnsLabelPrefix": "[concat('terraform-tdacctest', uniquestring(resourceGroup().id))]"
   208    },
   209    "resources": [
   210       {
   211        "type": "Microsoft.Network/publicIPAddresses",
   212        "apiVersion": "[variables('apiVersion')]",
   213        "name": "acctestpip-%d",
   214        "location": "[variables('location')]",
   215        "properties": {
   216          "publicIPAllocationMethod": "[variables('publicIPAddressType')]",
   217          "dnsSettings": {
   218            "domainNameLabel": "[variables('dnsLabelPrefix')]"
   219          }
   220        }
   221      }
   222    ]
   223  }
   224  DEPLOY
   225      deployment_mode = "Complete"
   226    }
   227  
   228  `
   229  
   230  var testAccAzureRMTemplateDeployment_basicMultiple = `
   231    resource "azurerm_resource_group" "test" {
   232      name = "acctestRG-%d"
   233      location = "West US"
   234    }
   235  
   236    resource "azurerm_template_deployment" "test" {
   237      name = "acctesttemplate-%d"
   238      resource_group_name = "${azurerm_resource_group.test.name}"
   239      template_body = <<DEPLOY
   240  {
   241    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
   242    "contentVersion": "1.0.0.0",
   243    "parameters": {
   244      "storageAccountType": {
   245        "type": "string",
   246        "defaultValue": "Standard_LRS",
   247        "allowedValues": [
   248          "Standard_LRS",
   249          "Standard_GRS",
   250          "Standard_ZRS"
   251        ],
   252        "metadata": {
   253          "description": "Storage Account type"
   254        }
   255      }
   256    },
   257    "variables": {
   258      "location": "[resourceGroup().location]",
   259      "storageAccountName": "[concat(uniquestring(resourceGroup().id), 'storage')]",
   260      "publicIPAddressName": "[concat('myPublicIp', uniquestring(resourceGroup().id))]",
   261      "publicIPAddressType": "Dynamic",
   262      "apiVersion": "2015-06-15",
   263      "dnsLabelPrefix": "[concat('terraform-tdacctest', uniquestring(resourceGroup().id))]"
   264    },
   265    "resources": [
   266      {
   267        "type": "Microsoft.Storage/storageAccounts",
   268        "name": "[variables('storageAccountName')]",
   269        "apiVersion": "[variables('apiVersion')]",
   270        "location": "[variables('location')]",
   271        "properties": {
   272          "accountType": "[parameters('storageAccountType')]"
   273        }
   274      },
   275      {
   276        "type": "Microsoft.Network/publicIPAddresses",
   277        "apiVersion": "[variables('apiVersion')]",
   278        "name": "[variables('publicIPAddressName')]",
   279        "location": "[variables('location')]",
   280        "properties": {
   281          "publicIPAllocationMethod": "[variables('publicIPAddressType')]",
   282          "dnsSettings": {
   283            "domainNameLabel": "[variables('dnsLabelPrefix')]"
   284          }
   285        }
   286      }
   287    ]
   288  }
   289  DEPLOY
   290      deployment_mode = "Complete"
   291    }
   292  
   293  `
   294  
   295  var testAccAzureRMTemplateDeployment_withParams = `
   296    resource "azurerm_resource_group" "test" {
   297      name = "acctestRG-%d"
   298      location = "West US"
   299    }
   300  
   301    output "test" {
   302      value = "${azurerm_template_deployment.test.outputs["testOutput"]}"
   303    }
   304  
   305    resource "azurerm_storage_container" "using-outputs" {
   306      name = "vhds"
   307      resource_group_name = "${azurerm_resource_group.test.name}"
   308      storage_account_name = "${azurerm_template_deployment.test.outputs["accountName"]}"
   309      container_access_type = "private"
   310    }
   311  
   312    resource "azurerm_template_deployment" "test" {
   313      name = "acctesttemplate-%d"
   314      resource_group_name = "${azurerm_resource_group.test.name}"
   315      template_body = <<DEPLOY
   316  {
   317    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
   318    "contentVersion": "1.0.0.0",
   319    "parameters": {
   320      "storageAccountType": {
   321        "type": "string",
   322        "defaultValue": "Standard_LRS",
   323        "allowedValues": [
   324          "Standard_LRS",
   325          "Standard_GRS",
   326          "Standard_ZRS"
   327        ],
   328        "metadata": {
   329          "description": "Storage Account type"
   330        }
   331      },
   332      "dnsLabelPrefix": {
   333        "type": "string",
   334        "metadata": {
   335          "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."
   336        }
   337      }
   338    },
   339    "variables": {
   340      "location": "[resourceGroup().location]",
   341      "storageAccountName": "[concat(uniquestring(resourceGroup().id), 'storage')]",
   342      "publicIPAddressName": "[concat('myPublicIp', uniquestring(resourceGroup().id))]",
   343      "publicIPAddressType": "Dynamic",
   344      "apiVersion": "2015-06-15"
   345    },
   346    "resources": [
   347      {
   348        "type": "Microsoft.Storage/storageAccounts",
   349        "name": "[variables('storageAccountName')]",
   350        "apiVersion": "[variables('apiVersion')]",
   351        "location": "[variables('location')]",
   352        "properties": {
   353          "accountType": "[parameters('storageAccountType')]"
   354        }
   355      },
   356      {
   357        "type": "Microsoft.Network/publicIPAddresses",
   358        "apiVersion": "[variables('apiVersion')]",
   359        "name": "[variables('publicIPAddressName')]",
   360        "location": "[variables('location')]",
   361        "properties": {
   362          "publicIPAllocationMethod": "[variables('publicIPAddressType')]",
   363          "dnsSettings": {
   364            "domainNameLabel": "[parameters('dnsLabelPrefix')]"
   365          }
   366        }
   367      }
   368    ],
   369    "outputs": {
   370      "testOutput": {
   371        "type": "string",
   372        "value": "Output Value"
   373      },
   374      "accountName": {
   375        "type": "string",
   376        "value": "[variables('storageAccountName')]"
   377      }
   378    }
   379  }
   380  DEPLOY
   381      parameters {
   382  	dnsLabelPrefix = "terraform-test-%d"
   383  	storageAccountType = "Standard_GRS"
   384      }
   385      deployment_mode = "Complete"
   386    }
   387  
   388  `
   389  
   390  var testAccAzureRMTemplateDeployment_withOutputs = `
   391    resource "azurerm_resource_group" "test" {
   392      name = "acctestRG-%d"
   393      location = "West US"
   394    }
   395  
   396    output "tfStringOutput" {
   397      value = "${azurerm_template_deployment.test.outputs.stringOutput}"
   398    }
   399  
   400    output "tfIntOutput" {
   401      value = "${azurerm_template_deployment.test.outputs.intOutput}"
   402    }
   403  
   404    output "tfFalseOutput" {
   405      value = "${azurerm_template_deployment.test.outputs.falseOutput}"
   406    }
   407  
   408    output "tfTrueOutput" {
   409      value = "${azurerm_template_deployment.test.outputs.trueOutput}"
   410    }
   411  
   412    resource "azurerm_template_deployment" "test" {
   413      name = "acctesttemplate-%d"
   414      resource_group_name = "${azurerm_resource_group.test.name}"
   415      template_body = <<DEPLOY
   416  {
   417    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
   418    "contentVersion": "1.0.0.0",
   419    "parameters": {
   420      "storageAccountType": {
   421        "type": "string",
   422        "defaultValue": "Standard_LRS",
   423        "allowedValues": [
   424          "Standard_LRS",
   425          "Standard_GRS",
   426          "Standard_ZRS"
   427        ],
   428        "metadata": {
   429          "description": "Storage Account type"
   430        }
   431      },
   432      "dnsLabelPrefix": {
   433        "type": "string",
   434        "metadata": {
   435          "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."
   436        }
   437      },
   438      "intParameter": {
   439        "type": "int",
   440        "defaultValue": -123
   441      },
   442      "falseParameter": {
   443        "type": "bool",
   444        "defaultValue": false
   445      },
   446      "trueParameter": {
   447        "type": "bool",
   448        "defaultValue": true
   449      }
   450    },
   451    "variables": {
   452      "location": "[resourceGroup().location]",
   453      "storageAccountName": "[concat(uniquestring(resourceGroup().id), 'storage')]",
   454      "publicIPAddressName": "[concat('myPublicIp', uniquestring(resourceGroup().id))]",
   455      "publicIPAddressType": "Dynamic",
   456      "apiVersion": "2015-06-15"
   457    },
   458    "resources": [
   459      {
   460        "type": "Microsoft.Storage/storageAccounts",
   461        "name": "[variables('storageAccountName')]",
   462        "apiVersion": "[variables('apiVersion')]",
   463        "location": "[variables('location')]",
   464        "properties": {
   465          "accountType": "[parameters('storageAccountType')]"
   466        }
   467      },
   468      {
   469        "type": "Microsoft.Network/publicIPAddresses",
   470        "apiVersion": "[variables('apiVersion')]",
   471        "name": "[variables('publicIPAddressName')]",
   472        "location": "[variables('location')]",
   473        "properties": {
   474          "publicIPAllocationMethod": "[variables('publicIPAddressType')]",
   475          "dnsSettings": {
   476            "domainNameLabel": "[parameters('dnsLabelPrefix')]"
   477          }
   478        }
   479      }
   480    ],
   481    "outputs": {
   482      "stringOutput": {
   483        "type": "string",
   484        "value": "[parameters('storageAccountType')]"
   485      },
   486      "intOutput": {
   487        "type": "int",
   488        "value": "[parameters('intParameter')]"
   489      },
   490      "falseOutput": {
   491        "type": "bool",
   492        "value": "[parameters('falseParameter')]"
   493      },
   494      "trueOutput": {
   495        "type": "bool",
   496        "value": "[parameters('trueParameter')]"
   497      }
   498    }
   499  }
   500  DEPLOY
   501      parameters {
   502        dnsLabelPrefix = "terraform-test-%d"
   503        storageAccountType = "Standard_GRS"
   504      }
   505      deployment_mode = "Incremental"
   506    }
   507  
   508  `
   509  
   510  // StorageAccount name is too long, forces error
   511  var testAccAzureRMTemplateDeployment_withError = `
   512    resource "azurerm_resource_group" "test" {
   513      name = "acctestRG-%d"
   514      location = "West US"
   515    }
   516  
   517    output "test" {
   518      value = "${azurerm_template_deployment.test.outputs.testOutput}"
   519    }
   520  
   521    resource "azurerm_template_deployment" "test" {
   522      name = "acctesttemplate-%d"
   523      resource_group_name = "${azurerm_resource_group.test.name}"
   524      template_body = <<DEPLOY
   525  {
   526    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
   527    "contentVersion": "1.0.0.0",
   528    "parameters": {
   529      "storageAccountType": {
   530        "type": "string",
   531        "defaultValue": "Standard_LRS",
   532        "allowedValues": [
   533          "Standard_LRS",
   534          "Standard_GRS",
   535          "Standard_ZRS"
   536        ],
   537        "metadata": {
   538          "description": "Storage Account type"
   539        }
   540      }
   541    },
   542    "variables": {
   543      "location": "[resourceGroup().location]",
   544      "storageAccountName": "badStorageAccountNameTooLong",
   545      "apiVersion": "2015-06-15"
   546    },
   547    "resources": [
   548      {
   549        "type": "Microsoft.Storage/storageAccounts",
   550        "name": "[variables('storageAccountName')]",
   551        "apiVersion": "[variables('apiVersion')]",
   552        "location": "[variables('location')]",
   553        "properties": {
   554          "accountType": "[parameters('storageAccountType')]"
   555        }
   556      }
   557    ],
   558    "outputs": {
   559      "testOutput": {
   560        "type": "string",
   561        "value": "Output Value"
   562      }
   563    }
   564  }
   565  DEPLOY
   566      parameters {
   567          storageAccountType = "Standard_GRS"
   568      }
   569      deployment_mode = "Complete"
   570    }
   571  `