From f30a8d7207039ed96b52a0870e5de70ed288a23d Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Mon, 1 Mar 2021 16:23:47 -0500 Subject: [PATCH] r/aws_dx_connection: Use internal finder package. Add '_disappears' test (#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 --- aws/resource_aws_dx_connection_test.go | 119 +++++++++++++------------ aws/resource_aws_dx_lag_test.go | 2 +- 2 files changed, 63 insertions(+), 58 deletions(-) diff --git a/aws/resource_aws_dx_connection_test.go b/aws/resource_aws_dx_connection_test.go index cf9f62547a4..0343e536f1f 100644 --- a/aws/resource_aws_dx_connection_test.go +++ b/aws/resource_aws_dx_connection_test.go @@ -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() { @@ -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) }, @@ -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/.+`)), @@ -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) }, @@ -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. @@ -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"), + ), + }, }, }) } @@ -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 @@ -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" {} @@ -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" {} @@ -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" {} @@ -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) } diff --git a/aws/resource_aws_dx_lag_test.go b/aws/resource_aws_dx_lag_test.go index e6e4d0d7f8d..568760aa5a1 100644 --- a/aws/resource_aws_dx_lag_test.go +++ b/aws/resource_aws_dx_lag_test.go @@ -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