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