Skip to content

Commit

Permalink
Merge pull request #10606 from terraform-providers/rfd-at002-cloudwatch
Browse files Browse the repository at this point in the history
Import test refactor for cloudwatch resources
  • Loading branch information
ryndaniels authored Oct 29, 2019
2 parents aede278 + 2053742 commit 3fd3726
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 224 deletions.
49 changes: 21 additions & 28 deletions aws/resource_aws_cloudwatch_dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func TestAccAWSCloudWatchDashboard_importBasic(t *testing.T) {
resourceName := "aws_cloudwatch_dashboard.foobar"
func TestAccAWSCloudWatchDashboard_basic(t *testing.T) {
var dashboard cloudwatch.GetDashboardOutput
resourceName := "aws_cloudwatch_dashboard.test"
rInt := acctest.RandInt()

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -25,6 +26,10 @@ func TestAccAWSCloudWatchDashboard_importBasic(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccAWSCloudWatchDashboardConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchDashboardExists(resourceName, &dashboard),
resource.TestCheckResourceAttr(resourceName, "dashboard_name", testAccAWSCloudWatchDashboardName(rInt)),
),
},
{
ResourceName: resourceName,
Expand All @@ -35,9 +40,11 @@ func TestAccAWSCloudWatchDashboard_importBasic(t *testing.T) {
})
}

func TestAccAWSCloudWatchDashboard_basic(t *testing.T) {
func TestAccAWSCloudWatchDashboard_update(t *testing.T) {
var dashboard cloudwatch.GetDashboardOutput
resourceName := "aws_cloudwatch_dashboard.test"
rInt := acctest.RandInt()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand All @@ -46,36 +53,22 @@ func TestAccAWSCloudWatchDashboard_basic(t *testing.T) {
{
Config: testAccAWSCloudWatchDashboardConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchDashboardExists("aws_cloudwatch_dashboard.foobar", &dashboard),
resource.TestCheckResourceAttr("aws_cloudwatch_dashboard.foobar", "dashboard_name", testAccAWSCloudWatchDashboardName(rInt)),
testAccCheckCloudWatchDashboardExists(resourceName, &dashboard),
testAccCloudWatchCheckDashboardBodyIsExpected(resourceName, basicWidget),
resource.TestCheckResourceAttr(resourceName, "dashboard_name", testAccAWSCloudWatchDashboardName(rInt)),
),
},
},
})
}

func TestAccAWSCloudWatchDashboard_update(t *testing.T) {
var dashboard cloudwatch.GetDashboardOutput
rInt := acctest.RandInt()
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCloudWatchDashboardDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCloudWatchDashboardConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchDashboardExists("aws_cloudwatch_dashboard.foobar", &dashboard),
testAccCloudWatchCheckDashboardBodyIsExpected("aws_cloudwatch_dashboard.foobar", basicWidget),
resource.TestCheckResourceAttr("aws_cloudwatch_dashboard.foobar", "dashboard_name", testAccAWSCloudWatchDashboardName(rInt)),
),
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSCloudWatchDashboardConfig_updateBody(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchDashboardExists("aws_cloudwatch_dashboard.foobar", &dashboard),
testAccCloudWatchCheckDashboardBodyIsExpected("aws_cloudwatch_dashboard.foobar", updatedWidget),
resource.TestCheckResourceAttr("aws_cloudwatch_dashboard.foobar", "dashboard_name", testAccAWSCloudWatchDashboardName(rInt)),
testAccCheckCloudWatchDashboardExists(resourceName, &dashboard),
testAccCloudWatchCheckDashboardBodyIsExpected(resourceName, updatedWidget),
resource.TestCheckResourceAttr(resourceName, "dashboard_name", testAccAWSCloudWatchDashboardName(rInt)),
),
},
},
Expand Down Expand Up @@ -163,7 +156,7 @@ func testAccAWSCloudWatchDashboardName(rInt int) string {

func testAccAWSCloudWatchDashboardConfig(rInt int) string {
return fmt.Sprintf(`
resource "aws_cloudwatch_dashboard" "foobar" {
resource "aws_cloudwatch_dashboard" "test" {
dashboard_name = "terraform-test-dashboard-%d"
dashboard_body = <<EOF
Expand All @@ -175,7 +168,7 @@ resource "aws_cloudwatch_dashboard" "foobar" {

func testAccAWSCloudWatchDashboardConfig_updateBody(rInt int) string {
return fmt.Sprintf(`
resource "aws_cloudwatch_dashboard" "foobar" {
resource "aws_cloudwatch_dashboard" "test" {
dashboard_name = "terraform-test-dashboard-%d"
dashboard_body = <<EOF
Expand Down
108 changes: 57 additions & 51 deletions aws/resource_aws_cloudwatch_event_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ func testSweepCloudWatchEventRules(region string) error {
return nil
}

func TestAccAWSCloudWatchEventRule_importBasic(t *testing.T) {
resourceName := "aws_cloudwatch_event_rule.foo"
func TestAccAWSCloudWatchEventRule_basic(t *testing.T) {
var rule events.DescribeRuleOutput
resourceName := "aws_cloudwatch_event_rule.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -78,38 +79,22 @@ func TestAccAWSCloudWatchEventRule_importBasic(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccAWSCloudWatchEventRuleConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchEventRuleExists(resourceName, &rule),
resource.TestCheckResourceAttr(resourceName, "name", "tf-acc-cw-event-rule"),
),
},

{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"is_enabled"}, //this has a default value
},
},
})
}

