Skip to content

Commit

Permalink
hashicorp#20068 logdeliveryconfigurations replication_group_data_sour…
Browse files Browse the repository at this point in the history
…ce tests, simplify to one test
  • Loading branch information
nick committed Mar 7, 2022
1 parent 05583c7 commit f858c11
Showing 1 changed file with 11 additions and 188 deletions.
199 changes: 11 additions & 188 deletions internal/service/elasticache/replication_group_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestAccElastiCacheReplicationGroupDataSource_nonExistent(t *testing.T) {
})
}

func TestAccElastiCacheReplicationGroupDataSource_Engine_Redis_LogDeliveryConfigurations_CloudWatch(t *testing.T) {
func TestAccElastiCacheReplicationGroupDataSource_Engine_Redis_LogDeliveryConfigurations(t *testing.T) {
rName := sdkacctest.RandomWithPrefix("tf-acc-test")
dataSourceName := "data.aws_elasticache_replication_group.test"

Expand All @@ -116,34 +116,20 @@ func TestAccElastiCacheReplicationGroupDataSource_Engine_Redis_LogDeliveryConfig
Providers: acctest.Providers,
Steps: []resource.TestStep{
{
Config: testAccElasticacheReplicationGroupConfig_Engine_Redis_LogDeliveryConfigurations_Cloudwatch(rName, true, false),
Config: testAccReplicationGroupConfig_Engine_Redis_LogDeliveryConfigurations(rName, true, true, elasticache.DestinationTypeCloudwatchLogs, elasticache.LogFormatText, true, elasticache.DestinationTypeKinesisFirehose, elasticache.LogFormatJson),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "log_delivery_configurations.0.destination_details.0.cloudwatch_logs.0.log_group", rName),
resource.TestCheckResourceAttr(dataSourceName, "engine", "redis"),
resource.TestCheckResourceAttr(dataSourceName, "cluster_enabled", "true"),
resource.TestCheckResourceAttr(dataSourceName, "cluster_mode.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "parameter_group_name", "default.redis6.x.cluster.on"),
resource.TestCheckResourceAttr(dataSourceName, "log_delivery_configurations.0.destination", rName),
resource.TestCheckResourceAttr(dataSourceName, "log_delivery_configurations.0.destination_type", "cloudwatch-logs"),
resource.TestCheckResourceAttr(dataSourceName, "log_delivery_configurations.0.log_format", "text"),
resource.TestCheckResourceAttr(dataSourceName, "log_delivery_configurations.0.log_type", "slow-log"),
),
},
},
})
}

