github.com/bendemaree/terraform@v0.5.4-0.20150613200311-f50d97d6eee6/builtin/providers/google/resource_storage_bucket_test.go (about) 1 package google 2 3 import ( 4 "bytes" 5 "fmt" 6 "math/rand" 7 "testing" 8 "time" 9 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 13 "google.golang.org/api/googleapi" 14 storage "google.golang.org/api/storage/v1" 15 ) 16 17 func TestAccStorage_basic(t *testing.T) { 18 var bucketName string 19 20 resource.Test(t, resource.TestCase{ 21 PreCheck: func() { testAccPreCheck(t) }, 22 Providers: testAccProviders, 23 CheckDestroy: testAccGoogleStorageDestroy, 24 Steps: []resource.TestStep{ 25 resource.TestStep{ 26 Config: testGoogleStorageBucketsReaderDefaults, 27 Check: resource.ComposeTestCheckFunc( 28 testAccCheckCloudStorageBucketExists( 29 "google_storage_bucket.bucket", &bucketName), 30 resource.TestCheckResourceAttr( 31 "google_storage_bucket.bucket", "predefined_acl", "projectPrivate"), 32 resource.TestCheckResourceAttr( 33 "google_storage_bucket.bucket", "location", "US"), 34 resource.TestCheckResourceAttr( 35 "google_storage_bucket.bucket", "force_destroy", "false"), 36 ), 37 }, 38 }, 39 }) 40 } 41 42 func TestAccStorageCustomAttributes(t *testing.T) { 43 var bucketName string 44 45 resource.Test(t, resource.TestCase{ 46 PreCheck: func() { testAccPreCheck(t) }, 47 Providers: testAccProviders, 48 CheckDestroy: testAccGoogleStorageDestroy, 49 Steps: []resource.TestStep{ 50 resource.TestStep{ 51 Config: testGoogleStorageBucketsReaderCustomAttributes, 52 Check: resource.ComposeTestCheckFunc( 53 testAccCheckCloudStorageBucketExists( 54 "google_storage_bucket.bucket", &bucketName), 55 resource.TestCheckResourceAttr( 56 "google_storage_bucket.bucket", "predefined_acl", "publicReadWrite"), 57 resource.TestCheckResourceAttr( 58 "google_storage_bucket.bucket", "location", "EU"), 59 resource.TestCheckResourceAttr( 60 "google_storage_bucket.bucket", "force_destroy", "true"), 61 ), 62 }, 63 }, 64 }) 65 } 66 67 func TestAccStorageBucketUpdate(t *testing.T) { 68 var bucketName string 69 70 resource.Test(t, resource.TestCase{ 71 PreCheck: func() { testAccPreCheck(t) }, 72 Providers: testAccProviders, 73 CheckDestroy: testAccGoogleStorageDestroy, 74 Steps: []resource.TestStep{ 75 resource.TestStep{ 76 Config: testGoogleStorageBucketsReaderDefaults, 77 Check: resource.ComposeTestCheckFunc( 78 testAccCheckCloudStorageBucketExists( 79 "google_storage_bucket.bucket", &bucketName), 80 resource.TestCheckResourceAttr( 81 "google_storage_bucket.bucket", "predefined_acl", "projectPrivate"), 82 resource.TestCheckResourceAttr( 83 "google_storage_bucket.bucket", "location", "US"), 84 resource.TestCheckResourceAttr( 85 "google_storage_bucket.bucket", "force_destroy", "false"), 86 ), 87 }, 88 resource.TestStep{ 89 Config: testGoogleStorageBucketsReaderCustomAttributes, 90 Check: resource.ComposeTestCheckFunc( 91 testAccCheckCloudStorageBucketExists( 92 "google_storage_bucket.bucket", &bucketName), 93 resource.TestCheckResourceAttr( 94 "google_storage_bucket.bucket", "predefined_acl", "publicReadWrite"), 95 resource.TestCheckResourceAttr( 96 "google_storage_bucket.bucket", "location", "EU"), 97 resource.TestCheckResourceAttr( 98 "google_storage_bucket.bucket", "force_destroy", "true"), 99 ), 100 }, 101 }, 102 }) 103 } 104 105 func TestAccStorageForceDestroy(t *testing.T) { 106 var bucketName string 107 108 resource.Test(t, resource.TestCase{ 109 PreCheck: func() { testAccPreCheck(t) }, 110 Providers: testAccProviders, 111 CheckDestroy: testAccGoogleStorageDestroy, 112 Steps: []resource.TestStep{ 113 resource.TestStep{ 114 Config: testGoogleStorageBucketsReaderCustomAttributes, 115 Check: resource.ComposeTestCheckFunc( 116 testAccCheckCloudStorageBucketExists( 117 "google_storage_bucket.bucket", &bucketName), 118 ), 119 }, 120 resource.TestStep{ 121 Config: testGoogleStorageBucketsReaderCustomAttributes, 122 Check: resource.ComposeTestCheckFunc( 123 testAccCheckCloudStorageBucketPutItem(&bucketName), 124 ), 125 }, 126 resource.TestStep{ 127 Config: "", 128 Check: resource.ComposeTestCheckFunc( 129 testAccCheckCloudStorageBucketMissing(&bucketName), 130 ), 131 }, 132 }, 133 }) 134 } 135 136 func testAccCheckCloudStorageBucketExists(n string, bucketName *string) resource.TestCheckFunc { 137 return func(s *terraform.State) error { 138 rs, ok := s.RootModule().Resources[n] 139 if !ok { 140 return fmt.Errorf("Not found: %s", n) 141 } 142 143 if rs.Primary.ID == "" { 144 return fmt.Errorf("No Project_ID is set") 145 } 146 147 config := testAccProvider.Meta().(*Config) 148 149 found, err := config.clientStorage.Buckets.Get(rs.Primary.ID).Do() 150 if err != nil { 151 return err 152 } 153 154 if found.Id != rs.Primary.ID { 155 return fmt.Errorf("Bucket not found") 156 } 157 158 *bucketName = found.Name 159 return nil 160 } 161 } 162 163 func testAccCheckCloudStorageBucketPutItem(bucketName *string) resource.TestCheckFunc { 164 return func(s *terraform.State) error { 165 config := testAccProvider.Meta().(*Config) 166 167 data := bytes.NewBufferString("test") 168 dataReader := bytes.NewReader(data.Bytes()) 169 object := &storage.Object{Name: "bucketDestroyTestFile"} 170 171 // This needs to use Media(io.Reader) call, otherwise it does not go to /upload API and fails 172 if res, err := config.clientStorage.Objects.Insert(*bucketName, object).Media(dataReader).Do(); err == nil { 173 fmt.Printf("Created object %v at location %v\n\n", res.Name, res.SelfLink) 174 } else { 175 return fmt.Errorf("Objects.Insert failed: %v", err) 176 } 177 178 return nil 179 } 180 } 181 182 func testAccCheckCloudStorageBucketMissing(bucketName *string) resource.TestCheckFunc { 183 return func(s *terraform.State) error { 184 config := testAccProvider.Meta().(*Config) 185 186 _, err := config.clientStorage.Buckets.Get(*bucketName).Do() 187 if err == nil { 188 return fmt.Errorf("Found %s", *bucketName) 189 } 190 191 if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { 192 return nil 193 } else { 194 return err 195 } 196 } 197 } 198 199 func testAccGoogleStorageDestroy(s *terraform.State) error { 200 config := testAccProvider.Meta().(*Config) 201 202 for _, rs := range s.RootModule().Resources { 203 if rs.Type != "google_storage_bucket" { 204 continue 205 } 206 207 _, err := config.clientStorage.Buckets.Get(rs.Primary.ID).Do() 208 if err == nil { 209 return fmt.Errorf("Bucket still exists") 210 } 211 } 212 213 return nil 214 } 215 216 var randInt = rand.New(rand.NewSource(time.Now().UnixNano())).Int() 217 218 var testGoogleStorageBucketsReaderDefaults = fmt.Sprintf(` 219 resource "google_storage_bucket" "bucket" { 220 name = "tf-test-bucket-%d" 221 } 222 `, randInt) 223 224 var testGoogleStorageBucketsReaderCustomAttributes = fmt.Sprintf(` 225 resource "google_storage_bucket" "bucket" { 226 name = "tf-test-bucket-%d" 227 predefined_acl = "publicReadWrite" 228 location = "EU" 229 force_destroy = "true" 230 } 231 `, randInt)