From 9377b301ca4a0ab69bfdfc209402800699d9fca4 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Wed, 24 Feb 2016 15:04:02 +0000 Subject: [PATCH] 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 --- .../aws/resource_aws_s3_bucket_object_test.go | 68 ++++++++++--------- ..._aws_security_group_rules_matching_test.go | 3 - 2 files changed, 35 insertions(+), 36 deletions(-) diff --git a/builtin/providers/aws/resource_aws_s3_bucket_object_test.go b/builtin/providers/aws/resource_aws_s3_bucket_object_test.go index dc147a4f1..69c3df286 100644 --- a/builtin/providers/aws/resource_aws_s3_bucket_object_test.go +++ b/builtin/providers/aws/resource_aws_s3_bucket_object_test.go @@ -14,24 +14,27 @@ import ( "github.com/aws/aws-sdk-go/service/s3" ) -var tf, err = ioutil.TempFile("", "tf") - 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() // 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{ - PreCheck: func() { - if err != nil { - panic(err) - } - testAccPreCheck(t) - }, + PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckAWSS3BucketObjectDestroy, Steps: []resource.TestStep{ resource.TestStep{ - Config: testAccAWSS3BucketObjectConfigSource(rInt), + Config: testAccAWSS3BucketObjectConfigSource(rInt, tmpFile.Name()), Check: testAccCheckAWSS3BucketObjectExists("aws_s3_bucket_object.object"), }, }, @@ -40,40 +43,42 @@ func TestAccAWSS3BucketObject_source(t *testing.T) { func TestAccAWSS3BucketObject_content(t *testing.T) { rInt := acctest.RandInt() + resource.Test(t, resource.TestCase{ - PreCheck: func() { - if err != nil { - panic(err) - } - testAccPreCheck(t) - }, + PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckAWSS3BucketObjectDestroy, Steps: []resource.TestStep{ resource.TestStep{ - Config: testAccAWSS3BucketObjectConfigContent(rInt), - Check: testAccCheckAWSS3BucketObjectExists("aws_s3_bucket_object.object"), + PreConfig: func() {}, + Config: testAccAWSS3BucketObjectConfigContent(rInt), + Check: testAccCheckAWSS3BucketObjectExists("aws_s3_bucket_object.object"), }, }, }) } 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() // 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{ - PreCheck: func() { - if err != nil { - panic(err) - } - testAccPreCheck(t) - }, + PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckAWSS3BucketObjectDestroy, Steps: []resource.TestStep{ resource.TestStep{ - Config: testAccAWSS3BucketObjectConfig_withContentCharacteristics(rInt), + Config: testAccAWSS3BucketObjectConfig_withContentCharacteristics(rInt, tmpFile.Name()), Check: resource.ComposeTestCheckFunc( testAccCheckAWSS3BucketObjectExists("aws_s3_bucket_object.object"), resource.TestCheckResourceAttr( @@ -107,9 +112,6 @@ func testAccCheckAWSS3BucketObjectDestroy(s *terraform.State) error { func testAccCheckAWSS3BucketObjectExists(n string) resource.TestCheckFunc { return func(s *terraform.State) error { - - defer os.Remove(tf.Name()) - rs, ok := s.RootModule().Resources[n] if !ok { 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(` resource "aws_s3_bucket" "object_bucket" { bucket = "tf-object-test-bucket-%d" @@ -144,10 +146,10 @@ resource "aws_s3_bucket_object" "object" { source = "%s" 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(` resource "aws_s3_bucket" "object_bucket_2" { bucket = "tf-object-test-bucket-%d" @@ -160,7 +162,7 @@ resource "aws_s3_bucket_object" "object" { content_language = "en" content_type = "binary/octet-stream" } -`, randInt, tf.Name()) +`, randInt, source) } func testAccAWSS3BucketObjectConfigContent(randInt int) string { diff --git a/builtin/providers/aws/resource_aws_security_group_rules_matching_test.go b/builtin/providers/aws/resource_aws_security_group_rules_matching_test.go index d3cd721b5..c68c9c5e6 100644 --- a/builtin/providers/aws/resource_aws_security_group_rules_matching_test.go +++ b/builtin/providers/aws/resource_aws_security_group_rules_matching_test.go @@ -565,9 +565,6 @@ func TestRulesMixedMatching(t *testing.T) { for i, c := range cases { 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) - if err != nil { - t.Fatal(err) - } log.Printf("\n\tTest %d:\n", i) if len(saves) != len(c.saves) {