provider/google: Refactor google_storage_bucket tests (#14694)
This commit is contained in:
parent
979ef66dbc
commit
6ac78404e0
|
@ -0,0 +1,30 @@
|
||||||
|
package google
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/terraform/helper/acctest"
|
||||||
|
"github.com/hashicorp/terraform/helper/resource"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAccStorageBucket_import(t *testing.T) {
|
||||||
|
bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", acctest.RandInt())
|
||||||
|
|
||||||
|
resource.Test(t, resource.TestCase{
|
||||||
|
PreCheck: func() { testAccPreCheck(t) },
|
||||||
|
Providers: testAccProviders,
|
||||||
|
CheckDestroy: testAccStorageBucketDestroy,
|
||||||
|
Steps: []resource.TestStep{
|
||||||
|
resource.TestStep{
|
||||||
|
Config: testAccStorageBucket_basic(bucketName),
|
||||||
|
},
|
||||||
|
resource.TestStep{
|
||||||
|
ResourceName: "google_storage_bucket.bucket",
|
||||||
|
ImportState: true,
|
||||||
|
ImportStateVerify: true,
|
||||||
|
ImportStateVerifyIgnore: []string{"force_destroy"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
|
@ -3,6 +3,7 @@ package google
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform/helper/acctest"
|
"github.com/hashicorp/terraform/helper/acctest"
|
||||||
|
@ -13,19 +14,20 @@ import (
|
||||||
storage "google.golang.org/api/storage/v1"
|
storage "google.golang.org/api/storage/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAccStorage_basic(t *testing.T) {
|
func TestAccStorageBucket_basic(t *testing.T) {
|
||||||
|
var bucket storage.Bucket
|
||||||
bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", acctest.RandInt())
|
bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", acctest.RandInt())
|
||||||
|
|
||||||
resource.Test(t, resource.TestCase{
|
resource.Test(t, resource.TestCase{
|
||||||
PreCheck: func() { testAccPreCheck(t) },
|
PreCheck: func() { testAccPreCheck(t) },
|
||||||
Providers: testAccProviders,
|
Providers: testAccProviders,
|
||||||
CheckDestroy: testAccGoogleStorageDestroy,
|
CheckDestroy: testAccStorageBucketDestroy,
|
||||||
Steps: []resource.TestStep{
|
Steps: []resource.TestStep{
|
||||||
resource.TestStep{
|
resource.TestStep{
|
||||||
Config: testGoogleStorageBucketsReaderDefaults(bucketName),
|
Config: testAccStorageBucket_basic(bucketName),
|
||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckCloudStorageBucketExists(
|
testAccCheckStorageBucketExists(
|
||||||
"google_storage_bucket.bucket", bucketName),
|
"google_storage_bucket.bucket", bucketName, &bucket),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"google_storage_bucket.bucket", "location", "US"),
|
"google_storage_bucket.bucket", "location", "US"),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
|
@ -36,19 +38,20 @@ func TestAccStorage_basic(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAccStorageCustomAttributes(t *testing.T) {
|
func TestAccStorageBucket_customAttributes(t *testing.T) {
|
||||||
|
var bucket storage.Bucket
|
||||||
bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", acctest.RandInt())
|
bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", acctest.RandInt())
|
||||||
|
|
||||||
resource.Test(t, resource.TestCase{
|
resource.Test(t, resource.TestCase{
|
||||||
PreCheck: func() { testAccPreCheck(t) },
|
PreCheck: func() { testAccPreCheck(t) },
|
||||||
Providers: testAccProviders,
|
Providers: testAccProviders,
|
||||||
CheckDestroy: testAccGoogleStorageDestroy,
|
CheckDestroy: testAccStorageBucketDestroy,
|
||||||
Steps: []resource.TestStep{
|
Steps: []resource.TestStep{
|
||||||
resource.TestStep{
|
resource.TestStep{
|
||||||
Config: testGoogleStorageBucketsReaderCustomAttributes(bucketName),
|
Config: testAccStorageBucket_customAttributes(bucketName),
|
||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckCloudStorageBucketExists(
|
testAccCheckStorageBucketExists(
|
||||||
"google_storage_bucket.bucket", bucketName),
|
"google_storage_bucket.bucket", bucketName, &bucket),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"google_storage_bucket.bucket", "location", "EU"),
|
"google_storage_bucket.bucket", "location", "EU"),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
|
@ -59,37 +62,38 @@ func TestAccStorageCustomAttributes(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAccStorageStorageClass(t *testing.T) {
|
func TestAccStorageBucket_storageClass(t *testing.T) {
|
||||||
|
var bucket storage.Bucket
|
||||||
bucketName := fmt.Sprintf("tf-test-acc-bucket-%d", acctest.RandInt())
|
bucketName := fmt.Sprintf("tf-test-acc-bucket-%d", acctest.RandInt())
|
||||||
|
|
||||||
resource.Test(t, resource.TestCase{
|
resource.Test(t, resource.TestCase{
|
||||||
PreCheck: func() { testAccPreCheck(t) },
|
PreCheck: func() { testAccPreCheck(t) },
|
||||||
Providers: testAccProviders,
|
Providers: testAccProviders,
|
||||||
CheckDestroy: testAccGoogleStorageDestroy,
|
CheckDestroy: testAccStorageBucketDestroy,
|
||||||
Steps: []resource.TestStep{
|
Steps: []resource.TestStep{
|
||||||
{
|
{
|
||||||
Config: testGoogleStorageBucketsReaderStorageClass(bucketName, "MULTI_REGIONAL", ""),
|
Config: testAccStorageBucket_storageClass(bucketName, "MULTI_REGIONAL", ""),
|
||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckCloudStorageBucketExists(
|
testAccCheckStorageBucketExists(
|
||||||
"google_storage_bucket.bucket", bucketName),
|
"google_storage_bucket.bucket", bucketName, &bucket),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"google_storage_bucket.bucket", "storage_class", "MULTI_REGIONAL"),
|
"google_storage_bucket.bucket", "storage_class", "MULTI_REGIONAL"),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Config: testGoogleStorageBucketsReaderStorageClass(bucketName, "NEARLINE", ""),
|
Config: testAccStorageBucket_storageClass(bucketName, "NEARLINE", ""),
|
||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckCloudStorageBucketExists(
|
testAccCheckStorageBucketExists(
|
||||||
"google_storage_bucket.bucket", bucketName),
|
"google_storage_bucket.bucket", bucketName, &bucket),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"google_storage_bucket.bucket", "storage_class", "NEARLINE"),
|
"google_storage_bucket.bucket", "storage_class", "NEARLINE"),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Config: testGoogleStorageBucketsReaderStorageClass(bucketName, "REGIONAL", "US-CENTRAL1"),
|
Config: testAccStorageBucket_storageClass(bucketName, "REGIONAL", "US-CENTRAL1"),
|
||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckCloudStorageBucketExists(
|
testAccCheckStorageBucketExists(
|
||||||
"google_storage_bucket.bucket", bucketName),
|
"google_storage_bucket.bucket", bucketName, &bucket),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"google_storage_bucket.bucket", "storage_class", "REGIONAL"),
|
"google_storage_bucket.bucket", "storage_class", "REGIONAL"),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
|
@ -100,19 +104,20 @@ func TestAccStorageStorageClass(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAccStorageBucketUpdate(t *testing.T) {
|
func TestAccStorageBucket_update(t *testing.T) {
|
||||||
|
var bucket storage.Bucket
|
||||||
bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", acctest.RandInt())
|
bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", acctest.RandInt())
|
||||||
|
|
||||||
resource.Test(t, resource.TestCase{
|
resource.Test(t, resource.TestCase{
|
||||||
PreCheck: func() { testAccPreCheck(t) },
|
PreCheck: func() { testAccPreCheck(t) },
|
||||||
Providers: testAccProviders,
|
Providers: testAccProviders,
|
||||||
CheckDestroy: testAccGoogleStorageDestroy,
|
CheckDestroy: testAccStorageBucketDestroy,
|
||||||
Steps: []resource.TestStep{
|
Steps: []resource.TestStep{
|
||||||
resource.TestStep{
|
resource.TestStep{
|
||||||
Config: testGoogleStorageBucketsReaderDefaults(bucketName),
|
Config: testAccStorageBucket_basic(bucketName),
|
||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckCloudStorageBucketExists(
|
testAccCheckStorageBucketExists(
|
||||||
"google_storage_bucket.bucket", bucketName),
|
"google_storage_bucket.bucket", bucketName, &bucket),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"google_storage_bucket.bucket", "location", "US"),
|
"google_storage_bucket.bucket", "location", "US"),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
|
@ -120,10 +125,10 @@ func TestAccStorageBucketUpdate(t *testing.T) {
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
resource.TestStep{
|
resource.TestStep{
|
||||||
Config: testGoogleStorageBucketsReaderCustomAttributes(bucketName),
|
Config: testAccStorageBucket_customAttributes(bucketName),
|
||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckCloudStorageBucketExists(
|
testAccCheckStorageBucketExists(
|
||||||
"google_storage_bucket.bucket", bucketName),
|
"google_storage_bucket.bucket", bucketName, &bucket),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
"google_storage_bucket.bucket", "predefined_acl", "publicReadWrite"),
|
"google_storage_bucket.bucket", "predefined_acl", "publicReadWrite"),
|
||||||
resource.TestCheckResourceAttr(
|
resource.TestCheckResourceAttr(
|
||||||
|
@ -136,59 +141,39 @@ func TestAccStorageBucketUpdate(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAccStorageBucketImport(t *testing.T) {
|
func TestAccStorageBucket_forceDestroy(t *testing.T) {
|
||||||
|
var bucket storage.Bucket
|
||||||
bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", acctest.RandInt())
|
bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", acctest.RandInt())
|
||||||
|
|
||||||
resource.Test(t, resource.TestCase{
|
resource.Test(t, resource.TestCase{
|
||||||
PreCheck: func() { testAccPreCheck(t) },
|
PreCheck: func() { testAccPreCheck(t) },
|
||||||
Providers: testAccProviders,
|
Providers: testAccProviders,
|
||||||
CheckDestroy: testAccGoogleStorageDestroy,
|
CheckDestroy: testAccStorageBucketDestroy,
|
||||||
Steps: []resource.TestStep{
|
Steps: []resource.TestStep{
|
||||||
resource.TestStep{
|
resource.TestStep{
|
||||||
Config: testGoogleStorageBucketsReaderDefaults(bucketName),
|
Config: testAccStorageBucket_customAttributes(bucketName),
|
||||||
},
|
|
||||||
resource.TestStep{
|
|
||||||
ResourceName: "google_storage_bucket.bucket",
|
|
||||||
ImportState: true,
|
|
||||||
ImportStateVerify: true,
|
|
||||||
ImportStateVerifyIgnore: []string{"force_destroy"},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAccStorageForceDestroy(t *testing.T) {
|
|
||||||
bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", acctest.RandInt())
|
|
||||||
|
|
||||||
resource.Test(t, resource.TestCase{
|
|
||||||
PreCheck: func() { testAccPreCheck(t) },
|
|
||||||
Providers: testAccProviders,
|
|
||||||
CheckDestroy: testAccGoogleStorageDestroy,
|
|
||||||
Steps: []resource.TestStep{
|
|
||||||
resource.TestStep{
|
|
||||||
Config: testGoogleStorageBucketsReaderCustomAttributes(bucketName),
|
|
||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckCloudStorageBucketExists(
|
testAccCheckStorageBucketExists(
|
||||||
"google_storage_bucket.bucket", bucketName),
|
"google_storage_bucket.bucket", bucketName, &bucket),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
resource.TestStep{
|
resource.TestStep{
|
||||||
Config: testGoogleStorageBucketsReaderCustomAttributes(bucketName),
|
Config: testAccStorageBucket_customAttributes(bucketName),
|
||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckCloudStorageBucketPutItem(bucketName),
|
testAccCheckStorageBucketPutItem(bucketName),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
resource.TestStep{
|
resource.TestStep{
|
||||||
Config: testGoogleStorageBucketsReaderCustomAttributes("idontexist"),
|
Config: testAccStorageBucket_customAttributes("idontexist"),
|
||||||
Check: resource.ComposeTestCheckFunc(
|
Check: resource.ComposeTestCheckFunc(
|
||||||
testAccCheckCloudStorageBucketMissing(bucketName),
|
testAccCheckStorageBucketMissing(bucketName),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func testAccCheckCloudStorageBucketExists(n string, bucketName string) resource.TestCheckFunc {
|
func testAccCheckStorageBucketExists(n string, bucketName string, bucket *storage.Bucket) resource.TestCheckFunc {
|
||||||
return func(s *terraform.State) error {
|
return func(s *terraform.State) error {
|
||||||
rs, ok := s.RootModule().Resources[n]
|
rs, ok := s.RootModule().Resources[n]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -213,11 +198,13 @@ func testAccCheckCloudStorageBucketExists(n string, bucketName string) resource.
|
||||||
if found.Name != bucketName {
|
if found.Name != bucketName {
|
||||||
return fmt.Errorf("expected name %s, got %s", bucketName, found.Name)
|
return fmt.Errorf("expected name %s, got %s", bucketName, found.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*bucket = *found
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testAccCheckCloudStorageBucketPutItem(bucketName string) resource.TestCheckFunc {
|
func testAccCheckStorageBucketPutItem(bucketName string) resource.TestCheckFunc {
|
||||||
return func(s *terraform.State) error {
|
return func(s *terraform.State) error {
|
||||||
config := testAccProvider.Meta().(*Config)
|
config := testAccProvider.Meta().(*Config)
|
||||||
|
|
||||||
|
@ -227,7 +214,7 @@ func testAccCheckCloudStorageBucketPutItem(bucketName string) resource.TestCheck
|
||||||
|
|
||||||
// This needs to use Media(io.Reader) call, otherwise it does not go to /upload API and fails
|
// This needs to use Media(io.Reader) call, otherwise it does not go to /upload API and fails
|
||||||
if res, err := config.clientStorage.Objects.Insert(bucketName, object).Media(dataReader).Do(); err == nil {
|
if res, err := config.clientStorage.Objects.Insert(bucketName, object).Media(dataReader).Do(); err == nil {
|
||||||
fmt.Printf("Created object %v at location %v\n\n", res.Name, res.SelfLink)
|
log.Printf("[INFO] Created object %v at location %v\n\n", res.Name, res.SelfLink)
|
||||||
} else {
|
} else {
|
||||||
return fmt.Errorf("Objects.Insert failed: %v", err)
|
return fmt.Errorf("Objects.Insert failed: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -236,7 +223,7 @@ func testAccCheckCloudStorageBucketPutItem(bucketName string) resource.TestCheck
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testAccCheckCloudStorageBucketMissing(bucketName string) resource.TestCheckFunc {
|
func testAccCheckStorageBucketMissing(bucketName string) resource.TestCheckFunc {
|
||||||
return func(s *terraform.State) error {
|
return func(s *terraform.State) error {
|
||||||
config := testAccProvider.Meta().(*Config)
|
config := testAccProvider.Meta().(*Config)
|
||||||
|
|
||||||
|
@ -253,7 +240,7 @@ func testAccCheckCloudStorageBucketMissing(bucketName string) resource.TestCheck
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testAccGoogleStorageDestroy(s *terraform.State) error {
|
func testAccStorageBucketDestroy(s *terraform.State) error {
|
||||||
config := testAccProvider.Meta().(*Config)
|
config := testAccProvider.Meta().(*Config)
|
||||||
|
|
||||||
for _, rs := range s.RootModule().Resources {
|
for _, rs := range s.RootModule().Resources {
|
||||||
|
@ -270,7 +257,7 @@ func testAccGoogleStorageDestroy(s *terraform.State) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func testGoogleStorageBucketsReaderDefaults(bucketName string) string {
|
func testAccStorageBucket_basic(bucketName string) string {
|
||||||
return fmt.Sprintf(`
|
return fmt.Sprintf(`
|
||||||
resource "google_storage_bucket" "bucket" {
|
resource "google_storage_bucket" "bucket" {
|
||||||
name = "%s"
|
name = "%s"
|
||||||
|
@ -278,7 +265,7 @@ resource "google_storage_bucket" "bucket" {
|
||||||
`, bucketName)
|
`, bucketName)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testGoogleStorageBucketsReaderCustomAttributes(bucketName string) string {
|
func testAccStorageBucket_customAttributes(bucketName string) string {
|
||||||
return fmt.Sprintf(`
|
return fmt.Sprintf(`
|
||||||
resource "google_storage_bucket" "bucket" {
|
resource "google_storage_bucket" "bucket" {
|
||||||
name = "%s"
|
name = "%s"
|
||||||
|
@ -289,7 +276,7 @@ resource "google_storage_bucket" "bucket" {
|
||||||
`, bucketName)
|
`, bucketName)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testGoogleStorageBucketsReaderStorageClass(bucketName, storageClass, location string) string {
|
func testAccStorageBucket_storageClass(bucketName, storageClass, location string) string {
|
||||||
var locationBlock string
|
var locationBlock string
|
||||||
if location != "" {
|
if location != "" {
|
||||||
locationBlock = fmt.Sprintf(`
|
locationBlock = fmt.Sprintf(`
|
||||||
|
|
Loading…
Reference in New Issue