-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Add acceptance tests for how provider handles impersonate_service_account
argument
#11641
Merged
SarahFrench
merged 6 commits into
GoogleCloudPlatform:main
from
SarahFrench:mux-refactor-9-replace-impersonate-service-account-tests
Sep 24, 2024
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3d36628
Update FrameworkProviderConfig struct and data source to enable testi…
SarahFrench 460846a
Formatting fixes
SarahFrench 596beac
Add SDK acceptance tests for impersonate_service_account
SarahFrench a4a04bc
Add PF acceptance tests for impersonate_service_account
SarahFrench 3a3f75f
Fix comments
SarahFrench bbf2367
Remove unused outputs from acc test config
SarahFrench File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
mmv1/third_party/terraform/fwprovider/framework_provider_impersonate_service_account_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package fwprovider_test | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-provider-google/google/acctest" | ||
) | ||
|
||
// TestAccFwProvider_impersonate_service_account is a series of acc tests asserting how the plugin-framework provider handles impersonate_service_account arguments | ||
// It is plugin-framework specific because the HCL used provisions plugin-framework-implemented resources | ||
// It is a counterpart to TestAccSdkProvider_impersonate_service_account | ||
func TestAccFwProvider_impersonate_service_account(t *testing.T) { | ||
testCases := map[string]func(t *testing.T){ | ||
// Configuring the provider using inputs | ||
"config takes precedence over environment variables": testAccFwProvider_impersonate_service_account_configPrecedenceOverEnvironmentVariables, | ||
"when impersonate_service_account is unset in the config, environment variables are used in a given order": testAccFwProvider_impersonate_service_account_precedenceOrderEnvironmentVariables, // GOOGLE_IMPERSONATE_SERVICE_ACCOUNT | ||
|
||
// Schema-level validation | ||
"when impersonate_service_account is set to an empty string in the config the value isn't ignored and results in an error": testAccFwProvider_impersonate_service_account_emptyStringValidation, | ||
|
||
// Usage | ||
// We need to wait for a non-Firebase resource to be migrated to the plugin-framework to enable writing this test | ||
// "impersonate_service_account controls which service account is used for actions" | ||
} | ||
|
||
for name, tc := range testCases { | ||
// shadow the tc variable into scope so that when | ||
// the loop continues, if t.Run hasn't executed tc(t) | ||
// yet, we don't have a race condition | ||
// see https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables | ||
tc := tc | ||
t.Run(name, func(t *testing.T) { | ||
tc(t) | ||
}) | ||
} | ||
} | ||
|
||
func testAccFwProvider_impersonate_service_account_configPrecedenceOverEnvironmentVariables(t *testing.T) { | ||
acctest.SkipIfVcr(t) // Test doesn't interact with API | ||
|
||
impersonateServiceAccountEnvironment := "value-from-envs@example.com" | ||
impersonateServiceAccountProviderBlock := "value-from-provider-block@example.com" | ||
|
||
// ensure all possible impersonate_service_account env vars set; show they aren't used | ||
t.Setenv("GOOGLE_IMPERSONATE_SERVICE_ACCOUNT", impersonateServiceAccountEnvironment) | ||
|
||
context := map[string]interface{}{ | ||
"impersonate_service_account": impersonateServiceAccountProviderBlock, | ||
} | ||
|
||
acctest.VcrTest(t, resource.TestCase{ | ||
// No PreCheck for checking ENVs | ||
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccFwProvider_impersonate_service_account_inProviderBlock(context), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.google_provider_config_plugin_framework.default", "impersonate_service_account", impersonateServiceAccountProviderBlock), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccFwProvider_impersonate_service_account_precedenceOrderEnvironmentVariables(t *testing.T) { | ||
acctest.SkipIfVcr(t) // Test doesn't interact with API | ||
/* | ||
These are all the ENVs for impersonate_service_account, and they are in order of precedence. | ||
GOOGLE_IMPERSONATE_SERVICE_ACCOUNT | ||
*/ | ||
|
||
impersonateServiceAccount := "foobar@example.com" | ||
|
||
context := map[string]interface{}{} | ||
|
||
acctest.VcrTest(t, resource.TestCase{ | ||
// No PreCheck for checking ENVs | ||
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), | ||
Steps: []resource.TestStep{ | ||
{ | ||
PreConfig: func() { | ||
t.Setenv("GOOGLE_IMPERSONATE_SERVICE_ACCOUNT", impersonateServiceAccount) | ||
}, | ||
Config: testAccFwProvider_impersonate_service_account_inEnvsOnly(context), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.google_provider_config_plugin_framework.default", "impersonate_service_account", impersonateServiceAccount), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccFwProvider_impersonate_service_account_emptyStringValidation(t *testing.T) { | ||
acctest.SkipIfVcr(t) // Test doesn't interact with API | ||
|
||
impersonateServiceAccountEnvironment := "value-from-envs@example.com" | ||
|
||
// ensure all possible impersonate_service_account env vars set; show they aren't used | ||
t.Setenv("GOOGLE_IMPERSONATE_SERVICE_ACCOUNT", impersonateServiceAccountEnvironment) | ||
|
||
context := map[string]interface{}{ | ||
"impersonate_service_account": "", // empty string used | ||
} | ||
|
||
acctest.VcrTest(t, resource.TestCase{ | ||
// No PreCheck for checking ENVs | ||
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccFwProvider_impersonate_service_account_inProviderBlock(context), | ||
PlanOnly: true, | ||
ExpectError: regexp.MustCompile("expected a non-empty string"), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
// testAccFwProvider_impersonate_service_account_inProviderBlock allows setting the impersonate_service_account argument in a provider block. | ||
func testAccFwProvider_impersonate_service_account_inProviderBlock(context map[string]interface{}) string { | ||
return acctest.Nprintf(` | ||
provider "google" { | ||
impersonate_service_account = "%{impersonate_service_account}" | ||
} | ||
|
||
data "google_provider_config_plugin_framework" "default" {} | ||
|
||
output "impersonate_service_account" { | ||
value = data.google_provider_config_plugin_framework.default.impersonate_service_account | ||
sensitive = true | ||
} | ||
SarahFrench marked this conversation as resolved.
Show resolved
Hide resolved
|
||
`, context) | ||
} | ||
|
||
// testAccFwProvider_impersonate_service_account_inEnvsOnly allows testing when the impersonate_service_account argument | ||
// is only supplied via ENVs | ||
func testAccFwProvider_impersonate_service_account_inEnvsOnly(context map[string]interface{}) string { | ||
return acctest.Nprintf(` | ||
data "google_provider_config_plugin_framework" "default" {} | ||
|
||
output "impersonate_service_account" { | ||
value = data.google_provider_config_plugin_framework.default.impersonate_service_account | ||
sensitive = true | ||
} | ||
SarahFrench marked this conversation as resolved.
Show resolved
Hide resolved
|
||
`, context) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think waiting for an 'easier' resource to use would be best - given that Firebase is the only PF-impemented resources/data source available to us and it's Beta-only.