package azurerm import ( "fmt" "net/http" "regexp" "testing" "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" ) func TestAccAzureRMTemplateDeployment_basic(t *testing.T) { ri := acctest.RandInt() config := fmt.Sprintf(testAccAzureRMTemplateDeployment_basicExample, ri, ri) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testCheckAzureRMTemplateDeploymentDestroy, Steps: []resource.TestStep{ { Config: config, Check: resource.ComposeTestCheckFunc( testCheckAzureRMTemplateDeploymentExists("azurerm_template_deployment.test"), ), }, }, }) } func TestAccAzureRMTemplateDeployment_disappears(t *testing.T) { ri := acctest.RandInt() config := fmt.Sprintf(testAccAzureRMTemplateDeployment_basicExample, ri, ri) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testCheckAzureRMTemplateDeploymentDestroy, Steps: []resource.TestStep{ { Config: config, Check: resource.ComposeTestCheckFunc( testCheckAzureRMTemplateDeploymentExists("azurerm_template_deployment.test"), testCheckAzureRMTemplateDeploymentDisappears("azurerm_template_deployment.test"), ), ExpectNonEmptyPlan: true, }, }, }) } func TestAccAzureRMTemplateDeployment_withParams(t *testing.T) { ri := acctest.RandInt() config := fmt.Sprintf(testAccAzureRMTemplateDeployment_withParams, ri, ri, ri) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testCheckAzureRMTemplateDeploymentDestroy, Steps: []resource.TestStep{ { Config: config, Check: resource.ComposeTestCheckFunc( testCheckAzureRMTemplateDeploymentExists("azurerm_template_deployment.test"), resource.TestCheckResourceAttr("azurerm_template_deployment.test", "outputs.testOutput", "Output Value"), ), }, }, }) } func TestAccAzureRMTemplateDeployment_withError(t *testing.T) { ri := acctest.RandInt() config := fmt.Sprintf(testAccAzureRMTemplateDeployment_withError, ri, ri) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testCheckAzureRMTemplateDeploymentDestroy, Steps: []resource.TestStep{ { Config: config, ExpectError: regexp.MustCompile("The deployment operation failed"), }, }, }) } func testCheckAzureRMTemplateDeploymentExists(name string) resource.TestCheckFunc { return func(s *terraform.State) error { // Ensure we have enough information in state to look up in API rs, ok := s.RootModule().Resources[name] if !ok { return fmt.Errorf("Not found: %s", name) } name := rs.Primary.Attributes["name"] resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] if !hasResourceGroup { return fmt.Errorf("Bad: no resource group found in state for template deployment: %s", name) } conn := testAccProvider.Meta().(*ArmClient).deploymentsClient resp, err := conn.Get(resourceGroup, name) if err != nil { return fmt.Errorf("Bad: Get on deploymentsClient: %s", err) } if resp.StatusCode == http.StatusNotFound { return fmt.Errorf("Bad: TemplateDeployment %q (resource group: %q) does not exist", name, resourceGroup) } return nil } } func testCheckAzureRMTemplateDeploymentDisappears(name string) resource.TestCheckFunc { return func(s *terraform.State) error { // Ensure we have enough information in state to look up in API rs, ok := s.RootModule().Resources[name] if !ok { return fmt.Errorf("Not found: %s", name) } name := rs.Primary.Attributes["name"] resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] if !hasResourceGroup { return fmt.Errorf("Bad: no resource group found in state for template deployment: %s", name) } conn := testAccProvider.Meta().(*ArmClient).deploymentsClient _, err := conn.Delete(resourceGroup, name, make(chan struct{})) if err != nil { return fmt.Errorf("Bad: Delete on deploymentsClient: %s", err) } return nil } } func testCheckAzureRMTemplateDeploymentDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*ArmClient).vmClient for _, rs := range s.RootModule().Resources { if rs.Type != "azurerm_template_deployment" { continue } name := rs.Primary.Attributes["name"] resourceGroup := rs.Primary.Attributes["resource_group_name"] resp, err := conn.Get(resourceGroup, name, "") if err != nil { return nil } if resp.StatusCode != http.StatusNotFound { return fmt.Errorf("Template Deployment still exists:\n%#v", resp.VirtualMachineProperties) } } return nil } var testAccAzureRMTemplateDeployment_basicExample = ` resource "azurerm_resource_group" "test" { name = "acctestRG-%d" location = "West US" } resource "azurerm_template_deployment" "test" { name = "acctesttemplate-%d" resource_group_name = "${azurerm_resource_group.test.name}" template_body = <