func TestAccElastiCacheReplicationGroupDataSource_Engine_Redis_LogDeliveryConfigurations_KinesisFirehose(t *testing.T) {
rName := sdkacctest.RandomWithPrefix("tf-acc-test")
dataSourceName := "data.aws_elasticache_replication_group.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, elasticache.EndpointsID),
Providers: acctest.Providers,
Steps: []resource.TestStep{
{
Config: testAccElasticacheReplicationGroupConfig_Engine_Redis_LogDeliveryConfigurations_KinesisFirehose(rName, true, false),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "log_delivery_configurations.0.destination_details.0.kinesis_firehose.0.delivery_stream", rName),
resource.TestCheckResourceAttr(dataSourceName, "log_delivery_configurations.0.destination_type", "kinesis-firehose"),
resource.TestCheckResourceAttr(dataSourceName, "log_delivery_configurations.0.log_format", "json"),
resource.TestCheckResourceAttr(dataSourceName, "log_delivery_configurations.0.log_type", "slow-log"),
resource.TestCheckResourceAttr(dataSourceName, "log_delivery_configurations.1.destination", rName),
resource.TestCheckResourceAttr(dataSourceName, "log_delivery_configurations.1.destination_type", "kinesis-firehose"),
resource.TestCheckResourceAttr(dataSourceName, "log_delivery_configurations.1.log_format", "json"),
resource.TestCheckResourceAttr(dataSourceName, "log_delivery_configurations.1.log_type", "engine-log"),
),
},
},
Expand Down Expand Up @@ -207,169 +193,6 @@ data "aws_elasticache_replication_group" "test" {
`, rName)
}

func testAccElasticacheReplicationGroupConfig_Engine_Redis_LogDeliveryConfigurations_Cloudwatch(rName string, enableLogDelivery bool, enableClusterMode bool) string {
return fmt.Sprintf(`
data "aws_iam_policy_document" "p" {
count = tobool("%[2]t") ? 1 : 0
statement {
actions = [
"logs:CreateLogStream",
"logs:PutLogEvents"
]
resources = ["${aws_cloudwatch_log_group.lg[0].arn}:log-stream:*"]
principals {
identifiers = ["delivery.logs.amazonaws.com"]
type = "Service"
}
}
}
resource "aws_cloudwatch_log_resource_policy" "rp" {
count = tobool("%[2]t") ? 1 : 0
policy_document = data.aws_iam_policy_document.p[0].json
policy_name = "%[1]s"
depends_on = [
aws_cloudwatch_log_group.lg[0]
]
}
resource "aws_cloudwatch_log_group" "lg" {
count = tobool("%[2]t") ? 1 : 0
retention_in_days = 1
name = "%[1]s"
}
resource "aws_elasticache_replication_group" "test" {
replication_group_id = "%[1]s"
replication_group_description = "test description"
node_type = "cache.t3.small"
port = 6379
apply_immediately = true
auto_minor_version_upgrade = false
maintenance_window = "tue:06:30-tue:07:30"
snapshot_window = "01:00-02:00"
parameter_group_name = tobool("%[3]t") ? "default.redis6.x.cluster.on" : "default.redis6.x"
automatic_failover_enabled = tobool("%[3]t")
dynamic "cluster_mode" {
for_each = tobool("%[3]t") ? [""] : []
content {
num_node_groups = 1
replicas_per_node_group = 0
}
}
dynamic "log_delivery_configurations" {
for_each = tobool("%[2]t") ? [""] : []
content {
destination_details {
cloudwatch_logs {
log_group = aws_cloudwatch_log_group.lg[0].name
}
}
destination_type = "cloudwatch-logs"
log_format = "text"
log_type = "slow-log"
}
}
}
data "aws_elasticache_replication_group" "test" {
replication_group_id = aws_elasticache_replication_group.test.replication_group_id
}
`, rName, enableLogDelivery, enableClusterMode)
}

func testAccElasticacheReplicationGroupConfig_Engine_Redis_LogDeliveryConfigurations_KinesisFirehose(rName string, enableLogDelivery bool, enableClusterMode bool) string {
return fmt.Sprintf(`
resource "aws_s3_bucket" "b" {
count = tobool("%[2]t") ? 1 : 0
acl = "private"
force_destroy = true
}
resource "aws_iam_role" "r" {
count = tobool("%[2]t") ? 1 : 0
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = ""
Principal = {
Service = "firehose.amazonaws.com"
}
},
]
})
inline_policy {
name = "my_inline_s3_policy"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = [
"s3:AbortMultipartUpload",
"s3:GetBucketLocation",
"s3:GetObject",
"s3:ListBucket",
"s3:ListBucketMultipartUploads",
"s3:PutObject",
"s3:PutObjectAcl",
]
Effect = "Allow"
Resource = ["${aws_s3_bucket.b[0].arn}", "${aws_s3_bucket.b[0].arn}/*"]
},
]
})
}
}
resource "aws_kinesis_firehose_delivery_stream" "ds" {
count = tobool("%[2]t") ? 1 : 0
name = "%[1]s"
destination = "s3"
s3_configuration {
role_arn = aws_iam_role.r[0].arn
bucket_arn = aws_s3_bucket.b[0].arn
}
lifecycle {
ignore_changes = [
tags["LogDeliveryEnabled"],
]
}
}
resource "aws_elasticache_replication_group" "test" {
replication_group_id = "%[1]s"
replication_group_description = "test description"
node_type = "cache.t3.small"
port = 6379
apply_immediately = true
auto_minor_version_upgrade = false
maintenance_window = "tue:06:30-tue:07:30"
snapshot_window = "01:00-02:00"
parameter_group_name = tobool("%[3]t") ? "default.redis6.x.cluster.on" : "default.redis6.x"
automatic_failover_enabled = tobool("%[3]t")
dynamic "cluster_mode" {
for_each = tobool("%[3]t") ? [""] : []
content {
num_node_groups = 1
replicas_per_node_group = 0
}
}
dynamic "log_delivery_configurations" {
for_each = tobool("%[2]t") ? [""] : []
content {
destination_details {
kinesis_firehose {
delivery_stream = aws_kinesis_firehose_delivery_stream.ds[0].name
}
}
destination_type = "kinesis-firehose"
log_format = "json"
log_type = "slow-log"
}
}
}
data "aws_elasticache_replication_group" "test" {
replication_group_id = aws_elasticache_replication_group.test.replication_group_id
}
`, rName, enableLogDelivery, enableClusterMode)
}

const testAccReplicationGroupDataSourceConfig_NonExistent = `
data "aws_elasticache_replication_group" "test" {
replication_group_id = "tf-acc-test-nonexistent"
Expand Down

0 comments on commit f858c11

Please sign in to comment.