github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/aws/resource_aws_cloudwatch_log_group_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"testing"
     7  
     8  	"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
     9  	"github.com/hashicorp/terraform/helper/acctest"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccAWSCloudWatchLogGroup_basic(t *testing.T) {
    15  	var lg cloudwatchlogs.LogGroup
    16  	rInt := acctest.RandInt()
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckAWSCloudWatchLogGroupDestroy,
    22  		Steps: []resource.TestStep{
    23  			{
    24  				Config: testAccAWSCloudWatchLogGroupConfig(rInt),
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckCloudWatchLogGroupExists("aws_cloudwatch_log_group.foobar", &lg),
    27  					resource.TestCheckResourceAttr("aws_cloudwatch_log_group.foobar", "retention_in_days", "0"),
    28  				),
    29  			},
    30  		},
    31  	})
    32  }
    33  
    34  func TestAccAWSCloudWatchLogGroup_namePrefix(t *testing.T) {
    35  	var lg cloudwatchlogs.LogGroup
    36  
    37  	resource.Test(t, resource.TestCase{
    38  		PreCheck:     func() { testAccPreCheck(t) },
    39  		Providers:    testAccProviders,
    40  		CheckDestroy: testAccCheckAWSCloudWatchLogGroupDestroy,
    41  		Steps: []resource.TestStep{
    42  			{
    43  				Config: testAccAWSCloudWatchLogGroup_namePrefix,
    44  				Check: resource.ComposeTestCheckFunc(
    45  					testAccCheckCloudWatchLogGroupExists("aws_cloudwatch_log_group.test", &lg),
    46  					resource.TestMatchResourceAttr("aws_cloudwatch_log_group.test", "name", regexp.MustCompile("^tf-test-")),
    47  				),
    48  			},
    49  		},
    50  	})
    51  }
    52  
    53  func TestAccAWSCloudWatchLogGroup_generatedName(t *testing.T) {
    54  	var lg cloudwatchlogs.LogGroup
    55  
    56  	resource.Test(t, resource.TestCase{
    57  		PreCheck:     func() { testAccPreCheck(t) },
    58  		Providers:    testAccProviders,
    59  		CheckDestroy: testAccCheckAWSCloudWatchLogGroupDestroy,
    60  		Steps: []resource.TestStep{
    61  			{
    62  				Config: testAccAWSCloudWatchLogGroup_generatedName,
    63  				Check: resource.ComposeTestCheckFunc(
    64  					testAccCheckCloudWatchLogGroupExists("aws_cloudwatch_log_group.test", &lg),
    65  				),
    66  			},
    67  		},
    68  	})
    69  }
    70  
    71  func TestAccAWSCloudWatchLogGroup_retentionPolicy(t *testing.T) {
    72  	var lg cloudwatchlogs.LogGroup
    73  	rInt := acctest.RandInt()
    74  
    75  	resource.Test(t, resource.TestCase{
    76  		PreCheck:     func() { testAccPreCheck(t) },
    77  		Providers:    testAccProviders,
    78  		CheckDestroy: testAccCheckAWSCloudWatchLogGroupDestroy,
    79  		Steps: []resource.TestStep{
    80  			{
    81  				Config: testAccAWSCloudWatchLogGroupConfig_withRetention(rInt),
    82  				Check: resource.ComposeTestCheckFunc(
    83  					testAccCheckCloudWatchLogGroupExists("aws_cloudwatch_log_group.foobar", &lg),
    84  					resource.TestCheckResourceAttr("aws_cloudwatch_log_group.foobar", "retention_in_days", "365"),
    85  				),
    86  			},
    87  			{
    88  				Config: testAccAWSCloudWatchLogGroupConfigModified_withRetention(rInt),
    89  				Check: resource.ComposeTestCheckFunc(
    90  					testAccCheckCloudWatchLogGroupExists("aws_cloudwatch_log_group.foobar", &lg),
    91  					resource.TestCheckResourceAttr("aws_cloudwatch_log_group.foobar", "retention_in_days", "0"),
    92  				),
    93  			},
    94  		},
    95  	})
    96  }
    97  
    98  func TestAccAWSCloudWatchLogGroup_multiple(t *testing.T) {
    99  	var lg cloudwatchlogs.LogGroup
   100  	rInt := acctest.RandInt()
   101  
   102  	resource.Test(t, resource.TestCase{
   103  		PreCheck:     func() { testAccPreCheck(t) },
   104  		Providers:    testAccProviders,
   105  		CheckDestroy: testAccCheckAWSCloudWatchLogGroupDestroy,
   106  		Steps: []resource.TestStep{
   107  			{
   108  				Config: testAccAWSCloudWatchLogGroupConfig_multiple(rInt),
   109  				Check: resource.ComposeTestCheckFunc(
   110  					testAccCheckCloudWatchLogGroupExists("aws_cloudwatch_log_group.alpha", &lg),
   111  					resource.TestCheckResourceAttr("aws_cloudwatch_log_group.alpha", "retention_in_days", "14"),
   112  					testAccCheckCloudWatchLogGroupExists("aws_cloudwatch_log_group.beta", &lg),
   113  					resource.TestCheckResourceAttr("aws_cloudwatch_log_group.beta", "retention_in_days", "0"),
   114  					testAccCheckCloudWatchLogGroupExists("aws_cloudwatch_log_group.charlie", &lg),
   115  					resource.TestCheckResourceAttr("aws_cloudwatch_log_group.charlie", "retention_in_days", "3653"),
   116  				),
   117  			},
   118  		},
   119  	})
   120  }
   121  
   122  func TestAccAWSCloudWatchLogGroup_disappears(t *testing.T) {
   123  	var lg cloudwatchlogs.LogGroup
   124  	rInt := acctest.RandInt()
   125  
   126  	resource.Test(t, resource.TestCase{
   127  		PreCheck:     func() { testAccPreCheck(t) },
   128  		Providers:    testAccProviders,
   129  		CheckDestroy: testAccCheckAWSCloudWatchLogGroupDestroy,
   130  		Steps: []resource.TestStep{
   131  			{
   132  				Config: testAccAWSCloudWatchLogGroupConfig(rInt),
   133  				Check: resource.ComposeTestCheckFunc(
   134  					testAccCheckCloudWatchLogGroupExists("aws_cloudwatch_log_group.foobar", &lg),
   135  					testAccCheckCloudWatchLogGroupDisappears(&lg),
   136  				),
   137  				ExpectNonEmptyPlan: true,
   138  			},
   139  		},
   140  	})
   141  }
   142  
   143  func TestAccAWSCloudWatchLogGroup_tagging(t *testing.T) {
   144  	var lg cloudwatchlogs.LogGroup
   145  	rInt := acctest.RandInt()
   146  
   147  	resource.Test(t, resource.TestCase{
   148  		PreCheck:     func() { testAccPreCheck(t) },
   149  		Providers:    testAccProviders,
   150  		CheckDestroy: testAccCheckAWSCloudWatchLogGroupDestroy,
   151  		Steps: []resource.TestStep{
   152  			{
   153  				Config: testAccAWSCloudWatchLogGroupConfigWithTags(rInt),
   154  				Check: resource.ComposeTestCheckFunc(
   155  					testAccCheckCloudWatchLogGroupExists("aws_cloudwatch_log_group.foobar", &lg),
   156  					resource.TestCheckResourceAttr("aws_cloudwatch_log_group.foobar", "tags.%", "2"),
   157  					resource.TestCheckResourceAttr("aws_cloudwatch_log_group.foobar", "tags.Environment", "Production"),
   158  					resource.TestCheckResourceAttr("aws_cloudwatch_log_group.foobar", "tags.Foo", "Bar"),
   159  				),
   160  			},
   161  			{
   162  				Config: testAccAWSCloudWatchLogGroupConfigWithTagsUpdated(rInt),
   163  				Check: resource.ComposeTestCheckFunc(
   164  					testAccCheckCloudWatchLogGroupExists("aws_cloudwatch_log_group.foobar", &lg),
   165  					resource.TestCheckResourceAttr("aws_cloudwatch_log_group.foobar", "tags.%", "3"),
   166  					resource.TestCheckResourceAttr("aws_cloudwatch_log_group.foobar", "tags.Environment", "Development"),
   167  					resource.TestCheckResourceAttr("aws_cloudwatch_log_group.foobar", "tags.Bar", "baz"),
   168  				),
   169  			},
   170  		},
   171  	})
   172  }
   173  
   174  func testAccCheckCloudWatchLogGroupDisappears(lg *cloudwatchlogs.LogGroup) resource.TestCheckFunc {
   175  	return func(s *terraform.State) error {
   176  		conn := testAccProvider.Meta().(*AWSClient).cloudwatchlogsconn
   177  		opts := &cloudwatchlogs.DeleteLogGroupInput{
   178  			LogGroupName: lg.LogGroupName,
   179  		}
   180  		if _, err := conn.DeleteLogGroup(opts); err != nil {
   181  			return err
   182  		}
   183  		return nil
   184  	}
   185  }
   186  
   187  func testAccCheckCloudWatchLogGroupExists(n string, lg *cloudwatchlogs.LogGroup) resource.TestCheckFunc {
   188  	return func(s *terraform.State) error {
   189  		rs, ok := s.RootModule().Resources[n]
   190  		if !ok {
   191  			return fmt.Errorf("Not found: %s", n)
   192  		}
   193  
   194  		conn := testAccProvider.Meta().(*AWSClient).cloudwatchlogsconn
   195  		logGroup, exists, err := lookupCloudWatchLogGroup(conn, rs.Primary.ID, nil)
   196  		if err != nil {
   197  			return err
   198  		}
   199  		if !exists {
   200  			return fmt.Errorf("Bad: LogGroup %q does not exist", rs.Primary.ID)
   201  		}
   202  
   203  		*lg = *logGroup
   204  
   205  		return nil
   206  	}
   207  }
   208  
   209  func testAccCheckAWSCloudWatchLogGroupDestroy(s *terraform.State) error {
   210  	conn := testAccProvider.Meta().(*AWSClient).cloudwatchlogsconn
   211  
   212  	for _, rs := range s.RootModule().Resources {
   213  		if rs.Type != "aws_cloudwatch_log_group" {
   214  			continue
   215  		}
   216  		_, exists, err := lookupCloudWatchLogGroup(conn, rs.Primary.ID, nil)
   217  		if err != nil {
   218  			return nil
   219  		}
   220  
   221  		if exists {
   222  			return fmt.Errorf("Bad: LogGroup still exists: %q", rs.Primary.ID)
   223  		}
   224  
   225  	}
   226  
   227  	return nil
   228  }
   229  
   230  func testAccAWSCloudWatchLogGroupConfig(rInt int) string {
   231  	return fmt.Sprintf(`
   232  resource "aws_cloudwatch_log_group" "foobar" {
   233      name = "foo-bar-%d"
   234  }
   235  `, rInt)
   236  }
   237  
   238  func testAccAWSCloudWatchLogGroupConfigWithTags(rInt int) string {
   239  	return fmt.Sprintf(`
   240  resource "aws_cloudwatch_log_group" "foobar" {
   241      name = "foo-bar-%d"
   242  
   243      tags {
   244      	Environment = "Production"
   245      	Foo = "Bar"
   246      }
   247  }
   248  `, rInt)
   249  }
   250  
   251  func testAccAWSCloudWatchLogGroupConfigWithTagsUpdated(rInt int) string {
   252  	return fmt.Sprintf(`
   253  resource "aws_cloudwatch_log_group" "foobar" {
   254      name = "foo-bar-%d"
   255  
   256      tags {
   257      	Environment = "Development"
   258      	Foo = "Bar"
   259      	Bar = "baz"
   260      }
   261  }
   262  `, rInt)
   263  }
   264  
   265  func testAccAWSCloudWatchLogGroupConfig_withRetention(rInt int) string {
   266  	return fmt.Sprintf(`
   267  resource "aws_cloudwatch_log_group" "foobar" {
   268      name = "foo-bar-%d"
   269      retention_in_days = 365
   270  }
   271  `, rInt)
   272  }
   273  
   274  func testAccAWSCloudWatchLogGroupConfigModified_withRetention(rInt int) string {
   275  	return fmt.Sprintf(`
   276  resource "aws_cloudwatch_log_group" "foobar" {
   277      name = "foo-bar-%d"
   278  }
   279  `, rInt)
   280  }
   281  
   282  func testAccAWSCloudWatchLogGroupConfig_multiple(rInt int) string {
   283  	return fmt.Sprintf(`
   284  resource "aws_cloudwatch_log_group" "alpha" {
   285      name = "foo-bar-%d"
   286      retention_in_days = 14
   287  }
   288  resource "aws_cloudwatch_log_group" "beta" {
   289      name = "foo-bar-%d"
   290  }
   291  resource "aws_cloudwatch_log_group" "charlie" {
   292      name = "foo-bar-%d"
   293      retention_in_days = 3653
   294  }
   295  `, rInt, rInt+1, rInt+2)
   296  }
   297  
   298  const testAccAWSCloudWatchLogGroup_namePrefix = `
   299  resource "aws_cloudwatch_log_group" "test" {
   300      name_prefix = "tf-test-"
   301  }
   302  `
   303  
   304  const testAccAWSCloudWatchLogGroup_generatedName = `
   305  resource "aws_cloudwatch_log_group" "test" {}
   306  `