terraform/builtin/providers/aws/resource_aws_s3_bucket_test.go

191 lines
5.1 KiB
Go
Raw Normal View History

package aws
import (
"fmt"
"math/rand"
"testing"
"time"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/awslabs/aws-sdk-go/aws"
"github.com/awslabs/aws-sdk-go/service/s3"
)
2015-05-07 17:13:08 +02:00
func TestAccAWSS3Bucket_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSS3BucketDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSS3BucketConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"),
resource.TestCheckResourceAttr(
"aws_s3_bucket.bucket", "hosted_zone_id", s3.HostedZoneIDForRegion("us-west-2")),
2015-05-07 18:09:19 +02:00
resource.TestCheckResourceAttr(
"aws_s3_bucket.bucket", "region", "us-west-2"),
resource.TestCheckResourceAttr(
"aws_s3_bucket.bucket", "website_endpoint", ""),
),
},
},
})
}
2015-05-07 17:13:08 +02:00
func TestAccAWSS3Bucket_Website(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSS3BucketDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSS3BucketWebsiteConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"),
testAccCheckAWSS3BucketWebsite(
"aws_s3_bucket.bucket", "index.html", ""),
resource.TestCheckResourceAttr(
"aws_s3_bucket.bucket", "website_endpoint", testAccWebsiteEndpoint),
),
},
resource.TestStep{
Config: testAccAWSS3BucketWebsiteConfigWithError,
Check: resource.ComposeTestCheckFunc(
2015-05-06 14:15:07 +02:00
testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"),
testAccCheckAWSS3BucketWebsite(
"aws_s3_bucket.bucket", "index.html", "error.html"),
resource.TestCheckResourceAttr(
"aws_s3_bucket.bucket", "website_endpoint", testAccWebsiteEndpoint),
2015-05-06 14:15:07 +02:00
),
},
resource.TestStep{
Config: testAccAWSS3BucketConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"),
testAccCheckAWSS3BucketWebsite(
2015-05-06 14:15:07 +02:00
"aws_s3_bucket.bucket", "", ""),
resource.TestCheckResourceAttr(
"aws_s3_bucket.bucket", "website_endpoint", ""),
),
},
},
})
}
func testAccCheckAWSS3BucketDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).s3conn
2014-09-17 02:44:42 +02:00
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_s3_bucket" {
continue
}
_, err := conn.DeleteBucket(&s3.DeleteBucketInput{
Bucket: aws.String(rs.Primary.ID),
})
if err != nil {
return err
}
}
return nil
}
func testAccCheckAWSS3BucketExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
2014-09-17 02:44:42 +02:00
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
2014-09-17 02:44:42 +02:00
if rs.Primary.ID == "" {
return fmt.Errorf("No S3 Bucket ID is set")
}
conn := testAccProvider.Meta().(*AWSClient).s3conn
_, err := conn.HeadBucket(&s3.HeadBucketInput{
Bucket: aws.String(rs.Primary.ID),
})
if err != nil {
return fmt.Errorf("S3Bucket error: %v", err)
}
return nil
}
}
func testAccCheckAWSS3BucketWebsite(n string, indexDoc string, errorDoc string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, _ := s.RootModule().Resources[n]
conn := testAccProvider.Meta().(*AWSClient).s3conn
out, err := conn.GetBucketWebsite(&s3.GetBucketWebsiteInput{
Bucket: aws.String(rs.Primary.ID),
})
if err != nil {
2015-05-06 14:15:07 +02:00
if indexDoc == "" {
// If we want to assert that the website is not there, than
// this error is expected
return nil
} else {
return fmt.Errorf("S3BucketWebsite error: %v", err)
}
}
if *out.IndexDocument.Suffix != indexDoc {
2015-05-07 17:03:28 +02:00
if out.IndexDocument.Suffix != nil {
return fmt.Errorf("bad index document suffix: %s", *out.IndexDocument.Suffix)
}
return fmt.Errorf("bad index document suffix, is nil")
}
if v := out.ErrorDocument; v == nil {
if errorDoc != "" {
2015-05-07 17:03:28 +02:00
return fmt.Errorf("bad error doc, found nil, expected: %s", errorDoc)
}
} else {
if *v.Key != errorDoc {
2015-05-07 17:03:28 +02:00
return fmt.Errorf("bad error doc, expected: %s, got %#v", errorDoc, out.ErrorDocument)
}
}
return nil
}
}
2015-05-06 14:15:07 +02:00
// These need a bit of randomness as the name can only be used once globally
// within AWS
var randInt = rand.New(rand.NewSource(time.Now().UnixNano())).Int()
2015-05-07 17:13:08 +02:00
var testAccWebsiteEndpoint = fmt.Sprintf("tf-test-bucket-%d.s3-website-us-west-2.amazonaws.com", randInt)
var testAccAWSS3BucketConfig = fmt.Sprintf(`
resource "aws_s3_bucket" "bucket" {
bucket = "tf-test-bucket-%d"
acl = "public-read"
}
`, randInt)
var testAccAWSS3BucketWebsiteConfig = fmt.Sprintf(`
2015-05-06 14:15:07 +02:00
resource "aws_s3_bucket" "bucket" {
bucket = "tf-test-bucket-%d"
acl = "public-read"
website {
index_document = "index.html"
}
}
`, randInt)
var testAccAWSS3BucketWebsiteConfigWithError = fmt.Sprintf(`
resource "aws_s3_bucket" "bucket" {
bucket = "tf-test-bucket-%d"
acl = "public-read"
website {
index_document = "index.html"
error_document = "error.html"
}
}
`, randInt)