From 174706985db1d369b72fe9d69e78b1498a71a517 Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Wed, 4 Nov 2020 19:37:56 -0500 Subject: [PATCH] tests/resource/aws_instance: Remove hardcoded us-east-1 handling Reference: https://github.com/hashicorp/terraform-provider-aws/issues/8316 Reference: https://github.com/hashicorp/terraform-provider-aws/issues/15737 Reference: https://github.com/hashicorp/terraform-provider-aws/issues/15791 Previously in AWS GovCloud (US): ``` === RUN TestAccAWSInstance_inEc2Classic TestAccAWSInstance_inEc2Classic: provider_test.go:184: [{0 error configuring Terraform AWS Provider: error validating provider credentials: error calling sts:GetCallerIdentity: InvalidClientTokenId: The security token included in the request is invalid. status code: 403, request id: 23aa824d-1186-4ea7-803b-4d857190e77c []}] --- FAIL: TestAccAWSInstance_inEc2Classic (0.71s) ``` Output from acceptance testing in AWS Commercial: ``` --- PASS: TestAccAWSInstance_inEc2Classic (96.92s) ``` Output from acceptance testing in AWS GovCloud (US): ``` --- SKIP: TestAccAWSInstance_inEc2Classic (2.79s) ``` --- aws/resource_aws_instance_test.go | 51 ++++++++++++------------------- 1 file changed, 20 insertions(+), 31 deletions(-) diff --git a/aws/resource_aws_instance_test.go b/aws/resource_aws_instance_test.go index 832bdd39988..5856d76565e 100644 --- a/aws/resource_aws_instance_test.go +++ b/aws/resource_aws_instance_test.go @@ -3,7 +3,6 @@ package aws import ( "fmt" "log" - "os" "reflect" "regexp" "strconv" @@ -204,27 +203,21 @@ func TestAccAWSInstance_inDefaultVpcBySgId(t *testing.T) { func TestAccAWSInstance_inEc2Classic(t *testing.T) { resourceName := "aws_instance.test" - rInt := acctest.RandInt() var v ec2.Instance - // EC2 Classic enabled - oldvar := os.Getenv("AWS_DEFAULT_REGION") - os.Setenv("AWS_DEFAULT_REGION", "us-east-1") - defer os.Setenv("AWS_DEFAULT_REGION", oldvar) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t); testAccEC2ClassicPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckInstanceDestroy, + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t); testAccEC2ClassicPreCheck(t) }, + ProviderFactories: testAccProviderFactories, + CheckDestroy: testAccCheckInstanceDestroy, Steps: []resource.TestStep{ { - Config: testAccInstanceConfigInEc2Classic(rInt), + Config: testAccInstanceConfigInEc2Classic(), Check: resource.ComposeTestCheckFunc( - testAccCheckInstanceExists(resourceName, &v), + testAccCheckInstanceEc2ClassicExists(resourceName, &v), ), }, { - Config: testAccInstanceConfigInEc2Classic(rInt), + Config: testAccInstanceConfigInEc2Classic(), ResourceName: resourceName, ImportState: true, ImportStateVerify: true, @@ -3172,6 +3165,10 @@ func testAccCheckInstanceExists(n string, i *ec2.Instance) resource.TestCheckFun return testAccCheckInstanceExistsWithProvider(n, i, func() *schema.Provider { return testAccProvider }) } +func testAccCheckInstanceEc2ClassicExists(n string, i *ec2.Instance) resource.TestCheckFunc { + return testAccCheckInstanceExistsWithProvider(n, i, func() *schema.Provider { return testAccProviderEc2Classic }) +} + func testAccCheckInstanceExistsWithProvider(n string, i *ec2.Instance, providerF func() *schema.Provider) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -3357,25 +3354,17 @@ resource "aws_instance" "test" { `, rName) } -func testAccInstanceConfigInEc2Classic(rInt int) string { - return composeConfig(testAccLatestAmazonLinuxHvmEbsAmiConfig(), fmt.Sprintf(` -provider "aws" { - region = "us-east-1" -} - -resource "aws_security_group" "sg" { - name = "tf_acc_test_%d" - description = "Test security group" -} - +func testAccInstanceConfigInEc2Classic() string { + return composeConfig( + testAccEc2ClassicRegionProviderConfig(), + testAccLatestAmazonLinuxHvmEbsAmiConfig(), + testAccAvailableEc2InstanceTypeForRegion("t1.micro", "m3.medium", "m3.large", "c3.large", "r3.large"), + ` resource "aws_instance" "test" { - ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id - - # tflint-ignore: aws_instance_previous_type - instance_type = "m3.medium" - security_groups = [aws_security_group.sg.name] + ami = data.aws_ami.amzn-ami-minimal-hvm-ebs.id + instance_type = data.aws_ec2_instance_type_offering.available.instance_type } -`, rInt)) +`) } func testAccInstanceConfigBasic() string {