Merge pull request #11561 from hashicorp/b-fix-cloudwatch-log-group-acctests

provider/aws: Fix Cloudwatch Log acceptance tests
This commit is contained in:
Jake Champlin 2017-01-31 12:12:14 -05:00 committed by GitHub
commit 8ab661ef3c
2 changed files with 25 additions and 19 deletions

View File

@ -254,5 +254,5 @@ resource "aws_cloudwatch_log_group" "charlie" {
name = "foo-bar-%d" name = "foo-bar-%d"
retention_in_days = 3653 retention_in_days = 3653
} }
`, rInt, rInt, rInt) `, rInt, rInt+1, rInt+2)
} }

View File

@ -6,27 +6,29 @@ import (
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs" "github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
) )
func TestAccAWSCloudWatchLogMetricFilter_basic(t *testing.T) { func TestAccAWSCloudWatchLogMetricFilter_basic(t *testing.T) {
var mf cloudwatchlogs.MetricFilter var mf cloudwatchlogs.MetricFilter
rInt := acctest.RandInt()
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCloudWatchLogMetricFilterDestroy, CheckDestroy: testAccCheckAWSCloudWatchLogMetricFilterDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ {
Config: testAccAWSCloudWatchLogMetricFilterConfig, Config: testAccAWSCloudWatchLogMetricFilterConfig(rInt),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchLogMetricFilterExists("aws_cloudwatch_log_metric_filter.foobar", &mf), testAccCheckCloudWatchLogMetricFilterExists("aws_cloudwatch_log_metric_filter.foobar", &mf),
resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "name", "MyAppAccessCount"), resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "name", fmt.Sprintf("MyAppAccessCount-%d", rInt)),
testAccCheckCloudWatchLogMetricFilterName(&mf, "MyAppAccessCount"), testAccCheckCloudWatchLogMetricFilterName(&mf, fmt.Sprintf("MyAppAccessCount-%d", rInt)),
resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "pattern", ""), resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "pattern", ""),
testAccCheckCloudWatchLogMetricFilterPattern(&mf, ""), testAccCheckCloudWatchLogMetricFilterPattern(&mf, ""),
resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "log_group_name", "MyApp/access.log"), resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "log_group_name", fmt.Sprintf("MyApp/access-%d.log", rInt)),
resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "metric_transformation.0.name", "EventCount"), resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "metric_transformation.0.name", "EventCount"),
resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "metric_transformation.0.namespace", "YourNamespace"), resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "metric_transformation.0.namespace", "YourNamespace"),
resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "metric_transformation.0.value", "1"), resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "metric_transformation.0.value", "1"),
@ -37,15 +39,15 @@ func TestAccAWSCloudWatchLogMetricFilter_basic(t *testing.T) {
}), }),
), ),
}, },
resource.TestStep{ {
Config: testAccAWSCloudWatchLogMetricFilterConfigModified, Config: testAccAWSCloudWatchLogMetricFilterConfigModified(rInt),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckCloudWatchLogMetricFilterExists("aws_cloudwatch_log_metric_filter.foobar", &mf), testAccCheckCloudWatchLogMetricFilterExists("aws_cloudwatch_log_metric_filter.foobar", &mf),
resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "name", "MyAppAccessCount"), resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "name", fmt.Sprintf("MyAppAccessCount-%d", rInt)),
testAccCheckCloudWatchLogMetricFilterName(&mf, "MyAppAccessCount"), testAccCheckCloudWatchLogMetricFilterName(&mf, fmt.Sprintf("MyAppAccessCount-%d", rInt)),
resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "pattern", "{ $.errorCode = \"AccessDenied\" }"), resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "pattern", "{ $.errorCode = \"AccessDenied\" }"),
testAccCheckCloudWatchLogMetricFilterPattern(&mf, "{ $.errorCode = \"AccessDenied\" }"), testAccCheckCloudWatchLogMetricFilterPattern(&mf, "{ $.errorCode = \"AccessDenied\" }"),
resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "log_group_name", "MyApp/access.log"), resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "log_group_name", fmt.Sprintf("MyApp/access-%d.log", rInt)),
resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "metric_transformation.0.name", "AccessDeniedCount"), resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "metric_transformation.0.name", "AccessDeniedCount"),
resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "metric_transformation.0.namespace", "MyNamespace"), resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "metric_transformation.0.namespace", "MyNamespace"),
resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "metric_transformation.0.value", "2"), resource.TestCheckResourceAttr("aws_cloudwatch_log_metric_filter.foobar", "metric_transformation.0.value", "2"),
@ -146,9 +148,10 @@ func testAccCheckAWSCloudWatchLogMetricFilterDestroy(s *terraform.State) error {
return nil return nil
} }
var testAccAWSCloudWatchLogMetricFilterConfig = ` func testAccAWSCloudWatchLogMetricFilterConfig(rInt int) string {
return fmt.Sprintf(`
resource "aws_cloudwatch_log_metric_filter" "foobar" { resource "aws_cloudwatch_log_metric_filter" "foobar" {
name = "MyAppAccessCount" name = "MyAppAccessCount-%d"
pattern = "" pattern = ""
log_group_name = "${aws_cloudwatch_log_group.dada.name}" log_group_name = "${aws_cloudwatch_log_group.dada.name}"
@ -160,13 +163,15 @@ resource "aws_cloudwatch_log_metric_filter" "foobar" {
} }
resource "aws_cloudwatch_log_group" "dada" { resource "aws_cloudwatch_log_group" "dada" {
name = "MyApp/access.log" name = "MyApp/access-%d.log"
}
`, rInt, rInt)
} }
`
var testAccAWSCloudWatchLogMetricFilterConfigModified = ` func testAccAWSCloudWatchLogMetricFilterConfigModified(rInt int) string {
return fmt.Sprintf(`
resource "aws_cloudwatch_log_metric_filter" "foobar" { resource "aws_cloudwatch_log_metric_filter" "foobar" {
name = "MyAppAccessCount" name = "MyAppAccessCount-%d"
pattern = <<PATTERN pattern = <<PATTERN
{ $.errorCode = "AccessDenied" } { $.errorCode = "AccessDenied" }
PATTERN PATTERN
@ -180,6 +185,7 @@ PATTERN
} }
resource "aws_cloudwatch_log_group" "dada" { resource "aws_cloudwatch_log_group" "dada" {
name = "MyApp/access.log" name = "MyApp/access-%d.log"
}
`, rInt, rInt)
} }
`