Skip to content

Commit 4890f1e

Browse files
SarahFrenchanavada
authored andcommitted
Add acceptance tests for how provider handles scopes argument (GoogleCloudPlatform#11860)
1 parent a7df7d3 commit 4890f1e

File tree

2 files changed

+351
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)