github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/aws/resource_aws_s3_bucket_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAWSS3Bucket(t *testing.T) {
    14  	rand.Seed(time.Now().UnixNano())
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckAWSS3BucketDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccAWSS3BucketConfig,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckAWSS3BucketExists("aws_s3_bucket.bar"),
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func testAccCheckAWSS3BucketDestroy(s *terraform.State) error {
    32  	conn := testAccProvider.Meta().(*AWSClient).s3conn
    33  
    34  	for _, rs := range s.RootModule().Resources {
    35  		if rs.Type != "aws_s3_bucket" {
    36  			continue
    37  		}
    38  
    39  		bucket := conn.Bucket(rs.Primary.ID)
    40  		resp, err := bucket.Head("/")
    41  		if err == nil {
    42  			return fmt.Errorf("S3Bucket still exists")
    43  		}
    44  		defer resp.Body.Close()
    45  	}
    46  	return nil
    47  }
    48  
    49  func testAccCheckAWSS3BucketExists(n string) resource.TestCheckFunc {
    50  	return func(s *terraform.State) error {
    51  		rs, ok := s.RootModule().Resources[n]
    52  		if !ok {
    53  			return fmt.Errorf("Not found: %s", n)
    54  		}
    55  
    56  		if rs.Primary.ID == "" {
    57  			return fmt.Errorf("No S3 Bucket ID is set")
    58  		}
    59  
    60  		conn := testAccProvider.Meta().(*AWSClient).s3conn
    61  		bucket := conn.Bucket(rs.Primary.ID)
    62  		resp, err := bucket.Head("/")
    63  		if err != nil {
    64  			return fmt.Errorf("S3Bucket error: %v", err)
    65  		}
    66  		defer resp.Body.Close()
    67  		return nil
    68  	}
    69  }
    70  
    71  // This needs a bit of randoness as the name can only be
    72  // used once globally within AWS
    73  var testAccAWSS3BucketConfig = fmt.Sprintf(`
    74  resource "aws_s3_bucket" "bar" {
    75  	bucket = "tf-test-bucket-%d"
    76  	acl = "public-read"
    77  }
    78  `, rand.Int())