From cf7b0ed70f64144c8cddb5fb6f34ce03c959fae7 Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Tue, 25 Aug 2020 14:00:57 -0400 Subject: [PATCH] tests/resource/aws_redshift_subnet_group: Implement sweeper and disappears test (#14828) Reference: https://github.com/terraform-providers/terraform-provider-aws/issues/13826 Reference: https://github.com/terraform-providers/terraform-provider-aws/issues/14574 Output from acceptance testing in AWS Commercial: ``` --- PASS: TestAccAWSRedshiftSubnetGroup_disappears (26.83s) ``` Output from acceptance testing in AWS GovCloud (US): ``` --- PASS: TestAccAWSRedshiftSubnetGroup_disappears (30.85s) ``` Output from sweeper in AWS Commercial (aws_redshift_cluster failures unrelated): ``` 2020/08/25 10:13:32 [DEBUG] Running Sweepers for region (us-west-2): 2020/08/25 10:13:32 [DEBUG] Running Sweeper (aws_redshift_cluster) in region (us-west-2) 2020/08/25 10:13:34 [ERROR] Failed deleting Redshift cluster (tf-redshift-cluster-2045223997271387682): InvalidClusterState: Unable to delete the cluster tf-redshift-cluster-2045223997271387682. You can only delete clusters with ACTIVE, INCOMPATIBLE_NETWORK, INCOMPATIBLE_HSM, INCOMPATIBLE_RESTORE, INSUFFICIENT_CAPACITY, or HARDWARE_FAILURE lifecycle. status code: 400, request id: fda93346-21d9-46ae-b70a-637edb91777d 2020/08/25 10:13:34 [ERROR] Failed deleting Redshift cluster (tf-redshift-cluster-8644196052749068490): InvalidClusterState: Unable to delete the cluster tf-redshift-cluster-8644196052749068490. You can only delete clusters with ACTIVE, INCOMPATIBLE_NETWORK, INCOMPATIBLE_HSM, INCOMPATIBLE_RESTORE, INSUFFICIENT_CAPACITY, or HARDWARE_FAILURE lifecycle. status code: 400, request id: 980f1a92-52d8-41d0-b942-608b8a28d2b0 2020/08/25 10:13:34 [DEBUG] Running Sweeper (aws_redshift_subnet_group) in region (us-west-2) 2020/08/25 10:13:34 [INFO] Deleting Redshift Cluster Subnet Group: foo-1532990571165558666 2020/08/25 10:13:35 [INFO] Deleting Redshift Cluster Subnet Group: foo-2826273713642737832 2020/08/25 10:13:35 [INFO] Deleting Redshift Cluster Subnet Group: foo-2972341912917468064 2020/08/25 10:13:35 [INFO] Deleting Redshift Cluster Subnet Group: foo-548170896135551408 2020/08/25 10:13:36 [INFO] Deleting Redshift Cluster Subnet Group: foo-6210951272445705940 2020/08/25 10:13:36 [INFO] Deleting Redshift Cluster Subnet Group: foo-6650827170013194054 2020/08/25 10:13:37 [INFO] Deleting Redshift Cluster Subnet Group: foo-8947122441846412195 2020/08/25 10:13:37 [INFO] Deleting Redshift Cluster Subnet Group: tf-acc-test-1033431615102227004 2020/08/25 10:13:37 [INFO] Deleting Redshift Cluster Subnet Group: tf-acc-test-3356016725088449244 2020/08/25 10:13:38 [INFO] Deleting Redshift Cluster Subnet Group: tf-acc-test-366062263239024705 2020/08/25 10:13:38 [INFO] Deleting Redshift Cluster Subnet Group: tf-acc-test-4635827958397283374 2020/08/25 10:13:38 [INFO] Deleting Redshift Cluster Subnet Group: tf-redshift-subnet-group-1676492653723899859 2020/08/25 10:13:39 [INFO] Deleting Redshift Cluster Subnet Group: tf-redshift-subnet-group-1799088798996484452 2020/08/25 10:13:39 [INFO] Deleting Redshift Cluster Subnet Group: tf-redshift-subnet-group-3024108242023612112 2020/08/25 10:13:40 [INFO] Deleting Redshift Cluster Subnet Group: tf-redshift-subnet-group-3299406627706074951 2020/08/25 10:13:40 [INFO] Deleting Redshift Cluster Subnet Group: tf-redshift-subnet-group-5403800648237234931 2020/08/25 10:13:40 [INFO] Deleting Redshift Cluster Subnet Group: tf-redshift-subnet-group-625135235143178083 2020/08/25 10:13:41 [INFO] Deleting Redshift Cluster Subnet Group: tf-redshift-subnet-group-8306727858374757773 2020/08/25 10:13:41 [INFO] Deleting Redshift Cluster Subnet Group: tf-redshift-subnet-group-8365684838675699564 2020/08/25 10:13:41 Sweeper Tests ran successfully: - aws_redshift_cluster - aws_redshift_subnet_group 2020/08/25 10:13:41 [DEBUG] Running Sweepers for region (us-east-1): 2020/08/25 10:13:41 [DEBUG] Running Sweeper (aws_redshift_cluster) in region (us-east-1) 2020/08/25 10:13:42 [DEBUG] No Redshift clusters to sweep 2020/08/25 10:13:42 [DEBUG] Running Sweeper (aws_redshift_subnet_group) in region (us-east-1) 2020/08/25 10:13:42 Sweeper Tests ran successfully: - aws_redshift_subnet_group - aws_redshift_cluster ok github.com/terraform-providers/terraform-provider-aws/aws 12.691s ``` Output from sweeper in AWS GovCloud (US) (aws_redshift_cluster failures unrelated): ``` 2020/08/25 10:13:40 [DEBUG] Running Sweepers for region (us-gov-west-1): 2020/08/25 10:13:40 [DEBUG] Running Sweeper (aws_redshift_cluster) in region (us-gov-west-1) 2020/08/25 10:13:43 [ERROR] Failed deleting Redshift cluster (tf-redshift-cluster-1216426925668034673): InvalidClusterState: Unable to delete the cluster tf-redshift-cluster-1216426925668034673. You can only delete clusters with ACTIVE, INCOMPATIBLE_NETWORK, INCOMPATIBLE_HSM, INCOMPATIBLE_RESTORE, INSUFFICIENT_CAPACITY, or HARDWARE_FAILURE lifecycle. status code: 400, request id: 7b5f4c9e-959f-4e80-b01a-65f2cd066f71 2020/08/25 10:13:44 [ERROR] Failed deleting Redshift cluster (tf-redshift-cluster-219584981761242852): InvalidClusterState: There is an operation running on the Cluster. Please try to delete it at a later time. status code: 400, request id: 1c672709-d60b-43b0-aadd-1bdf6a5f4797 2020/08/25 10:13:44 [ERROR] Failed deleting Redshift cluster (tf-redshift-cluster-2558072814228197965): InvalidClusterState: There is an operation running on the Cluster. Please try to delete it at a later time. status code: 400, request id: cc46c761-1a19-4f3e-844d-6c5514eedbf1 2020/08/25 10:13:45 [ERROR] Failed deleting Redshift cluster (tf-redshift-cluster-6093122516016678979): InvalidClusterState: There is an operation running on the Cluster. Please try to delete it at a later time. status code: 400, request id: 24d63cb2-7635-4448-878e-2ccde8e03478 2020/08/25 10:13:45 [ERROR] Failed deleting Redshift cluster (tf-redshift-cluster-6534318059499305980): InvalidClusterState: There is an operation running on the Cluster. Please try to delete it at a later time. status code: 400, request id: 28e83e42-6e20-4366-9c86-25f04d5138ad 2020/08/25 10:13:46 [ERROR] Failed deleting Redshift cluster (tf-redshift-cluster-7172132402108784395): InvalidClusterState: There is an operation running on the Cluster. Please try to delete it at a later time. status code: 400, request id: 1f2cef04-05ef-42dc-bc2c-ce937032db61 2020/08/25 10:13:46 [ERROR] Failed deleting Redshift cluster (tf-redshift-cluster-8068671586261296151): InvalidClusterState: There is an operation running on the Cluster. Please try to delete it at a later time. status code: 400, request id: 7abeafb4-268c-4b4d-abae-91e1bcab4e01 2020/08/25 10:13:46 [DEBUG] Running Sweeper (aws_redshift_subnet_group) in region (us-gov-west-1) 2020/08/25 10:13:47 [INFO] Deleting Redshift Cluster Subnet Group: tf-acc-test-2418503084750398980 2020/08/25 10:13:48 [INFO] Deleting Redshift Cluster Subnet Group: tf-acc-test-2939943196500636311 2020/08/25 10:13:48 [INFO] Deleting Redshift Cluster Subnet Group: tf-acc-test-8506717930441343050 2020/08/25 10:13:49 Sweeper Tests ran successfully: - aws_redshift_cluster - aws_redshift_subnet_group ok github.com/terraform-providers/terraform-provider-aws/aws 10.018s ``` --- ...resource_aws_redshift_subnet_group_test.go | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/aws/resource_aws_redshift_subnet_group_test.go b/aws/resource_aws_redshift_subnet_group_test.go index bfcc0f489bf..39b5d138c16 100644 --- a/aws/resource_aws_redshift_subnet_group_test.go +++ b/aws/resource_aws_redshift_subnet_group_test.go @@ -2,16 +2,82 @@ package aws import ( "fmt" + "log" "testing" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/redshift" + "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" ) +func init() { + resource.AddTestSweepers("aws_redshift_subnet_group", &resource.Sweeper{ + Name: "aws_redshift_subnet_group", + F: testSweepRedshiftSubnetGroups, + Dependencies: []string{ + "aws_redshift_cluster", + }, + }) +} + +func testSweepRedshiftSubnetGroups(region string) error { + client, err := sharedClientForRegion(region) + if err != nil { + return fmt.Errorf("error getting client: %w", err) + } + conn := client.(*AWSClient).redshiftconn + var sweeperErrs *multierror.Error + + input := &redshift.DescribeClusterSubnetGroupsInput{} + + err = conn.DescribeClusterSubnetGroupsPages(input, func(page *redshift.DescribeClusterSubnetGroupsOutput, isLast bool) bool { + if page == nil { + return !isLast + } + + for _, clusterSubnetGroup := range page.ClusterSubnetGroups { + if clusterSubnetGroup == nil { + continue + } + + name := aws.StringValue(clusterSubnetGroup.ClusterSubnetGroupName) + + if name == "default" { + continue + } + + log.Printf("[INFO] Deleting Redshift Cluster Subnet Group: %s", name) + r := resourceAwsRedshiftSubnetGroup() + d := r.Data(nil) + d.SetId(name) + err := r.Delete(d, client) + + if err != nil { + log.Printf("[ERROR] %s", err) + sweeperErrs = multierror.Append(sweeperErrs, err) + continue + } + } + + return !isLast + }) + + if testSweepSkipSweepError(err) { + log.Printf("[WARN] Skipping Redshift Cluster Subnet Group 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 Redshift Cluster Subnet Group: %w", err)) + } + + return sweeperErrs.ErrorOrNil() +} + func TestAccAWSRedshiftSubnetGroup_basic(t *testing.T) { var v redshift.ClusterSubnetGroup rInt := acctest.RandInt() @@ -43,6 +109,28 @@ func TestAccAWSRedshiftSubnetGroup_basic(t *testing.T) { }) } +func TestAccAWSRedshiftSubnetGroup_disappears(t *testing.T) { + var clusterSubnetGroup redshift.ClusterSubnetGroup + rInt := acctest.RandInt() + resourceName := "aws_redshift_subnet_group.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckRedshiftSubnetGroupDestroy, + Steps: []resource.TestStep{ + { + Config: testAccRedshiftSubnetGroupConfig(rInt), + Check: resource.ComposeTestCheckFunc( + testAccCheckRedshiftSubnetGroupExists(resourceName, &clusterSubnetGroup), + testAccCheckResourceDisappears(testAccProvider, resourceAwsRedshiftSubnetGroup(), resourceName), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + func TestAccAWSRedshiftSubnetGroup_updateDescription(t *testing.T) { var v redshift.ClusterSubnetGroup rInt := acctest.RandInt()