From b8e00554a16aebe4188f2e36eb648da9e36fa72d Mon Sep 17 00:00:00 2001 From: Dirk Avery Date: Tue, 5 Oct 2021 17:10:11 -0400 Subject: [PATCH] tests/emr_cluster: Clean-up, standardize tests --- aws/resource_aws_emr_cluster_test.go | 2336 +++++++++++++------------- 1 file changed, 1183 insertions(+), 1153 deletions(-) diff --git a/aws/resource_aws_emr_cluster_test.go b/aws/resource_aws_emr_cluster_test.go index eb8b754c14c..afebd0bec5a 100644 --- a/aws/resource_aws_emr_cluster_test.go +++ b/aws/resource_aws_emr_cluster_test.go @@ -80,7 +80,7 @@ func testSweepEmrClusters(region string) error { func TestAccAWSEMRCluster_basic(t *testing.T) { var cluster emr.Cluster - resourceName := "aws_emr_cluster.tf-test-cluster" + resourceName := "aws_emr_cluster.test" rName := acctest.RandomWithPrefix("tf-acc-test") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -123,7 +123,7 @@ func TestAccAWSEMRCluster_additionalInfo(t *testing.T) { } }` - resourceName := "aws_emr_cluster.tf-test-cluster" + resourceName := "aws_emr_cluster.test" rName := acctest.RandomWithPrefix("tf-acc-test") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -158,7 +158,7 @@ func TestAccAWSEMRCluster_additionalInfo(t *testing.T) { func TestAccAWSEMRCluster_disappears(t *testing.T) { var cluster emr.Cluster - resourceName := "aws_emr_cluster.tf-test-cluster" + resourceName := "aws_emr_cluster.test" rName := acctest.RandomWithPrefix("tf-acc-test") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -181,7 +181,7 @@ func TestAccAWSEMRCluster_disappears(t *testing.T) { func TestAccAWSEMRCluster_configurationsJson(t *testing.T) { var cluster emr.Cluster - resourceName := "aws_emr_cluster.tf-test-cluster" + resourceName := "aws_emr_cluster.test" rName := acctest.RandomWithPrefix("tf-acc-test") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -510,7 +510,7 @@ func TestAccAWSEMRCluster_Ec2Attributes_DefaultManagedSecurityGroups(t *testing. var vpc ec2.Vpc rName := acctest.RandomWithPrefix("tf-acc-test") - resourceName := "aws_emr_cluster.tf-test-cluster" + resourceName := "aws_emr_cluster.test" vpcResourceName := "aws_vpc.test" resource.ParallelTest(t, resource.TestCase{ @@ -551,7 +551,7 @@ func TestAccAWSEMRCluster_Ec2Attributes_DefaultManagedSecurityGroups(t *testing. func TestAccAWSEMRCluster_Kerberos_ClusterDedicatedKdc(t *testing.T) { var cluster emr.Cluster - resourceName := "aws_emr_cluster.tf-test-cluster" + resourceName := "aws_emr_cluster.test" rName := acctest.RandomWithPrefix("tf-acc-test") password := fmt.Sprintf("NeverKeepPasswordsInPlainText%s!", rName) resource.ParallelTest(t, resource.TestCase{ @@ -755,7 +755,7 @@ func TestAccAWSEMRCluster_MasterInstanceGroup_Name(t *testing.T) { func TestAccAWSEMRCluster_security_config(t *testing.T) { var cluster emr.Cluster - resourceName := "aws_emr_cluster.tf-test-cluster" + resourceName := "aws_emr_cluster.test" rName := acctest.RandomWithPrefix("tf-acc-test") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -767,7 +767,7 @@ func TestAccAWSEMRCluster_security_config(t *testing.T) { Config: testAccAWSEmrClusterConfig_SecurityConfiguration(rName), Check: resource.ComposeTestCheckFunc( testAccCheckAWSEmrClusterExists(resourceName, &cluster), - resource.TestCheckResourceAttrPair(resourceName, "security_configuration", "aws_emr_security_configuration.foo", "name"), + resource.TestCheckResourceAttrPair(resourceName, "security_configuration", "aws_emr_security_configuration.test", "name"), ), }, { @@ -787,7 +787,7 @@ func TestAccAWSEMRCluster_security_config(t *testing.T) { func TestAccAWSEMRCluster_Step_Basic(t *testing.T) { var cluster emr.Cluster - resourceName := "aws_emr_cluster.tf-test-cluster" + resourceName := "aws_emr_cluster.test" rName := acctest.RandomWithPrefix("tf-acc-test") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -825,7 +825,7 @@ func TestAccAWSEMRCluster_Step_Basic(t *testing.T) { func TestAccAWSEMRCluster_Step_ConfigMode(t *testing.T) { var cluster1, cluster2, cluster3 emr.Cluster - resourceName := "aws_emr_cluster.tf-test-cluster" + resourceName := "aws_emr_cluster.test" rName := acctest.RandomWithPrefix("tf-acc-test") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -891,7 +891,7 @@ func TestAccAWSEMRCluster_Step_ConfigMode(t *testing.T) { func TestAccAWSEMRCluster_Step_Multiple(t *testing.T) { var cluster emr.Cluster - resourceName := "aws_emr_cluster.tf-test-cluster" + resourceName := "aws_emr_cluster.test" rName := acctest.RandomWithPrefix("tf-acc-test") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -1033,7 +1033,7 @@ func TestAccAWSEMRCluster_bootstrap_ordering(t *testing.T) { func TestAccAWSEMRCluster_terminationProtected(t *testing.T) { var cluster emr.Cluster - resourceName := "aws_emr_cluster.tf-test-cluster" + resourceName := "aws_emr_cluster.test" rName := acctest.RandomWithPrefix("tf-acc-test") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -1090,7 +1090,7 @@ func TestAccAWSEMRCluster_terminationProtected(t *testing.T) { func TestAccAWSEMRCluster_keepJob(t *testing.T) { var cluster emr.Cluster - resourceName := "aws_emr_cluster.tf-test-cluster" + resourceName := "aws_emr_cluster.test" rName := acctest.RandomWithPrefix("tf-acc-test") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -1099,7 +1099,7 @@ func TestAccAWSEMRCluster_keepJob(t *testing.T) { CheckDestroy: testAccCheckAWSEmrDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSEmrClusterConfig_keepJob(rName, "false"), + Config: testAccAWSEmrClusterConfig_keepJob(rName, false), Check: resource.ComposeTestCheckFunc( testAccCheckAWSEmrClusterExists(resourceName, &cluster), resource.TestCheckResourceAttr(resourceName, "keep_job_flow_alive_when_no_steps", "false"), @@ -1122,7 +1122,7 @@ func TestAccAWSEMRCluster_keepJob(t *testing.T) { func TestAccAWSEMRCluster_visibleToAllUsers(t *testing.T) { var cluster emr.Cluster - resourceName := "aws_emr_cluster.tf-test-cluster" + resourceName := "aws_emr_cluster.test" rName := acctest.RandomWithPrefix("tf-acc-test") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -1171,7 +1171,7 @@ func TestAccAWSEMRCluster_visibleToAllUsers(t *testing.T) { func TestAccAWSEMRCluster_s3Logging(t *testing.T) { var cluster emr.Cluster - resourceName := "aws_emr_cluster.tf-test-cluster" + resourceName := "aws_emr_cluster.test" rName := acctest.RandomWithPrefix("tf-acc-test") bucketName := fmt.Sprintf("s3n://%s/", rName) resource.ParallelTest(t, resource.TestCase{ @@ -1204,7 +1204,7 @@ func TestAccAWSEMRCluster_s3Logging(t *testing.T) { func TestAccAWSEMRCluster_s3LogEncryption(t *testing.T) { var cluster emr.Cluster - resourceName := "aws_emr_cluster.tf-test-cluster" + resourceName := "aws_emr_cluster.test" rName := acctest.RandomWithPrefix("tf-acc-test") bucketName := fmt.Sprintf("s3n://%s/", rName) resource.ParallelTest(t, resource.TestCase{ @@ -1238,7 +1238,7 @@ func TestAccAWSEMRCluster_s3LogEncryption(t *testing.T) { func TestAccAWSEMRCluster_tags(t *testing.T) { var cluster emr.Cluster - resourceName := "aws_emr_cluster.tf-test-cluster" + resourceName := "aws_emr_cluster.test" rName := acctest.RandomWithPrefix("tf-acc-test") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -1283,7 +1283,7 @@ func TestAccAWSEMRCluster_tags(t *testing.T) { func TestAccAWSEMRCluster_root_volume_size(t *testing.T) { var cluster emr.Cluster - resourceName := "aws_emr_cluster.tf-test-cluster" + resourceName := "aws_emr_cluster.test" rName := acctest.RandomWithPrefix("tf-acc-test") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -1322,7 +1322,7 @@ func TestAccAWSEMRCluster_root_volume_size(t *testing.T) { func TestAccAWSEMRCluster_step_concurrency_level(t *testing.T) { var cluster emr.Cluster rName := acctest.RandomWithPrefix("tf-acc-test") - resourceName := "aws_emr_cluster.tf-test-cluster" + resourceName := "aws_emr_cluster.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, ErrorCheck: testAccErrorCheck(t, emr.EndpointsID), @@ -1360,7 +1360,7 @@ func TestAccAWSEMRCluster_step_concurrency_level(t *testing.T) { func TestAccAWSEMRCluster_ebs_config(t *testing.T) { var cluster emr.Cluster rName := acctest.RandomWithPrefix("tf-acc-test") - resourceName := "aws_emr_cluster.tf-test-cluster" + resourceName := "aws_emr_cluster.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, ErrorCheck: testAccErrorCheck(t, emr.EndpointsID), @@ -1392,7 +1392,7 @@ func TestAccAWSEMRCluster_ebs_config(t *testing.T) { func TestAccAWSEMRCluster_custom_ami_id(t *testing.T) { var cluster emr.Cluster - resourceName := "aws_emr_cluster.tf-test-cluster" + resourceName := "aws_emr_cluster.test" rName := acctest.RandomWithPrefix("tf-acc-test") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -1424,7 +1424,7 @@ func TestAccAWSEMRCluster_custom_ami_id(t *testing.T) { func TestAccAWSEMRCluster_InstanceFleet_basic(t *testing.T) { var cluster1, cluster2 emr.Cluster - resourceName := "aws_emr_cluster.tf-test-cluster" + resourceName := "aws_emr_cluster.test" subnetResourceName := "aws_subnet.test" subnet2ResourceName := "aws_subnet.test2" rName := acctest.RandomWithPrefix("tf-acc-test") @@ -1491,7 +1491,7 @@ func TestAccAWSEMRCluster_InstanceFleet_basic(t *testing.T) { func TestAccAWSEMRCluster_InstanceFleet_master_only(t *testing.T) { var cluster emr.Cluster - resourceName := "aws_emr_cluster.tf-test-cluster" + resourceName := "aws_emr_cluster.test" rName := acctest.RandomWithPrefix("tf-acc-test") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -1731,435 +1731,250 @@ func testAccEmrDeleteManagedSecurityGroup(conn *ec2.EC2, securityGroup *ec2.Secu return err } -func testAccAWSEmrComposeConfig(mapPublicIPOnLaunch bool, config ...string) string { - return composeConfig(append(config, testAccAWSEmrClusterConfigBaseVpc(mapPublicIPOnLaunch))...) -} - -func testAccAWSEmrClusterConfigCurrentPartition() string { - return ` -data "aws_partition" "current" {} -` -} - -func testAccAWSEmrClusterConfig_bootstrap(r string) string { - return testAccAWSEmrComposeConfig(false, - testAccAWSEmrClusterConfigCurrentPartition(), - testAccAWSEmrClusterConfigIAMServiceRoleBase(r), - testAccAWSEmrClusterConfigIAMInstanceProfileBase(r), - testAccAWSEmrClusterConfigBootstrapActionBucket(r), - fmt.Sprintf(` -resource "aws_emr_cluster" "test" { - name = "%[1]s" - release_label = "emr-5.0.0" - applications = ["Hadoop", "Hive"] - log_uri = "s3n://terraform/testlog/" - - master_instance_group { - instance_type = "c4.large" - } - - core_instance_group { - instance_count = 1 - instance_type = "c4.large" - } - - service_role = aws_iam_role.emr_service.arn - depends_on = [ - aws_route_table_association.test, - aws_iam_role_policy_attachment.emr_service, - aws_iam_role_policy_attachment.emr_instance_profile, - ] - - ec2_attributes { - subnet_id = aws_subnet.test.id - emr_managed_master_security_group = aws_security_group.test.id - emr_managed_slave_security_group = aws_security_group.test.id - instance_profile = aws_iam_instance_profile.emr_instance_profile.arn - } +// Sub-configs (used by other configs) - bootstrap_action { - path = "s3://elasticmapreduce/bootstrap-actions/run-if" - name = "runif" - args = ["instance.isMaster=true", "echo running on master node"] +func testAccAWSEmrClusterConfigBaseVpc(rName string, mapPublicIPOnLaunch bool) string { + return fmt.Sprintf(` +data "aws_availability_zones" "available" { + # Many instance types are not available in this availability zone + exclude_zone_ids = ["usw2-az4"] + state = "available" + filter { + name = "opt-in-status" + values = ["opt-in-not-required"] } +} - bootstrap_action { - path = "s3://${aws_s3_bucket_object.testobject.bucket}/${aws_s3_bucket_object.testobject.key}" - name = "test" +resource "aws_vpc" "test" { + cidr_block = "10.0.0.0/16" + enable_dns_hostnames = true - args = ["1", - "2", - "3", - "4", - "5", - "6", - "7", - "8", - "9", - "10", - ] + tags = { + Name = %[1]q } } -`, r), - ) -} -func testAccAWSEmrClusterConfig_bootstrapAdd(r string) string { - return testAccAWSEmrComposeConfig(false, - testAccAWSEmrClusterConfigCurrentPartition(), - testAccAWSEmrClusterConfigIAMServiceRoleBase(r), - testAccAWSEmrClusterConfigIAMInstanceProfileBase(r), - testAccAWSEmrClusterConfigBootstrapActionBucket(r), - fmt.Sprintf(` -resource "aws_emr_cluster" "test" { - name = "%[1]s" - release_label = "emr-5.0.0" - applications = ["Hadoop", "Hive"] - log_uri = "s3n://terraform/testlog/" - - master_instance_group { - instance_type = "c4.large" - } +resource "aws_internet_gateway" "test" { + vpc_id = aws_vpc.test.id - core_instance_group { - instance_count = 1 - instance_type = "c4.large" + tags = { + Name = %[1]q } +} - service_role = aws_iam_role.emr_service.arn - depends_on = [ - aws_route_table_association.test, - aws_iam_role_policy_attachment.emr_service, - aws_iam_role_policy_attachment.emr_instance_profile, - ] +resource "aws_security_group" "test" { + vpc_id = aws_vpc.test.id - ec2_attributes { - subnet_id = aws_subnet.test.id - emr_managed_master_security_group = aws_security_group.test.id - emr_managed_slave_security_group = aws_security_group.test.id - instance_profile = aws_iam_instance_profile.emr_instance_profile.arn + ingress { + from_port = 0 + protocol = "-1" + self = true + to_port = 0 } - bootstrap_action { - path = "s3://elasticmapreduce/bootstrap-actions/run-if" - name = "runif" - args = ["instance.isMaster=true", "echo running on master node"] + egress { + cidr_blocks = ["0.0.0.0/0"] + from_port = 0 + protocol = "-1" + to_port = 0 } - bootstrap_action { - path = "s3://${aws_s3_bucket_object.testobject.bucket}/${aws_s3_bucket_object.testobject.key}" - name = "test" - - args = ["1", - "2", - "3", - "4", - "5", - "6", - "7", - "8", - "9", - "10", - ] + tags = { + Name = %[1]q } - bootstrap_action { - path = "s3://elasticmapreduce/bootstrap-actions/run-if" - name = "runif-2" - args = ["instance.isMaster=true", "echo also running on master node"] + # EMR will modify ingress rules + lifecycle { + ignore_changes = [ingress] } } -`, r), - ) -} - -func testAccAWSEmrClusterConfig_bootstrapReorder(r string) string { - return testAccAWSEmrComposeConfig(false, - testAccAWSEmrClusterConfigCurrentPartition(), - testAccAWSEmrClusterConfigIAMServiceRoleBase(r), - testAccAWSEmrClusterConfigIAMInstanceProfileBase(r), - testAccAWSEmrClusterConfigBootstrapActionBucket(r), - fmt.Sprintf(` -resource "aws_emr_cluster" "test" { - name = "%[1]s" - release_label = "emr-5.0.0" - applications = ["Hadoop", "Hive"] - log_uri = "s3n://terraform/testlog/" - master_instance_group { - instance_type = "c4.large" - } +resource "aws_subnet" "test" { + availability_zone = data.aws_availability_zones.available.names[0] + cidr_block = cidrsubnet(aws_vpc.test.cidr_block, 8, 0) + map_public_ip_on_launch = %[2]t + vpc_id = aws_vpc.test.id - core_instance_group { - instance_count = 1 - instance_type = "c4.large" + tags = { + Name = %[1]q } +} - service_role = aws_iam_role.emr_service.arn - depends_on = [ - aws_route_table_association.test, - aws_iam_role_policy_attachment.emr_service, - aws_iam_role_policy_attachment.emr_instance_profile, - ] +resource "aws_route_table" "test" { + vpc_id = aws_vpc.test.id - ec2_attributes { - subnet_id = aws_subnet.test.id - emr_managed_master_security_group = aws_security_group.test.id - emr_managed_slave_security_group = aws_security_group.test.id - instance_profile = aws_iam_instance_profile.emr_instance_profile.arn + route { + cidr_block = "0.0.0.0/0" + gateway_id = aws_internet_gateway.test.id } +} - bootstrap_action { - path = "s3://elasticmapreduce/bootstrap-actions/run-if" - name = "runif" - args = ["instance.isMaster=true", "echo running on master node"] - } +resource "aws_route_table_association" "test" { + route_table_id = aws_route_table.test.id + subnet_id = aws_subnet.test.id +} +`, rName, mapPublicIPOnLaunch) +} - bootstrap_action { - path = "s3://elasticmapreduce/bootstrap-actions/run-if" - name = "runif-2" - args = ["instance.isMaster=true", "echo also running on master node"] - } +func testAccAWSEmrClusterConfigIAMInstanceProfileBase(rName string) string { + return fmt.Sprintf(` +resource "aws_iam_instance_profile" "emr_instance_profile" { + name = "%[1]s_profile" + role = aws_iam_role.emr_instance_profile.name +} - bootstrap_action { - path = "s3://${aws_s3_bucket_object.testobject.bucket}/${aws_s3_bucket_object.testobject.key}" - name = "test" +resource "aws_iam_role" "emr_instance_profile" { + name = "%[1]s_profile_role" - args = ["1", - "2", - "3", - "4", - "5", - "6", - "7", - "8", - "9", - "10", - ] - } + assume_role_policy = <