github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/data_source_aws_s3_bucket_object_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"testing"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/service/s3"
    10  	"github.com/hashicorp/terraform/helper/acctest"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccDataSourceAWSS3BucketObject_basic(t *testing.T) {
    16  	rInt := acctest.RandInt()
    17  	resourceOnlyConf, conf := testAccAWSDataSourceS3ObjectConfig_basic(rInt)
    18  
    19  	var rObj s3.GetObjectOutput
    20  	var dsObj s3.GetObjectOutput
    21  
    22  	resource.Test(t, resource.TestCase{
    23  		PreCheck:                  func() { testAccPreCheck(t) },
    24  		Providers:                 testAccProviders,
    25  		PreventPostDestroyRefresh: true,
    26  		Steps: []resource.TestStep{
    27  			resource.TestStep{
    28  				Config: resourceOnlyConf,
    29  				Check: resource.ComposeTestCheckFunc(
    30  					testAccCheckAWSS3BucketObjectExists("aws_s3_bucket_object.object", &rObj),
    31  				),
    32  			},
    33  			resource.TestStep{
    34  				Config: conf,
    35  				Check: resource.ComposeTestCheckFunc(
    36  					testAccCheckAwsS3ObjectDataSourceExists("data.aws_s3_bucket_object.obj", &dsObj),
    37  					resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "content_length", "11"),
    38  					resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "content_type", "binary/octet-stream"),
    39  					resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "etag", "b10a8db164e0754105b7a99be72e3fe5"),
    40  					resource.TestMatchResourceAttr("data.aws_s3_bucket_object.obj", "last_modified",
    41  						regexp.MustCompile("^[a-zA-Z]{3}, [0-9]+ [a-zA-Z]+ [0-9]{4} [0-9:]+ [A-Z]+$")),
    42  					resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "body", ""),
    43  				),
    44  			},
    45  		},
    46  	})
    47  }
    48  
    49  func TestAccDataSourceAWSS3BucketObject_readableBody(t *testing.T) {
    50  	rInt := acctest.RandInt()
    51  	resourceOnlyConf, conf := testAccAWSDataSourceS3ObjectConfig_readableBody(rInt)
    52  
    53  	var rObj s3.GetObjectOutput
    54  	var dsObj s3.GetObjectOutput
    55  
    56  	resource.Test(t, resource.TestCase{
    57  		PreCheck:                  func() { testAccPreCheck(t) },
    58  		Providers:                 testAccProviders,
    59  		PreventPostDestroyRefresh: true,
    60  		Steps: []resource.TestStep{
    61  			resource.TestStep{
    62  				Config: resourceOnlyConf,
    63  				Check: resource.ComposeTestCheckFunc(
    64  					testAccCheckAWSS3BucketObjectExists("aws_s3_bucket_object.object", &rObj),
    65  				),
    66  			},
    67  			resource.TestStep{
    68  				Config: conf,
    69  				Check: resource.ComposeTestCheckFunc(
    70  					testAccCheckAwsS3ObjectDataSourceExists("data.aws_s3_bucket_object.obj", &dsObj),
    71  					resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "content_length", "3"),
    72  					resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "content_type", "text/plain"),
    73  					resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "etag", "a6105c0a611b41b08f1209506350279e"),
    74  					resource.TestMatchResourceAttr("data.aws_s3_bucket_object.obj", "last_modified",
    75  						regexp.MustCompile("^[a-zA-Z]{3}, [0-9]+ [a-zA-Z]+ [0-9]{4} [0-9:]+ [A-Z]+$")),
    76  					resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "body", "yes"),
    77  				),
    78  			},
    79  		},
    80  	})
    81  }
    82  
    83  func TestAccDataSourceAWSS3BucketObject_kmsEncrypted(t *testing.T) {
    84  	rInt := acctest.RandInt()
    85  	resourceOnlyConf, conf := testAccAWSDataSourceS3ObjectConfig_kmsEncrypted(rInt)
    86  
    87  	var rObj s3.GetObjectOutput
    88  	var dsObj s3.GetObjectOutput
    89  
    90  	resource.Test(t, resource.TestCase{
    91  		PreCheck:                  func() { testAccPreCheck(t) },
    92  		Providers:                 testAccProviders,
    93  		PreventPostDestroyRefresh: true,
    94  		Steps: []resource.TestStep{
    95  			resource.TestStep{
    96  				Config: resourceOnlyConf,
    97  				Check: resource.ComposeTestCheckFunc(
    98  					testAccCheckAWSS3BucketObjectExists("aws_s3_bucket_object.object", &rObj),
    99  				),
   100  			},
   101  			resource.TestStep{
   102  				Config: conf,
   103  				Check: resource.ComposeTestCheckFunc(
   104  					testAccCheckAwsS3ObjectDataSourceExists("data.aws_s3_bucket_object.obj", &dsObj),
   105  					resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "content_length", "22"),
   106  					resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "content_type", "text/plain"),
   107  					resource.TestMatchResourceAttr("data.aws_s3_bucket_object.obj", "etag", regexp.MustCompile("^[a-f0-9]{32}$")),
   108  					resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "server_side_encryption", "aws:kms"),
   109  					resource.TestMatchResourceAttr("data.aws_s3_bucket_object.obj", "sse_kms_key_id",
   110  						regexp.MustCompile("^arn:aws:kms:us-west-2:[0-9]{12}:key/[a-z0-9-]{36}$")),
   111  					resource.TestMatchResourceAttr("data.aws_s3_bucket_object.obj", "last_modified",
   112  						regexp.MustCompile("^[a-zA-Z]{3}, [0-9]+ [a-zA-Z]+ [0-9]{4} [0-9:]+ [A-Z]+$")),
   113  					resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "body", "Keep Calm and Carry On"),
   114  				),
   115  			},
   116  		},
   117  	})
   118  }
   119  
   120  func TestAccDataSourceAWSS3BucketObject_allParams(t *testing.T) {
   121  	rInt := acctest.RandInt()
   122  	resourceOnlyConf, conf := testAccAWSDataSourceS3ObjectConfig_allParams(rInt)
   123  
   124  	var rObj s3.GetObjectOutput
   125  	var dsObj s3.GetObjectOutput
   126  
   127  	resource.Test(t, resource.TestCase{
   128  		PreCheck:                  func() { testAccPreCheck(t) },
   129  		Providers:                 testAccProviders,
   130  		PreventPostDestroyRefresh: true,
   131  		Steps: []resource.TestStep{
   132  			resource.TestStep{
   133  				Config: resourceOnlyConf,
   134  				Check: resource.ComposeTestCheckFunc(
   135  					testAccCheckAWSS3BucketObjectExists("aws_s3_bucket_object.object", &rObj),
   136  				),
   137  			},
   138  			resource.TestStep{
   139  				Config: conf,
   140  				Check: resource.ComposeTestCheckFunc(
   141  					testAccCheckAwsS3ObjectDataSourceExists("data.aws_s3_bucket_object.obj", &dsObj),
   142  					resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "content_length", "21"),
   143  					resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "content_type", "application/unknown"),
   144  					resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "etag", "723f7a6ac0c57b445790914668f98640"),
   145  					resource.TestMatchResourceAttr("data.aws_s3_bucket_object.obj", "last_modified",
   146  						regexp.MustCompile("^[a-zA-Z]{3}, [0-9]+ [a-zA-Z]+ [0-9]{4} [0-9:]+ [A-Z]+$")),
   147  					resource.TestMatchResourceAttr("data.aws_s3_bucket_object.obj", "version_id", regexp.MustCompile("^.{32}$")),
   148  					resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "body", ""),
   149  					resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "cache_control", "no-cache"),
   150  					resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "content_disposition", "attachment"),
   151  					resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "content_encoding", "gzip"),
   152  					resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "content_language", "en-GB"),
   153  					// Encryption is off
   154  					resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "server_side_encryption", ""),
   155  					resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "sse_kms_key_id", ""),
   156  					// Supported, but difficult to reproduce in short testing time
   157  					resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "storage_class", "STANDARD"),
   158  					resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "expiration", ""),
   159  					// Currently unsupported in aws_s3_bucket_object resource
   160  					resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "expires", ""),
   161  					resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "website_redirect_location", ""),
   162  					resource.TestCheckResourceAttr("data.aws_s3_bucket_object.obj", "metadata.%", "0"),
   163  				),
   164  			},
   165  		},
   166  	})
   167  }
   168  
   169  func testAccCheckAwsS3ObjectDataSourceExists(n string, obj *s3.GetObjectOutput) resource.TestCheckFunc {
   170  	return func(s *terraform.State) error {
   171  		rs, ok := s.RootModule().Resources[n]
   172  		if !ok {
   173  			return fmt.Errorf("Can't find S3 object data source: %s", n)
   174  		}
   175  
   176  		if rs.Primary.ID == "" {
   177  			return fmt.Errorf("S3 object data source ID not set")
   178  		}
   179  
   180  		s3conn := testAccProvider.Meta().(*AWSClient).s3conn
   181  		out, err := s3conn.GetObject(
   182  			&s3.GetObjectInput{
   183  				Bucket: aws.String(rs.Primary.Attributes["bucket"]),
   184  				Key:    aws.String(rs.Primary.Attributes["key"]),
   185  			})
   186  		if err != nil {
   187  			return fmt.Errorf("Failed getting S3 Object from %s: %s",
   188  				rs.Primary.Attributes["bucket"]+"/"+rs.Primary.Attributes["key"], err)
   189  		}
   190  
   191  		*obj = *out
   192  
   193  		return nil
   194  	}
   195  }
   196  
   197  func testAccAWSDataSourceS3ObjectConfig_basic(randInt int) (string, string) {
   198  	resources := fmt.Sprintf(`
   199  resource "aws_s3_bucket" "object_bucket" {
   200  	bucket = "tf-object-test-bucket-%d"
   201  }
   202  resource "aws_s3_bucket_object" "object" {
   203  	bucket = "${aws_s3_bucket.object_bucket.bucket}"
   204  	key = "tf-testing-obj-%d"
   205  	content = "Hello World"
   206  }
   207  `, randInt, randInt)
   208  
   209  	both := fmt.Sprintf(`%s
   210  data "aws_s3_bucket_object" "obj" {
   211  	bucket = "tf-object-test-bucket-%d"
   212  	key = "tf-testing-obj-%d"
   213  }`, resources, randInt, randInt)
   214  
   215  	return resources, both
   216  }
   217  
   218  func testAccAWSDataSourceS3ObjectConfig_readableBody(randInt int) (string, string) {
   219  	resources := fmt.Sprintf(`
   220  resource "aws_s3_bucket" "object_bucket" {
   221  	bucket = "tf-object-test-bucket-%d"
   222  }
   223  resource "aws_s3_bucket_object" "object" {
   224  	bucket = "${aws_s3_bucket.object_bucket.bucket}"
   225  	key = "tf-testing-obj-%d-readable"
   226  	content = "yes"
   227  	content_type = "text/plain"
   228  }
   229  `, randInt, randInt)
   230  
   231  	both := fmt.Sprintf(`%s
   232  data "aws_s3_bucket_object" "obj" {
   233  	bucket = "tf-object-test-bucket-%d"
   234  	key = "tf-testing-obj-%d-readable"
   235  }`, resources, randInt, randInt)
   236  
   237  	return resources, both
   238  }
   239  
   240  func testAccAWSDataSourceS3ObjectConfig_kmsEncrypted(randInt int) (string, string) {
   241  	resources := fmt.Sprintf(`
   242  resource "aws_s3_bucket" "object_bucket" {
   243  	bucket = "tf-object-test-bucket-%d"
   244  }
   245  resource "aws_kms_key" "example" {
   246    description             = "TF Acceptance Test KMS key"
   247    deletion_window_in_days = 7
   248  }
   249  resource "aws_s3_bucket_object" "object" {
   250  	bucket = "${aws_s3_bucket.object_bucket.bucket}"
   251  	key = "tf-testing-obj-%d-encrypted"
   252  	content = "Keep Calm and Carry On"
   253  	content_type = "text/plain"
   254  	kms_key_id = "${aws_kms_key.example.arn}"
   255  }
   256  `, randInt, randInt)
   257  
   258  	both := fmt.Sprintf(`%s
   259  data "aws_s3_bucket_object" "obj" {
   260  	bucket = "tf-object-test-bucket-%d"
   261  	key = "tf-testing-obj-%d-encrypted"
   262  }`, resources, randInt, randInt)
   263  
   264  	return resources, both
   265  }
   266  
   267  func testAccAWSDataSourceS3ObjectConfig_allParams(randInt int) (string, string) {
   268  	resources := fmt.Sprintf(`
   269  resource "aws_s3_bucket" "object_bucket" {
   270  	bucket = "tf-object-test-bucket-%d"
   271  	versioning {
   272  		enabled = true
   273  	}
   274  }
   275  
   276  resource "aws_s3_bucket_object" "object" {
   277  	bucket = "${aws_s3_bucket.object_bucket.bucket}"
   278  	key = "tf-testing-obj-%d-all-params"
   279  	content = <<CONTENT
   280  {"msg": "Hi there!"}
   281  CONTENT
   282  	content_type = "application/unknown"
   283  	cache_control = "no-cache"
   284  	content_disposition = "attachment"
   285  	content_encoding = "gzip"
   286  	content_language = "en-GB"
   287  }
   288  `, randInt, randInt)
   289  
   290  	both := fmt.Sprintf(`%s
   291  data "aws_s3_bucket_object" "obj" {
   292  	bucket = "tf-object-test-bucket-%d"
   293  	key = "tf-testing-obj-%d-all-params"
   294  }`, resources, randInt, randInt)
   295  
   296  	return resources, both
   297  }