mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-08 15:13:56 -06:00
49b8568bec
This is a requirement for enabling CloudWatch Logging on Kinesis Firehost % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSCloudWatchLogStream_' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/09/02 16:19:14 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSCloudWatchLogStream_ -timeout 120m === RUN TestAccAWSCloudWatchLogStream_basic --- PASS: TestAccAWSCloudWatchLogStream_basic (22.31s) === RUN TestAccAWSCloudWatchLogStream_disappears --- PASS: TestAccAWSCloudWatchLogStream_disappears (21.21s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 43.538s
153 lines
3.9 KiB
Go
153 lines
3.9 KiB
Go
package aws
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
"github.com/hashicorp/terraform/terraform"
|
|
)
|
|
|
|
func TestAccAWSCloudWatchLogStream_basic(t *testing.T) {
|
|
var ls cloudwatchlogs.LogStream
|
|
rName := acctest.RandString(15)
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testAccCheckAWSCloudWatchLogStreamDestroy,
|
|
Steps: []resource.TestStep{
|
|
{
|
|
Config: testAccAWSCloudWatchLogStreamConfig(rName),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckCloudWatchLogStreamExists("aws_cloudwatch_log_stream.foobar", &ls),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccAWSCloudWatchLogStream_disappears(t *testing.T) {
|
|
var ls cloudwatchlogs.LogStream
|
|
rName := acctest.RandString(15)
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testAccCheckAWSCloudWatchLogStreamDestroy,
|
|
Steps: []resource.TestStep{
|
|
{
|
|
Config: testAccAWSCloudWatchLogStreamConfig(rName),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckCloudWatchLogStreamExists("aws_cloudwatch_log_stream.foobar", &ls),
|
|
testAccCheckCloudWatchLogStreamDisappears(&ls, rName),
|
|
),
|
|
ExpectNonEmptyPlan: true,
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func testAccCheckCloudWatchLogStreamDisappears(ls *cloudwatchlogs.LogStream, lgn string) resource.TestCheckFunc {
|
|
return func(s *terraform.State) error {
|
|
conn := testAccProvider.Meta().(*AWSClient).cloudwatchlogsconn
|
|
opts := &cloudwatchlogs.DeleteLogStreamInput{
|
|
LogGroupName: aws.String(lgn),
|
|
LogStreamName: ls.LogStreamName,
|
|
}
|
|
if _, err := conn.DeleteLogStream(opts); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func testAccCheckCloudWatchLogStreamExists(n string, ls *cloudwatchlogs.LogStream) resource.TestCheckFunc {
|
|
return func(s *terraform.State) error {
|
|
rs, ok := s.RootModule().Resources[n]
|
|
if !ok {
|
|
return fmt.Errorf("Not found: %s", n)
|
|
}
|
|
|
|
logGroupName := rs.Primary.Attributes["log_group_name"]
|
|
conn := testAccProvider.Meta().(*AWSClient).cloudwatchlogsconn
|
|
logGroup, exists, err := lookupCloudWatchLogStream(conn, rs.Primary.ID, logGroupName, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !exists {
|
|
return fmt.Errorf("Bad: LogStream %q does not exist", rs.Primary.ID)
|
|
}
|
|
|
|
*ls = *logGroup
|
|
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func testAccCheckAWSCloudWatchLogStreamDestroy(s *terraform.State) error {
|
|
conn := testAccProvider.Meta().(*AWSClient).cloudwatchlogsconn
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
if rs.Type != "aws_cloudwatch_log_stream" {
|
|
continue
|
|
}
|
|
|
|
logGroupName := rs.Primary.Attributes["log_group_name"]
|
|
_, exists, err := lookupCloudWatchLogStream(conn, rs.Primary.ID, logGroupName, nil)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
if exists {
|
|
return fmt.Errorf("Bad: LogStream still exists: %q", rs.Primary.ID)
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func TestValidateCloudWatchLogStreamName(t *testing.T) {
|
|
validNames := []string{
|
|
"test-log-stream",
|
|
"my_sample_log_stream",
|
|
"012345678",
|
|
"logstream/1234",
|
|
}
|
|
for _, v := range validNames {
|
|
_, errors := validateCloudWatchLogStreamName(v, "name")
|
|
if len(errors) != 0 {
|
|
t.Fatalf("%q should be a valid CloudWatch LogStream name: %q", v, errors)
|
|
}
|
|
}
|
|
|
|
invalidNames := []string{
|
|
acctest.RandString(513),
|
|
"",
|
|
"stringwith:colon",
|
|
}
|
|
for _, v := range invalidNames {
|
|
_, errors := validateCloudWatchLogStreamName(v, "name")
|
|
if len(errors) == 0 {
|
|
t.Fatalf("%q should be an invalid CloudWatch LogStream name", v)
|
|
}
|
|
}
|
|
}
|
|
|
|
func testAccAWSCloudWatchLogStreamConfig(rName string) string {
|
|
return fmt.Sprintf(`
|
|
resource "aws_cloudwatch_log_group" "foobar" {
|
|
name = "%s"
|
|
}
|
|
|
|
resource "aws_cloudwatch_log_stream" "foobar" {
|
|
name = "%s"
|
|
log_group_name = "${aws_cloudwatch_log_group.foobar.id}"
|
|
}
|
|
`, rName, rName)
|
|
}
|