Skip to content

Commit

Permalink
Add acceptance tests for provider configuration behaviour using `add_…
Browse files Browse the repository at this point in the history
…terraform_attribution_label` and `terraform_attribution_label_addition_strategy` (GoogleCloudPlatform#11884)
  • Loading branch information
SarahFrench authored Oct 2, 2024
1 parent e6af55b commit 1347042
Show file tree
Hide file tree
Showing 6 changed files with 398 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ func (d *GoogleProviderConfigPluginFrameworkDataSource) Read(ctx context.Context
data.RequestReason = d.providerConfig.RequestReason
// TODO(SarahFrench) - request_timeout
data.DefaultLabels = d.providerConfig.DefaultLabels
// TODO(SarahFrench) - add_terraform_attribution_label
// TODO(SarahFrench) - terraform_attribution_label_addition_strategy
data.AddTerraformAttributionLabel = d.providerConfig.AddTerraformAttributionLabel
data.TerraformAttributionLabelAdditionStrategy = d.providerConfig.TerraformAttributionLabelAdditionStrategy

// Warn users against using this data source
resp.Diagnostics.Append(diag.NewWarningDiagnostic(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package fwprovider_test

import (
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-google/google/acctest"
)

// TestAccFwProvider_add_terraform_attribution_label is a series of acc tests asserting how the plugin-framework provider handles add_terraform_attribution_label arguments
// It is plugin-framework specific because the HCL used provisions plugin-framework-implemented resources
// It is a counterpart to TestAccSdkProvider_add_terraform_attribution_label
func TestAccFwProvider_add_terraform_attribution_label(t *testing.T) {
testCases := map[string]func(t *testing.T){
// Configuring the provider using inputs
"when add_terraform_attribution_label is set in the config, the value is set in the provider meta data": testAccFwProvider_add_terraform_attribution_label_configUsed,
"when add_terraform_attribution_label is unset in the config, the default value 'true' is NOT set on the provider meta data": testAccFwProvider_add_terraform_attribution_label_defaultValue,
}

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_add_terraform_attribution_label_configUsed(t *testing.T) {
acctest.SkipIfVcr(t) // Test doesn't interact with API

contextFalse := map[string]interface{}{
"add_terraform_attribution_label": "false",
}
contextTrue := map[string]interface{}{
"add_terraform_attribution_label": "true",
}

acctest.VcrTest(t, resource.TestCase{
// No PreCheck for checking ENVs
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Steps: []resource.TestStep{
{
Config: testAccFwProvider_add_terraform_attribution_label_inProviderBlock(contextFalse),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.google_provider_config_plugin_framework.default", "add_terraform_attribution_label", "false"),
),
},
{
Config: testAccFwProvider_add_terraform_attribution_label_inProviderBlock(contextTrue),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.google_provider_config_plugin_framework.default", "add_terraform_attribution_label", "true"),
),
},
},
})
}

func testAccFwProvider_add_terraform_attribution_label_defaultValue(t *testing.T) {
acctest.SkipIfVcr(t) // Test doesn't interact with API

context := map[string]interface{}{}

acctest.VcrTest(t, resource.TestCase{
// No PreCheck for checking ENVs
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Steps: []resource.TestStep{
{
Config: testAccFwProvider_add_terraform_attribution_label_inEnvsOnly(context),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckNoResourceAttr("data.google_provider_config_plugin_framework.default", "add_terraform_attribution_label"),
),
},
},
})
}

// testAccFwProvider_add_terraform_attribution_label_inProviderBlock allows setting the add_terraform_attribution_label argument in a provider block.
func testAccFwProvider_add_terraform_attribution_label_inProviderBlock(context map[string]interface{}) string {
return acctest.Nprintf(`
provider "google" {
add_terraform_attribution_label = "%{add_terraform_attribution_label}"
}
data "google_provider_config_plugin_framework" "default" {}
`, context)
}

// testAccFwProvider_add_terraform_attribution_label_inEnvsOnly allows testing when the add_terraform_attribution_label argument
// is only supplied via ENVs
func testAccFwProvider_add_terraform_attribution_label_inEnvsOnly(context map[string]interface{}) string {
return acctest.Nprintf(`
data "google_provider_config_plugin_framework" "default" {}
`, context)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package fwprovider_test

import (
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-google/google/acctest"
)

// TestAccFwProvider_terraform_attribution_label_addition_strategy is a series of acc tests asserting how the plugin-framework provider handles terraform_attribution_label_addition_strategy arguments
// It is plugin-framework specific because the HCL used provisions plugin-framework-implemented resources
// It is a counterpart to TestAccSdkProvider_terraform_attribution_label_addition_strategy
func TestAccFwProvider_terraform_attribution_label_addition_strategy(t *testing.T) {
testCases := map[string]func(t *testing.T){
// Configuring the provider using inputs
"config sets terraform_attribution_label_addition_strategy values": testAccFwProvider_terraform_attribution_label_addition_strategy_configUsed,
"when terraform_attribution_label_addition_strategy is unset in the config, the default value'CREATION_ONLY' is NOT set in the provider meta data": testAccFwProvider_terraform_attribution_label_addition_strategy_defaultValue,
}

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_terraform_attribution_label_addition_strategy_configUsed(t *testing.T) {
acctest.SkipIfVcr(t) // Test doesn't interact with API

context1 := map[string]interface{}{
"terraform_attribution_label_addition_strategy": "CREATION_ONLY",
}
context2 := map[string]interface{}{
"terraform_attribution_label_addition_strategy": "PROACTIVE",
}

acctest.VcrTest(t, resource.TestCase{
// No PreCheck for checking ENVs
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Steps: []resource.TestStep{
{
Config: testAccFwProvider_terraform_attribution_label_addition_strategy_inProviderBlock(context1),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.google_provider_config_plugin_framework.default", "terraform_attribution_label_addition_strategy", "CREATION_ONLY"),
),
},
{
Config: testAccFwProvider_terraform_attribution_label_addition_strategy_inProviderBlock(context2),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.google_provider_config_plugin_framework.default", "terraform_attribution_label_addition_strategy", "PROACTIVE"),
),
},
},
})
}

func testAccFwProvider_terraform_attribution_label_addition_strategy_defaultValue(t *testing.T) {
acctest.SkipIfVcr(t) // Test doesn't interact with API

context := map[string]interface{}{}

acctest.VcrTest(t, resource.TestCase{
// No PreCheck for checking ENVs
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Steps: []resource.TestStep{
{
Config: testAccFwProvider_terraform_attribution_label_addition_strategy_inEnvsOnly(context),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckNoResourceAttr("data.google_provider_config_plugin_framework.default", "terraform_attribution_label_addition_strategy"),
),
},
},
})
}

// testAccFwProvider_terraform_attribution_label_addition_strategy_inProviderBlock allows setting the terraform_attribution_label_addition_strategy argument in a provider block.
func testAccFwProvider_terraform_attribution_label_addition_strategy_inProviderBlock(context map[string]interface{}) string {
return acctest.Nprintf(`
provider "google" {
terraform_attribution_label_addition_strategy = "%{terraform_attribution_label_addition_strategy}"
}
data "google_provider_config_plugin_framework" "default" {}
`, context)
}

// testAccFwProvider_terraform_attribution_label_addition_strategy_inEnvsOnly allows testing when the terraform_attribution_label_addition_strategy argument
// is only supplied via ENVs
func testAccFwProvider_terraform_attribution_label_addition_strategy_inEnvsOnly(context map[string]interface{}) string {
return acctest.Nprintf(`
data "google_provider_config_plugin_framework" "default" {}
`, context)
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ type FrameworkProviderConfig struct {
ImpersonateServiceAccount types.String
ImpersonateServiceAccountDelegates types.List
RequestReason types.String
AddTerraformAttributionLabel types.Bool
TerraformAttributionLabelAdditionStrategy types.String
// End temporary

BillingProject types.String
Expand Down Expand Up @@ -111,7 +113,8 @@ func (p *FrameworkProviderConfig) LoadAndValidateFramework(ctx context.Context,
p.ImpersonateServiceAccount = data.ImpersonateServiceAccount
p.ImpersonateServiceAccountDelegates = data.ImpersonateServiceAccountDelegates
p.RequestReason = data.RequestReason

p.AddTerraformAttributionLabel = data.AddTerraformAttributionLabel
p.TerraformAttributionLabelAdditionStrategy = data.TerraformAttributionLabelAdditionStrategy
// End temporary

// Copy values from the ProviderModel struct containing data about the provider configuration (present only when responsing to ConfigureProvider rpc calls)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package provider_test

import (
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-google/google/acctest"
)

// TestAccSdkProvider_add_terraform_attribution_label is a series of acc tests asserting how the plugin-framework provider handles add_terraform_attribution_label arguments
// It is plugin-framework specific because the HCL used provisions plugin-framework-implemented resources
// It is a counterpart to TestAccFwProvider_add_terraform_attribution_label
func TestAccSdkProvider_add_terraform_attribution_label(t *testing.T) {
testCases := map[string]func(t *testing.T){
// Configuring the provider using inputs
"config sets add_terraform_attribution_label values": testAccSdkProvider_add_terraform_attribution_label_configUsed,
"when add_terraform_attribution_label is unset in the config, the default value 'true' is set in the provider meta data": testAccSdkProvider_add_terraform_attribution_label_defaultValue,
}

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 testAccSdkProvider_add_terraform_attribution_label_configUsed(t *testing.T) {
acctest.SkipIfVcr(t) // Test doesn't interact with API

contextFalse := map[string]interface{}{
"add_terraform_attribution_label": "false",
}
contextTrue := map[string]interface{}{
"add_terraform_attribution_label": "true",
}

acctest.VcrTest(t, resource.TestCase{
// No PreCheck for checking ENVs
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Steps: []resource.TestStep{
{
Config: testAccSdkProvider_add_terraform_attribution_label_inProviderBlock(contextFalse),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.google_provider_config_sdk.default", "add_terraform_attribution_label", "false"),
),
},
{
Config: testAccSdkProvider_add_terraform_attribution_label_inProviderBlock(contextTrue),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.google_provider_config_sdk.default", "add_terraform_attribution_label", "true"),
),
},
},
})
}

func testAccSdkProvider_add_terraform_attribution_label_defaultValue(t *testing.T) {
acctest.SkipIfVcr(t) // Test doesn't interact with API

context := map[string]interface{}{}

acctest.VcrTest(t, resource.TestCase{
// No PreCheck for checking ENVs
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Steps: []resource.TestStep{
{
Config: testAccSdkProvider_add_terraform_attribution_label_inEnvsOnly(context),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.google_provider_config_sdk.default", "add_terraform_attribution_label", "true"),
),
},
},
})
}

// testAccSdkProvider_add_terraform_attribution_label_inProviderBlock allows setting the add_terraform_attribution_label argument in a provider block.
func testAccSdkProvider_add_terraform_attribution_label_inProviderBlock(context map[string]interface{}) string {
return acctest.Nprintf(`
provider "google" {
add_terraform_attribution_label = "%{add_terraform_attribution_label}"
}
data "google_provider_config_sdk" "default" {}
`, context)
}

// testAccSdkProvider_add_terraform_attribution_label_inEnvsOnly allows testing when the add_terraform_attribution_label argument
// is only supplied via ENVs
func testAccSdkProvider_add_terraform_attribution_label_inEnvsOnly(context map[string]interface{}) string {
return acctest.Nprintf(`
data "google_provider_config_sdk" "default" {}
`, context)
}
Loading

0 comments on commit 1347042

Please sign in to comment.