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