github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/google/resource_storage_bucket_object_test.go (about) 1 package google 2 3 import ( 4 "crypto/md5" 5 "encoding/base64" 6 "fmt" 7 "io/ioutil" 8 "testing" 9 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 13 "google.golang.org/api/storage/v1" 14 ) 15 16 var tf, err = ioutil.TempFile("", "tf-gce-test") 17 var bucketName = "tf-gce-bucket-test" 18 var objectName = "tf-gce-test" 19 20 func TestAccGoogleStorageObject_basic(t *testing.T) { 21 data := []byte("data data data") 22 h := md5.New() 23 h.Write(data) 24 data_md5 := base64.StdEncoding.EncodeToString(h.Sum(nil)) 25 26 ioutil.WriteFile(tf.Name(), data, 0644) 27 resource.Test(t, resource.TestCase{ 28 PreCheck: func() { 29 if err != nil { 30 panic(err) 31 } 32 testAccPreCheck(t) 33 }, 34 Providers: testAccProviders, 35 CheckDestroy: testAccGoogleStorageObjectDestroy, 36 Steps: []resource.TestStep{ 37 resource.TestStep{ 38 Config: testGoogleStorageBucketsObjectBasic, 39 Check: testAccCheckGoogleStorageObject(bucketName, objectName, data_md5), 40 }, 41 }, 42 }) 43 } 44 45 func testAccCheckGoogleStorageObject(bucket, object, md5 string) resource.TestCheckFunc { 46 return func(s *terraform.State) error { 47 config := testAccProvider.Meta().(*Config) 48 49 objectsService := storage.NewObjectsService(config.clientStorage) 50 51 getCall := objectsService.Get(bucket, object) 52 res, err := getCall.Do() 53 54 if err != nil { 55 return fmt.Errorf("Error retrieving contents of object %s: %s", object, err) 56 } 57 58 if md5 != res.Md5Hash { 59 return fmt.Errorf("Error contents of %s garbled, md5 hashes don't match (%s, %s)", object, md5, res.Md5Hash) 60 } 61 62 return nil 63 } 64 } 65 66 func testAccGoogleStorageObjectDestroy(s *terraform.State) error { 67 config := testAccProvider.Meta().(*Config) 68 69 for _, rs := range s.RootModule().Resources { 70 if rs.Type != "google_storage_bucket_object" { 71 continue 72 } 73 74 bucket := rs.Primary.Attributes["bucket"] 75 name := rs.Primary.Attributes["name"] 76 77 objectsService := storage.NewObjectsService(config.clientStorage) 78 79 getCall := objectsService.Get(bucket, name) 80 _, err := getCall.Do() 81 82 if err == nil { 83 return fmt.Errorf("Object %s still exists", name) 84 } 85 } 86 87 return nil 88 } 89 90 var testGoogleStorageBucketsObjectBasic = fmt.Sprintf(` 91 resource "google_storage_bucket" "bucket" { 92 name = "%s" 93 } 94 95 resource "google_storage_bucket_object" "object" { 96 name = "%s" 97 bucket = "${google_storage_bucket.bucket.name}" 98 source = "%s" 99 predefined_acl = "projectPrivate" 100 } 101 `, bucketName, objectName, tf.Name())