From 55c09833ef8b763541a99782d3ee5b6b84876804 Mon Sep 17 00:00:00 2001 From: Will Yardley Date: Thu, 26 Sep 2024 12:17:51 -0700 Subject: [PATCH] container: fix storage pool resource names in test (#11832) --- .../terraform/acctest/bootstrap_test_utils.go | 9 +++++++- .../resource_container_cluster_test.go.tmpl | 23 ++----------------- .../resource_container_node_pool_test.go.tmpl | 21 ++++++----------- 3 files changed, 17 insertions(+), 36 deletions(-) diff --git a/mmv1/third_party/terraform/acctest/bootstrap_test_utils.go b/mmv1/third_party/terraform/acctest/bootstrap_test_utils.go index 807dea324306..17b15a699a63 100644 --- a/mmv1/third_party/terraform/acctest/bootstrap_test_utils.go +++ b/mmv1/third_party/terraform/acctest/bootstrap_test_utils.go @@ -1176,7 +1176,14 @@ func BootstrapComputeStoragePool(t *testing.T, storagePoolName, storagePoolType if err != nil { t.Fatalf("Error getting storage pool %s: %s", storagePoolName, err) } - return storagePool.SelfLink + + storagePoolResourceName, err := tpgresource.GetRelativePath(storagePool.SelfLink) + + if err != nil { + t.Fatal("Failed to extract Storage Pool resource name from URL.") + } + + return storagePoolResourceName } func SetupProjectsAndGetAccessToken(org, billing, pid, service string, config *transport_tpg.Config) (string, error) { diff --git a/mmv1/third_party/terraform/services/container/resource_container_cluster_test.go.tmpl b/mmv1/third_party/terraform/services/container/resource_container_cluster_test.go.tmpl index 4447c068b202..39ec4e0dc42b 100644 --- a/mmv1/third_party/terraform/services/container/resource_container_cluster_test.go.tmpl +++ b/mmv1/third_party/terraform/services/container/resource_container_cluster_test.go.tmpl @@ -11697,11 +11697,7 @@ func TestAccContainerCluster_storagePoolsWithNodePool(t *testing.T) { subnetworkName := acctest.BootstrapSubnet(t, "gke-cluster", networkName) location := envvar.GetTestZoneFromEnv() - storagePoolNameURL := acctest.BootstrapComputeStoragePool(t, "basic-1", "hyperdisk-balanced") - storagePoolResourceName, err := extractSPName(storagePoolNameURL) - if err != nil { - t.Fatal("Failed to extract Storage Pool resource name from URL.") - } + storagePoolResourceName := acctest.BootstrapComputeStoragePool(t, "basic-1", "hyperdisk-balanced") acctest.VcrTest(t, resource.TestCase{ PreCheck: func() { acctest.AccTestPreCheck(t) }, @@ -11754,11 +11750,7 @@ func TestAccContainerCluster_storagePoolsWithNodeConfig(t *testing.T) { subnetworkName := acctest.BootstrapSubnet(t, "gke-cluster", networkName) location := envvar.GetTestZoneFromEnv() - storagePoolNameURL := acctest.BootstrapComputeStoragePool(t, "basic-1", "hyperdisk-balanced") - storagePoolResourceName, err := extractSPName(storagePoolNameURL) - if err != nil { - t.Fatal("Failed to extract Storage Pool resource name from URL.") - } + storagePoolResourceName := acctest.BootstrapComputeStoragePool(t, "basic-1", "hyperdisk-balanced") acctest.VcrTest(t, resource.TestCase{ PreCheck: func() { acctest.AccTestPreCheck(t) }, @@ -11800,17 +11792,6 @@ resource "google_container_cluster" "storage_pools_with_node_config" { `, cluster, location, networkName, subnetworkName, storagePoolResourceName) } -func extractSPName(url string) (string, error) { - re := regexp.MustCompile(`https://www\.googleapis\.com/compute/beta/(projects/[^"]+)`) - matches := re.FindStringSubmatch(url) - - if len(matches) > 1 { - return matches[1], nil - } else { - return "", fmt.Errorf("no match found") - } -} - func TestAccContainerCluster_withAutopilotGcpFilestoreCsiDriver(t *testing.T) { t.Parallel() diff --git a/mmv1/third_party/terraform/services/container/resource_container_node_pool_test.go.tmpl b/mmv1/third_party/terraform/services/container/resource_container_node_pool_test.go.tmpl index a066953a01d7..31345acb5ace 100644 --- a/mmv1/third_party/terraform/services/container/resource_container_node_pool_test.go.tmpl +++ b/mmv1/third_party/terraform/services/container/resource_container_node_pool_test.go.tmpl @@ -4960,11 +4960,7 @@ func TestAccContainerNodePool_storagePools(t *testing.T) { subnetworkName := acctest.BootstrapSubnet(t, "gke-cluster", networkName) location := envvar.GetTestZoneFromEnv() - storagePoolNameURL := acctest.BootstrapComputeStoragePool(t, "basic-1", "hyperdisk-balanced") - storagePoolResourceName, err := extractSPName(storagePoolNameURL) - if err != nil { - t.Fatal("Failed to extract Storage Pool resource name from URL.") - } + storagePoolResourceName := acctest.BootstrapComputeStoragePool(t, "basic-1", "hyperdisk-balanced") acctest.VcrTest(t, resource.TestCase{ PreCheck: func() { acctest.AccTestPreCheck(t) }, @@ -5024,11 +5020,8 @@ func TestAccContainerNodePool_withMachineDiskStoragePoolsUpdate(t *testing.T) { subnetworkName := acctest.BootstrapSubnet(t, "gke-cluster", networkName) location := envvar.GetTestZoneFromEnv() - storagePoolNameURL := acctest.BootstrapComputeStoragePool(t, "basic-1", "hyperdisk-balanced") - storagePoolResourceName, err := extractSPName(storagePoolNameURL) - if err != nil { - t.Fatal("Failed to extract Storage Pool resource name from URL.") - } + storagePoolResourceName := acctest.BootstrapComputeStoragePool(t, "basic-1", "hyperdisk-balanced") + acctest.VcrTest(t, resource.TestCase{ PreCheck: func() { acctest.AccTestPreCheck(t) }, ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), @@ -5084,10 +5077,10 @@ resource "google_container_node_pool" "np" { initial_node_count = 2 node_config { - machine_type = "c3-standard-4" - disk_size_gb = 50 - disk_type = "hyperdisk-balanced" - storage_pools = ["%[5]s"] + machine_type = "c3-standard-4" + disk_size_gb = 50 + disk_type = "hyperdisk-balanced" + storage_pools = ["%[5]s"] } } `, cluster, np, networkName, subnetworkName, storagePoolResourceName, location)