Skip to content

Commit

Permalink
Adds tests for migrating
Browse files Browse the repository at this point in the history
  • Loading branch information
gdavison committed Mar 8, 2022
1 parent d11ee28 commit 053275a
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 17 deletions.
16 changes: 7 additions & 9 deletions internal/service/s3/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,11 +609,9 @@ func ResourceBucket() *schema.Resource {
},

"object_lock_configuration": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Deprecated: "Use the aws_s3_bucket_object_lock_configuration resource instead",
MaxItems: 1,
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"object_lock_enabled": {
Expand All @@ -624,10 +622,10 @@ func ResourceBucket() *schema.Resource {
},

"rule": {
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Type: schema.TypeList,
Optional: true,
Deprecated: "Use the aws_s3_bucket_object_lock_configuration resource instead",
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"default_retention": {
Expand Down
90 changes: 85 additions & 5 deletions internal/service/s3/bucket_object_lock_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestAccS3BucketObjectLockConfiguration_basic(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccBucketObjectLockConfigurationBasicConfig(rName),
Check: resource.ComposeTestCheckFunc(
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckBucketObjectLockConfigurationExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "object_lock_enabled", s3.ObjectLockEnabledEnabled),
resource.TestCheckResourceAttr(resourceName, "rule.#", "1"),
Expand Down Expand Up @@ -57,7 +57,7 @@ func TestAccS3BucketObjectLockConfiguration_disappears(t *testing.T) {
Steps: []resource.TestStep{
{
Config: testAccBucketObjectLockConfigurationBasicConfig(rName),
Check: resource.ComposeTestCheckFunc(
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckBucketObjectLockConfigurationExists(resourceName),
acctest.CheckResourceDisappears(acctest.Provider, tfs3.ResourceBucketObjectLockConfiguration(), resourceName),
),
Expand All @@ -75,17 +75,17 @@ func TestAccS3BucketObjectLockConfiguration_update(t *testing.T) {
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, s3.EndpointsID),
ProviderFactories: acctest.ProviderFactories,
CheckDestroy: testAccCheckBucketDestroy,
CheckDestroy: testAccCheckBucketObjectLockConfigurationDestroy,
Steps: []resource.TestStep{
{
Config: testAccBucketObjectLockConfigurationBasicConfig(rName),
Check: resource.ComposeTestCheckFunc(
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckBucketObjectLockConfigurationExists(resourceName),
),
},
{
Config: testAccBucketObjectLockConfigurationUpdateConfig(rName),
Check: resource.ComposeTestCheckFunc(
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "object_lock_enabled", s3.ObjectLockEnabledEnabled),
resource.TestCheckResourceAttr(resourceName, "rule.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.0.default_retention.#", "1"),
Expand All @@ -102,6 +102,78 @@ func TestAccS3BucketObjectLockConfiguration_update(t *testing.T) {
})
}

func TestAccS3BucketObjectLockConfiguration_migrate_noChange(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_s3_bucket_object_lock_configuration.test"
bucketResourceName := "aws_s3_bucket.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, s3.EndpointsID),
ProviderFactories: acctest.ProviderFactories,
CheckDestroy: testAccCheckBucketObjectLockConfigurationDestroy,
Steps: []resource.TestStep{
{
Config: testAccBucketObjectLockEnabledWithDefaultRetention(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckBucketExists(bucketResourceName),
resource.TestCheckResourceAttr(bucketResourceName, "object_lock_configuration.#", "1"),
resource.TestCheckResourceAttr(bucketResourceName, "object_lock_configuration.0.object_lock_enabled", s3.ObjectLockEnabledEnabled),
resource.TestCheckResourceAttr(bucketResourceName, "object_lock_configuration.0.rule.#", "1"),
resource.TestCheckResourceAttr(bucketResourceName, "object_lock_configuration.0.rule.0.default_retention.0.mode", s3.ObjectLockRetentionModeCompliance),
resource.TestCheckResourceAttr(bucketResourceName, "object_lock_configuration.0.rule.0.default_retention.0.days", "3"),
),
},
{
Config: testAccBucketObjectLockConfigurationBasicConfig(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckBucketObjectLockConfigurationExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "object_lock_enabled", s3.ObjectLockEnabledEnabled),
resource.TestCheckResourceAttr(resourceName, "rule.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.0.default_retention.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.0.default_retention.0.days", "3"),
resource.TestCheckResourceAttr(resourceName, "rule.0.default_retention.0.mode", s3.ObjectLockRetentionModeCompliance),
),
},
},
})
}

func TestAccS3BucketObjectLockConfiguration_migrate_withChange(t *testing.T) {
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_s3_bucket_object_lock_configuration.test"
bucketResourceName := "aws_s3_bucket.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, s3.EndpointsID),
ProviderFactories: acctest.ProviderFactories,
CheckDestroy: testAccCheckBucketObjectLockConfigurationDestroy,
Steps: []resource.TestStep{
{
Config: testAccBucketObjectLockEnabledNoDefaultRetention(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckBucketExists(bucketResourceName),
resource.TestCheckResourceAttr(bucketResourceName, "object_lock_configuration.#", "1"),
resource.TestCheckResourceAttr(bucketResourceName, "object_lock_configuration.0.object_lock_enabled", s3.ObjectLockEnabledEnabled),
resource.TestCheckResourceAttr(bucketResourceName, "object_lock_configuration.0.rule.#", "0"),
),
},
{
Config: testAccBucketObjectLockConfigurationBasicConfig(rName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckBucketObjectLockConfigurationExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "object_lock_enabled", s3.ObjectLockEnabledEnabled),
resource.TestCheckResourceAttr(resourceName, "rule.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.0.default_retention.#", "1"),
resource.TestCheckResourceAttr(resourceName, "rule.0.default_retention.0.days", "3"),
resource.TestCheckResourceAttr(resourceName, "rule.0.default_retention.0.mode", s3.ObjectLockRetentionModeCompliance),
),
},
},
})
}

func testAccCheckBucketObjectLockConfigurationDestroy(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).S3Conn

Expand Down Expand Up @@ -189,6 +261,10 @@ resource "aws_s3_bucket" "test" {
object_lock_configuration {
object_lock_enabled = "Enabled"
}
lifecycle {
ignore_changes = [object_lock_configuration[0].rule]
}
}
resource "aws_s3_bucket_object_lock_configuration" "test" {
Expand All @@ -212,6 +288,10 @@ resource "aws_s3_bucket" "test" {
object_lock_configuration {
object_lock_enabled = "Enabled"
}
lifecycle {
ignore_changes = [object_lock_configuration[0].rule]
}
}
resource "aws_s3_bucket_object_lock_configuration" "test" {
Expand Down
6 changes: 3 additions & 3 deletions internal/service/s3/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2519,7 +2519,7 @@ func TestAccS3Bucket_Replication_RTC_valid(t *testing.T) {

func TestAccS3Bucket_Manage_objectLock(t *testing.T) {
bucketName := sdkacctest.RandomWithPrefix("tf-test-bucket")
resourceName := "aws_s3_bucket.arbitrary"
resourceName := "aws_s3_bucket.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
Expand Down Expand Up @@ -5044,7 +5044,7 @@ resource "aws_s3_bucket" "bucket" {

func testAccBucketObjectLockEnabledNoDefaultRetention(bucketName string) string {
return fmt.Sprintf(`
resource "aws_s3_bucket" "arbitrary" {
resource "aws_s3_bucket" "test" {
bucket = %[1]q
object_lock_configuration {
Expand All @@ -5056,7 +5056,7 @@ resource "aws_s3_bucket" "arbitrary" {

func testAccBucketObjectLockEnabledWithDefaultRetention(bucketName string) string {
return fmt.Sprintf(`
resource "aws_s3_bucket" "arbitrary" {
resource "aws_s3_bucket" "test" {
bucket = %[1]q
object_lock_configuration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ resource "aws_s3_bucket" "example" {
object_lock_configuration {
object_lock_enabled = "Enabled"
}
lifecycle {
ignore_changes = [object_lock_configuration[0].rule]
}
}
resource "aws_s3_bucket_object_lock_configuration" "example" {
Expand All @@ -49,6 +53,10 @@ Doing so will generate an "Object Lock token" in the back-end.
```terraform
resource "aws_s3_bucket" "example" {
bucket = "mybucket"
lifecycle {
ignore_changes = [object_lock_configuration[0].rule]
}
}
resource "aws_s3_bucket_versioning" "example" {
Expand Down

0 comments on commit 053275a

Please sign in to comment.