Skip to content

Commit d4730fd

Browse files
Add acceptance tests for how provider handles scopes argument (#11860) (#19690)
[upstream:2c7fe8a6b59d7dcb0a031b1139410dc34e0d1bc8] Signed-off-by: Modular Magician <[email protected]>
1 parent 145fd21 commit d4730fd

File tree

3 files changed

+291
-0
lines changed

3 files changed

+291
-0
lines changed

.changelog/11860.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:none
2+
3+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
package fwprovider_test
4+
5+
import (
6+
"fmt"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
10+
"github.com/hashicorp/terraform-provider-google/google/acctest"
11+
"github.com/hashicorp/terraform-provider-google/google/transport"
12+
)
13+
14+
// TestAccFwProvider_scopes is a series of acc tests asserting how the PF provider handles scopes arguments
15+
// It is PF specific because the HCL used provisions PF-implemented resources
16+
// It is a counterpart to TestAccSdkProvider_scopes
17+
func TestAccFwProvider_scopes(t *testing.T) {
18+
testCases := map[string]func(t *testing.T){
19+
// Configuring the provider using inputs
20+
"default scopes are used when there are no user inputs": testAccFwProvider_scopes_providerDefault,
21+
"scopes can be set in config": testAccFwProvider_scopes_setInConfig,
22+
//no ENVs to test
23+
24+
// Schema-level validation
25+
"when scopes is set to an empty array in the config the value is ignored and default scopes are used": testAccFwProvider_scopes_emptyArray,
26+
27+
// Usage
28+
// No usage test cases are implemented in the GA provider because the only PF-implemented data sources are Beta-only
29+
}
30+
31+
for name, tc := range testCases {
32+
// shadow the tc variable into scope so that when
33+
// the loop continues, if t.Run hasn't executed tc(t)
34+
// yet, we don't have a race condition
35+
// see https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables
36+
tc := tc
37+
t.Run(name, func(t *testing.T) {
38+
tc(t)
39+
})
40+
}
41+
}
42+
43+
func testAccFwProvider_scopes_providerDefault(t *testing.T) {
44+
acctest.SkipIfVcr(t) // Test doesn't interact with API
45+
46+
acctest.VcrTest(t, resource.TestCase{
47+
// No PreCheck for checking ENVs
48+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
49+
Steps: []resource.TestStep{
50+
{
51+
Config: testAccFwProvider_scopes_unset(),
52+
Check: resource.ComposeTestCheckFunc(
53+
resource.TestCheckResourceAttr("data.google_provider_config_plugin_framework.default", "scopes.#", fmt.Sprintf("%d", len(transport.DefaultClientScopes))),
54+
resource.TestCheckResourceAttr("data.google_provider_config_plugin_framework.default", "scopes.0", transport.DefaultClientScopes[0]),
55+
resource.TestCheckResourceAttr("data.google_provider_config_plugin_framework.default", "scopes.1", transport.DefaultClientScopes[1]),
56+
),
57+
},
58+
},
59+
})
60+
}
61+
62+
func testAccFwProvider_scopes_setInConfig(t *testing.T) {
63+
acctest.SkipIfVcr(t) // Test doesn't interact with API
64+
65+
scopes := []string{"https://www.googleapis.com/auth/cloud-platform"} // first of the two default scopes
66+
context := map[string]interface{}{
67+
"scopes": fmt.Sprintf("[\"%s\"]", scopes[0]),
68+
}
69+
70+
acctest.VcrTest(t, resource.TestCase{
71+
// No PreCheck for checking ENVs
72+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
73+
Steps: []resource.TestStep{
74+
{
75+
Config: testAccFwProvider_scopes_inProviderBlock(context),
76+
Check: resource.ComposeTestCheckFunc(
77+
resource.TestCheckResourceAttr("data.google_provider_config_plugin_framework.default", "scopes.#", fmt.Sprintf("%d", len(scopes))),
78+
resource.TestCheckResourceAttr("data.google_provider_config_plugin_framework.default", "scopes.0", scopes[0]),
79+
),
80+
},
81+
},
82+
})
83+
}
84+
85+
func testAccFwProvider_scopes_emptyArray(t *testing.T) {
86+
acctest.SkipIfVcr(t) // Test doesn't interact with API
87+
88+
context := map[string]interface{}{
89+
"scopes": "[]",
90+
}
91+
92+
acctest.VcrTest(t, resource.TestCase{
93+
// No PreCheck for checking ENVs
94+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
95+
Steps: []resource.TestStep{
96+
{
97+
Config: testAccFwProvider_scopes_inProviderBlock(context),
98+
Check: resource.ComposeTestCheckFunc(
99+
resource.TestCheckResourceAttr("data.google_provider_config_plugin_framework.default", "scopes.#", fmt.Sprintf("%d", len(transport.DefaultClientScopes))),
100+
resource.TestCheckResourceAttr("data.google_provider_config_plugin_framework.default", "scopes.0", transport.DefaultClientScopes[0]),
101+
resource.TestCheckResourceAttr("data.google_provider_config_plugin_framework.default", "scopes.1", transport.DefaultClientScopes[1]),
102+
),
103+
},
104+
},
105+
})
106+
}
107+
108+
// testAccFwProvider_scopes_inProviderBlock allows setting the scopes argument in a provider block.
109+
// This function uses data.google_provider_config_plugin_framework because it is implemented with the PF
110+
func testAccFwProvider_scopes_inProviderBlock(context map[string]interface{}) string {
111+
return acctest.Nprintf(`
112+
provider "google" {
113+
scopes = %{scopes}
114+
}
115+
116+
data "google_provider_config_plugin_framework" "default" {}
117+
`, context)
118+
}
119+
120+
// testAccFwProvider_scopes_inEnvsOnly allows testing when the scopes argument is not set
121+
func testAccFwProvider_scopes_unset() string {
122+
return `
123+
data "google_provider_config_plugin_framework" "default" {}
124+
`
125+
}
+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
package provider_test
4+
5+
import (
6+
"fmt"
7+
"regexp"
8+
"testing"
9+
10+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
11+
"github.com/hashicorp/terraform-provider-google/google/acctest"
12+
"github.com/hashicorp/terraform-provider-google/google/transport"
13+
)
14+
15+
// TestAccSdkProvider_scopes is a series of acc tests asserting how the SDK provider handles scopes arguments
16+
// It is SDK specific because the HCL used provisions SDK-implemented resources
17+
// It is a counterpart to TestAccFwProvider_scopes
18+
func TestAccSdkProvider_scopes(t *testing.T) {
19+
testCases := map[string]func(t *testing.T){
20+
// Configuring the provider using inputs
21+
"default scopes are used when there are no user inputs": testAccSdkProvider_scopes_providerDefault,
22+
"scopes can be set in config": testAccSdkProvider_scopes_setInConfig,
23+
//no ENVs to test
24+
25+
// Schema-level validation
26+
"when scopes is set to an empty array in the config the value is ignored and default scopes are used": testAccSdkProvider_scopes_emptyArray,
27+
28+
// Usage
29+
"the scopes argument impacts provisioning resources": testAccSdkProvider_scopes_usage,
30+
}
31+
32+
for name, tc := range testCases {
33+
// shadow the tc variable into scope so that when
34+
// the loop continues, if t.Run hasn't executed tc(t)
35+
// yet, we don't have a race condition
36+
// see https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables
37+
tc := tc
38+
t.Run(name, func(t *testing.T) {
39+
tc(t)
40+
})
41+
}
42+
}
43+
44+
func testAccSdkProvider_scopes_providerDefault(t *testing.T) {
45+
acctest.SkipIfVcr(t) // Test doesn't interact with API
46+
47+
acctest.VcrTest(t, resource.TestCase{
48+
// No PreCheck for checking ENVs
49+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
50+
Steps: []resource.TestStep{
51+
{
52+
Config: testAccSdkProvider_scopes_unset(),
53+
Check: resource.ComposeTestCheckFunc(
54+
resource.TestCheckResourceAttr("data.google_provider_config_sdk.default", "scopes.#", fmt.Sprintf("%d", len(transport.DefaultClientScopes))),
55+
resource.TestCheckResourceAttr("data.google_provider_config_sdk.default", "scopes.0", transport.DefaultClientScopes[0]),
56+
resource.TestCheckResourceAttr("data.google_provider_config_sdk.default", "scopes.1", transport.DefaultClientScopes[1]),
57+
),
58+
},
59+
},
60+
})
61+
}
62+
63+
func testAccSdkProvider_scopes_setInConfig(t *testing.T) {
64+
acctest.SkipIfVcr(t) // Test doesn't interact with API
65+
66+
scopes := []string{"https://www.googleapis.com/auth/cloud-platform"} // first of the two default scopes
67+
context := map[string]interface{}{
68+
"scopes": fmt.Sprintf("[\"%s\"]", scopes[0]),
69+
}
70+
71+
acctest.VcrTest(t, resource.TestCase{
72+
// No PreCheck for checking ENVs
73+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
74+
Steps: []resource.TestStep{
75+
{
76+
Config: testAccSdkProvider_scopes_inProviderBlock(context),
77+
Check: resource.ComposeTestCheckFunc(
78+
resource.TestCheckResourceAttr("data.google_provider_config_sdk.default", "scopes.#", fmt.Sprintf("%d", len(scopes))),
79+
resource.TestCheckResourceAttr("data.google_provider_config_sdk.default", "scopes.0", scopes[0]),
80+
),
81+
},
82+
},
83+
})
84+
}
85+
86+
func testAccSdkProvider_scopes_emptyArray(t *testing.T) {
87+
acctest.SkipIfVcr(t) // Test doesn't interact with API
88+
89+
context := map[string]interface{}{
90+
"scopes": "[]",
91+
}
92+
93+
acctest.VcrTest(t, resource.TestCase{
94+
// No PreCheck for checking ENVs
95+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
96+
Steps: []resource.TestStep{
97+
{
98+
Config: testAccSdkProvider_scopes_inProviderBlock(context),
99+
Check: resource.ComposeTestCheckFunc(
100+
resource.TestCheckResourceAttr("data.google_provider_config_sdk.default", "scopes.#", fmt.Sprintf("%d", len(transport.DefaultClientScopes))),
101+
resource.TestCheckResourceAttr("data.google_provider_config_sdk.default", "scopes.0", transport.DefaultClientScopes[0]),
102+
resource.TestCheckResourceAttr("data.google_provider_config_sdk.default", "scopes.1", transport.DefaultClientScopes[1]),
103+
),
104+
},
105+
},
106+
})
107+
}
108+
109+
func testAccSdkProvider_scopes_usage(t *testing.T) {
110+
acctest.SkipIfVcr(t) // Test doesn't interact with API
111+
112+
// We include scopes that aren't sufficient to enable provisioning the resources in the config below
113+
context := map[string]interface{}{
114+
"scopes": "[\"https://www.googleapis.com/auth/pubsub\"]",
115+
"random_suffix": acctest.RandString(t, 10),
116+
}
117+
118+
acctest.VcrTest(t, resource.TestCase{
119+
// No PreCheck for checking ENVs
120+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
121+
Steps: []resource.TestStep{
122+
{
123+
Config: testAccSdkProvider_scopes_affectsProvisioning(context),
124+
ExpectError: regexp.MustCompile("Request had insufficient authentication scopes"),
125+
},
126+
},
127+
})
128+
}
129+
130+
// testAccSdkProvider_scopes_inProviderBlock allows setting the scopes argument in a provider block.
131+
// This function uses data.google_provider_config_sdk because it is implemented with the SDK
132+
func testAccSdkProvider_scopes_inProviderBlock(context map[string]interface{}) string {
133+
return acctest.Nprintf(`
134+
provider "google" {
135+
scopes = %{scopes}
136+
}
137+
138+
data "google_provider_config_sdk" "default" {}
139+
`, context)
140+
}
141+
142+
// testAccSdkProvider_scopes_inEnvsOnly allows testing when the scopes argument is not set
143+
func testAccSdkProvider_scopes_unset() string {
144+
return `
145+
data "google_provider_config_sdk" "default" {}
146+
`
147+
}
148+
149+
// testAccSdkProvider_scopes_affectsProvisioning allows testing the impact of the scopes argument on provisioning
150+
func testAccSdkProvider_scopes_affectsProvisioning(context map[string]interface{}) string {
151+
return acctest.Nprintf(`
152+
provider "google" {
153+
scopes = %{scopes}
154+
}
155+
156+
data "google_provider_config_sdk" "default" {}
157+
158+
resource "google_service_account" "default" {
159+
account_id = "tf-test-%{random_suffix}"
160+
display_name = "AccTest Service Account"
161+
}
162+
`, context)
163+
}

0 commit comments

Comments
 (0)