Skip to content

Commit

Permalink
updated test case to tf config
Browse files Browse the repository at this point in the history
  • Loading branch information
gurusai-voleti committed Dec 18, 2024
1 parent 28b0e37 commit ce8deca
Showing 1 changed file with 43 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,20 @@ func TestAccStorageFolder_FailDeleteNonEmptyFolder(t *testing.T) {
t.Parallel()

bucketName := acctest.TestBucketName(t)
folderName := "folder/"

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Steps: []resource.TestStep{
{
Config: testAccStorageFolder_storageBucket(bucketName, true, true) + testAccStorageFolder_storageOneFolder(false),
Check: resource.ComposeTestCheckFunc(
testAccStorageCreatSubFolder(t, bucketName, folderName),
testAccStorageDeleteFolder(t, bucketName, folderName),
),
ExpectError: regexp.MustCompile("googleapi: Error 409: The folder you tried to delete is not empty"),
Config: testAccStorageFolder_storageBucketAndFolders(bucketName, false, false),
},
{
Config: testAccStorageFolder_storageBucket(bucketName, true, true) + testAccStorageFolder_storageOneFolder(true),
Config: testAccStorageFolder_removeParentFolder(bucketName),
ExpectError: regexp.MustCompile("use force_destroy to true to delete all subfolders"),
},
{
Config: testAccStorageFolder_storageBucketAndFolders(bucketName, true, true),
},
},
})
Expand Down Expand Up @@ -191,32 +189,45 @@ resource "google_storage_folder" "folder" {
`, forceDestroy)
}

func testAccStorageCreatSubFolder(t *testing.T, bucketName, parentFolder string) resource.TestCheckFunc {
return func(s *terraform.State) error {
config := acctest.GoogleProviderConfig(t)
subFolder := &storage.Folder{
Name: parentFolder + "subfolder/",
}
if res, err := config.NewStorageClient(config.UserAgent).Folders.Insert(bucketName, subFolder).Do(); err == nil {
log.Printf("sub folder created: %s", res.Name)
} else {
log.Printf("failed to create sub folder: %s", subFolder.Name)
}
return nil
}
func testAccStorageFolder_storageBucketAndFolders(bucketName string, forceDestroy bool, subFolderForceDestroy bool) string {
return fmt.Sprintf(`
resource "google_storage_bucket" "bucket" {
name = "%s"
location = "EU"
uniform_bucket_level_access = true
hierarchical_namespace {
enabled = "true"
}
}
resource "google_storage_folder" "folder" {
bucket = google_storage_bucket.bucket.name
name = "folder/"
force_destroy = %t
}
resource "google_storage_folder" "subfolder" {
bucket = google_storage_bucket.bucket.name
name = "${google_storage_folder.folder.name}subfolder/"
force_destroy = %t
}
`, bucketName, forceDestroy, subFolderForceDestroy)
}

func testAccStorageDeleteFolder(t *testing.T, bucketName, parentFolder string) resource.TestCheckFunc {
return func(s *terraform.State) error {
config := acctest.GoogleProviderConfig(t)
var deleteError error
if err := config.NewStorageClient(config.UserAgent).Folders.Delete(bucketName, parentFolder).Do(); err == nil {
log.Printf("successfully deleted folder: %s", err)
} else {
deleteError = fmt.Errorf("failed to deleted folder: %s", err)
}
return deleteError
}
func testAccStorageFolder_removeParentFolder(bucketName string) string {
return fmt.Sprintf(`
resource "google_storage_bucket" "bucket" {
name = "%s"
location = "EU"
uniform_bucket_level_access = true
hierarchical_namespace {
enabled = "true"
}
}
resource "google_storage_folder" "subfolder" {
bucket = google_storage_bucket.bucket.name
name = "folder/subfolder/"
force_destroy = false
}
`, bucketName, hnsFlag)
}

func testAccCheckStorageBucketUploadItem(t *testing.T, bucketName string) resource.TestCheckFunc {
Expand Down

0 comments on commit ce8deca

Please sign in to comment.