mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
provider/aws: Refactor snapshot tests to always delete the snapshot (#12013)
first //cc @radeksimko
This commit is contained in:
parent
c080334c3f
commit
3e9b6f18f0
@ -170,16 +170,17 @@ func TestAccAWSDBInstanceNoSnapshot(t *testing.T) {
|
||||
|
||||
func TestAccAWSDBInstanceSnapshot(t *testing.T) {
|
||||
var snap rds.DBInstance
|
||||
rInt := acctest.RandInt()
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
// testAccCheckAWSDBInstanceSnapshot verifies a database snapshot is
|
||||
// created, and subequently deletes it
|
||||
CheckDestroy: testAccCheckAWSDBInstanceSnapshot,
|
||||
CheckDestroy: testAccCheckAWSDBInstanceSnapshot(rInt),
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccSnapshotInstanceConfigWithSnapshot(),
|
||||
Config: testAccSnapshotInstanceConfigWithSnapshot(rInt),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckAWSDBInstanceExists("aws_db_instance.snapshot", &snap),
|
||||
),
|
||||
@ -434,35 +435,19 @@ func testAccCheckAWSDBInstanceReplicaAttributes(source, replica *rds.DBInstance)
|
||||
}
|
||||
}
|
||||
|
||||
func testAccCheckAWSDBInstanceSnapshot(s *terraform.State) error {
|
||||
conn := testAccProvider.Meta().(*AWSClient).rdsconn
|
||||
|
||||
func testAccCheckAWSDBInstanceSnapshot(rInt int) resource.TestCheckFunc {
|
||||
return func(s *terraform.State) error {
|
||||
for _, rs := range s.RootModule().Resources {
|
||||
if rs.Type != "aws_db_instance" {
|
||||
continue
|
||||
}
|
||||
|
||||
awsClient := testAccProvider.Meta().(*AWSClient)
|
||||
conn := awsClient.rdsconn
|
||||
|
||||
var err error
|
||||
resp, err := conn.DescribeDBInstances(
|
||||
&rds.DescribeDBInstancesInput{
|
||||
DBInstanceIdentifier: aws.String(rs.Primary.ID),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
newerr, _ := err.(awserr.Error)
|
||||
if newerr.Code() != "DBInstanceNotFound" {
|
||||
return err
|
||||
}
|
||||
|
||||
} else {
|
||||
if len(resp.DBInstances) != 0 &&
|
||||
*resp.DBInstances[0].DBInstanceIdentifier == rs.Primary.ID {
|
||||
return fmt.Errorf("DB Instance still exists")
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("[INFO] Trying to locate the DBInstance Final Snapshot")
|
||||
snapshot_identifier := "foobarbaz-test-terraform-final-snapshot-2"
|
||||
snapshot_identifier := fmt.Sprintf("foobarbaz-test-terraform-final-snapshot-%d", rInt)
|
||||
_, snapErr := conn.DescribeDBSnapshots(
|
||||
&rds.DescribeDBSnapshotsInput{
|
||||
DBSnapshotIdentifier: aws.String(snapshot_identifier),
|
||||
@ -512,10 +497,29 @@ func testAccCheckAWSDBInstanceSnapshot(s *terraform.State) error {
|
||||
return err
|
||||
}
|
||||
} // end snapshot was found
|
||||
|
||||
resp, err := conn.DescribeDBInstances(
|
||||
&rds.DescribeDBInstancesInput{
|
||||
DBInstanceIdentifier: aws.String(rs.Primary.ID),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
newerr, _ := err.(awserr.Error)
|
||||
if newerr.Code() != "DBInstanceNotFound" {
|
||||
return err
|
||||
}
|
||||
|
||||
} else {
|
||||
if len(resp.DBInstances) != 0 &&
|
||||
*resp.DBInstances[0].DBInstanceIdentifier == rs.Primary.ID {
|
||||
return fmt.Errorf("DB Instance still exists")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func testAccCheckAWSDBInstanceNoSnapshot(s *terraform.State) error {
|
||||
conn := testAccProvider.Meta().(*AWSClient).rdsconn
|
||||
@ -758,7 +762,7 @@ resource "aws_db_instance" "snapshot" {
|
||||
}`, acctest.RandInt())
|
||||
}
|
||||
|
||||
func testAccSnapshotInstanceConfigWithSnapshot() string {
|
||||
func testAccSnapshotInstanceConfigWithSnapshot(rInt int) string {
|
||||
return fmt.Sprintf(`
|
||||
provider "aws" {
|
||||
region = "us-east-1"
|
||||
@ -780,12 +784,12 @@ resource "aws_db_instance" "snapshot" {
|
||||
parameter_group_name = "default.mysql5.6"
|
||||
|
||||
copy_tags_to_snapshot = true
|
||||
final_snapshot_identifier = "foobarbaz-test-terraform-final-snapshot-2"
|
||||
final_snapshot_identifier = "foobarbaz-test-terraform-final-snapshot-%d"
|
||||
tags {
|
||||
Name = "tf-tags-db"
|
||||
}
|
||||
}
|
||||
`, acctest.RandInt())
|
||||
`, rInt, rInt)
|
||||
}
|
||||
|
||||
func testAccSnapshotInstanceConfig_enhancedMonitoring(rName string) string {
|
||||
|
@ -226,10 +226,28 @@ func testAccCheckAWSClusterSnapshot(rInt int) resource.TestCheckFunc {
|
||||
continue
|
||||
}
|
||||
|
||||
// Try and delete the snapshot before we check for the cluster not found
|
||||
snapshot_identifier := fmt.Sprintf("foobarbaz-test-terraform-final-snapshot-%d", rInt)
|
||||
|
||||
awsClient := testAccProvider.Meta().(*AWSClient)
|
||||
conn := awsClient.rdsconn
|
||||
|
||||
arn, arnErr := buildRDSClusterARN(snapshot_identifier, awsClient.partition, awsClient.accountid, awsClient.region)
|
||||
tagsARN := strings.Replace(arn, ":cluster:", ":snapshot:", 1)
|
||||
if arnErr != nil {
|
||||
return fmt.Errorf("Error building ARN for tags check with ARN (%s): %s", tagsARN, arnErr)
|
||||
}
|
||||
|
||||
log.Printf("[INFO] Deleting the Snapshot %s", snapshot_identifier)
|
||||
_, snapDeleteErr := conn.DeleteDBClusterSnapshot(
|
||||
&rds.DeleteDBClusterSnapshotInput{
|
||||
DBClusterSnapshotIdentifier: aws.String(snapshot_identifier),
|
||||
})
|
||||
if snapDeleteErr != nil {
|
||||
return snapDeleteErr
|
||||
}
|
||||
|
||||
// Try to find the Group
|
||||
conn := testAccProvider.Meta().(*AWSClient).rdsconn
|
||||
var err error
|
||||
resp, err := conn.DescribeDBClusters(
|
||||
&rds.DescribeDBClustersInput{
|
||||
@ -248,23 +266,6 @@ func testAccCheckAWSClusterSnapshot(rInt int) resource.TestCheckFunc {
|
||||
if awsErr.Code() == "DBClusterNotFoundFault" {
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
awsClient := testAccProvider.Meta().(*AWSClient)
|
||||
arn, err := buildRDSClusterARN(snapshot_identifier, awsClient.partition, awsClient.accountid, awsClient.region)
|
||||
tagsARN := strings.Replace(arn, ":cluster:", ":snapshot:", 1)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error building ARN for tags check with ARN (%s): %s", tagsARN, err)
|
||||
}
|
||||
|
||||
log.Printf("[INFO] Deleting the Snapshot %s", snapshot_identifier)
|
||||
_, snapDeleteErr := conn.DeleteDBClusterSnapshot(
|
||||
&rds.DeleteDBClusterSnapshotInput{
|
||||
DBClusterSnapshotIdentifier: aws.String(snapshot_identifier),
|
||||
})
|
||||
if snapDeleteErr != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return err
|
||||
|
Loading…
Reference in New Issue
Block a user