Skip to content

Commit

Permalink
tests/resource/aws_instance: Remove hardcoded us-east-1 handling (#16035
Browse files Browse the repository at this point in the history
)

Reference: #8316
Reference: #15737
Reference: #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)
```
  • Loading branch information
bflad authored Nov 11, 2020
1 parent e3337dd commit 394fa67
Showing 1 changed file with 20 additions and 31 deletions.
51 changes: 20 additions & 31 deletions aws/resource_aws_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package aws
import (
"fmt"
"log"
"os"
"reflect"
"regexp"
"strconv"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 394fa67

Please sign in to comment.