Skip to content

Commit

Permalink
tests/resource/aws_db_cluster_snapshot: Add sweeper (hashicorp#13301)
Browse files Browse the repository at this point in the history
Output from sweeper in AWS Commercial:

```
2020/05/19 23:50:59 [DEBUG] Running Sweepers for region (us-west-2):
2020/05/19 23:50:59 [DEBUG] Running Sweeper (aws_db_cluster_snapshot) in region (us-west-2)
...
2020/05/19 23:51:02 [INFO] Deleting RDS DB Cluster Snapshot: tf-acc-6284739189931617431
2020/05/19 23:51:02 [INFO] Deleting RDS DB Cluster Snapshot: tf-acc-7694211504628872151
2020/05/19 23:51:03 [INFO] Deleting RDS DB Cluster Snapshot: tf-acc-7712270125278830714
2020/05/19 23:51:03 [INFO] Deleting RDS DB Cluster Snapshot: tf-acc-test-1034835137897245353
2020/05/19 23:51:04 [INFO] Deleting RDS DB Cluster Snapshot: tf-acc-test-1034835137897245353-incorrect
2020/05/19 23:51:04 [INFO] Deleting RDS DB Cluster Snapshot: tf-acc-test-1177744522386226699
2020/05/19 23:51:05 [INFO] Deleting RDS DB Cluster Snapshot: tf-acc-test-1894715743644967502
2020/05/19 23:51:05 [INFO] Deleting RDS DB Cluster Snapshot: tf-acc-test-1894715743644967502-incorrect
2020/05/19 23:51:05 [INFO] Deleting RDS DB Cluster Snapshot: tf-acc-test-2403831695778869292
2020/05/19 23:51:06 [INFO] Deleting RDS DB Cluster Snapshot: tf-acc-test-3154394584652669178
2020/05/19 23:51:06 [INFO] Deleting RDS DB Cluster Snapshot: tf-acc-test-3672590285800438157
2020/05/19 23:51:07 [INFO] Deleting RDS DB Cluster Snapshot: tf-acc-test-3672590285800438157-incorrect
2020/05/19 23:51:07 [INFO] Deleting RDS DB Cluster Snapshot: tf-acc-test-6818628255720508177
2020/05/19 23:51:08 [INFO] Deleting RDS DB Cluster Snapshot: tf-acc-test-7220579075951202508
2020/05/19 23:51:08 [INFO] Deleting RDS DB Cluster Snapshot: tf-acc-test-8478413316652622710
2020/05/19 23:51:09 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-docdbcluster-snapshot-208776221194075115
2020/05/19 23:51:09 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-docdbcluster-snapshot-2818346412137517497
2020/05/19 23:51:10 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-docdbcluster-snapshot-3016656015395871858
2020/05/19 23:51:10 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-docdbcluster-snapshot-4538854897230104146
2020/05/19 23:51:10 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-docdbcluster-snapshot-5252971764716972497
2020/05/19 23:51:11 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-docdbcluster-snapshot-53349618087131557
2020/05/19 23:51:11 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-docdbcluster-snapshot-6236748090645919885
2020/05/19 23:51:12 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-docdbcluster-snapshot-6690196973386539453
2020/05/19 23:51:12 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-docdbcluster-snapshot-6793880297909833423
2020/05/19 23:51:13 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-docdbcluster-snapshot-6844670658230160769
2020/05/19 23:51:13 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-docdbcluster-snapshot-7056488780188601715
2020/05/19 23:51:13 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-docdbcluster-snapshot-7796064647165572057
2020/05/19 23:51:14 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-docdbcluster-snapshot-8786492239320672601
2020/05/19 23:51:14 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-docdbcluster-snapshot-8821321520235073139
2020/05/19 23:51:15 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-docdbcluster-snapshot-8957994553806249343
2020/05/19 23:51:15 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-neptunecluster-snapshot-2298044058867838599
2020/05/19 23:51:16 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-neptunecluster-snapshot-2890564058937333346
2020/05/19 23:51:16 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-neptunecluster-snapshot-408755487276086665
2020/05/19 23:51:17 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-neptunecluster-snapshot-4336233379030840626
2020/05/19 23:51:17 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-neptunecluster-snapshot-4631399137659371329
2020/05/19 23:51:17 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-neptunecluster-snapshot-6777918919246115777
2020/05/19 23:51:18 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-neptunecluster-snapshot-751384545688565201
2020/05/19 23:51:18 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-neptunecluster-snapshot-8660214694146490007
2020/05/19 23:51:19 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-rdscluster-snapshot-3625374210620980505
2020/05/19 23:51:19 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-rdscluster-snapshot-4022657527142458865
2020/05/19 23:51:20 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-rdscluster-snapshot-6649937651949721828
2020/05/19 23:51:20 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-rdscluster-snapshot-7344132261107189471
2020/05/19 23:51:20 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-rdscluster-snapshot-7579832565865974620
2020/05/19 23:51:21 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-rdscluster-snapshot-7880011180435690535
2020/05/19 23:51:21 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-rdscluster-snapshot-8092261109895566052
2020/05/19 23:51:22 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-rdscluster-snapshot-8298908575388334331
2020/05/19 23:51:22 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-rdscluster-snapshot-8992617190947537577
2020/05/19 23:51:23 [INFO] Deleting RDS DB Cluster Snapshot: tf-acctest-rdscluster-snapshot-9055254231029142908
2020/05/19 23:51:23 Sweeper Tests ran successfully:
	- aws_db_cluster_snapshot
2020/05/19 23:51:23 [DEBUG] Running Sweepers for region (us-east-1):
2020/05/19 23:51:23 [DEBUG] Running Sweeper (aws_db_cluster_snapshot) in region (us-east-1)
...
2020/05/19 23:51:25 [INFO] Deleting RDS DB Cluster Snapshot: tf-acc-test-5692518413222267692
2020/05/19 23:51:25 [INFO] Deleting RDS DB Cluster Snapshot: tf-acc-test-8759945401750079573
2020/05/19 23:51:25 Sweeper Tests ran successfully:
	- aws_db_cluster_snapshot
ok  	github.com/terraform-providers/terraform-provider-aws/aws	28.425s
```

Output from sweeper in AWS GovCloud (US):

```
2020/05/19 23:51:37 [DEBUG] Running Sweepers for region (us-gov-west-1):
2020/05/19 23:51:37 [DEBUG] Running Sweeper (aws_db_cluster_snapshot) in region (us-gov-west-1)
...
2020/05/19 23:51:39 Sweeper Tests ran successfully:
	- aws_db_cluster_snapshot
ok  	github.com/terraform-providers/terraform-provider-aws/aws	3.493s
```
  • Loading branch information
ewbankkit authored and adamdecaf committed May 28, 2020
1 parent 9769cc4 commit e0adbbd
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions aws/resource_aws_db_cluster_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,78 @@ package aws

import (
"fmt"
"log"
"regexp"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/rds"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func init() {
resource.AddTestSweepers("aws_db_cluster_snapshot", &resource.Sweeper{
Name: "aws_db_cluster_snapshot",
F: testSweepDbClusterSnapshots,
})
}

func testSweepDbClusterSnapshots(region string) error {
client, err := sharedClientForRegion(region)
if err != nil {
return fmt.Errorf("error getting client: %w", err)
}
conn := client.(*AWSClient).rdsconn
input := &rds.DescribeDBClusterSnapshotsInput{
// "InvalidDBClusterSnapshotStateFault: Only manual snapshots may be deleted."
Filters: []*rds.Filter{{
Name: aws.String("snapshot-type"),
Values: aws.StringSlice([]string{"manual"}),
}},
}
var sweeperErrs *multierror.Error

for {
output, err := conn.DescribeDBClusterSnapshots(input)
if testSweepSkipSweepError(err) {
log.Printf("[WARN] Skipping RDS DB Cluster Snapshots sweep for %s: %s", region, err)
return sweeperErrs.ErrorOrNil() // In case we have completed some pages, but had errors
}
if err != nil {
sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error retrieving RDS DB Cluster Snapshots: %w", err))
return sweeperErrs
}

for _, dbClusterSnapshot := range output.DBClusterSnapshots {
id := aws.StringValue(dbClusterSnapshot.DBClusterSnapshotIdentifier)

log.Printf("[INFO] Deleting RDS DB Cluster Snapshot: %s", id)
_, err := conn.DeleteDBClusterSnapshot(&rds.DeleteDBClusterSnapshotInput{
DBClusterSnapshotIdentifier: aws.String(id),
})
if isAWSErr(err, rds.ErrCodeDBClusterSnapshotNotFoundFault, "") {
continue
}
if err != nil {
sweeperErr := fmt.Errorf("error deleting RDS DB Cluster Snapshot (%s): %w", id, err)
log.Printf("[ERROR] %s", sweeperErr)
sweeperErrs = multierror.Append(sweeperErrs, sweeperErr)
continue
}
}

if aws.StringValue(output.Marker) == "" {
break
}
input.Marker = output.Marker
}

return sweeperErrs.ErrorOrNil()
}

func TestAccAWSDBClusterSnapshot_basic(t *testing.T) {
var dbClusterSnapshot rds.DBClusterSnapshot
rName := acctest.RandomWithPrefix("tf-acc-test")
Expand Down

0 comments on commit e0adbbd

Please sign in to comment.