|
| 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