github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_cloudwatch_log_stream_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/aws" 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 TestAccAWSCloudWatchLogStream_basic(t *testing.T) { 15 var ls cloudwatchlogs.LogStream 16 rName := acctest.RandString(15) 17 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccCheckAWSCloudWatchLogStreamDestroy, 22 Steps: []resource.TestStep{ 23 { 24 Config: testAccAWSCloudWatchLogStreamConfig(rName), 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckCloudWatchLogStreamExists("aws_cloudwatch_log_stream.foobar", &ls), 27 ), 28 }, 29 }, 30 }) 31 } 32 33 func TestAccAWSCloudWatchLogStream_disappears(t *testing.T) { 34 var ls cloudwatchlogs.LogStream 35 rName := acctest.RandString(15) 36 37 resource.Test(t, resource.TestCase{ 38 PreCheck: func() { testAccPreCheck(t) }, 39 Providers: testAccProviders, 40 CheckDestroy: testAccCheckAWSCloudWatchLogStreamDestroy, 41 Steps: []resource.TestStep{ 42 { 43 Config: testAccAWSCloudWatchLogStreamConfig(rName), 44 Check: resource.ComposeTestCheckFunc( 45 testAccCheckCloudWatchLogStreamExists("aws_cloudwatch_log_stream.foobar", &ls), 46 testAccCheckCloudWatchLogStreamDisappears(&ls, rName), 47 ), 48 ExpectNonEmptyPlan: true, 49 }, 50 }, 51 }) 52 } 53 54 func testAccCheckCloudWatchLogStreamDisappears(ls *cloudwatchlogs.LogStream, lgn string) resource.TestCheckFunc { 55 return func(s *terraform.State) error { 56 conn := testAccProvider.Meta().(*AWSClient).cloudwatchlogsconn 57 opts := &cloudwatchlogs.DeleteLogStreamInput{ 58 LogGroupName: aws.String(lgn), 59 LogStreamName: ls.LogStreamName, 60 } 61 if _, err := conn.DeleteLogStream(opts); err != nil { 62 return err 63 } 64 return nil 65 } 66 } 67 68 func testAccCheckCloudWatchLogStreamExists(n string, ls *cloudwatchlogs.LogStream) resource.TestCheckFunc { 69 return func(s *terraform.State) error { 70 rs, ok := s.RootModule().Resources[n] 71 if !ok { 72 return fmt.Errorf("Not found: %s", n) 73 } 74 75 logGroupName := rs.Primary.Attributes["log_group_name"] 76 conn := testAccProvider.Meta().(*AWSClient).cloudwatchlogsconn 77 logGroup, exists, err := lookupCloudWatchLogStream(conn, rs.Primary.ID, logGroupName, nil) 78 if err != nil { 79 return err 80 } 81 if !exists { 82 return fmt.Errorf("Bad: LogStream %q does not exist", rs.Primary.ID) 83 } 84 85 *ls = *logGroup 86 87 return nil 88 } 89 } 90 91 func testAccCheckAWSCloudWatchLogStreamDestroy(s *terraform.State) error { 92 conn := testAccProvider.Meta().(*AWSClient).cloudwatchlogsconn 93 94 for _, rs := range s.RootModule().Resources { 95 if rs.Type != "aws_cloudwatch_log_stream" { 96 continue 97 } 98 99 logGroupName := rs.Primary.Attributes["log_group_name"] 100 _, exists, err := lookupCloudWatchLogStream(conn, rs.Primary.ID, logGroupName, nil) 101 if err != nil { 102 return nil 103 } 104 105 if exists { 106 return fmt.Errorf("Bad: LogStream still exists: %q", rs.Primary.ID) 107 } 108 109 } 110 111 return nil 112 } 113 114 func TestValidateCloudWatchLogStreamName(t *testing.T) { 115 validNames := []string{ 116 "test-log-stream", 117 "my_sample_log_stream", 118 "012345678", 119 "logstream/1234", 120 } 121 for _, v := range validNames { 122 _, errors := validateCloudWatchLogStreamName(v, "name") 123 if len(errors) != 0 { 124 t.Fatalf("%q should be a valid CloudWatch LogStream name: %q", v, errors) 125 } 126 } 127 128 invalidNames := []string{ 129 acctest.RandString(513), 130 "", 131 "stringwith:colon", 132 } 133 for _, v := range invalidNames { 134 _, errors := validateCloudWatchLogStreamName(v, "name") 135 if len(errors) == 0 { 136 t.Fatalf("%q should be an invalid CloudWatch LogStream name", v) 137 } 138 } 139 } 140 141 func testAccAWSCloudWatchLogStreamConfig(rName string) string { 142 return fmt.Sprintf(` 143 resource "aws_cloudwatch_log_group" "foobar" { 144 name = "%s" 145 } 146 147 resource "aws_cloudwatch_log_stream" "foobar" { 148 name = "%s" 149 log_group_name = "${aws_cloudwatch_log_group.foobar.id}" 150 } 151 `, rName, rName) 152 }