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

tests/resource/aws_default_security_group: Remove hardcoded us-east-1 handling #16026

Merged
merged 2 commits into from
Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions aws/ec2_classic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"sync"
"testing"

"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
Expand Down Expand Up @@ -75,3 +77,17 @@ func testAccGetEc2ClassicRegion() string {

return testAccGetRegion()
}

// testAccCheckResourceAttrRegionalARNEc2Classic ensures the Terraform state exactly matches a formatted ARN with EC2-Classic region
func testAccCheckResourceAttrRegionalARNEc2Classic(resourceName, attributeName, arnService, arnResource string) resource.TestCheckFunc {
return func(s *terraform.State) error {
attributeValue := arn.ARN{
AccountID: testAccGetAccountID(),
Partition: testAccGetPartition(),
Region: testAccGetEc2ClassicRegion(),
Resource: arnResource,
Service: arnService,
}.String()
return resource.TestCheckResourceAttr(resourceName, attributeName, attributeValue)(s)
}
}
95 changes: 66 additions & 29 deletions aws/resource_aws_default_security_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package aws

import (
"fmt"
"os"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -95,23 +94,18 @@ func TestAccAWSDefaultSecurityGroup_Vpc_empty(t *testing.T) {
}

func TestAccAWSDefaultSecurityGroup_Classic_basic(t *testing.T) {
oldvar := os.Getenv("AWS_DEFAULT_REGION")
os.Setenv("AWS_DEFAULT_REGION", "us-east-1")
defer os.Setenv("AWS_DEFAULT_REGION", oldvar)

var group ec2.SecurityGroup
resourceName := "aws_default_security_group.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccEC2ClassicPreCheck(t) },
IDRefreshName: resourceName,
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSDefaultSecurityGroupDestroy,
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccEC2ClassicPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckAWSDefaultSecurityGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSDefaultSecurityGroupConfig_Classic,
Config: testAccAWSDefaultSecurityGroupConfig_Classic(),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDefaultSecurityGroupExists(resourceName, &group),
testAccCheckAWSDefaultSecurityGroupEc2ClassicExists(resourceName, &group),
resource.TestCheckResourceAttr(resourceName, "name", "default"),
resource.TestCheckResourceAttr(resourceName, "description", "default group"),
resource.TestCheckResourceAttr(resourceName, "vpc_id", ""),
Expand All @@ -124,17 +118,18 @@ func TestAccAWSDefaultSecurityGroup_Classic_basic(t *testing.T) {
"cidr_blocks.0": "10.0.0.0/8",
}),
resource.TestCheckResourceAttr(resourceName, "egress.#", "0"),
testAccCheckAWSDefaultSecurityGroupARN(resourceName, &group),
testAccCheckAWSDefaultSecurityGroupARNEc2Classic(resourceName, &group),
testAccCheckResourceAttrAccountID(resourceName, "owner_id"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.Name", "tf-acc-test"),
),
},
{
Config: testAccAWSDefaultSecurityGroupConfig_Classic,
Config: testAccAWSDefaultSecurityGroupConfig_Classic(),
PlanOnly: true,
},
{
Config: testAccAWSDefaultSecurityGroupConfig_Classic(),
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
Expand All @@ -150,23 +145,18 @@ func TestAccAWSDefaultSecurityGroup_Classic_empty(t *testing.T) {
// Additional references:
// * https://github.com/hashicorp/terraform-provider-aws/issues/14631

oldvar := os.Getenv("AWS_DEFAULT_REGION")
os.Setenv("AWS_DEFAULT_REGION", "us-east-1")
defer os.Setenv("AWS_DEFAULT_REGION", oldvar)

var group ec2.SecurityGroup
resourceName := "aws_default_security_group.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccEC2ClassicPreCheck(t) },
IDRefreshName: resourceName,
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSDefaultSecurityGroupDestroy,
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccEC2ClassicPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckAWSDefaultSecurityGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSDefaultSecurityGroupConfig_Classic_empty,
Config: testAccAWSDefaultSecurityGroupConfig_Classic_empty(),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDefaultSecurityGroupExists(resourceName, &group),
testAccCheckAWSDefaultSecurityGroupEc2ClassicExists(resourceName, &group),
resource.TestCheckResourceAttr(resourceName, "ingress.#", "0"),
resource.TestCheckResourceAttr(resourceName, "egress.#", "0"),
),
Expand Down Expand Up @@ -209,12 +199,51 @@ func testAccCheckAWSDefaultSecurityGroupExists(n string, group *ec2.SecurityGrou
}
}

func testAccCheckAWSDefaultSecurityGroupEc2ClassicExists(n string, group *ec2.SecurityGroup) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No EC2 Default Security Group ID is set")
}

conn := testAccProviderEc2Classic.Meta().(*AWSClient).ec2conn

input := &ec2.DescribeSecurityGroupsInput{
GroupIds: []*string{aws.String(rs.Primary.ID)},
}

resp, err := conn.DescribeSecurityGroups(input)

if err != nil {
return fmt.Errorf("error describing EC2 Default Security Group (%s): %w", rs.Primary.ID, err)
}

if len(resp.SecurityGroups) == 0 || aws.StringValue(resp.SecurityGroups[0].GroupId) != rs.Primary.ID {
return fmt.Errorf("EC2 Default Security Group (%s) not found", rs.Primary.ID)
}

*group = *resp.SecurityGroups[0]

return nil
}
}

func testAccCheckAWSDefaultSecurityGroupARN(resourceName string, group *ec2.SecurityGroup) resource.TestCheckFunc {
return func(s *terraform.State) error {
return testAccCheckResourceAttrRegionalARN(resourceName, "arn", "ec2", fmt.Sprintf("security-group/%s", aws.StringValue(group.GroupId)))(s)
}
}

func testAccCheckAWSDefaultSecurityGroupARNEc2Classic(resourceName string, group *ec2.SecurityGroup) resource.TestCheckFunc {
return func(s *terraform.State) error {
return testAccCheckResourceAttrRegionalARNEc2Classic(resourceName, "arn", "ec2", fmt.Sprintf("security-group/%s", aws.StringValue(group.GroupId)))(s)
}
}

const testAccAWSDefaultSecurityGroupConfig_Vpc = `
resource "aws_vpc" "test" {
cidr_block = "10.1.0.0/16"
Expand Down Expand Up @@ -261,7 +290,10 @@ resource "aws_default_security_group" "test" {
}
`

const testAccAWSDefaultSecurityGroupConfig_Classic = `
func testAccAWSDefaultSecurityGroupConfig_Classic() string {
return composeConfig(
testAccEc2ClassicRegionProviderConfig(),
`
resource "aws_default_security_group" "test" {
ingress {
protocol = "6"
Expand All @@ -274,13 +306,18 @@ resource "aws_default_security_group" "test" {
Name = "tf-acc-test"
}
}
`
`)
}

const testAccAWSDefaultSecurityGroupConfig_Classic_empty = `
func testAccAWSDefaultSecurityGroupConfig_Classic_empty() string {
return composeConfig(
testAccEc2ClassicRegionProviderConfig(),
`
resource "aws_default_security_group" "test" {
# No attributes set.
}
`
`)
}

func TestAWSDefaultSecurityGroupMigrateState(t *testing.T) {
cases := map[string]struct {
Expand Down