github.com/bradfeehan/terraform@v0.7.0-rc3.0.20170529055808-34b45c5ad841/builtin/providers/google/resource_storage_bucket_test.go (about) 1 package google 2 3 import ( 4 "bytes" 5 "fmt" 6 "log" 7 "testing" 8 9 "github.com/hashicorp/terraform/helper/acctest" 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 TestAccStorageBucket_basic(t *testing.T) { 18 var bucket storage.Bucket 19 bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", acctest.RandInt()) 20 21 resource.Test(t, resource.TestCase{ 22 PreCheck: func() { testAccPreCheck(t) }, 23 Providers: testAccProviders, 24 CheckDestroy: testAccStorageBucketDestroy, 25 Steps: []resource.TestStep{ 26 resource.TestStep{ 27 Config: testAccStorageBucket_basic(bucketName), 28 Check: resource.ComposeTestCheckFunc( 29 testAccCheckStorageBucketExists( 30 "google_storage_bucket.bucket", bucketName, &bucket), 31 resource.TestCheckResourceAttr( 32 "google_storage_bucket.bucket", "location", "US"), 33 resource.TestCheckResourceAttr( 34 "google_storage_bucket.bucket", "force_destroy", "false"), 35 ), 36 }, 37 }, 38 }) 39 } 40 41 func TestAccStorageBucket_customAttributes(t *testing.T) { 42 var bucket storage.Bucket 43 bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", acctest.RandInt()) 44 45 resource.Test(t, resource.TestCase{ 46 PreCheck: func() { testAccPreCheck(t) }, 47 Providers: testAccProviders, 48 CheckDestroy: testAccStorageBucketDestroy, 49 Steps: []resource.TestStep{ 50 resource.TestStep{ 51 Config: testAccStorageBucket_customAttributes(bucketName), 52 Check: resource.ComposeTestCheckFunc( 53 testAccCheckStorageBucketExists( 54 "google_storage_bucket.bucket", bucketName, &bucket), 55 resource.TestCheckResourceAttr( 56 "google_storage_bucket.bucket", "location", "EU"), 57 resource.TestCheckResourceAttr( 58 "google_storage_bucket.bucket", "force_destroy", "true"), 59 ), 60 }, 61 }, 62 }) 63 } 64 65 func TestAccStorageBucket_storageClass(t *testing.T) { 66 var bucket storage.Bucket 67 bucketName := fmt.Sprintf("tf-test-acc-bucket-%d", acctest.RandInt()) 68 69 resource.Test(t, resource.TestCase{ 70 PreCheck: func() { testAccPreCheck(t) }, 71 Providers: testAccProviders, 72 CheckDestroy: testAccStorageBucketDestroy, 73 Steps: []resource.TestStep{ 74 { 75 Config: testAccStorageBucket_storageClass(bucketName, "MULTI_REGIONAL", ""), 76 Check: resource.ComposeTestCheckFunc( 77 testAccCheckStorageBucketExists( 78 "google_storage_bucket.bucket", bucketName, &bucket), 79 resource.TestCheckResourceAttr( 80 "google_storage_bucket.bucket", "storage_class", "MULTI_REGIONAL"), 81 ), 82 }, 83 { 84 Config: testAccStorageBucket_storageClass(bucketName, "NEARLINE", ""), 85 Check: resource.ComposeTestCheckFunc( 86 testAccCheckStorageBucketExists( 87 "google_storage_bucket.bucket", bucketName, &bucket), 88 resource.TestCheckResourceAttr( 89 "google_storage_bucket.bucket", "storage_class", "NEARLINE"), 90 ), 91 }, 92 { 93 Config: testAccStorageBucket_storageClass(bucketName, "REGIONAL", "US-CENTRAL1"), 94 Check: resource.ComposeTestCheckFunc( 95 testAccCheckStorageBucketExists( 96 "google_storage_bucket.bucket", bucketName, &bucket), 97 resource.TestCheckResourceAttr( 98 "google_storage_bucket.bucket", "storage_class", "REGIONAL"), 99 resource.TestCheckResourceAttr( 100 "google_storage_bucket.bucket", "location", "US-CENTRAL1"), 101 ), 102 }, 103 }, 104 }) 105 } 106 107 func TestAccStorageBucket_update(t *testing.T) { 108 var bucket storage.Bucket 109 bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", acctest.RandInt()) 110 111 resource.Test(t, resource.TestCase{ 112 PreCheck: func() { testAccPreCheck(t) }, 113 Providers: testAccProviders, 114 CheckDestroy: testAccStorageBucketDestroy, 115 Steps: []resource.TestStep{ 116 resource.TestStep{ 117 Config: testAccStorageBucket_basic(bucketName), 118 Check: resource.ComposeTestCheckFunc( 119 testAccCheckStorageBucketExists( 120 "google_storage_bucket.bucket", bucketName, &bucket), 121 resource.TestCheckResourceAttr( 122 "google_storage_bucket.bucket", "location", "US"), 123 resource.TestCheckResourceAttr( 124 "google_storage_bucket.bucket", "force_destroy", "false"), 125 ), 126 }, 127 resource.TestStep{ 128 Config: testAccStorageBucket_customAttributes(bucketName), 129 Check: resource.ComposeTestCheckFunc( 130 testAccCheckStorageBucketExists( 131 "google_storage_bucket.bucket", bucketName, &bucket), 132 resource.TestCheckResourceAttr( 133 "google_storage_bucket.bucket", "predefined_acl", "publicReadWrite"), 134 resource.TestCheckResourceAttr( 135 "google_storage_bucket.bucket", "location", "EU"), 136 resource.TestCheckResourceAttr( 137 "google_storage_bucket.bucket", "force_destroy", "true"), 138 ), 139 }, 140 }, 141 }) 142 } 143 144 func TestAccStorageBucket_forceDestroy(t *testing.T) { 145 var bucket storage.Bucket 146 bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", acctest.RandInt()) 147 148 resource.Test(t, resource.TestCase{ 149 PreCheck: func() { testAccPreCheck(t) }, 150 Providers: testAccProviders, 151 CheckDestroy: testAccStorageBucketDestroy, 152 Steps: []resource.TestStep{ 153 resource.TestStep{ 154 Config: testAccStorageBucket_customAttributes(bucketName), 155 Check: resource.ComposeTestCheckFunc( 156 testAccCheckStorageBucketExists( 157 "google_storage_bucket.bucket", bucketName, &bucket), 158 ), 159 }, 160 resource.TestStep{ 161 Config: testAccStorageBucket_customAttributes(bucketName), 162 Check: resource.ComposeTestCheckFunc( 163 testAccCheckStorageBucketPutItem(bucketName), 164 ), 165 }, 166 resource.TestStep{ 167 Config: testAccStorageBucket_customAttributes("idontexist"), 168 Check: resource.ComposeTestCheckFunc( 169 testAccCheckStorageBucketMissing(bucketName), 170 ), 171 }, 172 }, 173 }) 174 } 175 176 func testAccCheckStorageBucketExists(n string, bucketName string, bucket *storage.Bucket) resource.TestCheckFunc { 177 return func(s *terraform.State) error { 178 rs, ok := s.RootModule().Resources[n] 179 if !ok { 180 return fmt.Errorf("Not found: %s", n) 181 } 182 183 if rs.Primary.ID == "" { 184 return fmt.Errorf("No Project_ID is set") 185 } 186 187 config := testAccProvider.Meta().(*Config) 188 189 found, err := config.clientStorage.Buckets.Get(rs.Primary.ID).Do() 190 if err != nil { 191 return err 192 } 193 194 if found.Id != rs.Primary.ID { 195 return fmt.Errorf("Bucket not found") 196 } 197 198 if found.Name != bucketName { 199 return fmt.Errorf("expected name %s, got %s", bucketName, found.Name) 200 } 201 202 *bucket = *found 203 return nil 204 } 205 } 206 207 func testAccCheckStorageBucketPutItem(bucketName string) resource.TestCheckFunc { 208 return func(s *terraform.State) error { 209 config := testAccProvider.Meta().(*Config) 210 211 data := bytes.NewBufferString("test") 212 dataReader := bytes.NewReader(data.Bytes()) 213 object := &storage.Object{Name: "bucketDestroyTestFile"} 214 215 // This needs to use Media(io.Reader) call, otherwise it does not go to /upload API and fails 216 if res, err := config.clientStorage.Objects.Insert(bucketName, object).Media(dataReader).Do(); err == nil { 217 log.Printf("[INFO] Created object %v at location %v\n\n", res.Name, res.SelfLink) 218 } else { 219 return fmt.Errorf("Objects.Insert failed: %v", err) 220 } 221 222 return nil 223 } 224 } 225 226 func testAccCheckStorageBucketMissing(bucketName string) resource.TestCheckFunc { 227 return func(s *terraform.State) error { 228 config := testAccProvider.Meta().(*Config) 229 230 _, err := config.clientStorage.Buckets.Get(bucketName).Do() 231 if err == nil { 232 return fmt.Errorf("Found %s", bucketName) 233 } 234 235 if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { 236 return nil 237 } 238 239 return err 240 } 241 } 242 243 func testAccStorageBucketDestroy(s *terraform.State) error { 244 config := testAccProvider.Meta().(*Config) 245 246 for _, rs := range s.RootModule().Resources { 247 if rs.Type != "google_storage_bucket" { 248 continue 249 } 250 251 _, err := config.clientStorage.Buckets.Get(rs.Primary.ID).Do() 252 if err == nil { 253 return fmt.Errorf("Bucket still exists") 254 } 255 } 256 257 return nil 258 } 259 260 func testAccStorageBucket_basic(bucketName string) string { 261 return fmt.Sprintf(` 262 resource "google_storage_bucket" "bucket" { 263 name = "%s" 264 } 265 `, bucketName) 266 } 267 268 func testAccStorageBucket_customAttributes(bucketName string) string { 269 return fmt.Sprintf(` 270 resource "google_storage_bucket" "bucket" { 271 name = "%s" 272 predefined_acl = "publicReadWrite" 273 location = "EU" 274 force_destroy = "true" 275 } 276 `, bucketName) 277 } 278 279 func testAccStorageBucket_storageClass(bucketName, storageClass, location string) string { 280 var locationBlock string 281 if location != "" { 282 locationBlock = fmt.Sprintf(` 283 location = "%s"`, location) 284 } 285 return fmt.Sprintf(` 286 resource "google_storage_bucket" "bucket" { 287 name = "%s" 288 storage_class = "%s"%s 289 } 290 `, bucketName, storageClass, locationBlock) 291 }