Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bootstrap storage pool in tests #19399

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/11598.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:none

```
72 changes: 72 additions & 0 deletions google/acctest/bootstrap_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,78 @@ func BootstrapFirewallForDataprocSharedNetwork(t *testing.T, firewallName string
return firewall.Name
}

const SharedStoragePoolPrefix = "tf-bootstrap-storage-pool-"

func BootstrapComputeStoragePool(t *testing.T, storagePoolName, storagePoolType string) string {
projectID := envvar.GetTestProjectFromEnv()
zone := envvar.GetTestZoneFromEnv()

storagePoolName = SharedStoragePoolPrefix + storagePoolType + "-" + storagePoolName

config := BootstrapConfig(t)
if config == nil {
t.Fatal("Could not bootstrap config.")
}

computeService := config.NewComputeClient(config.UserAgent)
if computeService == nil {
t.Fatal("Could not create compute client.")
}

_, err := computeService.StoragePools.Get(projectID, zone, storagePoolName).Do()
if err != nil && transport_tpg.IsGoogleApiErrorWithCode(err, 404) {
log.Printf("[DEBUG] Storage pool %q not found, bootstrapping", storagePoolName)

url := fmt.Sprintf("%sprojects/%s/zones/%s/storagePools", config.ComputeBasePath, projectID, zone)
storagePoolTypeUrl := fmt.Sprintf("/projects/%s/zones/%s/storagePoolTypes/%s", projectID, zone, storagePoolType)

storagePoolObj := map[string]interface{}{
"name": storagePoolName,
"poolProvisionedCapacityGb": 10240,
"poolProvisionedThroughput": 180,
"storagePoolType": storagePoolTypeUrl,
"capacityProvisioningType": "ADVANCED",
}

if storagePoolType == "hyperdisk-balanced" {
storagePoolObj["poolProvisionedIops"] = 10000
storagePoolObj["poolProvisionedThroughput"] = 1024
}

res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "POST",
Project: projectID,
RawURL: url,
UserAgent: config.UserAgent,
Body: storagePoolObj,
Timeout: 20 * time.Minute,
})

log.Printf("Response is, %s", res)
if err != nil {
t.Fatalf("Error bootstrapping storage pool %s: %s", storagePoolName, err)
}

log.Printf("[DEBUG] Waiting for storage pool creation to finish")
err = tpgcompute.ComputeOperationWaitTime(config, res, projectID, "Error bootstrapping storage pool", config.UserAgent, 4*time.Minute)
if err != nil {
t.Fatalf("Error bootstrapping test storage pool %s: %s", storagePoolName, err)
}
}

storagePool, err := computeService.StoragePools.Get(projectID, zone, storagePoolName).Do()

if storagePool == nil {
t.Fatalf("Error getting storage pool %s: is nil", storagePoolName)
}

if err != nil {
t.Fatalf("Error getting storage pool %s: %s", storagePoolName, err)
}
return storagePool.SelfLink
}

func SetupProjectsAndGetAccessToken(org, billing, pid, service string, config *transport_tpg.Config) (string, error) {
// Create project-1 and project-2
rmService := config.NewResourceManagerClient(config.UserAgent)
Expand Down
82 changes: 2 additions & 80 deletions google/services/compute/resource_compute_disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ package compute_test

import (
"fmt"
"net/http"
"os"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-provider-google/google/acctest"
"github.com/hashicorp/terraform-provider-google/google/envvar"
tpgcompute "github.com/hashicorp/terraform-provider-google/google/services/compute"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"

"google.golang.org/api/compute/v1"
)
Expand Down Expand Up @@ -1419,24 +1416,17 @@ resource "google_compute_disk" "foobar" {
}

func TestAccComputeDisk_storagePoolSpecified(t *testing.T) {
// Currently failing
acctest.SkipIfVcr(t)
t.Parallel()

storagePoolName := fmt.Sprintf("tf-test-storage-pool-%s", acctest.RandString(t, 10))
storagePoolUrl := fmt.Sprintf("/projects/%s/zones/%s/storagePools/%s", envvar.GetTestProjectFromEnv(), envvar.GetTestZoneFromEnv(), storagePoolName)
storagePoolNameLong := acctest.BootstrapComputeStoragePool(t, "basic-1", "hyperdisk-throughput")
diskName := fmt.Sprintf("tf-test-disk-%s", acctest.RandString(t, 10))

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Steps: []resource.TestStep{
{
PreConfig: setupTestingStoragePool(t, storagePoolName),
Config: testAccComputeDisk_storagePoolSpecified(diskName, storagePoolUrl),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("google_compute_disk.foobar", "storage_pool", storagePoolName),
),
Config: testAccComputeDisk_storagePoolSpecified(diskName, storagePoolNameLong),
},
{
ResourceName: "google_compute_disk.foobar",
Expand All @@ -1445,74 +1435,6 @@ func TestAccComputeDisk_storagePoolSpecified(t *testing.T) {
},
},
})

cleanupTestingStoragePool(t, storagePoolName)
}

func setupTestingStoragePool(t *testing.T, storagePoolName string) func() {
return func() {
config := acctest.GoogleProviderConfig(t)
headers := make(http.Header)
project := envvar.GetTestProjectFromEnv()
zone := envvar.GetTestZoneFromEnv()
url := fmt.Sprintf("%sprojects/%s/zones/%s/storagePools", config.ComputeBasePath, project, zone)
storagePoolTypeUrl := fmt.Sprintf("/projects/%s/zones/%s/storagePoolTypes/hyperdisk-throughput", project, zone)
defaultTimeout := 20 * time.Minute
obj := make(map[string]interface{})
obj["name"] = storagePoolName
obj["poolProvisionedCapacityGb"] = 10240
obj["poolProvisionedThroughput"] = 180
obj["storagePoolType"] = storagePoolTypeUrl
obj["capacityProvisioningType"] = "ADVANCED"

res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "POST",
Project: project,
RawURL: url,
UserAgent: config.UserAgent,
Body: obj,
Timeout: defaultTimeout,
Headers: headers,
})
if err != nil {
t.Errorf("Error creating StoragePool: %s", err)
}

err = tpgcompute.ComputeOperationWaitTime(config, res, project, "Creating StoragePool", config.UserAgent, defaultTimeout)
if err != nil {
t.Errorf("Error waiting to create StoragePool: %s", err)
}
}
}

func cleanupTestingStoragePool(t *testing.T, storagePoolName string) {
config := acctest.GoogleProviderConfig(t)
headers := make(http.Header)
project := envvar.GetTestProjectFromEnv()
zone := envvar.GetTestZoneFromEnv()
url := fmt.Sprintf("%sprojects/%s/zones/%s/storagePools/%s", config.ComputeBasePath, project, zone, storagePoolName)
defaultTimeout := 20 * time.Minute
var obj map[string]interface{}

res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "DELETE",
Project: project,
RawURL: url,
UserAgent: config.UserAgent,
Body: obj,
Timeout: defaultTimeout,
Headers: headers,
})
if err != nil {
t.Errorf("Error deleting StoragePool: %s", err)
}

err = tpgcompute.ComputeOperationWaitTime(config, res, project, "Deleting StoragePool", config.UserAgent, defaultTimeout)
if err != nil {
t.Errorf("Error waiting to delete StoragePool: %s", err)
}
}

func testAccComputeDisk_storagePoolSpecified(diskName, storagePoolUrl string) string {
Expand Down
53 changes: 2 additions & 51 deletions google/services/compute/resource_compute_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package compute_test

import (
"fmt"
"net/http"
"reflect"
"regexp"
"sort"
Expand All @@ -20,7 +19,6 @@ import (
"github.com/hashicorp/terraform-provider-google/google/envvar"
tpgcompute "github.com/hashicorp/terraform-provider-google/google/services/compute"
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
"github.com/stretchr/testify/assert"

"google.golang.org/api/compute/v1"
Expand Down Expand Up @@ -8541,24 +8539,17 @@ resource "google_compute_instance" "foobar" {
}

func TestAccComputeInstance_bootDisk_storagePoolSpecified(t *testing.T) {
// Currently failing
acctest.SkipIfVcr(t)
t.Parallel()

instanceName := fmt.Sprintf("tf-test-instance-%s", acctest.RandString(t, 10))
storagePoolName := fmt.Sprintf("tf-test-storage-pool-%s", acctest.RandString(t, 10))
storagePoolUrl := fmt.Sprintf("/projects/%s/zones/%s/storagePools/%s", envvar.GetTestProjectFromEnv(), envvar.GetTestZoneFromEnv(), storagePoolName)
storagePoolNameLong := acctest.BootstrapComputeStoragePool(t, "basic-1", "hyperdisk-balanced")

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Steps: []resource.TestStep{
{
PreConfig: setupTestingStoragePool_HyperdiskBalanced(t, storagePoolName),
Config: testAccComputeInstance_bootDisk_storagePoolSpecified(instanceName, storagePoolUrl, envvar.GetTestZoneFromEnv()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("google_compute_instance.foobar", "boot_disk.0.initialize_params.0.storage_pool", storagePoolName),
),
Config: testAccComputeInstance_bootDisk_storagePoolSpecified(instanceName, storagePoolNameLong, envvar.GetTestZoneFromEnv()),
},
{
ResourceName: "google_compute_instance.foobar",
Expand All @@ -8567,46 +8558,6 @@ func TestAccComputeInstance_bootDisk_storagePoolSpecified(t *testing.T) {
},
},
})

cleanupTestingStoragePool(t, storagePoolName)
}

func setupTestingStoragePool_HyperdiskBalanced(t *testing.T, storagePoolName string) func() {
return func() {
config := acctest.GoogleProviderConfig(t)
headers := make(http.Header)
project := envvar.GetTestProjectFromEnv()
zone := envvar.GetTestZoneFromEnv()
url := fmt.Sprintf("%sprojects/%s/zones/%s/storagePools", config.ComputeBasePath, project, zone)
storagePoolTypeUrl := fmt.Sprintf("/projects/%s/zones/%s/storagePoolTypes/hyperdisk-balanced", project, zone)
defaultTimeout := 20 * time.Minute
obj := make(map[string]interface{})
obj["name"] = storagePoolName
obj["poolProvisionedCapacityGb"] = 10240
obj["poolProvisionedIops"] = 10000
obj["poolProvisionedThroughput"] = 1024
obj["storagePoolType"] = storagePoolTypeUrl
obj["capacityProvisioningType"] = "ADVANCED"

res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "POST",
Project: project,
RawURL: url,
UserAgent: config.UserAgent,
Body: obj,
Timeout: defaultTimeout,
Headers: headers,
})
if err != nil {
t.Errorf("Error creating StoragePool: %s", err)
}

err = tpgcompute.ComputeOperationWaitTime(config, res, project, "Creating StoragePool", config.UserAgent, defaultTimeout)
if err != nil {
t.Errorf("Error waiting to create StoragePool: %s", err)
}
}
}

func testAccComputeInstance_bootDisk_storagePoolSpecified(instanceName, storagePoolUrl, zone string) string {
Expand Down