provider/aws: Cleanup s3_bucket_object acceptance tests

- the goal was to allow running tests in parallel and to get rid of global variables in the aws package
This commit is contained in:
Radek Simko 2016-02-24 15:04:02 +00:00
parent 24ac996963
commit 9377b301ca
2 changed files with 35 additions and 36 deletions

View File

@ -14,24 +14,27 @@ import (
"github.com/aws/aws-sdk-go/service/s3" "github.com/aws/aws-sdk-go/service/s3"
) )
var tf, err = ioutil.TempFile("", "tf")
func TestAccAWSS3BucketObject_source(t *testing.T) { func TestAccAWSS3BucketObject_source(t *testing.T) {
tmpFile, err := ioutil.TempFile("", "tf-acc-s3-obj-source")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpFile.Name())
rInt := acctest.RandInt() rInt := acctest.RandInt()
// first write some data to the tempfile just so it's not 0 bytes. // first write some data to the tempfile just so it's not 0 bytes.
ioutil.WriteFile(tf.Name(), []byte("{anything will do }"), 0644) err = ioutil.WriteFile(tmpFile.Name(), []byte("{anything will do }"), 0644)
if err != nil {
t.Fatal(err)
}
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { PreCheck: func() { testAccPreCheck(t) },
if err != nil {
panic(err)
}
testAccPreCheck(t)
},
Providers: testAccProviders, Providers: testAccProviders,
CheckDestroy: testAccCheckAWSS3BucketObjectDestroy, CheckDestroy: testAccCheckAWSS3BucketObjectDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ resource.TestStep{
Config: testAccAWSS3BucketObjectConfigSource(rInt), Config: testAccAWSS3BucketObjectConfigSource(rInt, tmpFile.Name()),
Check: testAccCheckAWSS3BucketObjectExists("aws_s3_bucket_object.object"), Check: testAccCheckAWSS3BucketObjectExists("aws_s3_bucket_object.object"),
}, },
}, },
@ -40,40 +43,42 @@ func TestAccAWSS3BucketObject_source(t *testing.T) {
func TestAccAWSS3BucketObject_content(t *testing.T) { func TestAccAWSS3BucketObject_content(t *testing.T) {
rInt := acctest.RandInt() rInt := acctest.RandInt()
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { PreCheck: func() { testAccPreCheck(t) },
if err != nil {
panic(err)
}
testAccPreCheck(t)
},
Providers: testAccProviders, Providers: testAccProviders,
CheckDestroy: testAccCheckAWSS3BucketObjectDestroy, CheckDestroy: testAccCheckAWSS3BucketObjectDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ resource.TestStep{
Config: testAccAWSS3BucketObjectConfigContent(rInt), PreConfig: func() {},
Check: testAccCheckAWSS3BucketObjectExists("aws_s3_bucket_object.object"), Config: testAccAWSS3BucketObjectConfigContent(rInt),
Check: testAccCheckAWSS3BucketObjectExists("aws_s3_bucket_object.object"),
}, },
}, },
}) })
} }
func TestAccAWSS3BucketObject_withContentCharacteristics(t *testing.T) { func TestAccAWSS3BucketObject_withContentCharacteristics(t *testing.T) {
tmpFile, err := ioutil.TempFile("", "tf-acc-s3-obj-content-characteristics")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpFile.Name())
rInt := acctest.RandInt() rInt := acctest.RandInt()
// first write some data to the tempfile just so it's not 0 bytes. // first write some data to the tempfile just so it's not 0 bytes.
ioutil.WriteFile(tf.Name(), []byte("{anything will do }"), 0644) err = ioutil.WriteFile(tmpFile.Name(), []byte("{anything will do }"), 0644)
if err != nil {
t.Fatal(err)
}
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { PreCheck: func() { testAccPreCheck(t) },
if err != nil {
panic(err)
}
testAccPreCheck(t)
},
Providers: testAccProviders, Providers: testAccProviders,
CheckDestroy: testAccCheckAWSS3BucketObjectDestroy, CheckDestroy: testAccCheckAWSS3BucketObjectDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
resource.TestStep{ resource.TestStep{
Config: testAccAWSS3BucketObjectConfig_withContentCharacteristics(rInt), Config: testAccAWSS3BucketObjectConfig_withContentCharacteristics(rInt, tmpFile.Name()),
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testAccCheckAWSS3BucketObjectExists("aws_s3_bucket_object.object"), testAccCheckAWSS3BucketObjectExists("aws_s3_bucket_object.object"),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
@ -107,9 +112,6 @@ func testAccCheckAWSS3BucketObjectDestroy(s *terraform.State) error {
func testAccCheckAWSS3BucketObjectExists(n string) resource.TestCheckFunc { func testAccCheckAWSS3BucketObjectExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
defer os.Remove(tf.Name())
rs, ok := s.RootModule().Resources[n] rs, ok := s.RootModule().Resources[n]
if !ok { if !ok {
return fmt.Errorf("Not Found: %s", n) return fmt.Errorf("Not Found: %s", n)
@ -133,7 +135,7 @@ func testAccCheckAWSS3BucketObjectExists(n string) resource.TestCheckFunc {
} }
} }
func testAccAWSS3BucketObjectConfigSource(randInt int) string { func testAccAWSS3BucketObjectConfigSource(randInt int, source string) string {
return fmt.Sprintf(` return fmt.Sprintf(`
resource "aws_s3_bucket" "object_bucket" { resource "aws_s3_bucket" "object_bucket" {
bucket = "tf-object-test-bucket-%d" bucket = "tf-object-test-bucket-%d"
@ -144,10 +146,10 @@ resource "aws_s3_bucket_object" "object" {
source = "%s" source = "%s"
content_type = "binary/octet-stream" content_type = "binary/octet-stream"
} }
`, randInt, tf.Name()) `, randInt, source)
} }
func testAccAWSS3BucketObjectConfig_withContentCharacteristics(randInt int) string { func testAccAWSS3BucketObjectConfig_withContentCharacteristics(randInt int, source string) string {
return fmt.Sprintf(` return fmt.Sprintf(`
resource "aws_s3_bucket" "object_bucket_2" { resource "aws_s3_bucket" "object_bucket_2" {
bucket = "tf-object-test-bucket-%d" bucket = "tf-object-test-bucket-%d"
@ -160,7 +162,7 @@ resource "aws_s3_bucket_object" "object" {
content_language = "en" content_language = "en"
content_type = "binary/octet-stream" content_type = "binary/octet-stream"
} }
`, randInt, tf.Name()) `, randInt, source)
} }
func testAccAWSS3BucketObjectConfigContent(randInt int) string { func testAccAWSS3BucketObjectConfigContent(randInt int) string {

View File

@ -565,9 +565,6 @@ func TestRulesMixedMatching(t *testing.T) {
for i, c := range cases { for i, c := range cases {
saves := matchRules("ingress", c.local, c.remote) saves := matchRules("ingress", c.local, c.remote)
log.Printf("\n======\n\nSaves:\n%#v\n\nCS Saves:\n%#v\n\n======\n", saves, c.saves) log.Printf("\n======\n\nSaves:\n%#v\n\nCS Saves:\n%#v\n\n======\n", saves, c.saves)
if err != nil {
t.Fatal(err)
}
log.Printf("\n\tTest %d:\n", i) log.Printf("\n\tTest %d:\n", i)
if len(saves) != len(c.saves) { if len(saves) != len(c.saves) {