Repository: GoogleCloudPlatform/terraform-validator
Pull request #1426 (2023-03-03 21:48:58)

Make some test utility functions public
<!-- Put a description of what this PR is for here, along with any references to issues that this resolves or contributes to -->

Make some test utility functions public
* bootstrappedKMS
* getTestProjectFromEnv
* getTestOrgFromEnv
* getTestCredsFromEnv
* getTestZoneFromEnv
* getTestRegionFromEnv
* getTestCustIdFromEnv
* getTestOrgDomainFromEnv
* getTestOrgTargetFromEnv
* getTestBillingAccountFromEnv
* getTestMasterBillingAccountFromEnv
* getTestServiceAccountFromEnv
* sharedConfigForRegion
* projectEnvVars
* credsEnvVars
* isSweepableTestResource
* testEnvVar
* testAccProvidersOiCS
* googleProviderConfig
* vcrTest
* randString
* randInt
* getTestRegion
* getTestProject
* getTestIdentityUserFromEnv
* getTestFirestoreProjectFromEnv
* skipIfVcr
* sleepInSecondsForTest


The case insensitive diffs looks good

git config --global difftool.idiff.cmd 'diff -i $LOCAL $REMOTE'
git difftool -y --tool idiff  f42899ac83e12eaefb8016f8d94bf0555c067aff origin/acctest-pre-2

```
<         // Location is the location attribute to match, if matched, append Name to list of Testhide
<         Location string `yaml:"location"`
<         // Name specifies sample file name to add to Testhide if location matches.
<         Name string `yaml:"file_name"`
---
> 	// Location is the location attribute to match, if matched, append Name to list of Testhide
> 	Location string `yaml:"location"`
> 	// Name specifies sample file name to add to Testhide if location matches.
> 	Name string `yaml:"file_name"`
108a109
> 
```

<!--
Replace each [ ] with [X] to check it. Switch to the preview view to make it easier to click on links.
These steps will speed up the review process, and we appreciate you spending time on them before sending
your code to be reviewed.
-->
If this PR is for Terraform, I acknowledge that I have:

- [ ] Searched through the [issue tracker](https://github.com/hashicorp/terraform-provider-google/issues) for an open issue that this either resolves or contributes to, commented on it to claim it, and written "fixes {url}" or "part of {url}" in this PR description. If there were no relevant open issues, I opened one and commented that I would like to work on it (not necessary for very small changes).
- [ ] Ensured that all new fields I added that can be set by a user appear in at least one [example](https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/templates/terraform/examples) (for generated resources) or [third_party test](https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/third_party/terraform/tests) (for handwritten resources or update tests).
- [X] [Generated Terraform providers](https://github.com/GoogleCloudPlatform/magic-modules/blob/main/docs/content/docs/getting-started/generate-providers.md), and ran [`make test` and `make lint`](https://googlecloudplatform.github.io/magic-modules/docs/getting-started/run-provider-tests/#run-unit-tests) in the generated providers to ensure it passes unit and linter tests.
- [X] [Ran](https://github.com/GoogleCloudPlatform/magic-modules/blob/main/docs/content/docs/getting-started/run-provider-tests.md) relevant acceptance tests using my own Google Cloud project and credentials (If the acceptance tests do not yet pass or you are unable to run them, please let your reviewer know).
- [X] Read the [Release Notes Guide](https://github.com/GoogleCloudPlatform/magic-modules/blob/main/.ci/RELEASE_NOTES_GUIDE.md) before writing my release note below.

<!-- AUTOCHANGELOG for Downstream PRs.

Please select one of the following "release-note:" headings:
    - release-note:enhancement
    - release-note:bug
    - release-note:note
    - release-note:new-resource
    - release-note:new-datasource
    - release-note:deprecation
    - release-note:breaking-change
    - release-note:none

Unless you choose release-note:none, please add a release note.

See .ci/RELEASE_NOTES_GUIDE.md for writing good release notes.

You can add more release note blocks if you want more than one CHANGELOG
entry for this PR.
-->
**Release Note Template for Downstream PRs (will be copied)**

```release-note:none

```

Derived from https://github.com/GoogleCloudPlatform/magic-modules/pull/7403


Patch:
diff --git a/converters/google/resources/cai.go b/converters/google/resources/cai.go
index 78ed7a19..93c04246 100644
--- a/converters/google/resources/cai.go
+++ b/converters/google/resources/cai.go
@@ -102,7 +102,7 @@ func assetName(d TerraformResourceData, config *Config, linkTmpl string) (string
 	// workaround for empty project
 	placeholderSet := false
 	if config.Project == "" {
-		config.Project = fmt.Sprintf("placeholder-%s", randString(8))
+		config.Project = fmt.Sprintf("placeholder-%s", RandString(8))
 		placeholderSet = true
 	}
 
@@ -117,7 +117,7 @@ func assetName(d TerraformResourceData, config *Config, linkTmpl string) (string
 	fWithPlaceholder := func(key string) string {
 		val := f(key)
 		if val == "" {
-			val = fmt.Sprintf("placeholder-%s", randString(8))
+			val = fmt.Sprintf("placeholder-%s", RandString(8))
 		}
 		return val
 	}
@@ -125,7 +125,7 @@ func assetName(d TerraformResourceData, config *Config, linkTmpl string) (string
 	return re.ReplaceAllStringFunc(linkTmpl, fWithPlaceholder), nil
 }
 
-func randString(n int) string {
+func RandString(n int) string {
 	const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
 	b := make([]byte, n)
 	for i := range b {


Test patch:
diff --git a/converters/google/resources/cai_test.go b/converters/google/resources/cai_test.go
index 2646ef81..94b0a245 100644
--- a/converters/google/resources/cai_test.go
+++ b/converters/google/resources/cai_test.go
@@ -54,7 +54,7 @@ func TestAssetName(t *testing.T) {
 func TestRandString(t *testing.T) {
 	memory := make(map[string]bool)
 	for i := 0; i < 100; i++ {
-		s := randString(i)
+		s := RandString(i)
 		if n := len(s); n != i {
 			t.Fatalf("expected len = %v, got %v", i, n)
 		}
diff --git a/test/environment_test.go b/test/environment_test.go
index 791ae6a0..814f5436 100644
--- a/test/environment_test.go
+++ b/test/environment_test.go
@@ -31,7 +31,7 @@ func Nprintf(format string, params map[string]interface{}) string {
 }
 
 // testAccPreCheck ensures at least one of the project env variables is set.
-func getTestProjectFromEnv() string {
+func GetTestProjectFromEnv() string {
 	project := MultiEnvSearch([]string{"TEST_PROJECT", "GOOGLE_PROJECT"})
 	if project == "" {
 		log.Printf("Missing required env var TEST_PROJECT. Default (%s) will be used.", defaultProject)
@@ -42,7 +42,7 @@ func getTestProjectFromEnv() string {
 }
 
 // testAccPreCheck ensures at least one of the credentials env variables is set.
-func getTestCredsFromEnv() string {
+func GetTestCredsFromEnv() string {
 	cwd, err := os.Getwd()
 	if err != nil {
 		log.Fatalf("cannot get current directory: %v", err)
@@ -63,25 +63,25 @@ func getTestCredsFromEnv() string {
 }
 
 // testAccPreCheck ensures at least one of the region env variables is set.
-func getTestRegionFromEnv() string {
+func GetTestRegionFromEnv() string {
 	return defaultRegion
 }
 
-func getTestCustIdFromEnv(t *testing.T) string {
+func GetTestCustIdFromEnv(t *testing.T) string {
 	return defaultCustId
 }
 
-func getTestIdentityUserFromEnv(t *testing.T) string {
+func GetTestIdentityUserFromEnv(t *testing.T) string {
 	return defaultIdentityUser
 }
 
 // Firestore can't be enabled at the same time as Datastore, so we need a new
 // project to manage it until we can enable Firestore programmatically.
-func getTestFirestoreProjectFromEnv(t *testing.T) string {
+func GetTestFirestoreProjectFromEnv(t *testing.T) string {
 	return defaultFirestoreProject
 }
 
-func getTestOrgFromEnv(t *testing.T) string {
+func GetTestOrgFromEnv(t *testing.T) string {
 	org, ok := os.LookupEnv("TEST_ORG_ID")
 	if !ok {
 		log.Printf("Missing required env var TEST_ORG_ID. Default (%s) will be used.", defaultOrganization)
@@ -91,19 +91,19 @@ func getTestOrgFromEnv(t *testing.T) string {
 	return org
 }
 
-func getTestOrgDomainFromEnv(t *testing.T) string {
+func GetTestOrgDomainFromEnv(t *testing.T) string {
 	return defaultOrganizationDomain
 }
 
-func getTestOrgTargetFromEnv(t *testing.T) string {
+func GetTestOrgTargetFromEnv(t *testing.T) string {
 	return defaultOrganizationTarget
 }
 
-func getTestBillingAccountFromEnv(t *testing.T) string {
+func GetTestBillingAccountFromEnv(t *testing.T) string {
 	return defaultBillingAccount
 }
 
-func getTestServiceAccountFromEnv(t *testing.T) string {
+func GetTestServiceAccountFromEnv(t *testing.T) string {
 	return defaultServiceAccount
 }
 
diff --git a/test/init_test.go b/test/init_test.go
index 1e7374a1..daae2077 100644
--- a/test/init_test.go
+++ b/test/init_test.go
@@ -53,10 +53,10 @@ func init() {
 		log.Fatalf("cannot get current directory: %v", err)
 	}
 	tfvBinary = filepath.Join(cwd, "..", "bin", "terraform-validator")
-	credentials := getTestCredsFromEnv()
-	project := getTestProjectFromEnv()
-	org := getTestOrgFromEnv(nil)
-	billingAccount := getTestBillingAccountFromEnv(nil)
+	credentials := GetTestCredsFromEnv()
+	project := GetTestProjectFromEnv()
+	org := GetTestOrgFromEnv(nil)
+	billingAccount := GetTestBillingAccountFromEnv(nil)
 	folder, ok := os.LookupEnv("TEST_FOLDER_ID")
 	if !ok {
 		log.Printf("Missing required env var TEST_FOLDER_ID. Default (%s) will be used.", defaultFolder)
diff --git a/test/resource_cloud_run_domain_mapping_generated_test.go b/test/resource_cloud_run_domain_mapping_generated_test.go
index ba7a5022..20385142 100644
--- a/test/resource_cloud_run_domain_mapping_generated_test.go
+++ b/test/resource_cloud_run_domain_mapping_generated_test.go
@@ -42,7 +42,7 @@ func testAccCloudRunDomainMapping_cloudRunDomainMappingBasicExample_shared(t *te
 
 	t.Parallel()
 	context := map[string]interface{}{
-		"namespace":     getTestProjectFromEnv(),
+		"namespace":     GetTestProjectFromEnv(),
 		"random_suffix": "meepmerp", // true randomization isn't needed for validator
 	}
 
diff --git a/test/resource_cloud_run_service_generated_test.go b/test/resource_cloud_run_service_generated_test.go
index dba0ac13..c2f8570b 100644
--- a/test/resource_cloud_run_service_generated_test.go
+++ b/test/resource_cloud_run_service_generated_test.go
@@ -42,7 +42,7 @@ func testAccCloudRunService_cloudRunServiceBasicExample_shared(t *testing.T, tes
 
 	t.Parallel()
 	context := map[string]interface{}{
-		"project":       getTestProjectFromEnv(),
+		"project":       GetTestProjectFromEnv(),
 		"random_suffix": "meepmerp", // true randomization isn't needed for validator
 	}
 
@@ -199,7 +199,7 @@ func testAccCloudRunService_cloudRunServiceMultipleEnvironmentVariablesExample_s
 
 	t.Parallel()
 	context := map[string]interface{}{
-		"project":       getTestProjectFromEnv(),
+		"project":       GetTestProjectFromEnv(),
 		"random_suffix": "meepmerp", // true randomization isn't needed for validator
 	}