func TestAccAWSCloudWatchEventRule_basic(t *testing.T) {
var rule events.DescribeRuleOutput

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCloudWatchEventRuleDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCloudWatchEventRuleConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchEventRuleExists("aws_cloudwatch_event_rule.foo", &rule),
resource.TestCheckResourceAttr("aws_cloudwatch_event_rule.foo", "name", "tf-acc-cw-event-rule"),
),
},
{
Config: testAccAWSCloudWatchEventRuleConfigModified,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchEventRuleExists("aws_cloudwatch_event_rule.foo", &rule),
resource.TestCheckResourceAttr("aws_cloudwatch_event_rule.foo", "name", "tf-acc-cw-event-rule-mod"),
testAccCheckCloudWatchEventRuleExists(resourceName, &rule),
resource.TestCheckResourceAttr(resourceName, "name", "tf-acc-cw-event-rule-mod"),
),
},
},
Expand All @@ -119,6 +104,7 @@ func TestAccAWSCloudWatchEventRule_basic(t *testing.T) {
func TestAccAWSCloudWatchEventRule_prefix(t *testing.T) {
var rule events.DescribeRuleOutput
startsWithPrefix := regexp.MustCompile("^tf-acc-cw-event-rule-prefix-")
resourceName := "aws_cloudwatch_event_rule.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -128,8 +114,8 @@ func TestAccAWSCloudWatchEventRule_prefix(t *testing.T) {
{
Config: testAccAWSCloudWatchEventRuleConfig_prefix,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchEventRuleExists("aws_cloudwatch_event_rule.moobar", &rule),
resource.TestMatchResourceAttr("aws_cloudwatch_event_rule.moobar", "name", startsWithPrefix),
testAccCheckCloudWatchEventRuleExists(resourceName, &rule),
resource.TestMatchResourceAttr(resourceName, "name", startsWithPrefix),
),
},
},
Expand All @@ -151,16 +137,22 @@ func TestAccAWSCloudWatchEventRule_tags(t *testing.T) {
testAccCheckCloudWatchEventRuleExists(resourceName, &rule),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.fizz", "buzz"),
resource.TestCheckResourceAttr(resourceName, "tags.foo", "bar"),
resource.TestCheckResourceAttr(resourceName, "tags.test", "bar"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"is_enabled"},
},
{
Config: testAccAWSCloudWatchEventRuleConfig_updateTags,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchEventRuleExists(resourceName, &rule),
resource.TestCheckResourceAttr(resourceName, "tags.%", "3"),
resource.TestCheckResourceAttr(resourceName, "tags.fizz", "buzz"),
resource.TestCheckResourceAttr(resourceName, "tags.foo", "bar2"),
resource.TestCheckResourceAttr(resourceName, "tags.test", "bar2"),
resource.TestCheckResourceAttr(resourceName, "tags.good", "bad"),
),
},
Expand All @@ -183,6 +175,7 @@ func TestAccAWSCloudWatchEventRule_tags(t *testing.T) {

func TestAccAWSCloudWatchEventRule_full(t *testing.T) {
var rule events.DescribeRuleOutput
resourceName := "aws_cloudwatch_event_rule.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -192,23 +185,30 @@ func TestAccAWSCloudWatchEventRule_full(t *testing.T) {
{
Config: testAccAWSCloudWatchEventRuleConfig_full,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchEventRuleExists("aws_cloudwatch_event_rule.moobar", &rule),
resource.TestCheckResourceAttr("aws_cloudwatch_event_rule.moobar", "name", "tf-acc-cw-event-rule-full"),
resource.TestCheckResourceAttr("aws_cloudwatch_event_rule.moobar", "schedule_expression", "rate(5 minutes)"),
resource.TestCheckResourceAttr("aws_cloudwatch_event_rule.moobar", "event_pattern", "{\"source\":[\"aws.ec2\"]}"),
resource.TestCheckResourceAttr("aws_cloudwatch_event_rule.moobar", "description", "He's not dead, he's just resting!"),
resource.TestCheckResourceAttr("aws_cloudwatch_event_rule.moobar", "role_arn", ""),
testAccCheckCloudWatchEventRuleEnabled("aws_cloudwatch_event_rule.moobar", "DISABLED", &rule),
resource.TestCheckResourceAttr("aws_cloudwatch_event_rule.moobar", "tags.%", "1"),
resource.TestCheckResourceAttr("aws_cloudwatch_event_rule.moobar", "tags.Name", "tf-acc-cw-event-rule-full"),
testAccCheckCloudWatchEventRuleExists(resourceName, &rule),
resource.TestCheckResourceAttr(resourceName, "name", "tf-acc-cw-event-rule-full"),
resource.TestCheckResourceAttr(resourceName, "schedule_expression", "rate(5 minutes)"),
resource.TestCheckResourceAttr(resourceName, "event_pattern", "{\"source\":[\"aws.ec2\"]}"),
resource.TestCheckResourceAttr(resourceName, "description", "He's not dead, he's just resting!"),
resource.TestCheckResourceAttr(resourceName, "role_arn", ""),
testAccCheckCloudWatchEventRuleEnabled(resourceName, "DISABLED", &rule),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.Name", "tf-acc-cw-event-rule-full"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"is_enabled"},
},
},
})
}

