-
Notifications
You must be signed in to change notification settings - Fork 9.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue #4540 Add new attribute bucket_regional_domain_name in aws_s3_bucket #4556
Changes from 5 commits
1021e48
e1bd1dc
9c9195d
95f1fba
bf516ae
97f375e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,11 +4,13 @@ import ( | |
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"math/rand" | ||
"reflect" | ||
"regexp" | ||
"strconv" | ||
"testing" | ||
"text/template" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
|
@@ -18,6 +20,7 @@ import ( | |
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/aws/endpoints" | ||
"github.com/aws/aws-sdk-go/service/s3" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
@@ -126,6 +129,28 @@ func TestAccAWSS3Bucket_region(t *testing.T) { | |
}) | ||
} | ||
|
||
func TestAccAWSS3Bucket_bucketRegionalDomainName(t *testing.T) { | ||
rInt := acctest.RandInt() | ||
region := testAccFindRandomRegion() | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAWSS3BucketDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAWSS3BucketConfigRegionalDomainName(rInt, region), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), | ||
resource.TestCheckResourceAttr("aws_s3_bucket.bucket", "region", region), | ||
resource.TestCheckResourceAttr( | ||
"aws_s3_bucket.bucket", "bucket_regional_domain_name", testAccBucketRegionalDomainName(rInt, region)), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAWSS3Bucket_acceleration(t *testing.T) { | ||
rInt := acctest.RandInt() | ||
|
||
|
@@ -1331,6 +1356,26 @@ func testAccBucketDomainName(randInt int) string { | |
return fmt.Sprintf("tf-test-bucket-%d.s3.amazonaws.com", randInt) | ||
} | ||
|
||
func testAccFindRandomRegion() string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generally in our acceptance testing we do not want to insert randomness as it leads to inconsistent testing behavior that makes troubleshooting harder than it needs to be. Aside from that, as written this currently will enumerate a list of regions across all AWS partitions (Commercial, GovCloud (US), and China) while the testing credentials are only valid for one partition. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
regions := []string{} | ||
for _, partition := range endpoints.DefaultPartitions() { | ||
for k, _ := range partition.Regions() { | ||
regions = append(regions, k) | ||
} | ||
} | ||
rand.Seed(time.Now().Unix()) | ||
return regions[rand.Intn(len(regions))] | ||
} | ||
|
||
func testAccBucketRegionalDomainName(randInt int, region string) string { | ||
bucket := fmt.Sprintf("tf-test-bucket-%d", randInt) | ||
regionalEndpoint, err := BucketRegionalDomainName(bucket, region) | ||
if err != nil { | ||
return fmt.Sprintf("Regional endpoint not found for bucket %s", bucket) | ||
} | ||
return regionalEndpoint | ||
} | ||
|
||
func testAccWebsiteEndpoint(randInt int) string { | ||
return fmt.Sprintf("tf-test-bucket-%d.s3-website-us-west-2.amazonaws.com", randInt) | ||
} | ||
|
@@ -1431,6 +1476,21 @@ resource "aws_s3_bucket" "bucket" { | |
`, randInt) | ||
} | ||
|
||
func testAccAWSS3BucketConfigRegionalDomainName(randInt int, region string) string { | ||
return fmt.Sprintf(` | ||
provider "aws" { | ||
alias = "custom" | ||
region = "%s" | ||
} | ||
|
||
resource "aws_s3_bucket" "bucket" { | ||
provider = "aws.custom" | ||
bucket = "tf-test-bucket-%d" | ||
region = "%s" | ||
} | ||
`, region, randInt, region) | ||
} | ||
|
||
func testAccAWSS3BucketWebsiteConfig(randInt int) string { | ||
return fmt.Sprintf(` | ||
resource "aws_s3_bucket" "bucket" { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned in the other PR with a
Computed: true
attribute that is always available, this can be moved as a single check under the_basic
test.We also explicitly need the S3 testing to be able to run from AWS Commercial, AWS GovCloud (US), and AWS China which means that we cannot hard code a specific partition region. I believe there is a
testAccProviderRegion()
helper function which can aid us here.If we want to ensure that specific regions are returning their expected domain names (e.g.
cn-north-1
actually returningBUCKET.s3.cn-north-1.amazonaws.com.cn
) I would highly recommend setting up unit testing forbucketRegionalDomainName
as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since I need to create separate test config, I used separate acceptance test
TestAccAWSS3Bucket_bucketRegionalDomainName
. I couldn't findtestAccProviderRegion()
, so I used the sameendpoints
package to choose region.