Skip to content

Commit

Permalink
r/aws_dx_connection: Use internal finder package. Add '_disappears' t…
Browse files Browse the repository at this point in the history
…est (hashicorp#13826).

Acceptance test output:

$ make testacc TEST=./aws/ TESTARGS='-run=TestAccAwsDxConnection_'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -count 1 -parallel 20 -run=TestAccAwsDxConnection_ -timeout 120m
=== RUN   TestAccAwsDxConnection_basic
=== PAUSE TestAccAwsDxConnection_basic
=== RUN   TestAccAwsDxConnection_disappears
=== PAUSE TestAccAwsDxConnection_disappears
=== RUN   TestAccAwsDxConnection_Tags
=== PAUSE TestAccAwsDxConnection_Tags
=== CONT  TestAccAwsDxConnection_basic
=== CONT  TestAccAwsDxConnection_Tags
=== CONT  TestAccAwsDxConnection_disappears
--- PASS: TestAccAwsDxConnection_disappears (24.89s)
--- PASS: TestAccAwsDxConnection_basic (29.22s)
--- PASS: TestAccAwsDxConnection_Tags (51.74s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	51.844s
  • Loading branch information
ewbankkit committed Aug 24, 2021
1 parent 73a6252 commit f30a8d7
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 58 deletions.
119 changes: 62 additions & 57 deletions aws/resource_aws_dx_connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
"regexp"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/directconnect"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/directconnect/finder"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource"
)

func init() {
Expand Down Expand Up @@ -81,7 +82,7 @@ func testSweepDxConnections(region string) error {
func TestAccAwsDxConnection_basic(t *testing.T) {
var connection directconnect.Connection
resourceName := "aws_dx_connection.test"
rName := fmt.Sprintf("tf-testacc-dxconn-%s", acctest.RandString(14))
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -90,7 +91,7 @@ func TestAccAwsDxConnection_basic(t *testing.T) {
CheckDestroy: testAccCheckAwsDxConnectionDestroy,
Steps: []resource.TestStep{
{
Config: testAccDxConnectionConfig_basic(rName),
Config: testAccDxConnectionConfigBasic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsDxConnectionExists(resourceName, &connection),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexp.MustCompile(`dxcon/.+`)),
Expand All @@ -110,10 +111,10 @@ func TestAccAwsDxConnection_basic(t *testing.T) {
})
}

func TestAccAwsDxConnection_Tags(t *testing.T) {
func TestAccAwsDxConnection_disappears(t *testing.T) {
var connection directconnect.Connection
resourceName := "aws_dx_connection.test"
rName := fmt.Sprintf("tf-testacc-dxconn-%s", acctest.RandString(14))
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -122,31 +123,34 @@ func TestAccAwsDxConnection_Tags(t *testing.T) {
CheckDestroy: testAccCheckAwsDxConnectionDestroy,
Steps: []resource.TestStep{
{
Config: testAccDxConnectionConfig_tags(rName),
Config: testAccDxConnectionConfigBasic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsDxConnectionExists(resourceName, &connection),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexp.MustCompile(`dxcon/.+`)),
resource.TestCheckResourceAttr(resourceName, "bandwidth", "1Gbps"),
resource.TestCheckResourceAttrSet(resourceName, "location"),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "3"),
resource.TestCheckResourceAttr(resourceName, "tags.Name", rName),
resource.TestCheckResourceAttr(resourceName, "tags.Key1", "Value1"),
resource.TestCheckResourceAttr(resourceName, "tags.Key2", "Value2a"),
testAccCheckResourceDisappears(testAccProvider, resourceAwsDxConnection(), resourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccAwsDxConnection_Tags(t *testing.T) {
var connection directconnect.Connection
resourceName := "aws_dx_connection.test"
rName := acctest.RandomWithPrefix("tf-acc-test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsDxConnectionDestroy,
Steps: []resource.TestStep{
{
Config: testAccDxConnectionConfig_tagsUpdated(rName),
Config: testAccDxConnectionConfigTags1(rName, "key1", "value1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsDxConnectionExists(resourceName, &connection),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "directconnect", regexp.MustCompile(`dxcon/.+`)),
resource.TestCheckResourceAttr(resourceName, "bandwidth", "1Gbps"),
resource.TestCheckResourceAttrSet(resourceName, "location"),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "3"),
resource.TestCheckResourceAttr(resourceName, "tags.Name", rName),
resource.TestCheckResourceAttr(resourceName, "tags.Key2", "Value2b"),
resource.TestCheckResourceAttr(resourceName, "tags.Key3", "Value3"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
),
},
// Test import.
Expand All @@ -155,6 +159,25 @@ func TestAccAwsDxConnection_Tags(t *testing.T) {
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccDxConnectionConfigTags2(rName, "key1", "value1updated", "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsDxConnectionExists(resourceName, &connection),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
{
Config: testAccDxConnectionConfigTags1(rName, "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsDxConnectionExists(resourceName, &connection),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
},
})
}
Expand All @@ -166,25 +189,18 @@ func testAccCheckAwsDxConnectionDestroy(s *terraform.State) error {
if rs.Type != "aws_dx_connection" {
continue
}
if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}

resp, err := conn.DescribeConnections(&directconnect.DescribeConnectionsInput{
ConnectionId: aws.String(rs.Primary.ID),
})
if isAWSErr(err, directconnect.ErrCodeClientException, "does not exist") {
_, err := finder.ConnectionByID(conn, rs.Primary.ID)

if tfresource.NotFound(err) {
continue
}

if err != nil {
return err
}

for _, v := range resp.Connections {
if aws.StringValue(v.ConnectionId) == rs.Primary.ID && aws.StringValue(v.ConnectionState) != directconnect.ConnectionStateDeleted {
return fmt.Errorf("[DESTROY ERROR] Direct Connect connection (%s) not deleted", rs.Primary.ID)
}
}
return fmt.Errorf("Direct Connect connection %s still exists", rs.Primary.ID)
}

return nil
Expand All @@ -198,30 +214,22 @@ func testAccCheckAwsDxConnectionExists(name string, connection *directconnect.Co
if !ok {
return fmt.Errorf("Not found: %s", name)
}

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

resp, err := conn.DescribeConnections(&directconnect.DescribeConnectionsInput{
ConnectionId: aws.String(rs.Primary.ID),
})
_, err := finder.ConnectionByID(conn, rs.Primary.ID)

if err != nil {
return err
}

for _, c := range resp.Connections {
if aws.StringValue(c.ConnectionId) == rs.Primary.ID {
*connection = *c

return nil
}
}

return fmt.Errorf("Direct Connect connection (%s) not found", rs.Primary.ID)
return nil
}
}

func testAccDxConnectionConfig_basic(rName string) string {
func testAccDxConnectionConfigBasic(rName string) string {
return fmt.Sprintf(`
data "aws_dx_locations" "test" {}
Expand All @@ -233,7 +241,7 @@ resource "aws_dx_connection" "test" {
`, rName)
}

func testAccDxConnectionConfig_tags(rName string) string {
func testAccDxConnectionConfigTags1(rName, tagKey1, tagValue1 string) string {
return fmt.Sprintf(`
data "aws_dx_locations" "test" {}
Expand All @@ -243,15 +251,13 @@ resource "aws_dx_connection" "test" {
location = tolist(data.aws_dx_locations.test.location_codes)[0]
tags = {
Name = %[1]q
Key1 = "Value1"
Key2 = "Value2a"
%[2]q = %[3]q
}
}
`, rName)
`, rName, tagKey1, tagValue1)
}

func testAccDxConnectionConfig_tagsUpdated(rName string) string {
func testAccDxConnectionConfigTags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string {
return fmt.Sprintf(`
data "aws_dx_locations" "test" {}
Expand All @@ -261,10 +267,9 @@ resource "aws_dx_connection" "test" {
location = tolist(data.aws_dx_locations.test.location_codes)[0]
tags = {
Name = %[1]q
Key2 = "Value2b"
Key3 = "Value3"
%[2]q = %[3]q
%[4]q = %[5]q
}
}
`, rName)
`, rName, tagKey1, tagValue1, tagKey2, tagValue2)
}
2 changes: 1 addition & 1 deletion aws/resource_aws_dx_lag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func testAccCheckAwsDxLagDestroy(s *terraform.State) error {
return err
}

return fmt.Errorf("DirectConnect LAG %s still exists", rs.Primary.ID)
return fmt.Errorf("Direct Connect LAG %s still exists", rs.Primary.ID)
}

return nil
Expand Down

0 comments on commit f30a8d7

Please sign in to comment.