github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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 var content = "now this is content!" 20 21 func TestAccGoogleStorageObject_basic(t *testing.T) { 22 bucketName := testBucketName() 23 data := []byte("data data data") 24 h := md5.New() 25 h.Write(data) 26 data_md5 := base64.StdEncoding.EncodeToString(h.Sum(nil)) 27 28 ioutil.WriteFile(tf.Name(), data, 0644) 29 resource.Test(t, resource.TestCase{ 30 PreCheck: func() { 31 if err != nil { 32 panic(err) 33 } 34 testAccPreCheck(t) 35 }, 36 Providers: testAccProviders, 37 CheckDestroy: testAccGoogleStorageObjectDestroy, 38 Steps: []resource.TestStep{ 39 resource.TestStep{ 40 Config: testGoogleStorageBucketsObjectBasic(bucketName), 41 Check: testAccCheckGoogleStorageObject(bucketName, objectName, data_md5), 42 }, 43 }, 44 }) 45 } 46 47 func TestAccGoogleStorageObject_content(t *testing.T) { 48 bucketName := testBucketName() 49 data := []byte(content) 50 h := md5.New() 51 h.Write(data) 52 data_md5 := base64.StdEncoding.EncodeToString(h.Sum(nil)) 53 54 ioutil.WriteFile(tf.Name(), data, 0644) 55 resource.Test(t, resource.TestCase{ 56 PreCheck: func() { 57 if err != nil { 58 panic(err) 59 } 60 testAccPreCheck(t) 61 }, 62 Providers: testAccProviders, 63 CheckDestroy: testAccGoogleStorageObjectDestroy, 64 Steps: []resource.TestStep{ 65 resource.TestStep{ 66 Config: testGoogleStorageBucketsObjectContent(bucketName), 67 Check: testAccCheckGoogleStorageObject(bucketName, objectName, data_md5), 68 }, 69 }, 70 }) 71 } 72 73 func testAccCheckGoogleStorageObject(bucket, object, md5 string) resource.TestCheckFunc { 74 return func(s *terraform.State) error { 75 config := testAccProvider.Meta().(*Config) 76 77 objectsService := storage.NewObjectsService(config.clientStorage) 78 79 getCall := objectsService.Get(bucket, object) 80 res, err := getCall.Do() 81 82 if err != nil { 83 return fmt.Errorf("Error retrieving contents of object %s: %s", object, err) 84 } 85 86 if md5 != res.Md5Hash { 87 return fmt.Errorf("Error contents of %s garbled, md5 hashes don't match (%s, %s)", object, md5, res.Md5Hash) 88 } 89 90 return nil 91 } 92 } 93 94 func testAccGoogleStorageObjectDestroy(s *terraform.State) error { 95 config := testAccProvider.Meta().(*Config) 96 97 for _, rs := range s.RootModule().Resources { 98 if rs.Type != "google_storage_bucket_object" { 99 continue 100 } 101 102 bucket := rs.Primary.Attributes["bucket"] 103 name := rs.Primary.Attributes["name"] 104 105 objectsService := storage.NewObjectsService(config.clientStorage) 106 107 getCall := objectsService.Get(bucket, name) 108 _, err := getCall.Do() 109 110 if err == nil { 111 return fmt.Errorf("Object %s still exists", name) 112 } 113 } 114 115 return nil 116 } 117 118 func testGoogleStorageBucketsObjectContent(bucketName string) string { 119 return fmt.Sprintf(` 120 resource "google_storage_bucket" "bucket" { 121 name = "%s" 122 } 123 124 resource "google_storage_bucket_object" "object" { 125 name = "%s" 126 bucket = "${google_storage_bucket.bucket.name}" 127 content = "%s" 128 predefined_acl = "projectPrivate" 129 } 130 `, bucketName, objectName, content) 131 } 132 func testGoogleStorageBucketsObjectBasic(bucketName string) string { 133 return fmt.Sprintf(` 134 resource "google_storage_bucket" "bucket" { 135 name = "%s" 136 } 137 138 resource "google_storage_bucket_object" "object" { 139 name = "%s" 140 bucket = "${google_storage_bucket.bucket.name}" 141 source = "%s" 142 predefined_acl = "projectPrivate" 143 } 144 `, bucketName, objectName, tf.Name()) 145 }