Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/aws_emr_cluster: Test empty strings in bootstrap_action.args #32738

Merged
merged 10 commits into from
Jul 28, 2023
9 changes: 4 additions & 5 deletions internal/flex/flex.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ const (
ResourceIdSeparator = ","
)

// Takes the result of flatmap.Expand for an array of strings
// and returns a []*string
// ExpandStringList the result of flatmap.Expand for an array of strings
// and returns a []*string. Empty strings are skipped.
func ExpandStringList(configured []interface{}) []*string {
vs := make([]*string, 0, len(configured))
for _, v := range configured {
val, ok := v.(string)
if ok && val != "" {
vs = append(vs, aws.String(v.(string)))
if v, ok := v.(string); ok && v != "" {
vs = append(vs, aws.String(v))
}
}
return vs
Expand Down
46 changes: 21 additions & 25 deletions internal/flex/flex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,27 @@ import (
func TestExpandStringList(t *testing.T) {
t.Parallel()

configured := []interface{}{"abc", "xyz123"}
got := ExpandStringList(configured)
want := []*string{
aws.String("abc"),
aws.String("xyz123"),
}

if !cmp.Equal(got, want) {
t.Errorf("expanded = %v, want = %v", got, want)
}
}

func TestExpandStringListEmptyItems(t *testing.T) {
t.Parallel()

configured := []interface{}{"foo", "bar", "", "baz"}
got := ExpandStringList(configured)
want := []*string{
aws.String("foo"),
aws.String("bar"),
aws.String("baz"),
}

if !cmp.Equal(got, want) {
t.Errorf("expanded = %v, want = %v", got, want)
testCases := []struct {
configured []interface{}
want []*string
}{
{
configured: []interface{}{"abc", "xyz123"},
want: []*string{aws.String("abc"), aws.String("xyz123")},
},
{
configured: []interface{}{"abc", 123, "xyz123"},
want: []*string{aws.String("abc"), aws.String("xyz123")},
},
{
configured: []interface{}{"foo", "bar", "", "baz"},
want: []*string{aws.String("foo"), aws.String("bar"), aws.String("baz")},
},
}
for _, testCase := range testCases {
if got, want := ExpandStringList(testCase.configured), testCase.want; !cmp.Equal(got, want) {
t.Errorf("ExpandStringList(%v) = %v, want %v", testCase.configured, got, want)
}
}
}

Expand Down
101 changes: 80 additions & 21 deletions internal/service/emr/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,7 @@ func TestAccEMRCluster_Bootstrap_ordering(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccClusterConfig_bootstrap(rName),
Check: resource.ComposeTestCheckFunc(
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckClusterExists(ctx, resourceName, &cluster),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.#", "2"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.0.name", "runif"),
Expand All @@ -1040,7 +1040,18 @@ func TestAccEMRCluster_Bootstrap_ordering(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.0.args.1", "echo running on master node"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.name", "test"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.path", fmt.Sprintf("s3://%s/testscript.sh", rName)),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.#", "10"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.#", "11"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.0", "0"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.1", "1"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.2", "\"\""),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.3", "3"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.4", "4"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.5", "5"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.6", "6"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.7", "7"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.8", "8"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.9", "9"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.10", "10"),
),
},
{
Expand All @@ -1055,7 +1066,7 @@ func TestAccEMRCluster_Bootstrap_ordering(t *testing.T) {
},
{
Config: testAccClusterConfig_bootstrapAdd(rName),
Check: resource.ComposeTestCheckFunc(
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckClusterExists(ctx, resourceName, &cluster),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.#", "3"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.0.name", "runif"),
Expand All @@ -1065,7 +1076,18 @@ func TestAccEMRCluster_Bootstrap_ordering(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.0.args.1", "echo running on master node"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.name", "test"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.path", fmt.Sprintf("s3://%s/testscript.sh", rName)),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.#", "10"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.#", "11"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.0", "0"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.1", "1"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.2", "\"\""),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.3", "3"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.4", "4"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.5", "5"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.6", "6"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.7", "7"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.8", "8"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.9", "9"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.10", "10"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.2.name", "runif-2"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.2.path", "s3://elasticmapreduce/bootstrap-actions/run-if"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.2.args.#", "2"),
Expand All @@ -1085,7 +1107,7 @@ func TestAccEMRCluster_Bootstrap_ordering(t *testing.T) {
},
{
Config: testAccClusterConfig_bootstrapReorder(rName),
Check: resource.ComposeTestCheckFunc(
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckClusterExists(ctx, resourceName, &cluster),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.#", "3"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.0.name", "runif"),
Expand All @@ -1095,7 +1117,18 @@ func TestAccEMRCluster_Bootstrap_ordering(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.0.args.1", "echo running on master node"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.2.name", "test"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.2.path", fmt.Sprintf("s3://%s/testscript.sh", rName)),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.2.args.#", "10"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.2.args.#", "11"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.2.args.0", "0"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.2.args.1", "1"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.2.args.2", "\"\""),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.2.args.3", "3"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.2.args.4", "4"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.2.args.5", "5"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.2.args.6", "6"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.2.args.7", "7"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.2.args.8", "8"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.2.args.9", "9"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.2.args.10", "10"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.name", "runif-2"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.path", "s3://elasticmapreduce/bootstrap-actions/run-if"),
resource.TestCheckResourceAttr(resourceName, "bootstrap_action.1.args.#", "2"),
Expand Down Expand Up @@ -2042,26 +2075,46 @@ resource "aws_iam_role_policy_attachment" "emr_autoscaling_role" {
`, rName)
}

func testAccClusterBootstrapActionBucketConfig(rName string) string {
func testAccClusterConfig_baseBootstrapActionBucket(rName string) string {
return fmt.Sprintf(`
resource "aws_s3_bucket" "tester" {
bucket = %[1]q
}

resource "aws_s3_bucket_public_access_block" "tester" {
bucket = aws_s3_bucket.tester.id

block_public_acls = false
block_public_policy = false
ignore_public_acls = false
restrict_public_buckets = false
}

resource "aws_s3_bucket_ownership_controls" "tester" {
bucket = aws_s3_bucket.tester.id
rule {
object_ownership = "BucketOwnerPreferred"
}
}

resource "aws_s3_bucket_acl" "tester" {
depends_on = [
aws_s3_bucket_public_access_block.tester,
aws_s3_bucket_ownership_controls.tester,
]

bucket = aws_s3_bucket.tester.id
acl = "public-read"
}

resource "aws_s3_object" "testobject" {
bucket = aws_s3_bucket.tester.bucket
bucket = aws_s3_bucket_acl.tester.bucket
key = "testscript.sh"
content = <<EOF
#!/bin/bash
echo $@
EOF


acl = "public-read"
}
`, rName)
Expand Down Expand Up @@ -3016,7 +3069,7 @@ func testAccClusterConfig_bootstrap(rName string) string {
testAccClusterConfig_baseVPC(rName, false),
testAccClusterConfig_baseIAMServiceRole(rName),
testAccClusterConfig_baseIAMInstanceProfile(rName),
testAccClusterBootstrapActionBucketConfig(rName),
testAccClusterConfig_baseBootstrapActionBucket(rName),
fmt.Sprintf(`
data "aws_partition" "current" {}

Expand Down Expand Up @@ -3059,8 +3112,10 @@ resource "aws_emr_cluster" "test" {
path = "s3://${aws_s3_object.testobject.bucket}/${aws_s3_object.testobject.key}"
name = "test"

args = ["1",
"2",
args = [
"0",
"1",
"\"\"",
"3",
"4",
"5",
Expand All @@ -3080,7 +3135,7 @@ func testAccClusterConfig_bootstrapAdd(rName string) string {
testAccClusterConfig_baseVPC(rName, false),
testAccClusterConfig_baseIAMServiceRole(rName),
testAccClusterConfig_baseIAMInstanceProfile(rName),
testAccClusterBootstrapActionBucketConfig(rName),
testAccClusterConfig_baseBootstrapActionBucket(rName),
fmt.Sprintf(`
data "aws_partition" "current" {}

Expand Down Expand Up @@ -3123,8 +3178,10 @@ resource "aws_emr_cluster" "test" {
path = "s3://${aws_s3_object.testobject.bucket}/${aws_s3_object.testobject.key}"
name = "test"

args = ["1",
"2",
args = [
"0",
"1",
"\"\"",
"3",
"4",
"5",
Expand All @@ -3150,7 +3207,7 @@ func testAccClusterConfig_bootstrapReorder(rName string) string {
testAccClusterConfig_baseVPC(rName, false),
testAccClusterConfig_baseIAMServiceRole(rName),
testAccClusterConfig_baseIAMInstanceProfile(rName),
testAccClusterBootstrapActionBucketConfig(rName),
testAccClusterConfig_baseBootstrapActionBucket(rName),
fmt.Sprintf(`
data "aws_partition" "current" {}

Expand Down Expand Up @@ -3199,8 +3256,10 @@ resource "aws_emr_cluster" "test" {
path = "s3://${aws_s3_object.testobject.bucket}/${aws_s3_object.testobject.key}"
name = "test"

args = ["1",
"2",
args = [
"0",
"1",
"\"\"",
"3",
"4",
"5",
Expand Down Expand Up @@ -3783,7 +3842,7 @@ func testAccClusterConfig_instanceFleets(rName string) string {
testAccClusterConfig_baseVPC(rName, false),
testAccClusterConfig_baseIAMServiceRole(rName),
testAccClusterConfig_baseIAMInstanceProfile(rName),
testAccClusterBootstrapActionBucketConfig(rName),
testAccClusterConfig_baseBootstrapActionBucket(rName),
fmt.Sprintf(`
data "aws_partition" "current" {}

Expand Down Expand Up @@ -3871,7 +3930,7 @@ func testAccClusterConfig_instanceFleetMultipleSubnets(rName string) string {
testAccClusterConfig_baseVPC(rName, false),
testAccClusterConfig_baseIAMServiceRole(rName),
testAccClusterConfig_baseIAMInstanceProfile(rName),
testAccClusterBootstrapActionBucketConfig(rName),
testAccClusterConfig_baseBootstrapActionBucket(rName),
fmt.Sprintf(`
data "aws_partition" "current" {}

Expand Down Expand Up @@ -3971,7 +4030,7 @@ func testAccClusterConfig_instanceFleetsMasterOnly(rName string) string {
testAccClusterConfig_baseVPC(rName, false),
testAccClusterConfig_baseIAMServiceRole(rName),
testAccClusterConfig_baseIAMInstanceProfile(rName),
testAccClusterBootstrapActionBucketConfig(rName),
testAccClusterConfig_baseBootstrapActionBucket(rName),
fmt.Sprintf(`
data "aws_partition" "current" {}

Expand Down