Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bgelens committed Jul 6, 2018
1 parent 3da2fb4 commit 479ee2b
Show file tree
Hide file tree
Showing 3 changed files with 422 additions and 0 deletions.
130 changes: 130 additions & 0 deletions azurerm/resource_arm_automation_dsc_configuration_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
160 changes: 160 additions & 0 deletions azurerm/resource_arm_automation_dsc_nodeconfiguration_test.go
Original file line number Diff line number Diff line change
@@ -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 = <<mofcontent
instance of MSFT_FileDirectoryConfiguration as $MSFT_FileDirectoryConfiguration1ref
{
ResourceID = "[File]bla";
Ensure = "Present";
Contents = "bogus Content";
DestinationPath = "c:\\bogus.txt";
ModuleName = "PSDesiredStateConfiguration";
SourceInfo = "::3::9::file";
ModuleVersion = "1.0";
ConfigurationName = "bla";
};
instance of OMI_ConfigurationDocument
{
Version="2.0.0";
MinimumCompatibleVersion = "1.0.0";
CompatibleVersionAdditionalProperties= {"Omi_BaseResource:ConfigurationName"};
Author="bogusAuthor";
GenerationDate="06/15/2018 14:06:24";
GenerationHost="bogusComputer";
Name="test";
};
mofcontent
}
`, rInt, location, rInt)
}
Loading

0 comments on commit 479ee2b

Please sign in to comment.