Skip to content

Commit

Permalink
tests/resource/aws_securityhub_member: Handle BadRequestException in …
Browse files Browse the repository at this point in the history
…CheckDestroy (#16408)

Reference: https://docs.aws.amazon.com/securityhub/1.0/APIReference/API_ListMembers.html

Previously the test would pass but the CheckDestroy would fail:

```
=== RUN   TestAccAWSSecurityHub_serial/Member/invite
TestAccAWSSecurityHub_serial/Member/invite: testing_new.go:63: Error running post-test destroy, there may be dangling resources: BadRequestException:
  status code: 400, request id: 34dfc5cd-16bb-4e31-9924-6b060f6f30b7
=== RUN   TestAccAWSSecurityHub_serial/Member/basic
TestAccAWSSecurityHub_serial/Member/basic: testing_new.go:63: Error running post-test destroy, there may be dangling resources: BadRequestException:
  status code: 400, request id: 5049c5b7-be35-4fd2-93d3-4b58e13c7140
```

This error code is not listed in the API Reference, but the same exact call is used in the CheckExists function, so seems related to only when SecurityHub has been disabled after the test has been completed.

Output from acceptance testing:

```
--- PASS: TestAccAWSSecurityHub_serial (28.16s)
    --- PASS: TestAccAWSSecurityHub_serial/Member (28.16s)
        --- PASS: TestAccAWSSecurityHub_serial/Member/invite (14.81s)
        --- PASS: TestAccAWSSecurityHub_serial/Member/basic (13.35s)
```
  • Loading branch information
bflad authored Nov 24, 2020
1 parent 65b9bd2 commit 48d31d8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
5 changes: 5 additions & 0 deletions aws/internal/service/securityhub/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package securityhub

const (
ErrCodeBadRequestException = "BadRequestException"
)
15 changes: 11 additions & 4 deletions aws/resource_aws_securityhub_member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/securityhub"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
tfsecurityhub "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/securityhub"
)

func testAccAWSSecurityHubMember_basic(t *testing.T) {
Expand Down Expand Up @@ -99,11 +101,16 @@ func testAccCheckAWSSecurityHubMemberDestroy(s *terraform.State) error {
AccountIds: []*string{aws.String(rs.Primary.ID)},
})

if tfawserr.ErrCodeEquals(err, tfsecurityhub.ErrCodeBadRequestException) {
continue
}

if tfawserr.ErrCodeEquals(err, securityhub.ErrCodeResourceNotFoundException) {
continue
}

if err != nil {
if isAWSErr(err, securityhub.ErrCodeResourceNotFoundException, "") {
return nil
}
return err
return fmt.Errorf("error getting Security Hub Member (%s): %w", rs.Primary.ID, err)
}

if len(resp.Members) != 0 {
Expand Down

0 comments on commit 48d31d8

Please sign in to comment.