Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharon Nam authored and Sharon Nam committed Dec 1, 2023
1 parent 912fb6f commit 0b1b4c6
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
5 changes: 2 additions & 3 deletions internal/service/backup/vault_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,15 @@ func resourceVaultPolicyPut(ctx context.Context, d *schema.ResourceData, meta in
Policy: aws.String(policy),
}

outputRaw, err := tfresource.RetryWhenAWSErrMessageContains(ctx, iamPropagationTimeout,
_, err = tfresource.RetryWhenAWSErrMessageContains(ctx, iamPropagationTimeout,
func() (interface{}, error) {
return conn.PutBackupVaultAccessPolicyWithContext(ctx, input)
},
errCodeInvalidVaultPolicyConfig, "VaultPolicyyConfig.IamBackupRole",
)

if err != nil {
return sdkdiag.AppendErrorf(diags, "creating Backup Vault Policy (%s): %s", outputRaw.(d.)
, err)
return sdkdiag.AppendErrorf(diags, "creating Backup Vault Policy (%s): %s", name, err)
}

d.SetId(name)
Expand Down
65 changes: 65 additions & 0 deletions internal/service/backup/vault_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,41 @@ func TestAccBackupVaultPolicy_basic(t *testing.T) {
})
}

func TestAccBackupVaultPolicy_eventual_consistency(t *testing.T) {
ctx := acctest.Context(t)
var vault backup.GetBackupVaultAccessPolicyOutput
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_backup_vault_policy.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, backup.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckVaultPolicyDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccVaultPolicyConfig_eventual_consistency(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckVaultPolicyExists(ctx, resourceName, &vault),
resource.TestMatchResourceAttr(resourceName, "policy", regexache.MustCompile("^{\"Id\":\"default\".+"))),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccVaultPolicyConfig_updated(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckVaultPolicyExists(ctx, resourceName, &vault),
resource.TestMatchResourceAttr(resourceName, "policy", regexache.MustCompile("^{\"Id\":\"default\".+")),
resource.TestMatchResourceAttr(resourceName, "policy", regexache.MustCompile("backup:ListRecoveryPointsByBackupVault")),
),
},
},
})
}

func TestAccBackupVaultPolicy_disappears(t *testing.T) {
ctx := acctest.Context(t)
var vault backup.GetBackupVaultAccessPolicyOutput
Expand Down Expand Up @@ -285,3 +320,33 @@ resource "aws_backup_vault_policy" "test" {
}
`, rName)
}

func testAccVaultPolicyConfig_eventual_consistency(rName string) string {
return acctest.ConfigCompose(
testAccVaultPolicyConfig_basic(rName),
fmt.Sprintf(`
data "aws_partition" "current" {}
resource "aws_iam_role" "test" {
name = %[1]q
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = ""
Principal = {
Service = "backup.amazonaws.com"
}
},
]
})
}
resource "aws_iam_role_policy_attachment" "test" {
role = aws_iam_role.test.name
policy_arn = "arn:${data.aws_partition.current.partition}:iam::${data.aws_partition.current.partition}:policy/service-role/AWSBackupServiceRolePolicyForBackup"
}
`, rName))
}

0 comments on commit 0b1b4c6

Please sign in to comment.