Skip to content

Commit 9c7b6dd

Browse files
authored
Documented how to create new test projects (#14140)
1 parent f55da64 commit 9c7b6dd

File tree

1 file changed

+67
-1
lines changed

1 file changed

+67
-1
lines changed

docs/content/test/test.md

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ An update test is an **acceptance test** that creates the target resource and th
277277
{{< /tab >}}
278278
{{< /tabs >}}
279279

280-
## Bootstrapping API resources {#bootstrapping}
280+
## Bootstrap API resources {#bootstrapping}
281281

282282
Most acceptance tests run in a the default org and default test project, which means that they can conflict for quota, resource namespaces, and control over shared resources. You can work around these limitations with "bootstrapped" resources.
283283

@@ -445,6 +445,72 @@ func TestAccProductResource_update(t *testing.T) {
445445
{{< /tab >}}
446446
{{< /tabs >}}
447447

448+
## Create test projects
449+
If [bootstrapping]({{< ref "#bootstrapping" >}}) doesn't work or isn't an option for some reason, you can also work around project quota issues or test project-global resources by creating a new test project. You will also need to enable any necessary APIs and wait for their enablement to propagate.
450+
451+
```go
452+
import (
453+
"testing"
454+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest"
455+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/envvar"
456+
)
457+
func TestAccProductResourceName_update(t *testing.T) {
458+
t.Parallel()
459+
460+
context := map[string]interface{}{
461+
"random_suffix": acctest.RandString(t, 10),
462+
"billing_account": envvar.GetTestBillingAccountFromEnv(t),
463+
"org_id": envvar.GetTestOrgFromEnv(t),
464+
}
465+
acctest.VcrTest(t, resource.TestCase{
466+
// ...
467+
Steps: []resource.TestStep{
468+
{
469+
testAccProductResourceName_update1(context),
470+
},
471+
// ...
472+
},
473+
})
474+
}
475+
476+
func testAccProductResourceName_update1(context map[string]interface{}) string {
477+
return accest.Nprintf(`
478+
// Set up a test project
479+
resource "google_project" "project" {
480+
project_id = "tf-test%{random_suffix}"
481+
name = "tf-test%{random_suffix}"
482+
org_id = "%{org_id}"
483+
billing_account = "%{billing_account}"
484+
deletion_policy = "DELETE"
485+
}
486+
487+
// Enable APIs in a deterministic order to avoid inconsistent VCR recordings
488+
resource "google_project_service" "servicenetworking" {
489+
project = google_project.project.project_id
490+
service = "servicenetworking.googleapis.com"
491+
}
492+
493+
resource "google_project_service" "compute" {
494+
project = google_project.project.project_id
495+
service = "compute.googleapis.com"
496+
depends_on = [google_project_service.servicenetworking]
497+
}
498+
499+
// wait for API enablement
500+
resource "time_sleep" "wait_120_seconds" {
501+
create_duration = "120s"
502+
depends_on = [google_project_service.compute]
503+
}
504+
505+
resource "google_product_resource" "example" {
506+
// ...
507+
depends_on = [time_sleep.wait_120_seconds]
508+
}
509+
510+
`, context)
511+
}
512+
```
513+
448514
## Skip tests in VCR replaying mode {#skip-vcr}
449515

450516
Acceptance tests are run in VCR replaying mode on PRs (using pre-recorded HTTP requests and responses) to reduce the time it takes to present results to contributors. However, not all resources or tests are possible to run in replaying mode. Incompatible tests should be skipped during VCR replaying mode. They will still run in our nightly test suite.

0 commit comments

Comments
 (0)