Skip to content

Commit

Permalink
Merge pull request #35220 from hashicorp/t-release/4.x-s3-object-key-…
Browse files Browse the repository at this point in the history
…leading-dot-slash

[release/4.x] d/aws_s3_object: Test key with leading `./`
  • Loading branch information
ewbankkit committed Jan 10, 2024
2 parents 87877bb + 504e699 commit 7ee4abc
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions internal/service/s3/object_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,52 @@ func TestAccS3ObjectDataSource_singleSlashAsKey(t *testing.T) {
})
}

func TestAccS3ObjectDataSource_leadingDotSlash(t *testing.T) {
ctx := acctest.Context(t)
var rObj s3.GetObjectOutput
var dsObj1, dsObj2 s3.GetObjectOutput

resourceName := "aws_s3_object.object"
dataSourceName1 := "data.aws_s3_object.obj1"
dataSourceName2 := "data.aws_s3_object.obj2"

rInt := sdkacctest.RandInt()
resourceOnlyConf, conf := testAccObjectDataSourceConfig_leadingDotSlash(rInt)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, s3.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
PreventPostDestroyRefresh: true,
Steps: []resource.TestStep{
{ // nosemgrep:ci.test-config-funcs-correct-form
Config: resourceOnlyConf,
Check: resource.ComposeTestCheckFunc(
testAccCheckObjectExists(ctx, resourceName, &rObj),
),
},
{ // nosemgrep:ci.test-config-funcs-correct-form
Config: conf,
Check: resource.ComposeTestCheckFunc(
testAccCheckObjectExistsDataSource(ctx, dataSourceName1, &dsObj1),
resource.TestCheckResourceAttr(dataSourceName1, "content_length", "3"),
resource.TestCheckResourceAttrPair(dataSourceName1, "content_type", resourceName, "content_type"),
resource.TestCheckResourceAttrPair(dataSourceName1, "etag", resourceName, "etag"),
resource.TestMatchResourceAttr(dataSourceName1, "last_modified", regexp.MustCompile(rfc1123RegexPattern)),
resource.TestCheckResourceAttr(dataSourceName1, "body", "yes"),

testAccCheckObjectExistsDataSource(ctx, dataSourceName2, &dsObj2),
resource.TestCheckResourceAttr(dataSourceName2, "content_length", "3"),
resource.TestCheckResourceAttrPair(dataSourceName2, "content_type", resourceName, "content_type"),
resource.TestCheckResourceAttrPair(dataSourceName2, "etag", resourceName, "etag"),
resource.TestMatchResourceAttr(dataSourceName2, "last_modified", regexp.MustCompile(rfc1123RegexPattern)),
resource.TestCheckResourceAttr(dataSourceName2, "body", "yes"),
),
},
},
})
}

func testAccCheckObjectExistsDataSource(ctx context.Context, n string, obj *s3.GetObjectOutput) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -782,3 +828,34 @@ data "aws_s3_object" "test" {
}
`, rName)
}

func testAccObjectDataSourceConfig_leadingDotSlash(randInt int) (string, string) {
resources := fmt.Sprintf(`
resource "aws_s3_bucket" "object_bucket" {
bucket = "tf-object-test-bucket-%[1]d"
}
resource "aws_s3_object" "object" {
bucket = aws_s3_bucket.object_bucket.bucket
key = "./tf-testing-obj-%[1]d-readable"
content = "yes"
content_type = "text/plain"
}
`, randInt)

both := fmt.Sprintf(`
%[1]s
data "aws_s3_object" "obj1" {
bucket = aws_s3_bucket.object_bucket.bucket
key = "tf-testing-obj-%[2]d-readable"
}
data "aws_s3_object" "obj2" {
bucket = aws_s3_bucket.object_bucket.bucket
key = "./tf-testing-obj-%[2]d-readable"
}
`, resources, randInt)

return resources, both
}

0 comments on commit 7ee4abc

Please sign in to comment.