github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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_withError(t *testing.T) {
    72  	ri := acctest.RandInt()
    73  	config := fmt.Sprintf(testAccAzureRMTemplateDeployment_withError, 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  				ExpectError: regexp.MustCompile("The deployment operation failed"),
    82  			},
    83  		},
    84  	})
    85  }
    86  
    87  func testCheckAzureRMTemplateDeploymentExists(name string) resource.TestCheckFunc {
    88  	return func(s *terraform.State) error {
    89  		// Ensure we have enough information in state to look up in API
    90  		rs, ok := s.RootModule().Resources[name]
    91  		if !ok {
    92  			return fmt.Errorf("Not found: %s", name)
    93  		}
    94  
    95  		name := rs.Primary.Attributes["name"]
    96  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
    97  		if !hasResourceGroup {
    98  			return fmt.Errorf("Bad: no resource group found in state for template deployment: %s", name)
    99  		}
   100  
   101  		conn := testAccProvider.Meta().(*ArmClient).deploymentsClient
   102  
   103  		resp, err := conn.Get(resourceGroup, name)
   104  		if err != nil {
   105  			return fmt.Errorf("Bad: Get on deploymentsClient: %s", err)
   106  		}
   107  
   108  		if resp.StatusCode == http.StatusNotFound {
   109  			return fmt.Errorf("Bad: TemplateDeployment %q (resource group: %q) does not exist", name, resourceGroup)
   110  		}
   111  
   112  		return nil
   113  	}
   114  }
   115  
   116  func testCheckAzureRMTemplateDeploymentDisappears(name string) resource.TestCheckFunc {
   117  	return func(s *terraform.State) error {
   118  		// Ensure we have enough information in state to look up in API
   119  		rs, ok := s.RootModule().Resources[name]
   120  		if !ok {
   121  			return fmt.Errorf("Not found: %s", name)
   122  		}
   123  
   124  		name := rs.Primary.Attributes["name"]
   125  		resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"]
   126  		if !hasResourceGroup {
   127  			return fmt.Errorf("Bad: no resource group found in state for template deployment: %s", name)
   128  		}
   129  
   130  		conn := testAccProvider.Meta().(*ArmClient).deploymentsClient
   131  
   132  		_, err := conn.Delete(resourceGroup, name, make(chan struct{}))
   133  		if err != nil {
   134  			return fmt.Errorf("Bad: Delete on deploymentsClient: %s", err)
   135  		}
   136  
   137  		return nil
   138  	}
   139  }
   140  
   141  func testCheckAzureRMTemplateDeploymentDestroy(s *terraform.State) error {
   142  	conn := testAccProvider.Meta().(*ArmClient).vmClient
   143  
   144  	for _, rs := range s.RootModule().Resources {
   145  		if rs.Type != "azurerm_template_deployment" {
   146  			continue
   147  		}
   148  
   149  		name := rs.Primary.Attributes["name"]
   150  		resourceGroup := rs.Primary.Attributes["resource_group_name"]
   151  
   152  		resp, err := conn.Get(resourceGroup, name, "")
   153  
   154  		if err != nil {
   155  			return nil
   156  		}
   157  
   158  		if resp.StatusCode != http.StatusNotFound {
   159  			return fmt.Errorf("Template Deployment still exists:\n%#v", resp.VirtualMachineProperties)
   160  		}
   161  	}
   162  
   163  	return nil
   164  }
   165  
   166  var testAccAzureRMTemplateDeployment_basicSingle = `
   167    resource "azurerm_resource_group" "test" {
   168      name = "acctestRG-%d"
   169      location = "West US"
   170    }
   171  
   172    resource "azurerm_template_deployment" "test" {
   173      name = "acctesttemplate-%d"
   174      resource_group_name = "${azurerm_resource_group.test.name}"
   175      template_body = <<DEPLOY
   176  {
   177    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
   178    "contentVersion": "1.0.0.0",
   179    "variables": {
   180      "location": "[resourceGroup().location]",
   181      "publicIPAddressType": "Dynamic",
   182      "apiVersion": "2015-06-15",
   183      "dnsLabelPrefix": "[concat('terraform-tdacctest', uniquestring(resourceGroup().id))]"
   184    },
   185    "resources": [
   186       {
   187        "type": "Microsoft.Network/publicIPAddresses",
   188        "apiVersion": "[variables('apiVersion')]",
   189        "name": "acctestpip-%d",
   190        "location": "[variables('location')]",
   191        "properties": {
   192          "publicIPAllocationMethod": "[variables('publicIPAddressType')]",
   193          "dnsSettings": {
   194            "domainNameLabel": "[variables('dnsLabelPrefix')]"
   195          }
   196        }
   197      }
   198    ]
   199  }
   200  DEPLOY
   201      deployment_mode = "Complete"
   202    }
   203  
   204  `
   205  
   206  var testAccAzureRMTemplateDeployment_basicMultiple = `
   207    resource "azurerm_resource_group" "test" {
   208      name = "acctestRG-%d"
   209      location = "West US"
   210    }
   211  
   212    resource "azurerm_template_deployment" "test" {
   213      name = "acctesttemplate-%d"
   214      resource_group_name = "${azurerm_resource_group.test.name}"
   215      template_body = <<DEPLOY
   216  {
   217    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
   218    "contentVersion": "1.0.0.0",
   219    "parameters": {
   220      "storageAccountType": {
   221        "type": "string",
   222        "defaultValue": "Standard_LRS",
   223        "allowedValues": [
   224          "Standard_LRS",
   225          "Standard_GRS",
   226          "Standard_ZRS"
   227        ],
   228        "metadata": {
   229          "description": "Storage Account type"
   230        }
   231      }
   232    },
   233    "variables": {
   234      "location": "[resourceGroup().location]",
   235      "storageAccountName": "[concat(uniquestring(resourceGroup().id), 'storage')]",
   236      "publicIPAddressName": "[concat('myPublicIp', uniquestring(resourceGroup().id))]",
   237      "publicIPAddressType": "Dynamic",
   238      "apiVersion": "2015-06-15",
   239      "dnsLabelPrefix": "[concat('terraform-tdacctest', uniquestring(resourceGroup().id))]"
   240    },
   241    "resources": [
   242      {
   243        "type": "Microsoft.Storage/storageAccounts",
   244        "name": "[variables('storageAccountName')]",
   245        "apiVersion": "[variables('apiVersion')]",
   246        "location": "[variables('location')]",
   247        "properties": {
   248          "accountType": "[parameters('storageAccountType')]"
   249        }
   250      },
   251      {
   252        "type": "Microsoft.Network/publicIPAddresses",
   253        "apiVersion": "[variables('apiVersion')]",
   254        "name": "[variables('publicIPAddressName')]",
   255        "location": "[variables('location')]",
   256        "properties": {
   257          "publicIPAllocationMethod": "[variables('publicIPAddressType')]",
   258          "dnsSettings": {
   259            "domainNameLabel": "[variables('dnsLabelPrefix')]"
   260          }
   261        }
   262      }
   263    ]
   264  }
   265  DEPLOY
   266      deployment_mode = "Complete"
   267    }
   268  
   269  `
   270  
   271  var testAccAzureRMTemplateDeployment_withParams = `
   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      "dnsLabelPrefix": {
   302        "type": "string",
   303        "metadata": {
   304          "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."
   305        }
   306      }
   307    },
   308    "variables": {
   309      "location": "[resourceGroup().location]",
   310      "storageAccountName": "[concat(uniquestring(resourceGroup().id), 'storage')]",
   311      "publicIPAddressName": "[concat('myPublicIp', uniquestring(resourceGroup().id))]",
   312      "publicIPAddressType": "Dynamic",
   313      "apiVersion": "2015-06-15"
   314    },
   315    "resources": [
   316      {
   317        "type": "Microsoft.Storage/storageAccounts",
   318        "name": "[variables('storageAccountName')]",
   319        "apiVersion": "[variables('apiVersion')]",
   320        "location": "[variables('location')]",
   321        "properties": {
   322          "accountType": "[parameters('storageAccountType')]"
   323        }
   324      },
   325      {
   326        "type": "Microsoft.Network/publicIPAddresses",
   327        "apiVersion": "[variables('apiVersion')]",
   328        "name": "[variables('publicIPAddressName')]",
   329        "location": "[variables('location')]",
   330        "properties": {
   331          "publicIPAllocationMethod": "[variables('publicIPAddressType')]",
   332          "dnsSettings": {
   333            "domainNameLabel": "[parameters('dnsLabelPrefix')]"
   334          }
   335        }
   336      }
   337    ],
   338    "outputs": {
   339      "testOutput": {
   340        "type": "string",
   341        "value": "Output Value"
   342      }
   343    }
   344  }
   345  DEPLOY
   346      parameters {
   347  	dnsLabelPrefix = "terraform-test-%d"
   348  	storageAccountType = "Standard_GRS"
   349      }
   350      deployment_mode = "Complete"
   351    }
   352  
   353  `
   354  
   355  // StorageAccount name is too long, forces error
   356  var testAccAzureRMTemplateDeployment_withError = `
   357    resource "azurerm_resource_group" "test" {
   358      name = "acctestRG-%d"
   359      location = "West US"
   360    }
   361  
   362    output "test" {
   363      value = "${azurerm_template_deployment.test.outputs.testOutput}"
   364    }
   365  
   366    resource "azurerm_template_deployment" "test" {
   367      name = "acctesttemplate-%d"
   368      resource_group_name = "${azurerm_resource_group.test.name}"
   369      template_body = <<DEPLOY
   370  {
   371    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
   372    "contentVersion": "1.0.0.0",
   373    "parameters": {
   374      "storageAccountType": {
   375        "type": "string",
   376        "defaultValue": "Standard_LRS",
   377        "allowedValues": [
   378          "Standard_LRS",
   379          "Standard_GRS",
   380          "Standard_ZRS"
   381        ],
   382        "metadata": {
   383          "description": "Storage Account type"
   384        }
   385      }
   386    },
   387    "variables": {
   388      "location": "[resourceGroup().location]",
   389      "storageAccountName": "badStorageAccountNameTooLong",
   390      "apiVersion": "2015-06-15"
   391    },
   392    "resources": [
   393      {
   394        "type": "Microsoft.Storage/storageAccounts",
   395        "name": "[variables('storageAccountName')]",
   396        "apiVersion": "[variables('apiVersion')]",
   397        "location": "[variables('location')]",
   398        "properties": {
   399          "accountType": "[parameters('storageAccountType')]"
   400        }
   401      }
   402    ],
   403    "outputs": {
   404      "testOutput": {
   405        "type": "string",
   406        "value": "Output Value"
   407      }
   408    }
   409  }
   410  DEPLOY
   411      parameters {
   412          storageAccountType = "Standard_GRS"
   413      }
   414      deployment_mode = "Complete"
   415    }
   416  `