Skip to content

Commit

Permalink
Merge pull request #4466 from jmcarp/multi-region-s3-tests
Browse files Browse the repository at this point in the history
Refactor s3 acceptance tests to run in multiple partitions.
  • Loading branch information
bflad authored May 21, 2018
2 parents 3366c75 + 4d873ba commit 86cecfd
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 67 deletions.
11 changes: 6 additions & 5 deletions aws/data_source_aws_s3_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (

func TestAccDataSourceS3Bucket_basic(t *testing.T) {
rInt := acctest.RandInt()
arnRegexp := regexp.MustCompile(
"^arn:aws:s3:::")
hostedZoneID, _ := HostedZoneIDForRegion("us-west-2")
arnRegexp := regexp.MustCompile(`^arn:aws[\w-]*:s3:::`)
region := testAccGetRegion()
hostedZoneID, _ := HostedZoneIDForRegion(region)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -24,7 +24,7 @@ func TestAccDataSourceS3Bucket_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSS3BucketExists("data.aws_s3_bucket.bucket"),
resource.TestMatchResourceAttr("data.aws_s3_bucket.bucket", "arn", arnRegexp),
resource.TestCheckResourceAttr("data.aws_s3_bucket.bucket", "region", "us-west-2"),
resource.TestCheckResourceAttr("data.aws_s3_bucket.bucket", "region", region),
resource.TestCheckResourceAttr(
"data.aws_s3_bucket.bucket", "bucket_domain_name", testAccBucketDomainName(rInt)),
resource.TestCheckResourceAttr(
Expand All @@ -38,6 +38,7 @@ func TestAccDataSourceS3Bucket_basic(t *testing.T) {

func TestAccDataSourceS3Bucket_website(t *testing.T) {
rInt := acctest.RandInt()
region := testAccGetRegion()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -50,7 +51,7 @@ func TestAccDataSourceS3Bucket_website(t *testing.T) {
testAccCheckAWSS3BucketWebsite(
"data.aws_s3_bucket.bucket", "index.html", "error.html", "", ""),
resource.TestCheckResourceAttr(
"data.aws_s3_bucket.bucket", "website_endpoint", testAccWebsiteEndpoint(rInt)),
"data.aws_s3_bucket.bucket", "website_endpoint", testAccWebsiteEndpoint(rInt, region)),
),
},
},
Expand Down
3 changes: 2 additions & 1 deletion aws/import_aws_s3_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func TestAccAWSS3Bucket_importBasic(t *testing.T) {

func TestAccAWSS3Bucket_importWithPolicy(t *testing.T) {
rInt := acctest.RandInt()
partition := testAccGetPartition()

checkFn := func(s []*terraform.InstanceState) error {
// Expect 2: bucket + policy
Expand Down Expand Up @@ -64,7 +65,7 @@ func TestAccAWSS3Bucket_importWithPolicy(t *testing.T) {
CheckDestroy: testAccCheckAWSS3BucketDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSS3BucketConfigWithPolicy(rInt),
Config: testAccAWSS3BucketConfigWithPolicy(rInt, partition),
},

{
Expand Down
26 changes: 26 additions & 0 deletions aws/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package aws

import (
"fmt"
"log"
"os"
"testing"

"github.com/aws/aws-sdk-go/aws/endpoints"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
Expand Down Expand Up @@ -81,6 +84,13 @@ func testAccGetRegion() string {
return v
}

func testAccGetPartition() string {
if partition, ok := endpoints.PartitionForRegion(endpoints.DefaultPartitions(), testAccGetRegion()); ok {
return partition.ID()
}
return "aws"
}

func testAccEC2ClassicPreCheck(t *testing.T) {
client := testAccProvider.Meta().(*AWSClient)
platforms := client.supportedplatforms
Expand All @@ -91,6 +101,22 @@ func testAccEC2ClassicPreCheck(t *testing.T) {
}
}

func testAccHasServicePreCheck(service string, t *testing.T) {
if partition, ok := endpoints.PartitionForRegion(endpoints.DefaultPartitions(), testAccGetRegion()); ok {
if _, ok := partition.Services()[service]; !ok {
t.Skip(fmt.Sprintf("skipping tests; partition does not support %s service", service))
}
}
}

func testAccMultipleRegionsPreCheck(t *testing.T) {
if partition, ok := endpoints.PartitionForRegion(endpoints.DefaultPartitions(), testAccGetRegion()); ok {
if len(partition.Regions()) < 2 {
t.Skip("skipping tests; partition only includes a single region")
}
}
}

func testAccAwsRegionProviderFunc(region string, providers *[]*schema.Provider) func() *schema.Provider {
return func() *schema.Provider {
if region == "" {
Expand Down
11 changes: 8 additions & 3 deletions aws/resource_aws_s3_bucket_notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ func testAccCheckAWSS3BucketLambdaFunctionConfiguration(n, i, t string, events [

func testAccAWSS3BucketConfigWithTopicNotification(topicName, bucketName string) string {
return fmt.Sprintf(`
data "aws_partition" "current" {}
resource "aws_sns_topic" "topic" {
name = "%s"
policy = <<POLICY
Expand All @@ -364,7 +366,7 @@ resource "aws_sns_topic" "topic" {
"Effect": "Allow",
"Principal": {"AWS":"*"},
"Action": "SNS:Publish",
"Resource": "arn:aws:sns:*:*:%s",
"Resource": "arn:${data.aws_partition.current.partition}:sns:*:*:%s",
"Condition":{
"ArnLike":{"aws:SourceArn":"${aws_s3_bucket.bucket.arn}"}
}
Expand Down Expand Up @@ -405,6 +407,8 @@ resource "aws_s3_bucket_notification" "notification" {

func testAccAWSS3BucketConfigWithQueueNotification(queueName, bucketName string) string {
return fmt.Sprintf(`
data "aws_partition" "current" {}
resource "aws_sqs_queue" "queue" {
name = "%s"
policy = <<POLICY
Expand All @@ -414,7 +418,7 @@ resource "aws_sqs_queue" "queue" {
"Effect":"Allow",
"Principal":"*",
"Action":"sqs:SendMessage",
"Resource":"arn:aws:sqs:*:*:%s",
"Resource":"arn:${data.aws_partition.current.partition}:sqs:*:*:%s",
"Condition":{
"ArnEquals":{
"aws:SourceArn":"${aws_s3_bucket.bucket.arn}"
Expand Down Expand Up @@ -506,6 +510,7 @@ resource "aws_s3_bucket_notification" "notification" {

func testAccAWSS3BucketConfigWithTopicNotificationWithoutFilter(topicName, bucketName string) string {
return fmt.Sprintf(`
data "aws_partition" "current" {}
resource "aws_sns_topic" "topic" {
name = "%s"
policy = <<POLICY
Expand All @@ -516,7 +521,7 @@ resource "aws_sns_topic" "topic" {
"Effect": "Allow",
"Principal": {"AWS":"*"},
"Action": "SNS:Publish",
"Resource": "arn:aws:sns:*:*:%s",
"Resource": "arn:${data.aws_partition.current.partition}:sns:*:*:%s",
"Condition":{
"ArnLike":{"aws:SourceArn":"${aws_s3_bucket.bucket.arn}"}
}
Expand Down
47 changes: 35 additions & 12 deletions aws/resource_aws_s3_bucket_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@ import (

func TestAccAWSS3BucketPolicy_basic(t *testing.T) {
name := fmt.Sprintf("tf-test-bucket-%d", acctest.RandInt())

expectedPolicyText := fmt.Sprintf(
`{"Version":"2012-10-17","Statement":[{"Sid": "", "Effect":"Allow","Principal":"*","Action":"s3:*","Resource":["arn:aws:s3:::%s/*","arn:aws:s3:::%s"]}]}`,
name, name)
partition := testAccGetPartition()

expectedPolicyText := fmt.Sprintf(`{
"Version": "2012-10-17",
"Statement": [{
"Sid": "",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:*",
"Resource": ["arn:%s:s3:::%s/*","arn:%s:s3:::%s"]
}]
}`, partition, name, partition, name)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -37,14 +45,29 @@ func TestAccAWSS3BucketPolicy_basic(t *testing.T) {

func TestAccAWSS3BucketPolicy_policyUpdate(t *testing.T) {
name := fmt.Sprintf("tf-test-bucket-%d", acctest.RandInt())

expectedPolicyText1 := fmt.Sprintf(
`{"Version":"2012-10-17","Statement":[{"Sid": "", "Effect":"Allow","Principal":"*","Action":"s3:*","Resource":["arn:aws:s3:::%s/*","arn:aws:s3:::%s"]}]}`,
name, name)

expectedPolicyText2 := fmt.Sprintf(
`{"Version":"2012-10-17","Statement":[{"Sid": "", "Effect":"Allow","Principal":"*","Action":["s3:DeleteBucket", "s3:ListBucket", "s3:ListBucketVersions"], "Resource":["arn:aws:s3:::%s/*","arn:aws:s3:::%s"]}]}`,
name, name)
partition := testAccGetPartition()

expectedPolicyText1 := fmt.Sprintf(`{
"Version": "2012-10-17",
"Statement": [{
"Sid": "",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:*",
"Resource": ["arn:%s:s3:::%s/*","arn:%s:s3:::%s"]
}]
}`, partition, name, partition, name)

expectedPolicyText2 := fmt.Sprintf(`{
"Version":"2012-10-17",
"Statement":[{
"Sid": "",
"Effect": "Allow",
"Principal": "*",
"Action": ["s3:DeleteBucket", "s3:ListBucket", "s3:ListBucketVersions"],
"Resource": ["arn:%s:s3:::%s/*","arn:%s:s3:::%s"]
}]
}`, partition, name, partition, name)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down
Loading

0 comments on commit 86cecfd

Please sign in to comment.