2015-09-16 22:57:12 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
|
2016-11-25 13:50:19 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2015-09-16 22:57:12 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccAWSCloudWatchLogGroup_basic(t *testing.T) {
|
|
|
|
var lg cloudwatchlogs.LogGroup
|
2016-11-25 13:50:19 +01:00
|
|
|
rInt := acctest.RandInt()
|
2015-09-16 22:57:12 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSCloudWatchLogGroupDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-01-10 13:28:36 +01:00
|
|
|
{
|
2016-11-25 13:50:19 +01:00
|
|
|
Config: testAccAWSCloudWatchLogGroupConfig(rInt),
|
2015-09-16 22:57:12 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudWatchLogGroupExists("aws_cloudwatch_log_group.foobar", &lg),
|
|
|
|
resource.TestCheckResourceAttr("aws_cloudwatch_log_group.foobar", "retention_in_days", "0"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAWSCloudWatchLogGroup_retentionPolicy(t *testing.T) {
|
|
|
|
var lg cloudwatchlogs.LogGroup
|
2016-11-25 13:50:19 +01:00
|
|
|
rInt := acctest.RandInt()
|
2015-09-16 22:57:12 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSCloudWatchLogGroupDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-01-10 13:28:36 +01:00
|
|
|
{
|
2016-11-25 13:50:19 +01:00
|
|
|
Config: testAccAWSCloudWatchLogGroupConfig_withRetention(rInt),
|
2015-09-16 22:57:12 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudWatchLogGroupExists("aws_cloudwatch_log_group.foobar", &lg),
|
|
|
|
resource.TestCheckResourceAttr("aws_cloudwatch_log_group.foobar", "retention_in_days", "365"),
|
|
|
|
),
|
|
|
|
},
|
2017-01-10 13:28:36 +01:00
|
|
|
{
|
2016-11-25 13:50:19 +01:00
|
|
|
Config: testAccAWSCloudWatchLogGroupConfigModified_withRetention(rInt),
|
2015-09-16 22:57:12 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudWatchLogGroupExists("aws_cloudwatch_log_group.foobar", &lg),
|
|
|
|
resource.TestCheckResourceAttr("aws_cloudwatch_log_group.foobar", "retention_in_days", "0"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccAWSCloudWatchLogGroup_multiple(t *testing.T) {
|
|
|
|
var lg cloudwatchlogs.LogGroup
|
2016-11-25 13:50:19 +01:00
|
|
|
rInt := acctest.RandInt()
|
2015-09-16 22:57:12 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSCloudWatchLogGroupDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-01-10 13:28:36 +01:00
|
|
|
{
|
2016-11-25 13:50:19 +01:00
|
|
|
Config: testAccAWSCloudWatchLogGroupConfig_multiple(rInt),
|
2015-09-16 22:57:12 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudWatchLogGroupExists("aws_cloudwatch_log_group.alpha", &lg),
|
|
|
|
resource.TestCheckResourceAttr("aws_cloudwatch_log_group.alpha", "retention_in_days", "14"),
|
|
|
|
testAccCheckCloudWatchLogGroupExists("aws_cloudwatch_log_group.beta", &lg),
|
|
|
|
resource.TestCheckResourceAttr("aws_cloudwatch_log_group.beta", "retention_in_days", "0"),
|
|
|
|
testAccCheckCloudWatchLogGroupExists("aws_cloudwatch_log_group.charlie", &lg),
|
|
|
|
resource.TestCheckResourceAttr("aws_cloudwatch_log_group.charlie", "retention_in_days", "3653"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-07-11 20:06:44 +02:00
|
|
|
func TestAccAWSCloudWatchLogGroup_disappears(t *testing.T) {
|
|
|
|
var lg cloudwatchlogs.LogGroup
|
2016-11-25 13:50:19 +01:00
|
|
|
rInt := acctest.RandInt()
|
2016-07-11 20:06:44 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSCloudWatchLogGroupDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
2017-01-10 13:28:36 +01:00
|
|
|
{
|
2016-11-25 13:50:19 +01:00
|
|
|
Config: testAccAWSCloudWatchLogGroupConfig(rInt),
|
2016-07-11 20:06:44 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudWatchLogGroupExists("aws_cloudwatch_log_group.foobar", &lg),
|
|
|
|
testAccCheckCloudWatchLogGroupDisappears(&lg),
|
|
|
|
),
|
|
|
|
ExpectNonEmptyPlan: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-01-10 13:28:36 +01:00
|
|
|
func TestAccAWSCloudWatchLogGroup_tagging(t *testing.T) {
|
|
|
|
var lg cloudwatchlogs.LogGroup
|
|
|
|
rInt := acctest.RandInt()
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSCloudWatchLogGroupDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
Config: testAccAWSCloudWatchLogGroupConfigWithTags(rInt),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudWatchLogGroupExists("aws_cloudwatch_log_group.foobar", &lg),
|
|
|
|
resource.TestCheckResourceAttr("aws_cloudwatch_log_group.foobar", "tags.%", "2"),
|
|
|
|
resource.TestCheckResourceAttr("aws_cloudwatch_log_group.foobar", "tags.Environment", "Production"),
|
|
|
|
resource.TestCheckResourceAttr("aws_cloudwatch_log_group.foobar", "tags.Foo", "Bar"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Config: testAccAWSCloudWatchLogGroupConfigWithTagsUpdated(rInt),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudWatchLogGroupExists("aws_cloudwatch_log_group.foobar", &lg),
|
|
|
|
resource.TestCheckResourceAttr("aws_cloudwatch_log_group.foobar", "tags.%", "3"),
|
|
|
|
resource.TestCheckResourceAttr("aws_cloudwatch_log_group.foobar", "tags.Environment", "Development"),
|
|
|
|
resource.TestCheckResourceAttr("aws_cloudwatch_log_group.foobar", "tags.Bar", "baz"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-07-11 20:06:44 +02:00
|
|
|
func testAccCheckCloudWatchLogGroupDisappears(lg *cloudwatchlogs.LogGroup) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).cloudwatchlogsconn
|
|
|
|
opts := &cloudwatchlogs.DeleteLogGroupInput{
|
|
|
|
LogGroupName: lg.LogGroupName,
|
|
|
|
}
|
|
|
|
if _, err := conn.DeleteLogGroup(opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-16 22:57:12 +02:00
|
|
|
func testAccCheckCloudWatchLogGroupExists(n string, lg *cloudwatchlogs.LogGroup) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).cloudwatchlogsconn
|
2016-07-11 20:06:44 +02:00
|
|
|
logGroup, exists, err := lookupCloudWatchLogGroup(conn, rs.Primary.ID, nil)
|
2015-09-16 22:57:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-11 20:06:44 +02:00
|
|
|
if !exists {
|
|
|
|
return fmt.Errorf("Bad: LogGroup %q does not exist", rs.Primary.ID)
|
|
|
|
}
|
2015-09-16 22:57:12 +02:00
|
|
|
|
|
|
|
*lg = *logGroup
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAWSCloudWatchLogGroupDestroy(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).cloudwatchlogsconn
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "aws_cloudwatch_log_group" {
|
|
|
|
continue
|
|
|
|
}
|
2016-07-11 20:06:44 +02:00
|
|
|
_, exists, err := lookupCloudWatchLogGroup(conn, rs.Primary.ID, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2015-09-16 22:57:12 +02:00
|
|
|
|
2016-07-11 20:06:44 +02:00
|
|
|
if exists {
|
|
|
|
return fmt.Errorf("Bad: LogGroup still exists: %q", rs.Primary.ID)
|
2015-09-16 22:57:12 +02:00
|
|
|
}
|
2016-07-11 20:06:44 +02:00
|
|
|
|
2015-09-16 22:57:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-25 13:50:19 +01:00
|
|
|
func testAccAWSCloudWatchLogGroupConfig(rInt int) string {
|
|
|
|
return fmt.Sprintf(`
|
2015-09-16 22:57:12 +02:00
|
|
|
resource "aws_cloudwatch_log_group" "foobar" {
|
2016-11-25 13:50:19 +01:00
|
|
|
name = "foo-bar-%d"
|
|
|
|
}
|
|
|
|
`, rInt)
|
2015-09-16 22:57:12 +02:00
|
|
|
}
|
|
|
|
|
2017-01-10 13:28:36 +01:00
|
|
|
func testAccAWSCloudWatchLogGroupConfigWithTags(rInt int) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "aws_cloudwatch_log_group" "foobar" {
|
|
|
|
name = "foo-bar-%d"
|
|
|
|
|
|
|
|
tags {
|
|
|
|
Environment = "Production"
|
|
|
|
Foo = "Bar"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, rInt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccAWSCloudWatchLogGroupConfigWithTagsUpdated(rInt int) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "aws_cloudwatch_log_group" "foobar" {
|
|
|
|
name = "foo-bar-%d"
|
|
|
|
|
|
|
|
tags {
|
|
|
|
Environment = "Development"
|
|
|
|
Foo = "Bar"
|
|
|
|
Bar = "baz"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, rInt)
|
|
|
|
}
|
|
|
|
|
2016-11-25 13:50:19 +01:00
|
|
|
func testAccAWSCloudWatchLogGroupConfig_withRetention(rInt int) string {
|
|
|
|
return fmt.Sprintf(`
|
2015-09-16 22:57:12 +02:00
|
|
|
resource "aws_cloudwatch_log_group" "foobar" {
|
2016-11-25 13:50:19 +01:00
|
|
|
name = "foo-bar-%d"
|
2015-09-16 22:57:12 +02:00
|
|
|
retention_in_days = 365
|
|
|
|
}
|
2016-11-25 13:50:19 +01:00
|
|
|
`, rInt)
|
|
|
|
}
|
2015-09-16 22:57:12 +02:00
|
|
|
|
2016-11-25 13:50:19 +01:00
|
|
|
func testAccAWSCloudWatchLogGroupConfigModified_withRetention(rInt int) string {
|
|
|
|
return fmt.Sprintf(`
|
2015-09-16 22:57:12 +02:00
|
|
|
resource "aws_cloudwatch_log_group" "foobar" {
|
2016-11-25 13:50:19 +01:00
|
|
|
name = "foo-bar-%d"
|
|
|
|
}
|
|
|
|
`, rInt)
|
2015-09-16 22:57:12 +02:00
|
|
|
}
|
|
|
|
|
2016-11-25 13:50:19 +01:00
|
|
|
func testAccAWSCloudWatchLogGroupConfig_multiple(rInt int) string {
|
|
|
|
return fmt.Sprintf(`
|
2015-09-16 22:57:12 +02:00
|
|
|
resource "aws_cloudwatch_log_group" "alpha" {
|
2016-11-25 13:50:19 +01:00
|
|
|
name = "foo-bar-%d"
|
2015-09-16 22:57:12 +02:00
|
|
|
retention_in_days = 14
|
|
|
|
}
|
|
|
|
resource "aws_cloudwatch_log_group" "beta" {
|
2016-11-25 13:50:19 +01:00
|
|
|
name = "foo-bar-%d"
|
2015-09-16 22:57:12 +02:00
|
|
|
}
|
|
|
|
resource "aws_cloudwatch_log_group" "charlie" {
|
2016-11-25 13:50:19 +01:00
|
|
|
name = "foo-bar-%d"
|
2015-09-16 22:57:12 +02:00
|
|
|
retention_in_days = 3653
|
|
|
|
}
|
2017-01-31 18:06:06 +01:00
|
|
|
`, rInt, rInt+1, rInt+2)
|
2016-11-25 13:50:19 +01:00
|
|
|
}
|