func TestAccAWSCloudWatchEventRule_enable(t *testing.T) {
var rule events.DescribeRuleOutput
resourceName := "aws_cloudwatch_event_rule.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -218,22 +218,28 @@ func TestAccAWSCloudWatchEventRule_enable(t *testing.T) {
{
Config: testAccAWSCloudWatchEventRuleConfigEnabled,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchEventRuleExists("aws_cloudwatch_event_rule.moo", &rule),
testAccCheckCloudWatchEventRuleEnabled("aws_cloudwatch_event_rule.moo", "ENABLED", &rule),
testAccCheckCloudWatchEventRuleExists("aws_cloudwatch_event_rule.test", &rule),
testAccCheckCloudWatchEventRuleEnabled("aws_cloudwatch_event_rule.test", "ENABLED", &rule),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"is_enabled"},
},
{
Config: testAccAWSCloudWatchEventRuleConfigDisabled,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchEventRuleExists("aws_cloudwatch_event_rule.moo", &rule),
testAccCheckCloudWatchEventRuleEnabled("aws_cloudwatch_event_rule.moo", "DISABLED", &rule),
testAccCheckCloudWatchEventRuleExists("aws_cloudwatch_event_rule.test", &rule),
testAccCheckCloudWatchEventRuleEnabled("aws_cloudwatch_event_rule.test", "DISABLED", &rule),
),
},
{
Config: testAccAWSCloudWatchEventRuleConfigEnabled,
Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchEventRuleExists("aws_cloudwatch_event_rule.moo", &rule),
testAccCheckCloudWatchEventRuleEnabled("aws_cloudwatch_event_rule.moo", "ENABLED", &rule),
testAccCheckCloudWatchEventRuleExists("aws_cloudwatch_event_rule.test", &rule),
testAccCheckCloudWatchEventRuleEnabled("aws_cloudwatch_event_rule.test", "ENABLED", &rule),
),
},
},
Expand Down Expand Up @@ -364,35 +370,35 @@ func TestResourceAWSCloudWatchEventRule_validateEventPatternValue(t *testing.T)
}

var testAccAWSCloudWatchEventRuleConfig = `
resource "aws_cloudwatch_event_rule" "foo" {
resource "aws_cloudwatch_event_rule" "test" {
name = "tf-acc-cw-event-rule"
schedule_expression = "rate(1 hour)"
}
`

var testAccAWSCloudWatchEventRuleConfigEnabled = `
resource "aws_cloudwatch_event_rule" "moo" {
resource "aws_cloudwatch_event_rule" "test" {
name = "tf-acc-cw-event-rule-state"
schedule_expression = "rate(1 hour)"
}
`
var testAccAWSCloudWatchEventRuleConfigDisabled = `
resource "aws_cloudwatch_event_rule" "moo" {
resource "aws_cloudwatch_event_rule" "test" {
name = "tf-acc-cw-event-rule-state"
schedule_expression = "rate(1 hour)"
is_enabled = false
}
`

var testAccAWSCloudWatchEventRuleConfigModified = `
resource "aws_cloudwatch_event_rule" "foo" {
resource "aws_cloudwatch_event_rule" "test" {
name = "tf-acc-cw-event-rule-mod"
schedule_expression = "rate(1 hour)"
}
`

var testAccAWSCloudWatchEventRuleConfig_prefix = `
resource "aws_cloudwatch_event_rule" "moobar" {
resource "aws_cloudwatch_event_rule" "test" {
name_prefix = "tf-acc-cw-event-rule-prefix-"
schedule_expression = "rate(5 minutes)"
event_pattern = <<PATTERN
Expand All @@ -410,7 +416,7 @@ resource "aws_cloudwatch_event_rule" "default" {
tags = {
fizz = "buzz"
foo = "bar"
test = "bar"
}
}
`
Expand All @@ -422,7 +428,7 @@ resource "aws_cloudwatch_event_rule" "default" {
tags = {
fizz = "buzz"
foo = "bar2"
test = "bar2"
good = "bad"
}
}
Expand All @@ -440,7 +446,7 @@ resource "aws_cloudwatch_event_rule" "default" {
`

var testAccAWSCloudWatchEventRuleConfig_full = `
resource "aws_cloudwatch_event_rule" "moobar" {
resource "aws_cloudwatch_event_rule" "test" {
name = "tf-acc-cw-event-rule-full"
schedule_expression = "rate(5 minutes)"
event_pattern = <<PATTERN
Expand Down
Loading

0 comments on commit 3fd3726

Please sign in to comment.