From 479ee2b2b6cf6cd540fa87aa92f39a51aab0a697 Mon Sep 17 00:00:00 2001 From: Ben Gelens Date: Fri, 6 Jul 2018 15:58:38 +0200 Subject: [PATCH] added tests --- ...e_arm_automation_dsc_configuration_test.go | 130 ++++++++++++++ ...m_automation_dsc_nodeconfiguration_test.go | 160 ++++++++++++++++++ .../resource_arm_automation_module_test.go | 132 +++++++++++++++ 3 files changed, 422 insertions(+) create mode 100644 azurerm/resource_arm_automation_dsc_configuration_test.go create mode 100644 azurerm/resource_arm_automation_dsc_nodeconfiguration_test.go create mode 100644 azurerm/resource_arm_automation_module_test.go diff --git a/azurerm/resource_arm_automation_dsc_configuration_test.go b/azurerm/resource_arm_automation_dsc_configuration_test.go new file mode 100644 index 000000000000..c4e4dea0099b --- /dev/null +++ b/azurerm/resource_arm_automation_dsc_configuration_test.go @@ -0,0 +1,130 @@ +package azurerm + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func TestAccAzureRMAutomationDscConfiguration_basic(t *testing.T) { + resourceName := "azurerm_automation_dsc_configuration.test" + ri := acctest.RandInt() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMAutomationDscConfigurationDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMAutomationDscConfiguration_basic(ri, testLocation()), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMAutomationDscConfigurationExists(resourceName), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"content", "location"}, + }, + }, + }) +} + +func testCheckAzureRMAutomationDscConfigurationDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*ArmClient).automationDscConfigurationClient + ctx := testAccProvider.Meta().(*ArmClient).StopContext + + for _, rs := range s.RootModule().Resources { + if rs.Type != "azurerm_automation_dsc_configuration" { + continue + } + + name := rs.Primary.Attributes["name"] + accName := rs.Primary.Attributes["account_name"] + + resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] + if !hasResourceGroup { + return fmt.Errorf("Bad: no resource group found in state for Automation Dsc Configuration: '%s'", name) + } + + resp, err := conn.Get(ctx, resourceGroup, accName, name) + + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + return nil + } + + return err + } + + return fmt.Errorf("Automation Dsc Configuration still exists:\n%#v", resp) + + } + + return nil +} + +func testCheckAzureRMAutomationDscConfigurationExists(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"] + accName := rs.Primary.Attributes["account_name"] + + resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] + if !hasResourceGroup { + return fmt.Errorf("Bad: no resource group found in state for Automation Dsc Configuration: '%s'", name) + } + + conn := testAccProvider.Meta().(*ArmClient).automationDscConfigurationClient + ctx := testAccProvider.Meta().(*ArmClient).StopContext + + resp, err := conn.Get(ctx, resourceGroup, accName, name) + + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("Automation Dsc Configuration '%s' (resource group: '%s') does not exist", name, resourceGroup) + } + + return fmt.Errorf("Bad: Get on automationDscConfigurationClient: %s\nName: %s, Account name: %s, Resource group: %s OBJECT: %+v", err, name, accName, resourceGroup, rs.Primary) + } + + return nil + } +} + +func testAccAzureRMAutomationDscConfiguration_basic(rInt int, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +resource "azurerm_automation_account" "test" { + name = "acctest-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + sku { + name = "Basic" + } +} + +resource "azurerm_automation_dsc_configuration" "test" { + name = "test" + resource_group_name = "${azurerm_resource_group.test.name}" + account_name = "${azurerm_automation_account.test.name}" + location = "${azurerm_resource_group.test.location}" + content = "configuration test {}" + } +`, rInt, location, rInt) +} diff --git a/azurerm/resource_arm_automation_dsc_nodeconfiguration_test.go b/azurerm/resource_arm_automation_dsc_nodeconfiguration_test.go new file mode 100644 index 000000000000..d1a8db8639ed --- /dev/null +++ b/azurerm/resource_arm_automation_dsc_nodeconfiguration_test.go @@ -0,0 +1,160 @@ +package azurerm + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func TestAccAzureRMAutomationDscNodeConfiguration_basic(t *testing.T) { + resourceName := "azurerm_automation_dsc_nodeconfiguration.test" + ri := acctest.RandInt() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMAutomationDscNodeConfigurationDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMAutomationDscNodeConfiguration_basic(ri, testLocation()), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMAutomationDscNodeConfigurationExists(resourceName), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"content"}, + }, + }, + }) +} + +func testCheckAzureRMAutomationDscNodeConfigurationDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*ArmClient).automationDscNodeConfigurationClient + ctx := testAccProvider.Meta().(*ArmClient).StopContext + + for _, rs := range s.RootModule().Resources { + if rs.Type != "azurerm_automation_dsc_nodeconfiguration" { + continue + } + + name := rs.Primary.Attributes["name"] + accName := rs.Primary.Attributes["account_name"] + + resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] + if !hasResourceGroup { + return fmt.Errorf("Bad: no resource group found in state for Automation Dsc Node Configuration: '%s'", name) + } + + resp, err := conn.Get(ctx, resourceGroup, accName, name) + + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + return nil + } + + return err + } + + return fmt.Errorf("Automation Dsc Node Configuration still exists:\n%#v", resp) + + } + + return nil +} + +func testCheckAzureRMAutomationDscNodeConfigurationExists(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"] + accName := rs.Primary.Attributes["account_name"] + + resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] + if !hasResourceGroup { + return fmt.Errorf("Bad: no resource group found in state for Automation Dsc Node Configuration: '%s'", name) + } + + conn := testAccProvider.Meta().(*ArmClient).automationDscNodeConfigurationClient + ctx := testAccProvider.Meta().(*ArmClient).StopContext + + resp, err := conn.Get(ctx, resourceGroup, accName, name) + + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("Automation Dsc Node Configuration '%s' (resource group: '%s') does not exist", name, resourceGroup) + } + + return fmt.Errorf("Bad: Get on automationDscNodeConfigurationClient: %s\nName: %s, Account name: %s, Resource group: %s OBJECT: %+v", err, name, accName, resourceGroup, rs.Primary) + } + + return nil + } +} + +func testAccAzureRMAutomationDscNodeConfiguration_basic(rInt int, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +resource "azurerm_automation_account" "test" { + name = "acctest-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + sku { + name = "Basic" + } +} + +resource "azurerm_automation_dsc_configuration" "test" { + name = "test" + resource_group_name = "${azurerm_resource_group.test.name}" + account_name = "${azurerm_automation_account.test.name}" + location = "${azurerm_resource_group.test.location}" + content = "configuration test {}" + } + +resource "azurerm_automation_dsc_nodeconfiguration" "test" { + name = "test.localhost" + resource_group_name = "${azurerm_resource_group.test.name}" + account_name = "${azurerm_automation_account.test.name}" + depends_on = ["azurerm_automation_dsc_configuration.test"] + content = <