2014-07-21 20:34:47 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccAWSS3Bucket(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.bar"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAWSS3BucketDestroy(s *terraform.State) error {
|
|
|
|
conn := testAccProvider.s3conn
|
|
|
|
|
2014-09-17 02:44:42 +02:00
|
|
|
for _, rs := range s.RootModule().Resources {
|
2014-07-21 20:34:47 +02:00
|
|
|
if rs.Type != "aws_s3_bucket" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:44:42 +02:00
|
|
|
bucket := conn.Bucket(rs.Primary.ID)
|
2014-07-21 20:34:47 +02:00
|
|
|
resp, err := bucket.Head("/")
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("S3Bucket still exists")
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
}
|
|
|
|
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]
|
2014-07-21 20:34:47 +02:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:44:42 +02:00
|
|
|
if rs.Primary.ID == "" {
|
2014-07-21 20:34:47 +02:00
|
|
|
return fmt.Errorf("No S3 Bucket ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := testAccProvider.s3conn
|
2014-09-17 02:44:42 +02:00
|
|
|
bucket := conn.Bucket(rs.Primary.ID)
|
2014-07-21 20:34:47 +02:00
|
|
|
resp, err := bucket.Head("/")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("S3Bucket error: %v", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testAccAWSS3BucketConfig = `
|
|
|
|
resource "aws_s3_bucket" "bar" {
|
|
|
|
bucket = "tf-test-bucket"
|
|
|
|
acl = "public-read"
|
|
|
|
}
|
|
|
|
`
|