mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-24 23:46:26 -06:00
e916bd1527
Generate bucket names and object names per test instead of once at the top level. Should help avoid failures like this one: https://travis-ci.org/hashicorp/terraform/jobs/100254008 All storage tests checked on this commit: ``` TF_ACC=1 go test -v ./builtin/providers/google -run TestAccGoogleStorage === RUN TestAccGoogleStorageBucketAcl_basic --- PASS: TestAccGoogleStorageBucketAcl_basic (8.90s) === RUN TestAccGoogleStorageBucketAcl_upgrade --- PASS: TestAccGoogleStorageBucketAcl_upgrade (14.18s) === RUN TestAccGoogleStorageBucketAcl_downgrade --- PASS: TestAccGoogleStorageBucketAcl_downgrade (12.83s) === RUN TestAccGoogleStorageBucketAcl_predefined --- PASS: TestAccGoogleStorageBucketAcl_predefined (4.51s) === RUN TestAccGoogleStorageObject_basic --- PASS: TestAccGoogleStorageObject_basic (3.77s) === RUN TestAccGoogleStorageObjectAcl_basic --- PASS: TestAccGoogleStorageObjectAcl_basic (4.85s) === RUN TestAccGoogleStorageObjectAcl_upgrade --- PASS: TestAccGoogleStorageObjectAcl_upgrade (7.68s) === RUN TestAccGoogleStorageObjectAcl_downgrade --- PASS: TestAccGoogleStorageObjectAcl_downgrade (7.37s) === RUN TestAccGoogleStorageObjectAcl_predefined --- PASS: TestAccGoogleStorageObjectAcl_predefined (4.16s) PASS ok github.com/hashicorp/terraform/builtin/providers/google 68.275s ```
246 lines
7.0 KiB
Go
246 lines
7.0 KiB
Go
package google
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
//"google.golang.org/api/storage/v1"
|
|
)
|
|
|
|
var roleEntityBasic1 = "OWNER:user-omeemail@gmail.com"
|
|
|
|
var roleEntityBasic2 = "READER:user-anotheremail@gmail.com"
|
|
|
|
var roleEntityBasic3_owner = "OWNER:user-yetanotheremail@gmail.com"
|
|
|
|
var roleEntityBasic3_reader = "READER:user-yetanotheremail@gmail.com"
|
|
|
|
func testAclBucketName() string {
|
|
return fmt.Sprintf("%s-%d", "tf-test-acl-bucket", acctest.RandInt())
|
|
}
|
|
|
|
func TestAccGoogleStorageBucketAcl_basic(t *testing.T) {
|
|
bucketName := testAclBucketName()
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testAccGoogleStorageBucketAclDestroy,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: testGoogleStorageBucketsAclBasic1(bucketName),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckGoogleStorageBucketAcl(bucketName, roleEntityBasic1),
|
|
testAccCheckGoogleStorageBucketAcl(bucketName, roleEntityBasic2),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccGoogleStorageBucketAcl_upgrade(t *testing.T) {
|
|
bucketName := testAclBucketName()
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testAccGoogleStorageBucketAclDestroy,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: testGoogleStorageBucketsAclBasic1(bucketName),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckGoogleStorageBucketAcl(bucketName, roleEntityBasic1),
|
|
testAccCheckGoogleStorageBucketAcl(bucketName, roleEntityBasic2),
|
|
),
|
|
},
|
|
|
|
resource.TestStep{
|
|
Config: testGoogleStorageBucketsAclBasic2(bucketName),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckGoogleStorageBucketAcl(bucketName, roleEntityBasic2),
|
|
testAccCheckGoogleStorageBucketAcl(bucketName, roleEntityBasic3_owner),
|
|
),
|
|
},
|
|
|
|
resource.TestStep{
|
|
Config: testGoogleStorageBucketsAclBasicDelete(bucketName),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckGoogleStorageBucketAclDelete(bucketName, roleEntityBasic1),
|
|
testAccCheckGoogleStorageBucketAclDelete(bucketName, roleEntityBasic2),
|
|
testAccCheckGoogleStorageBucketAclDelete(bucketName, roleEntityBasic3_owner),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccGoogleStorageBucketAcl_downgrade(t *testing.T) {
|
|
bucketName := testAclBucketName()
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testAccGoogleStorageBucketAclDestroy,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: testGoogleStorageBucketsAclBasic2(bucketName),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckGoogleStorageBucketAcl(bucketName, roleEntityBasic2),
|
|
testAccCheckGoogleStorageBucketAcl(bucketName, roleEntityBasic3_owner),
|
|
),
|
|
},
|
|
|
|
resource.TestStep{
|
|
Config: testGoogleStorageBucketsAclBasic3(bucketName),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckGoogleStorageBucketAcl(bucketName, roleEntityBasic2),
|
|
testAccCheckGoogleStorageBucketAcl(bucketName, roleEntityBasic3_reader),
|
|
),
|
|
},
|
|
|
|
resource.TestStep{
|
|
Config: testGoogleStorageBucketsAclBasicDelete(bucketName),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
testAccCheckGoogleStorageBucketAclDelete(bucketName, roleEntityBasic1),
|
|
testAccCheckGoogleStorageBucketAclDelete(bucketName, roleEntityBasic2),
|
|
testAccCheckGoogleStorageBucketAclDelete(bucketName, roleEntityBasic3_owner),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccGoogleStorageBucketAcl_predefined(t *testing.T) {
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
Providers: testAccProviders,
|
|
CheckDestroy: testAccGoogleStorageBucketAclDestroy,
|
|
Steps: []resource.TestStep{
|
|
resource.TestStep{
|
|
Config: testGoogleStorageBucketsAclPredefined(bucketName),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func testAccCheckGoogleStorageBucketAclDelete(bucket, roleEntityS string) resource.TestCheckFunc {
|
|
return func(s *terraform.State) error {
|
|
roleEntity, _ := getRoleEntityPair(roleEntityS)
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
_, err := config.clientStorage.BucketAccessControls.Get(bucket, roleEntity.Entity).Do()
|
|
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
return fmt.Errorf("Error, entity %s still exists", roleEntity.Entity)
|
|
}
|
|
}
|
|
|
|
func testAccCheckGoogleStorageBucketAcl(bucket, roleEntityS string) resource.TestCheckFunc {
|
|
return func(s *terraform.State) error {
|
|
roleEntity, _ := getRoleEntityPair(roleEntityS)
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
res, err := config.clientStorage.BucketAccessControls.Get(bucket, roleEntity.Entity).Do()
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("Error retrieving contents of acl for bucket %s: %s", bucket, err)
|
|
}
|
|
|
|
if res.Role != roleEntity.Role {
|
|
return fmt.Errorf("Error, Role mismatch %s != %s", res.Role, roleEntity.Role)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func testAccGoogleStorageBucketAclDestroy(s *terraform.State) error {
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
if rs.Type != "google_storage_bucket_acl" {
|
|
continue
|
|
}
|
|
|
|
bucket := rs.Primary.Attributes["bucket"]
|
|
|
|
_, err := config.clientStorage.BucketAccessControls.List(bucket).Do()
|
|
|
|
if err == nil {
|
|
return fmt.Errorf("Acl for bucket %s still exists", bucket)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func testGoogleStorageBucketsAclBasic1(bucketName string) string {
|
|
return fmt.Sprintf(`
|
|
resource "google_storage_bucket" "bucket" {
|
|
name = "%s"
|
|
}
|
|
|
|
resource "google_storage_bucket_acl" "acl" {
|
|
bucket = "${google_storage_bucket.bucket.name}"
|
|
role_entity = ["%s", "%s"]
|
|
}
|
|
`, bucketName, roleEntityBasic1, roleEntityBasic2)
|
|
}
|
|
|
|
func testGoogleStorageBucketsAclBasic2(bucketName string) string {
|
|
return fmt.Sprintf(`
|
|
resource "google_storage_bucket" "bucket" {
|
|
name = "%s"
|
|
}
|
|
|
|
resource "google_storage_bucket_acl" "acl" {
|
|
bucket = "${google_storage_bucket.bucket.name}"
|
|
role_entity = ["%s", "%s"]
|
|
}
|
|
`, bucketName, roleEntityBasic2, roleEntityBasic3_owner)
|
|
}
|
|
|
|
func testGoogleStorageBucketsAclBasicDelete(bucketName string) string {
|
|
return fmt.Sprintf(`
|
|
resource "google_storage_bucket" "bucket" {
|
|
name = "%s"
|
|
}
|
|
|
|
resource "google_storage_bucket_acl" "acl" {
|
|
bucket = "${google_storage_bucket.bucket.name}"
|
|
role_entity = []
|
|
}
|
|
`, bucketName)
|
|
}
|
|
|
|
func testGoogleStorageBucketsAclBasic3(bucketName string) string {
|
|
return fmt.Sprintf(`
|
|
resource "google_storage_bucket" "bucket" {
|
|
name = "%s"
|
|
}
|
|
|
|
resource "google_storage_bucket_acl" "acl" {
|
|
bucket = "${google_storage_bucket.bucket.name}"
|
|
role_entity = ["%s", "%s"]
|
|
}
|
|
`, bucketName, roleEntityBasic2, roleEntityBasic3_reader)
|
|
}
|
|
|
|
func testGoogleStorageBucketsAclPredefined(bucketName string) string {
|
|
return fmt.Sprintf(`
|
|
resource "google_storage_bucket" "bucket" {
|
|
name = "%s"
|
|
}
|
|
|
|
resource "google_storage_bucket_acl" "acl" {
|
|
bucket = "${google_storage_bucket.bucket.name}"
|
|
predefined_acl = "projectPrivate"
|
|
default_acl = "projectPrivate"
|
|
}
|
|
`, bucketName)
|
|
}
|