github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/resource_aws_s3_bucket_test.go (about) 1 package aws 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "reflect" 7 "regexp" 8 "strconv" 9 "testing" 10 11 "github.com/hashicorp/terraform/helper/acctest" 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/terraform" 14 15 "github.com/aws/aws-sdk-go/aws" 16 "github.com/aws/aws-sdk-go/aws/awserr" 17 "github.com/aws/aws-sdk-go/service/s3" 18 ) 19 20 func TestAccAWSS3Bucket_basic(t *testing.T) { 21 rInt := acctest.RandInt() 22 arnRegexp := regexp.MustCompile( 23 "^arn:aws:s3:::") 24 25 resource.Test(t, resource.TestCase{ 26 PreCheck: func() { testAccPreCheck(t) }, 27 /* 28 IDRefreshName: "aws_s3_bucket.bucket", 29 IDRefreshIgnore: []string{"force_destroy"}, 30 */ 31 Providers: testAccProviders, 32 CheckDestroy: testAccCheckAWSS3BucketDestroy, 33 Steps: []resource.TestStep{ 34 resource.TestStep{ 35 Config: testAccAWSS3BucketConfig(rInt), 36 Check: resource.ComposeTestCheckFunc( 37 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 38 resource.TestCheckResourceAttr( 39 "aws_s3_bucket.bucket", "hosted_zone_id", HostedZoneIDForRegion("us-west-2")), 40 resource.TestCheckResourceAttr( 41 "aws_s3_bucket.bucket", "region", "us-west-2"), 42 resource.TestCheckResourceAttr( 43 "aws_s3_bucket.bucket", "website_endpoint", ""), 44 resource.TestMatchResourceAttr( 45 "aws_s3_bucket.bucket", "arn", arnRegexp), 46 ), 47 }, 48 }, 49 }) 50 } 51 52 func TestAccAWSS3Bucket_acceleration(t *testing.T) { 53 rInt := acctest.RandInt() 54 55 resource.Test(t, resource.TestCase{ 56 PreCheck: func() { testAccPreCheck(t) }, 57 Providers: testAccProviders, 58 CheckDestroy: testAccCheckAWSS3BucketDestroy, 59 Steps: []resource.TestStep{ 60 resource.TestStep{ 61 Config: testAccAWSS3BucketConfigWithAcceleration(rInt), 62 Check: resource.ComposeTestCheckFunc( 63 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 64 resource.TestCheckResourceAttr( 65 "aws_s3_bucket.bucket", "acceleration_status", "Enabled"), 66 ), 67 }, 68 resource.TestStep{ 69 Config: testAccAWSS3BucketConfigWithoutAcceleration(rInt), 70 Check: resource.ComposeTestCheckFunc( 71 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 72 resource.TestCheckResourceAttr( 73 "aws_s3_bucket.bucket", "acceleration_status", "Suspended"), 74 ), 75 }, 76 }, 77 }) 78 } 79 80 func TestAccAWSS3Bucket_RequestPayer(t *testing.T) { 81 rInt := acctest.RandInt() 82 83 resource.Test(t, resource.TestCase{ 84 PreCheck: func() { testAccPreCheck(t) }, 85 Providers: testAccProviders, 86 CheckDestroy: testAccCheckAWSS3BucketDestroy, 87 Steps: []resource.TestStep{ 88 resource.TestStep{ 89 Config: testAccAWSS3BucketConfigRequestPayerBucketOwner(rInt), 90 Check: resource.ComposeTestCheckFunc( 91 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 92 resource.TestCheckResourceAttr( 93 "aws_s3_bucket.bucket", 94 "request_payer", 95 "BucketOwner"), 96 testAccCheckAWSS3RequestPayer( 97 "aws_s3_bucket.bucket", 98 "BucketOwner"), 99 ), 100 }, 101 resource.TestStep{ 102 Config: testAccAWSS3BucketConfigRequestPayerRequester(rInt), 103 Check: resource.ComposeTestCheckFunc( 104 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 105 resource.TestCheckResourceAttr( 106 "aws_s3_bucket.bucket", 107 "request_payer", 108 "Requester"), 109 testAccCheckAWSS3RequestPayer( 110 "aws_s3_bucket.bucket", 111 "Requester"), 112 ), 113 }, 114 }, 115 }) 116 } 117 118 func TestResourceAWSS3BucketRequestPayer_validation(t *testing.T) { 119 _, errors := validateS3BucketRequestPayerType("incorrect", "request_payer") 120 if len(errors) == 0 { 121 t.Fatalf("Expected to trigger a validation error") 122 } 123 124 var testCases = []struct { 125 Value string 126 ErrCount int 127 }{ 128 { 129 Value: "Requester", 130 ErrCount: 0, 131 }, 132 { 133 Value: "BucketOwner", 134 ErrCount: 0, 135 }, 136 } 137 138 for _, tc := range testCases { 139 _, errors := validateS3BucketRequestPayerType(tc.Value, "request_payer") 140 if len(errors) != tc.ErrCount { 141 t.Fatalf("Expected not to trigger a validation error") 142 } 143 } 144 } 145 146 func TestAccAWSS3Bucket_Policy(t *testing.T) { 147 rInt := acctest.RandInt() 148 149 resource.Test(t, resource.TestCase{ 150 PreCheck: func() { testAccPreCheck(t) }, 151 Providers: testAccProviders, 152 CheckDestroy: testAccCheckAWSS3BucketDestroy, 153 Steps: []resource.TestStep{ 154 resource.TestStep{ 155 Config: testAccAWSS3BucketConfigWithPolicy(rInt), 156 Check: resource.ComposeTestCheckFunc( 157 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 158 testAccCheckAWSS3BucketPolicy( 159 "aws_s3_bucket.bucket", testAccAWSS3BucketPolicy(rInt)), 160 ), 161 }, 162 resource.TestStep{ 163 Config: testAccAWSS3BucketConfig(rInt), 164 Check: resource.ComposeTestCheckFunc( 165 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 166 testAccCheckAWSS3BucketPolicy( 167 "aws_s3_bucket.bucket", ""), 168 ), 169 }, 170 resource.TestStep{ 171 Config: testAccAWSS3BucketConfigWithEmptyPolicy(rInt), 172 Check: resource.ComposeTestCheckFunc( 173 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 174 testAccCheckAWSS3BucketPolicy( 175 "aws_s3_bucket.bucket", ""), 176 ), 177 }, 178 }, 179 }) 180 } 181 182 func TestAccAWSS3Bucket_UpdateAcl(t *testing.T) { 183 ri := acctest.RandInt() 184 preConfig := fmt.Sprintf(testAccAWSS3BucketConfigWithAcl, ri) 185 postConfig := fmt.Sprintf(testAccAWSS3BucketConfigWithAclUpdate, ri) 186 187 resource.Test(t, resource.TestCase{ 188 PreCheck: func() { testAccPreCheck(t) }, 189 Providers: testAccProviders, 190 CheckDestroy: testAccCheckAWSS3BucketDestroy, 191 Steps: []resource.TestStep{ 192 resource.TestStep{ 193 Config: preConfig, 194 Check: resource.ComposeTestCheckFunc( 195 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 196 resource.TestCheckResourceAttr( 197 "aws_s3_bucket.bucket", "acl", "public-read"), 198 ), 199 }, 200 resource.TestStep{ 201 Config: postConfig, 202 Check: resource.ComposeTestCheckFunc( 203 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 204 resource.TestCheckResourceAttr( 205 "aws_s3_bucket.bucket", "acl", "private"), 206 ), 207 }, 208 }, 209 }) 210 } 211 212 func TestAccAWSS3Bucket_Website_Simple(t *testing.T) { 213 rInt := acctest.RandInt() 214 resource.Test(t, resource.TestCase{ 215 PreCheck: func() { testAccPreCheck(t) }, 216 Providers: testAccProviders, 217 CheckDestroy: testAccCheckAWSS3BucketDestroy, 218 Steps: []resource.TestStep{ 219 resource.TestStep{ 220 Config: testAccAWSS3BucketWebsiteConfig(rInt), 221 Check: resource.ComposeTestCheckFunc( 222 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 223 testAccCheckAWSS3BucketWebsite( 224 "aws_s3_bucket.bucket", "index.html", "", "", ""), 225 resource.TestCheckResourceAttr( 226 "aws_s3_bucket.bucket", "website_endpoint", testAccWebsiteEndpoint(rInt)), 227 ), 228 }, 229 resource.TestStep{ 230 Config: testAccAWSS3BucketWebsiteConfigWithError(rInt), 231 Check: resource.ComposeTestCheckFunc( 232 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 233 testAccCheckAWSS3BucketWebsite( 234 "aws_s3_bucket.bucket", "index.html", "error.html", "", ""), 235 resource.TestCheckResourceAttr( 236 "aws_s3_bucket.bucket", "website_endpoint", testAccWebsiteEndpoint(rInt)), 237 ), 238 }, 239 resource.TestStep{ 240 Config: testAccAWSS3BucketConfig(rInt), 241 Check: resource.ComposeTestCheckFunc( 242 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 243 testAccCheckAWSS3BucketWebsite( 244 "aws_s3_bucket.bucket", "", "", "", ""), 245 resource.TestCheckResourceAttr( 246 "aws_s3_bucket.bucket", "website_endpoint", ""), 247 ), 248 }, 249 }, 250 }) 251 } 252 253 func TestAccAWSS3Bucket_WebsiteRedirect(t *testing.T) { 254 rInt := acctest.RandInt() 255 resource.Test(t, resource.TestCase{ 256 PreCheck: func() { testAccPreCheck(t) }, 257 Providers: testAccProviders, 258 CheckDestroy: testAccCheckAWSS3BucketDestroy, 259 Steps: []resource.TestStep{ 260 resource.TestStep{ 261 Config: testAccAWSS3BucketWebsiteConfigWithRedirect(rInt), 262 Check: resource.ComposeTestCheckFunc( 263 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 264 testAccCheckAWSS3BucketWebsite( 265 "aws_s3_bucket.bucket", "", "", "", "hashicorp.com"), 266 resource.TestCheckResourceAttr( 267 "aws_s3_bucket.bucket", "website_endpoint", testAccWebsiteEndpoint(rInt)), 268 ), 269 }, 270 resource.TestStep{ 271 Config: testAccAWSS3BucketWebsiteConfigWithHttpsRedirect(rInt), 272 Check: resource.ComposeTestCheckFunc( 273 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 274 testAccCheckAWSS3BucketWebsite( 275 "aws_s3_bucket.bucket", "", "", "https", "hashicorp.com"), 276 resource.TestCheckResourceAttr( 277 "aws_s3_bucket.bucket", "website_endpoint", testAccWebsiteEndpoint(rInt)), 278 ), 279 }, 280 resource.TestStep{ 281 Config: testAccAWSS3BucketConfig(rInt), 282 Check: resource.ComposeTestCheckFunc( 283 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 284 testAccCheckAWSS3BucketWebsite( 285 "aws_s3_bucket.bucket", "", "", "", ""), 286 resource.TestCheckResourceAttr( 287 "aws_s3_bucket.bucket", "website_endpoint", ""), 288 ), 289 }, 290 }, 291 }) 292 } 293 294 func TestAccAWSS3Bucket_WebsiteRoutingRules(t *testing.T) { 295 rInt := acctest.RandInt() 296 resource.Test(t, resource.TestCase{ 297 PreCheck: func() { testAccPreCheck(t) }, 298 Providers: testAccProviders, 299 CheckDestroy: testAccCheckAWSS3BucketDestroy, 300 Steps: []resource.TestStep{ 301 resource.TestStep{ 302 Config: testAccAWSS3BucketWebsiteConfigWithRoutingRules(rInt), 303 Check: resource.ComposeTestCheckFunc( 304 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 305 testAccCheckAWSS3BucketWebsite( 306 "aws_s3_bucket.bucket", "index.html", "error.html", "", ""), 307 testAccCheckAWSS3BucketWebsiteRoutingRules( 308 "aws_s3_bucket.bucket", 309 []*s3.RoutingRule{ 310 &s3.RoutingRule{ 311 Condition: &s3.Condition{ 312 KeyPrefixEquals: aws.String("docs/"), 313 }, 314 Redirect: &s3.Redirect{ 315 ReplaceKeyPrefixWith: aws.String("documents/"), 316 }, 317 }, 318 }, 319 ), 320 resource.TestCheckResourceAttr( 321 "aws_s3_bucket.bucket", "website_endpoint", testAccWebsiteEndpoint(rInt)), 322 ), 323 }, 324 resource.TestStep{ 325 Config: testAccAWSS3BucketConfig(rInt), 326 Check: resource.ComposeTestCheckFunc( 327 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 328 testAccCheckAWSS3BucketWebsite( 329 "aws_s3_bucket.bucket", "", "", "", ""), 330 testAccCheckAWSS3BucketWebsiteRoutingRules("aws_s3_bucket.bucket", nil), 331 resource.TestCheckResourceAttr( 332 "aws_s3_bucket.bucket", "website_endpoint", ""), 333 ), 334 }, 335 }, 336 }) 337 } 338 339 // Test TestAccAWSS3Bucket_shouldFailNotFound is designed to fail with a "plan 340 // not empty" error in Terraform, to check against regresssions. 341 // See https://github.com/hashicorp/terraform/pull/2925 342 func TestAccAWSS3Bucket_shouldFailNotFound(t *testing.T) { 343 rInt := acctest.RandInt() 344 resource.Test(t, resource.TestCase{ 345 PreCheck: func() { testAccPreCheck(t) }, 346 Providers: testAccProviders, 347 CheckDestroy: testAccCheckAWSS3BucketDestroy, 348 Steps: []resource.TestStep{ 349 resource.TestStep{ 350 Config: testAccAWSS3BucketDestroyedConfig(rInt), 351 Check: resource.ComposeTestCheckFunc( 352 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 353 testAccCheckAWSS3DestroyBucket("aws_s3_bucket.bucket"), 354 ), 355 ExpectNonEmptyPlan: true, 356 }, 357 }, 358 }) 359 } 360 361 func TestAccAWSS3Bucket_Versioning(t *testing.T) { 362 rInt := acctest.RandInt() 363 resource.Test(t, resource.TestCase{ 364 PreCheck: func() { testAccPreCheck(t) }, 365 Providers: testAccProviders, 366 CheckDestroy: testAccCheckAWSS3BucketDestroy, 367 Steps: []resource.TestStep{ 368 resource.TestStep{ 369 Config: testAccAWSS3BucketConfig(rInt), 370 Check: resource.ComposeTestCheckFunc( 371 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 372 testAccCheckAWSS3BucketVersioning( 373 "aws_s3_bucket.bucket", ""), 374 ), 375 }, 376 resource.TestStep{ 377 Config: testAccAWSS3BucketConfigWithVersioning(rInt), 378 Check: resource.ComposeTestCheckFunc( 379 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 380 testAccCheckAWSS3BucketVersioning( 381 "aws_s3_bucket.bucket", s3.BucketVersioningStatusEnabled), 382 ), 383 }, 384 resource.TestStep{ 385 Config: testAccAWSS3BucketConfigWithDisableVersioning(rInt), 386 Check: resource.ComposeTestCheckFunc( 387 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 388 testAccCheckAWSS3BucketVersioning( 389 "aws_s3_bucket.bucket", s3.BucketVersioningStatusSuspended), 390 ), 391 }, 392 }, 393 }) 394 } 395 396 func TestAccAWSS3Bucket_Cors(t *testing.T) { 397 rInt := acctest.RandInt() 398 399 updateBucketCors := func(n string) resource.TestCheckFunc { 400 return func(s *terraform.State) error { 401 rs, ok := s.RootModule().Resources[n] 402 if !ok { 403 return fmt.Errorf("Not found: %s", n) 404 } 405 406 conn := testAccProvider.Meta().(*AWSClient).s3conn 407 _, err := conn.PutBucketCors(&s3.PutBucketCorsInput{ 408 Bucket: aws.String(rs.Primary.ID), 409 CORSConfiguration: &s3.CORSConfiguration{ 410 CORSRules: []*s3.CORSRule{ 411 &s3.CORSRule{ 412 AllowedHeaders: []*string{aws.String("*")}, 413 AllowedMethods: []*string{aws.String("GET")}, 414 AllowedOrigins: []*string{aws.String("https://www.example.com")}, 415 }, 416 }, 417 }, 418 }) 419 if err != nil { 420 if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() != "NoSuchCORSConfiguration" { 421 return err 422 } 423 } 424 return nil 425 } 426 } 427 428 resource.Test(t, resource.TestCase{ 429 PreCheck: func() { testAccPreCheck(t) }, 430 Providers: testAccProviders, 431 CheckDestroy: testAccCheckAWSS3BucketDestroy, 432 Steps: []resource.TestStep{ 433 resource.TestStep{ 434 Config: testAccAWSS3BucketConfigWithCORS(rInt), 435 Check: resource.ComposeTestCheckFunc( 436 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 437 testAccCheckAWSS3BucketCors( 438 "aws_s3_bucket.bucket", 439 []*s3.CORSRule{ 440 &s3.CORSRule{ 441 AllowedHeaders: []*string{aws.String("*")}, 442 AllowedMethods: []*string{aws.String("PUT"), aws.String("POST")}, 443 AllowedOrigins: []*string{aws.String("https://www.example.com")}, 444 ExposeHeaders: []*string{aws.String("x-amz-server-side-encryption"), aws.String("ETag")}, 445 MaxAgeSeconds: aws.Int64(3000), 446 }, 447 }, 448 ), 449 updateBucketCors("aws_s3_bucket.bucket"), 450 ), 451 ExpectNonEmptyPlan: true, 452 }, 453 resource.TestStep{ 454 Config: testAccAWSS3BucketConfigWithCORS(rInt), 455 Check: resource.ComposeTestCheckFunc( 456 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 457 testAccCheckAWSS3BucketCors( 458 "aws_s3_bucket.bucket", 459 []*s3.CORSRule{ 460 &s3.CORSRule{ 461 AllowedHeaders: []*string{aws.String("*")}, 462 AllowedMethods: []*string{aws.String("PUT"), aws.String("POST")}, 463 AllowedOrigins: []*string{aws.String("https://www.example.com")}, 464 ExposeHeaders: []*string{aws.String("x-amz-server-side-encryption"), aws.String("ETag")}, 465 MaxAgeSeconds: aws.Int64(3000), 466 }, 467 }, 468 ), 469 ), 470 }, 471 }, 472 }) 473 } 474 475 func TestAccAWSS3Bucket_Logging(t *testing.T) { 476 rInt := acctest.RandInt() 477 resource.Test(t, resource.TestCase{ 478 PreCheck: func() { testAccPreCheck(t) }, 479 Providers: testAccProviders, 480 CheckDestroy: testAccCheckAWSS3BucketDestroy, 481 Steps: []resource.TestStep{ 482 resource.TestStep{ 483 Config: testAccAWSS3BucketConfigWithLogging(rInt), 484 Check: resource.ComposeTestCheckFunc( 485 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 486 testAccCheckAWSS3BucketLogging( 487 "aws_s3_bucket.bucket", "aws_s3_bucket.log_bucket", "log/"), 488 ), 489 }, 490 }, 491 }) 492 } 493 494 func TestAccAWSS3Bucket_Lifecycle(t *testing.T) { 495 rInt := acctest.RandInt() 496 resource.Test(t, resource.TestCase{ 497 PreCheck: func() { testAccPreCheck(t) }, 498 Providers: testAccProviders, 499 CheckDestroy: testAccCheckAWSS3BucketDestroy, 500 Steps: []resource.TestStep{ 501 resource.TestStep{ 502 Config: testAccAWSS3BucketConfigWithLifecycle(rInt), 503 Check: resource.ComposeTestCheckFunc( 504 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 505 resource.TestCheckResourceAttr( 506 "aws_s3_bucket.bucket", "lifecycle_rule.0.id", "id1"), 507 resource.TestCheckResourceAttr( 508 "aws_s3_bucket.bucket", "lifecycle_rule.0.prefix", "path1/"), 509 resource.TestCheckResourceAttr( 510 "aws_s3_bucket.bucket", "lifecycle_rule.0.expiration.2613713285.days", "365"), 511 resource.TestCheckResourceAttr( 512 "aws_s3_bucket.bucket", "lifecycle_rule.0.expiration.2613713285.date", ""), 513 resource.TestCheckResourceAttr( 514 "aws_s3_bucket.bucket", "lifecycle_rule.0.expiration.2613713285.expired_object_delete_marker", "false"), 515 resource.TestCheckResourceAttr( 516 "aws_s3_bucket.bucket", "lifecycle_rule.0.transition.2000431762.date", ""), 517 resource.TestCheckResourceAttr( 518 "aws_s3_bucket.bucket", "lifecycle_rule.0.transition.2000431762.days", "30"), 519 resource.TestCheckResourceAttr( 520 "aws_s3_bucket.bucket", "lifecycle_rule.0.transition.2000431762.storage_class", "STANDARD_IA"), 521 resource.TestCheckResourceAttr( 522 "aws_s3_bucket.bucket", "lifecycle_rule.0.transition.6450812.date", ""), 523 resource.TestCheckResourceAttr( 524 "aws_s3_bucket.bucket", "lifecycle_rule.0.transition.6450812.days", "60"), 525 resource.TestCheckResourceAttr( 526 "aws_s3_bucket.bucket", "lifecycle_rule.0.transition.6450812.storage_class", "GLACIER"), 527 resource.TestCheckResourceAttr( 528 "aws_s3_bucket.bucket", "lifecycle_rule.1.id", "id2"), 529 resource.TestCheckResourceAttr( 530 "aws_s3_bucket.bucket", "lifecycle_rule.1.prefix", "path2/"), 531 resource.TestCheckResourceAttr( 532 "aws_s3_bucket.bucket", "lifecycle_rule.1.expiration.2855832418.date", "2016-01-12"), 533 resource.TestCheckResourceAttr( 534 "aws_s3_bucket.bucket", "lifecycle_rule.1.expiration.2855832418.days", "0"), 535 resource.TestCheckResourceAttr( 536 "aws_s3_bucket.bucket", "lifecycle_rule.1.expiration.2855832418.expired_object_delete_marker", "false"), 537 ), 538 }, 539 resource.TestStep{ 540 Config: testAccAWSS3BucketConfigWithVersioningLifecycle(rInt), 541 Check: resource.ComposeTestCheckFunc( 542 testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"), 543 resource.TestCheckResourceAttr( 544 "aws_s3_bucket.bucket", "lifecycle_rule.0.id", "id1"), 545 resource.TestCheckResourceAttr( 546 "aws_s3_bucket.bucket", "lifecycle_rule.0.prefix", "path1/"), 547 resource.TestCheckResourceAttr( 548 "aws_s3_bucket.bucket", "lifecycle_rule.0.enabled", "true"), 549 resource.TestCheckResourceAttr( 550 "aws_s3_bucket.bucket", "lifecycle_rule.0.noncurrent_version_expiration.80908210.days", "365"), 551 resource.TestCheckResourceAttr( 552 "aws_s3_bucket.bucket", "lifecycle_rule.0.noncurrent_version_transition.1377917700.days", "30"), 553 resource.TestCheckResourceAttr( 554 "aws_s3_bucket.bucket", "lifecycle_rule.0.noncurrent_version_transition.1377917700.storage_class", "STANDARD_IA"), 555 resource.TestCheckResourceAttr( 556 "aws_s3_bucket.bucket", "lifecycle_rule.0.noncurrent_version_transition.2528035817.days", "60"), 557 resource.TestCheckResourceAttr( 558 "aws_s3_bucket.bucket", "lifecycle_rule.0.noncurrent_version_transition.2528035817.storage_class", "GLACIER"), 559 resource.TestCheckResourceAttr( 560 "aws_s3_bucket.bucket", "lifecycle_rule.1.id", "id2"), 561 resource.TestCheckResourceAttr( 562 "aws_s3_bucket.bucket", "lifecycle_rule.1.prefix", "path2/"), 563 resource.TestCheckResourceAttr( 564 "aws_s3_bucket.bucket", "lifecycle_rule.1.enabled", "false"), 565 resource.TestCheckResourceAttr( 566 "aws_s3_bucket.bucket", "lifecycle_rule.1.noncurrent_version_expiration.80908210.days", "365"), 567 ), 568 }, 569 }, 570 }) 571 } 572 573 func testAccCheckAWSS3BucketDestroy(s *terraform.State) error { 574 conn := testAccProvider.Meta().(*AWSClient).s3conn 575 576 for _, rs := range s.RootModule().Resources { 577 if rs.Type != "aws_s3_bucket" { 578 continue 579 } 580 _, err := conn.DeleteBucket(&s3.DeleteBucketInput{ 581 Bucket: aws.String(rs.Primary.ID), 582 }) 583 if err != nil { 584 if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NoSuchBucket" { 585 return nil 586 } 587 return err 588 } 589 } 590 return nil 591 } 592 593 func testAccCheckAWSS3BucketExists(n string) resource.TestCheckFunc { 594 return func(s *terraform.State) error { 595 rs, ok := s.RootModule().Resources[n] 596 if !ok { 597 return fmt.Errorf("Not found: %s", n) 598 } 599 600 if rs.Primary.ID == "" { 601 return fmt.Errorf("No S3 Bucket ID is set") 602 } 603 604 conn := testAccProvider.Meta().(*AWSClient).s3conn 605 _, err := conn.HeadBucket(&s3.HeadBucketInput{ 606 Bucket: aws.String(rs.Primary.ID), 607 }) 608 609 if err != nil { 610 return fmt.Errorf("S3Bucket error: %v", err) 611 } 612 return nil 613 } 614 } 615 616 func testAccCheckAWSS3DestroyBucket(n string) resource.TestCheckFunc { 617 return func(s *terraform.State) error { 618 rs, ok := s.RootModule().Resources[n] 619 if !ok { 620 return fmt.Errorf("Not found: %s", n) 621 } 622 623 if rs.Primary.ID == "" { 624 return fmt.Errorf("No S3 Bucket ID is set") 625 } 626 627 conn := testAccProvider.Meta().(*AWSClient).s3conn 628 _, err := conn.DeleteBucket(&s3.DeleteBucketInput{ 629 Bucket: aws.String(rs.Primary.ID), 630 }) 631 632 if err != nil { 633 return fmt.Errorf("Error destroying Bucket (%s) in testAccCheckAWSS3DestroyBucket: %s", rs.Primary.ID, err) 634 } 635 return nil 636 } 637 } 638 639 func testAccCheckAWSS3BucketPolicy(n string, policy string) resource.TestCheckFunc { 640 return func(s *terraform.State) error { 641 rs, _ := s.RootModule().Resources[n] 642 conn := testAccProvider.Meta().(*AWSClient).s3conn 643 644 out, err := conn.GetBucketPolicy(&s3.GetBucketPolicyInput{ 645 Bucket: aws.String(rs.Primary.ID), 646 }) 647 648 if err != nil { 649 if policy == "" { 650 // expected 651 return nil 652 } else { 653 return fmt.Errorf("GetBucketPolicy error: %v, expected %s", err, policy) 654 } 655 } 656 657 if v := out.Policy; v == nil { 658 if policy != "" { 659 return fmt.Errorf("bad policy, found nil, expected: %s", policy) 660 } 661 } else { 662 expected := make(map[string]interface{}) 663 if err := json.Unmarshal([]byte(policy), &expected); err != nil { 664 return err 665 } 666 actual := make(map[string]interface{}) 667 if err := json.Unmarshal([]byte(*v), &actual); err != nil { 668 return err 669 } 670 671 if !reflect.DeepEqual(expected, actual) { 672 return fmt.Errorf("bad policy, expected: %#v, got %#v", expected, actual) 673 } 674 } 675 676 return nil 677 } 678 } 679 680 func testAccCheckAWSS3BucketWebsite(n string, indexDoc string, errorDoc string, redirectProtocol string, redirectTo string) resource.TestCheckFunc { 681 return func(s *terraform.State) error { 682 rs, _ := s.RootModule().Resources[n] 683 conn := testAccProvider.Meta().(*AWSClient).s3conn 684 685 out, err := conn.GetBucketWebsite(&s3.GetBucketWebsiteInput{ 686 Bucket: aws.String(rs.Primary.ID), 687 }) 688 689 if err != nil { 690 if indexDoc == "" { 691 // If we want to assert that the website is not there, than 692 // this error is expected 693 return nil 694 } else { 695 return fmt.Errorf("S3BucketWebsite error: %v", err) 696 } 697 } 698 699 if v := out.IndexDocument; v == nil { 700 if indexDoc != "" { 701 return fmt.Errorf("bad index doc, found nil, expected: %s", indexDoc) 702 } 703 } else { 704 if *v.Suffix != indexDoc { 705 return fmt.Errorf("bad index doc, expected: %s, got %#v", indexDoc, out.IndexDocument) 706 } 707 } 708 709 if v := out.ErrorDocument; v == nil { 710 if errorDoc != "" { 711 return fmt.Errorf("bad error doc, found nil, expected: %s", errorDoc) 712 } 713 } else { 714 if *v.Key != errorDoc { 715 return fmt.Errorf("bad error doc, expected: %s, got %#v", errorDoc, out.ErrorDocument) 716 } 717 } 718 719 if v := out.RedirectAllRequestsTo; v == nil { 720 if redirectTo != "" { 721 return fmt.Errorf("bad redirect to, found nil, expected: %s", redirectTo) 722 } 723 } else { 724 if *v.HostName != redirectTo { 725 return fmt.Errorf("bad redirect to, expected: %s, got %#v", redirectTo, out.RedirectAllRequestsTo) 726 } 727 if redirectProtocol != "" && v.Protocol != nil && *v.Protocol != redirectProtocol { 728 return fmt.Errorf("bad redirect protocol to, expected: %s, got %#v", redirectProtocol, out.RedirectAllRequestsTo) 729 } 730 } 731 732 return nil 733 } 734 } 735 736 func testAccCheckAWSS3BucketWebsiteRoutingRules(n string, routingRules []*s3.RoutingRule) resource.TestCheckFunc { 737 return func(s *terraform.State) error { 738 rs, _ := s.RootModule().Resources[n] 739 conn := testAccProvider.Meta().(*AWSClient).s3conn 740 741 out, err := conn.GetBucketWebsite(&s3.GetBucketWebsiteInput{ 742 Bucket: aws.String(rs.Primary.ID), 743 }) 744 745 if err != nil { 746 if routingRules == nil { 747 return nil 748 } 749 return fmt.Errorf("GetBucketWebsite error: %v", err) 750 } 751 752 if !reflect.DeepEqual(out.RoutingRules, routingRules) { 753 return fmt.Errorf("bad routing rule, expected: %v, got %v", routingRules, out.RoutingRules) 754 } 755 756 return nil 757 } 758 } 759 760 func testAccCheckAWSS3BucketVersioning(n string, versioningStatus string) resource.TestCheckFunc { 761 return func(s *terraform.State) error { 762 rs, _ := s.RootModule().Resources[n] 763 conn := testAccProvider.Meta().(*AWSClient).s3conn 764 765 out, err := conn.GetBucketVersioning(&s3.GetBucketVersioningInput{ 766 Bucket: aws.String(rs.Primary.ID), 767 }) 768 769 if err != nil { 770 return fmt.Errorf("GetBucketVersioning error: %v", err) 771 } 772 773 if v := out.Status; v == nil { 774 if versioningStatus != "" { 775 return fmt.Errorf("bad error versioning status, found nil, expected: %s", versioningStatus) 776 } 777 } else { 778 if *v != versioningStatus { 779 return fmt.Errorf("bad error versioning status, expected: %s, got %s", versioningStatus, *v) 780 } 781 } 782 783 return nil 784 } 785 } 786 787 func testAccCheckAWSS3BucketCors(n string, corsRules []*s3.CORSRule) resource.TestCheckFunc { 788 return func(s *terraform.State) error { 789 rs, _ := s.RootModule().Resources[n] 790 conn := testAccProvider.Meta().(*AWSClient).s3conn 791 792 out, err := conn.GetBucketCors(&s3.GetBucketCorsInput{ 793 Bucket: aws.String(rs.Primary.ID), 794 }) 795 796 if err != nil { 797 return fmt.Errorf("GetBucketCors error: %v", err) 798 } 799 800 if !reflect.DeepEqual(out.CORSRules, corsRules) { 801 return fmt.Errorf("bad error cors rule, expected: %v, got %v", corsRules, out.CORSRules) 802 } 803 804 return nil 805 } 806 } 807 808 func testAccCheckAWSS3RequestPayer(n, expectedPayer string) resource.TestCheckFunc { 809 return func(s *terraform.State) error { 810 rs, _ := s.RootModule().Resources[n] 811 conn := testAccProvider.Meta().(*AWSClient).s3conn 812 813 out, err := conn.GetBucketRequestPayment(&s3.GetBucketRequestPaymentInput{ 814 Bucket: aws.String(rs.Primary.ID), 815 }) 816 817 if err != nil { 818 return fmt.Errorf("GetBucketRequestPayment error: %v", err) 819 } 820 821 if *out.Payer != expectedPayer { 822 return fmt.Errorf("bad error request payer type, expected: %v, got %v", 823 expectedPayer, out.Payer) 824 } 825 826 return nil 827 } 828 } 829 830 func testAccCheckAWSS3BucketLogging(n, b, p string) resource.TestCheckFunc { 831 return func(s *terraform.State) error { 832 rs, _ := s.RootModule().Resources[n] 833 conn := testAccProvider.Meta().(*AWSClient).s3conn 834 835 out, err := conn.GetBucketLogging(&s3.GetBucketLoggingInput{ 836 Bucket: aws.String(rs.Primary.ID), 837 }) 838 839 if err != nil { 840 return fmt.Errorf("GetBucketLogging error: %v", err) 841 } 842 843 tb, _ := s.RootModule().Resources[b] 844 845 if v := out.LoggingEnabled.TargetBucket; v == nil { 846 if tb.Primary.ID != "" { 847 return fmt.Errorf("bad target bucket, found nil, expected: %s", tb.Primary.ID) 848 } 849 } else { 850 if *v != tb.Primary.ID { 851 return fmt.Errorf("bad target bucket, expected: %s, got %s", tb.Primary.ID, *v) 852 } 853 } 854 855 if v := out.LoggingEnabled.TargetPrefix; v == nil { 856 if p != "" { 857 return fmt.Errorf("bad target prefix, found nil, expected: %s", p) 858 } 859 } else { 860 if *v != p { 861 return fmt.Errorf("bad target prefix, expected: %s, got %s", p, *v) 862 } 863 } 864 865 return nil 866 } 867 } 868 869 // These need a bit of randomness as the name can only be used once globally 870 // within AWS 871 func testAccWebsiteEndpoint(randInt int) string { 872 return fmt.Sprintf("tf-test-bucket-%d.s3-website-us-west-2.amazonaws.com", randInt) 873 } 874 875 func testAccAWSS3BucketPolicy(randInt int) string { 876 return fmt.Sprintf(`{ "Version": "2012-10-17", "Statement": [ { "Sid": "", "Effect": "Allow", "Principal": { "AWS": "*" }, "Action": "s3:GetObject", "Resource": "arn:aws:s3:::tf-test-bucket-%d/*" } ] }`, randInt) 877 } 878 879 func testAccAWSS3BucketConfig(randInt int) string { 880 return fmt.Sprintf(` 881 resource "aws_s3_bucket" "bucket" { 882 bucket = "tf-test-bucket-%d" 883 acl = "public-read" 884 } 885 `, randInt) 886 } 887 888 func testAccAWSS3BucketWebsiteConfig(randInt int) string { 889 return fmt.Sprintf(` 890 resource "aws_s3_bucket" "bucket" { 891 bucket = "tf-test-bucket-%d" 892 acl = "public-read" 893 894 website { 895 index_document = "index.html" 896 } 897 } 898 `, randInt) 899 } 900 901 func testAccAWSS3BucketWebsiteConfigWithError(randInt int) string { 902 return fmt.Sprintf(` 903 resource "aws_s3_bucket" "bucket" { 904 bucket = "tf-test-bucket-%d" 905 acl = "public-read" 906 907 website { 908 index_document = "index.html" 909 error_document = "error.html" 910 } 911 } 912 `, randInt) 913 } 914 915 func testAccAWSS3BucketWebsiteConfigWithRedirect(randInt int) string { 916 return fmt.Sprintf(` 917 resource "aws_s3_bucket" "bucket" { 918 bucket = "tf-test-bucket-%d" 919 acl = "public-read" 920 921 website { 922 redirect_all_requests_to = "hashicorp.com" 923 } 924 } 925 `, randInt) 926 } 927 928 func testAccAWSS3BucketWebsiteConfigWithHttpsRedirect(randInt int) string { 929 return fmt.Sprintf(` 930 resource "aws_s3_bucket" "bucket" { 931 bucket = "tf-test-bucket-%d" 932 acl = "public-read" 933 934 website { 935 redirect_all_requests_to = "https://hashicorp.com" 936 } 937 } 938 `, randInt) 939 } 940 941 func testAccAWSS3BucketWebsiteConfigWithRoutingRules(randInt int) string { 942 return fmt.Sprintf(` 943 resource "aws_s3_bucket" "bucket" { 944 bucket = "tf-test-bucket-%d" 945 acl = "public-read" 946 947 website { 948 index_document = "index.html" 949 error_document = "error.html" 950 routing_rules = <<EOF 951 [{ 952 "Condition": { 953 "KeyPrefixEquals": "docs/" 954 }, 955 "Redirect": { 956 "ReplaceKeyPrefixWith": "documents/" 957 } 958 }] 959 EOF 960 } 961 } 962 `, randInt) 963 } 964 965 func testAccAWSS3BucketConfigWithAcceleration(randInt int) string { 966 return fmt.Sprintf(` 967 resource "aws_s3_bucket" "bucket" { 968 bucket = "tf-test-bucket-%d" 969 acl = "public-read" 970 acceleration_status = "Enabled" 971 } 972 `, randInt) 973 } 974 975 func testAccAWSS3BucketConfigWithoutAcceleration(randInt int) string { 976 return fmt.Sprintf(` 977 resource "aws_s3_bucket" "bucket" { 978 bucket = "tf-test-bucket-%d" 979 acl = "public-read" 980 acceleration_status = "Suspended" 981 } 982 `, randInt) 983 } 984 985 func testAccAWSS3BucketConfigRequestPayerBucketOwner(randInt int) string { 986 return fmt.Sprintf(` 987 resource "aws_s3_bucket" "bucket" { 988 bucket = "tf-test-bucket-%d" 989 acl = "public-read" 990 request_payer = "BucketOwner" 991 } 992 `, randInt) 993 } 994 995 func testAccAWSS3BucketConfigRequestPayerRequester(randInt int) string { 996 return fmt.Sprintf(` 997 resource "aws_s3_bucket" "bucket" { 998 bucket = "tf-test-bucket-%d" 999 acl = "public-read" 1000 request_payer = "Requester" 1001 } 1002 `, randInt) 1003 } 1004 1005 func testAccAWSS3BucketConfigWithPolicy(randInt int) string { 1006 return fmt.Sprintf(` 1007 resource "aws_s3_bucket" "bucket" { 1008 bucket = "tf-test-bucket-%d" 1009 acl = "public-read" 1010 policy = %s 1011 } 1012 `, randInt, strconv.Quote(testAccAWSS3BucketPolicy(randInt))) 1013 } 1014 1015 func testAccAWSS3BucketDestroyedConfig(randInt int) string { 1016 return fmt.Sprintf(` 1017 resource "aws_s3_bucket" "bucket" { 1018 bucket = "tf-test-bucket-%d" 1019 acl = "public-read" 1020 } 1021 `, randInt) 1022 } 1023 1024 func testAccAWSS3BucketConfigWithEmptyPolicy(randInt int) string { 1025 return fmt.Sprintf(` 1026 resource "aws_s3_bucket" "bucket" { 1027 bucket = "tf-test-bucket-%d" 1028 acl = "public-read" 1029 policy = "" 1030 } 1031 `, randInt) 1032 } 1033 1034 func testAccAWSS3BucketConfigWithVersioning(randInt int) string { 1035 return fmt.Sprintf(` 1036 resource "aws_s3_bucket" "bucket" { 1037 bucket = "tf-test-bucket-%d" 1038 acl = "public-read" 1039 versioning { 1040 enabled = true 1041 } 1042 } 1043 `, randInt) 1044 } 1045 1046 func testAccAWSS3BucketConfigWithDisableVersioning(randInt int) string { 1047 return fmt.Sprintf(` 1048 resource "aws_s3_bucket" "bucket" { 1049 bucket = "tf-test-bucket-%d" 1050 acl = "public-read" 1051 versioning { 1052 enabled = false 1053 } 1054 } 1055 `, randInt) 1056 } 1057 1058 func testAccAWSS3BucketConfigWithCORS(randInt int) string { 1059 return fmt.Sprintf(` 1060 resource "aws_s3_bucket" "bucket" { 1061 bucket = "tf-test-bucket-%d" 1062 acl = "public-read" 1063 cors_rule { 1064 allowed_headers = ["*"] 1065 allowed_methods = ["PUT","POST"] 1066 allowed_origins = ["https://www.example.com"] 1067 expose_headers = ["x-amz-server-side-encryption","ETag"] 1068 max_age_seconds = 3000 1069 } 1070 } 1071 `, randInt) 1072 } 1073 1074 var testAccAWSS3BucketConfigWithAcl = ` 1075 resource "aws_s3_bucket" "bucket" { 1076 bucket = "tf-test-bucket-%d" 1077 acl = "public-read" 1078 } 1079 ` 1080 1081 var testAccAWSS3BucketConfigWithAclUpdate = ` 1082 resource "aws_s3_bucket" "bucket" { 1083 bucket = "tf-test-bucket-%d" 1084 acl = "private" 1085 } 1086 ` 1087 1088 func testAccAWSS3BucketConfigWithLogging(randInt int) string { 1089 return fmt.Sprintf(` 1090 resource "aws_s3_bucket" "log_bucket" { 1091 bucket = "tf-test-log-bucket-%d" 1092 acl = "log-delivery-write" 1093 } 1094 resource "aws_s3_bucket" "bucket" { 1095 bucket = "tf-test-bucket-%d" 1096 acl = "private" 1097 logging { 1098 target_bucket = "${aws_s3_bucket.log_bucket.id}" 1099 target_prefix = "log/" 1100 } 1101 } 1102 `, randInt, randInt) 1103 } 1104 1105 func testAccAWSS3BucketConfigWithLifecycle(randInt int) string { 1106 return fmt.Sprintf(` 1107 resource "aws_s3_bucket" "bucket" { 1108 bucket = "tf-test-bucket-%d" 1109 acl = "private" 1110 lifecycle_rule { 1111 id = "id1" 1112 prefix = "path1/" 1113 enabled = true 1114 1115 expiration { 1116 days = 365 1117 } 1118 1119 transition { 1120 days = 30 1121 storage_class = "STANDARD_IA" 1122 } 1123 transition { 1124 days = 60 1125 storage_class = "GLACIER" 1126 } 1127 } 1128 lifecycle_rule { 1129 id = "id2" 1130 prefix = "path2/" 1131 enabled = true 1132 1133 expiration { 1134 date = "2016-01-12" 1135 } 1136 } 1137 } 1138 `, randInt) 1139 } 1140 1141 func testAccAWSS3BucketConfigWithVersioningLifecycle(randInt int) string { 1142 return fmt.Sprintf(` 1143 resource "aws_s3_bucket" "bucket" { 1144 bucket = "tf-test-bucket-%d" 1145 acl = "private" 1146 versioning { 1147 enabled = false 1148 } 1149 lifecycle_rule { 1150 id = "id1" 1151 prefix = "path1/" 1152 enabled = true 1153 1154 noncurrent_version_expiration { 1155 days = 365 1156 } 1157 noncurrent_version_transition { 1158 days = 30 1159 storage_class = "STANDARD_IA" 1160 } 1161 noncurrent_version_transition { 1162 days = 60 1163 storage_class = "GLACIER" 1164 } 1165 } 1166 lifecycle_rule { 1167 id = "id2" 1168 prefix = "path2/" 1169 enabled = false 1170 1171 noncurrent_version_expiration { 1172 days = 365 1173 } 1174 } 1175 } 1176 `, randInt) 1177 }