github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_s3_bucket_object_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  
    12  	"github.com/aws/aws-sdk-go/aws"
    13  	"github.com/aws/aws-sdk-go/service/s3"
    14  )
    15  
    16  var tf, err = ioutil.TempFile("", "tf")
    17  
    18  func TestAccAWSS3BucketObject_source(t *testing.T) {
    19  	// first write some data to the tempfile just so it's not 0 bytes.
    20  	ioutil.WriteFile(tf.Name(), []byte("{anything will do }"), 0644)
    21  	resource.Test(t, resource.TestCase{
    22  		PreCheck: func() {
    23  			if err != nil {
    24  				panic(err)
    25  			}
    26  			testAccPreCheck(t)
    27  		},
    28  		Providers:    testAccProviders,
    29  		CheckDestroy: testAccCheckAWSS3BucketObjectDestroy,
    30  		Steps: []resource.TestStep{
    31  			resource.TestStep{
    32  				Config: testAccAWSS3BucketObjectConfigSource,
    33  				Check:  testAccCheckAWSS3BucketObjectExists("aws_s3_bucket_object.object"),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func TestAccAWSS3BucketObject_content(t *testing.T) {
    40  	resource.Test(t, resource.TestCase{
    41  		PreCheck: func() {
    42  			if err != nil {
    43  				panic(err)
    44  			}
    45  			testAccPreCheck(t)
    46  		},
    47  		Providers:    testAccProviders,
    48  		CheckDestroy: testAccCheckAWSS3BucketObjectDestroy,
    49  		Steps: []resource.TestStep{
    50  			resource.TestStep{
    51  				Config: testAccAWSS3BucketObjectConfigContent,
    52  				Check:  testAccCheckAWSS3BucketObjectExists("aws_s3_bucket_object.object"),
    53  			},
    54  		},
    55  	})
    56  }
    57  
    58  func TestAccAWSS3BucketObject_withContentCharacteristics(t *testing.T) {
    59  	// first write some data to the tempfile just so it's not 0 bytes.
    60  	ioutil.WriteFile(tf.Name(), []byte("{anything will do }"), 0644)
    61  	resource.Test(t, resource.TestCase{
    62  		PreCheck: func() {
    63  			if err != nil {
    64  				panic(err)
    65  			}
    66  			testAccPreCheck(t)
    67  		},
    68  		Providers:    testAccProviders,
    69  		CheckDestroy: testAccCheckAWSS3BucketObjectDestroy,
    70  		Steps: []resource.TestStep{
    71  			resource.TestStep{
    72  				Config: testAccAWSS3BucketObjectConfig_withContentCharacteristics,
    73  				Check: resource.ComposeTestCheckFunc(
    74  					testAccCheckAWSS3BucketObjectExists("aws_s3_bucket_object.object"),
    75  					resource.TestCheckResourceAttr(
    76  						"aws_s3_bucket_object.object", "content_type", "binary/octet-stream"),
    77  				),
    78  			},
    79  		},
    80  	})
    81  }
    82  
    83  func testAccCheckAWSS3BucketObjectDestroy(s *terraform.State) error {
    84  	s3conn := testAccProvider.Meta().(*AWSClient).s3conn
    85  
    86  	for _, rs := range s.RootModule().Resources {
    87  		if rs.Type != "aws_s3_bucket_object" {
    88  			continue
    89  		}
    90  
    91  		_, err := s3conn.HeadObject(
    92  			&s3.HeadObjectInput{
    93  				Bucket:  aws.String(rs.Primary.Attributes["bucket"]),
    94  				Key:     aws.String(rs.Primary.Attributes["key"]),
    95  				IfMatch: aws.String(rs.Primary.Attributes["etag"]),
    96  			})
    97  		if err == nil {
    98  			return fmt.Errorf("AWS S3 Object still exists: %s", rs.Primary.ID)
    99  		}
   100  	}
   101  	return nil
   102  }
   103  
   104  func testAccCheckAWSS3BucketObjectExists(n string) resource.TestCheckFunc {
   105  	return func(s *terraform.State) error {
   106  
   107  		defer os.Remove(tf.Name())
   108  
   109  		rs, ok := s.RootModule().Resources[n]
   110  		if !ok {
   111  			return fmt.Errorf("Not Found: %s", n)
   112  		}
   113  
   114  		if rs.Primary.ID == "" {
   115  			return fmt.Errorf("No S3 Bucket Object ID is set")
   116  		}
   117  
   118  		s3conn := testAccProvider.Meta().(*AWSClient).s3conn
   119  		_, err := s3conn.GetObject(
   120  			&s3.GetObjectInput{
   121  				Bucket:  aws.String(rs.Primary.Attributes["bucket"]),
   122  				Key:     aws.String(rs.Primary.Attributes["key"]),
   123  				IfMatch: aws.String(rs.Primary.Attributes["etag"]),
   124  			})
   125  		if err != nil {
   126  			return fmt.Errorf("S3Bucket Object error: %s", err)
   127  		}
   128  		return nil
   129  	}
   130  }
   131  
   132  var randomBucket = randInt
   133  var testAccAWSS3BucketObjectConfigSource = fmt.Sprintf(`
   134  resource "aws_s3_bucket" "object_bucket" {
   135      bucket = "tf-object-test-bucket-%d"
   136  }
   137  resource "aws_s3_bucket_object" "object" {
   138  	bucket = "${aws_s3_bucket.object_bucket.bucket}"
   139  	key = "test-key"
   140  	source = "%s"
   141  	content_type = "binary/octet-stream"
   142  }
   143  `, randomBucket, tf.Name())
   144  
   145  var testAccAWSS3BucketObjectConfig_withContentCharacteristics = fmt.Sprintf(`
   146  resource "aws_s3_bucket" "object_bucket_2" {
   147  	bucket = "tf-object-test-bucket-%d"
   148  }
   149  
   150  resource "aws_s3_bucket_object" "object" {
   151  	bucket = "${aws_s3_bucket.object_bucket_2.bucket}"
   152  	key = "test-key"
   153  	source = "%s"
   154  	content_language = "en"
   155  	content_type = "binary/octet-stream"
   156  }
   157  `, randomBucket, tf.Name())
   158  
   159  var testAccAWSS3BucketObjectConfigContent = fmt.Sprintf(`
   160  resource "aws_s3_bucket" "object_bucket" {
   161          bucket = "tf-object-test-bucket-%d"
   162  }
   163  resource "aws_s3_bucket_object" "object" {
   164          bucket = "${aws_s3_bucket.object_bucket.bucket}"
   165          key = "test-key"
   166          content = "some_bucket_content"
   167  }
   168  `, randomBucket)