Skip to content

Commit

Permalink
Fix test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
rileykarson committed Jun 12, 2019
1 parent 24909ae commit 3c7f5d6
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,9 @@ func getInitializedConfig(t *testing.T) *Config {
Region: getTestRegionFromEnv(),
Zone: getTestZoneFromEnv(),
}

ConfigureBasePaths(config)

err := config.LoadAndValidate()
if err != nil {
t.Fatal(err)
Expand Down
12 changes: 8 additions & 4 deletions third_party/terraform/utils/bootstrap_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ func BootstrapKMSKeyInLocation(t *testing.T, locationID string) bootstrappedKMS
keyParent := fmt.Sprintf("projects/%s/locations/%s/keyRings/%s", projectID, locationID, SharedKeyRing)
keyName := fmt.Sprintf("%s/cryptoKeys/%s", keyParent, SharedCyptoKey)

config := Config{
config := &Config{
Credentials: getTestCredsFromEnv(),
Project: getTestProjectFromEnv(),
Region: getTestRegionFromEnv(),
Zone: getTestZoneFromEnv(),
}

ConfigureBasePaths(config)

if err := config.LoadAndValidate(); err != nil {
t.Errorf("Unable to bootstrap KMS key: %s", err)
}
Expand Down Expand Up @@ -116,7 +118,7 @@ var serviceAccountDisplay = "Bootstrapped Service Account for Terraform tests"
// Some tests need a second service account, other than the test runner, to assert functionality on.
// This provides a well-known service account that can be used when dynamically creating a service
// account isn't an option.
func getOrCreateServiceAccount(config Config, project string) (*iam.ServiceAccount, error) {
func getOrCreateServiceAccount(config *Config, project string) (*iam.ServiceAccount, error) {
name := fmt.Sprintf("projects/%s/serviceAccounts/%s@%s.iam.gserviceaccount.com", project, serviceAccountEmail, project)
log.Printf("[DEBUG] Verifying %s as bootstrapped service account.\n", name)

Expand Down Expand Up @@ -148,7 +150,7 @@ func getOrCreateServiceAccount(config Config, project string) (*iam.ServiceAccou
// on a different service account. Granting permissions takes time and there is no operation to wait on
// so instead this creates a single service account once per test-suite with the correct permissions.
// The first time this test is run it will fail, but subsequent runs will succeed.
func impersonationServiceAccountPermissions(config Config, sa *iam.ServiceAccount, testRunner string) error {
func impersonationServiceAccountPermissions(config *Config, sa *iam.ServiceAccount, testRunner string) error {
log.Printf("[DEBUG] Setting service account permissions.\n")
policy := iam.Policy{
Bindings: []*iam.Binding{},
Expand Down Expand Up @@ -179,13 +181,15 @@ func BootstrapServiceAccount(t *testing.T, project, testRunner string) string {
return ""
}

config := Config{
config := &Config{
Credentials: getTestCredsFromEnv(),
Project: getTestProjectFromEnv(),
Region: getTestRegionFromEnv(),
Zone: getTestZoneFromEnv(),
}

ConfigureBasePaths(config)

if err := config.LoadAndValidate(); err != nil {
t.Fatalf("Bootstrapping failed. Unable to load test config: %s", err)
}
Expand Down
7 changes: 5 additions & 2 deletions third_party/terraform/utils/config.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,15 @@ type Config struct {
<% unless version == 'ga' -%>
BinaryAuthorizationBasePath string
ContainerAnalysisBasePath string
MonitoringBasePath string
SecurityScannerBasePath string
<% end -%>

AccessContextManagerBasePath string
CloudSchedulerBasePath string
FirestoreBasePath string
RedisBasePath string
TpuBasePath string

CloudBillingBasePath string
clientBilling *cloudbilling.APIService
Expand Down Expand Up @@ -273,7 +276,7 @@ func (c *Config) LoadAndValidate() error {
return err
}
c.clientDns.UserAgent = userAgent
c.clientDns.BasePath = removeBasePathVersion(dnsClientBasePath)
c.clientDns.BasePath = dnsClientBasePath

dnsBetaClientBasePath := removeBasePathVersion(c.DnsBetaBasePath) + "v1beta2/projects/"
log.Printf("[INFO] Instantiating Google Cloud DNS Beta client for path %s", dnsBetaClientBasePath)
Expand Down Expand Up @@ -302,7 +305,7 @@ func (c *Config) LoadAndValidate() error {
c.clientLogging.UserAgent = userAgent
c.clientLogging.BasePath = loggingClientBasePath

storageClientBasePath := removeBasePathVersion(c.StorageBasePath)
storageClientBasePath := removeBasePathVersion(c.StorageBasePath) + "v1/"
log.Printf("[INFO] Instantiating Google Storage client for path %s", storageClientBasePath)
c.clientStorage, err = storage.NewService(context, option.WithHTTPClient(client))
if err != nil {
Expand Down
25 changes: 19 additions & 6 deletions third_party/terraform/utils/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ const testFakeCredentialsPath = "./test-fixtures/fake_account.json"
const testOauthScope = "https://www.googleapis.com/auth/compute"

func TestConfigLoadAndValidate_accountFilePath(t *testing.T) {
config := Config{
config := &Config{
Credentials: testFakeCredentialsPath,
Project: "my-gce-project",
Region: "us-central1",
}

ConfigureBasePaths(config)

err := config.LoadAndValidate()
if err != nil {
t.Fatalf("error: %v", err)
Expand All @@ -32,25 +34,29 @@ func TestConfigLoadAndValidate_accountFileJSON(t *testing.T) {
if err != nil {
t.Fatalf("error: %v", err)
}
config := Config{
config := &Config{
Credentials: string(contents),
Project: "my-gce-project",
Region: "us-central1",
}

ConfigureBasePaths(config)

err = config.LoadAndValidate()
if err != nil {
t.Fatalf("error: %v", err)
}
}

func TestConfigLoadAndValidate_accountFileJSONInvalid(t *testing.T) {
config := Config{
config := &Config{
Credentials: "{this is not json}",
Project: "my-gce-project",
Region: "us-central1",
}

ConfigureBasePaths(config)

if config.LoadAndValidate() == nil {
t.Fatalf("expected error, but got nil")
}
Expand All @@ -65,12 +71,14 @@ func TestAccConfigLoadValidate_credentials(t *testing.T) {
creds := getTestCredsFromEnv()
proj := getTestProjectFromEnv()

config := Config{
config := &Config{
Credentials: creds,
Project: proj,
Region: "us-central1",
}

ConfigureBasePaths(config)

err := config.LoadAndValidate()
if err != nil {
t.Fatalf("error: %v", err)
Expand Down Expand Up @@ -101,12 +109,14 @@ func TestAccConfigLoadValidate_accessToken(t *testing.T) {
t.Fatalf("Unable to generate test access token: %s", err)
}

config := Config{
config := &Config{
AccessToken: token.AccessToken,
Project: proj,
Region: "us-central1",
}

ConfigureBasePaths(config)

err = config.LoadAndValidate()
if err != nil {
t.Fatalf("error: %v", err)
Expand All @@ -119,12 +129,15 @@ func TestAccConfigLoadValidate_accessToken(t *testing.T) {
}

func TestConfigLoadAndValidate_customScopes(t *testing.T) {
config := Config{
config := &Config{
Credentials: testFakeCredentialsPath,
Project: "my-gce-project",
Region: "us-central1",
Scopes: []string{"https://www.googleapis.com/auth/compute"},
}

ConfigureBasePaths(config)

err := config.LoadAndValidate()
if err != nil {
t.Fatalf("unexpected error: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion third_party/terraform/utils/gcp_sweeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func sharedConfigForRegion(region string) (*Config, error) {
Project: project,
}

BasePathsConfigure(conf)
ConfigureBasePaths(conf)

return conf, nil
}
10 changes: 9 additions & 1 deletion third_party/terraform/utils/provider.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func Provider() terraform.ResourceProvider {
// start beta-only products
BinaryAuthorizationCustomEndpointEntryKey: BinaryAuthorizationCustomEndpointEntry,
ContainerAnalysisCustomEndpointEntryKey: ContainerAnalysisCustomEndpointEntry,
MonitoringCustomEndpointEntryKey: MonitoringCustomEndpointEntry,
SecurityScannerCustomEndpointEntryKey: SecurityScannerCustomEndpointEntry,
// end beta-only products
<% end -%>
Expand All @@ -95,11 +96,13 @@ func Provider() terraform.ResourceProvider {
FirestoreCustomEndpointEntryKey: FirestoreCustomEndpointEntry,
KmsCustomEndpointEntryKey: KmsCustomEndpointEntry,
PubsubCustomEndpointEntryKey: PubsubCustomEndpointEntry,
RedisCustomEndpointEntryKey: RedisCustomEndpointEntry,
ResourceManagerCustomEndpointEntryKey: ResourceManagerCustomEndpointEntry,
SourceRepoCustomEndpointEntryKey: SourceRepoCustomEndpointEntry,
SpannerCustomEndpointEntryKey: SpannerCustomEndpointEntry,
SqlCustomEndpointEntryKey: SqlCustomEndpointEntry,
StorageCustomEndpointEntryKey: StorageCustomEndpointEntry,
TpuCustomEndpointEntryKey: TpuCustomEndpointEntry,

// Handwritten Products / Versioned / Atypical Entries
<% unless version == 'ga' -%>
Expand Down Expand Up @@ -375,6 +378,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
<% unless version == 'ga' -%>
config.BinaryAuthorizationBasePath = d.Get(BinaryAuthorizationCustomEndpointEntryKey).(string)
config.ContainerAnalysisBasePath = d.Get(ContainerAnalysisCustomEndpointEntryKey).(string)
config.MonitoringBasePath = d.Get(MonitoringCustomEndpointEntryKey).(string)
config.SecurityScannerBasePath = d.Get(SecurityScannerCustomEndpointEntryKey).(string)
<% end -%>

Expand All @@ -394,6 +398,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
config.SpannerBasePath = d.Get(SpannerCustomEndpointEntryKey).(string)
config.SqlBasePath = d.Get(SqlCustomEndpointEntryKey).(string)
config.StorageBasePath = d.Get(StorageCustomEndpointEntryKey).(string)
config.TpuBasePath = d.Get(TpuCustomEndpointEntryKey).(string)

<% unless version == 'ga' -%>
config.IAPBasePath = d.Get(IAPCustomEndpointEntryKey).(string)
Expand Down Expand Up @@ -429,12 +434,13 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
// For a consumer of config.go that isn't a full fledged provider and doesn't
// have its own endpoint mechanism such as sweepers, init {{service}}BasePath
// values to a default. After using this, you should call config.LoadAndValidate.
func BasePathsConfigure(c *Config) {
func ConfigureBasePaths(c *Config) {
// Generated Products
<% unless version == 'ga' -%>
// start beta-only products
c.BinaryAuthorizationBasePath = BinaryAuthorizationDefaultBasePath
c.ContainerAnalysisBasePath = ContainerAnalysisDefaultBasePath
c.MonitoringBasePath = MonitoringDefaultBasePath
c.SecurityScannerBasePath = SecurityScannerDefaultBasePath
// end beta-only products
<% end -%>
Expand All @@ -448,11 +454,13 @@ func BasePathsConfigure(c *Config) {
c.FirestoreBasePath = FirestoreDefaultBasePath
c.KmsBasePath = KmsDefaultBasePath
c.PubsubBasePath = PubsubDefaultBasePath
c.RedisBasePath = RedisDefaultBasePath
c.ResourceManagerBasePath = ResourceManagerDefaultBasePath
c.SourceRepoBasePath = SourceRepoDefaultBasePath
c.SpannerBasePath = SpannerDefaultBasePath
c.SqlBasePath = SqlDefaultBasePath
c.StorageBasePath = StorageDefaultBasePath
c.TpuBasePath = TpuDefaultBasePath

// Handwritten Products / Versioned / Atypical Entries
<% unless version == 'ga' -%>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ The following keys are available exclusively in the `google-beta` provider:
* `binary_authorization_custom_endpoint` (`GOOGLE_BINARY_AUTHORIZATION_CUSTOM_ENDPOINT`) - `https://binaryauthorization.googleapis.com/v1beta1/`
* `container_analysis_custom_endpoint` (`GOOGLE_CONTAINER_ANALYSIS_CUSTOM_ENDPOINT`) - `https://containeranalysis.googleapis.com/v1beta1/`
* `iap_custom_endpoint` (`GOOGLE_IAP_CUSTOM_ENDPOINT`) - `https://iap.googleapis.com/v1beta1/`
* `monitoring_custom_endpoint` (`GOOGLE_MONITORING_CUSTOM_ENDPOINT`) - `https://monitoring.googleapis.com/v3/`
* `security_scanner_custom_endpoint` (`GOOGLE_SECURITY_SCANNER_CUSTOM_ENDPOINT`) - `https://websecurityscanner.googleapis.com/v1beta/`
* `service_networking_custom_endpoint` (`GOOGLE_SERVICE_NETWORKING_CUSTOM_ENDPOINT`) - `https://servicenetworking.googleapis.com/v1beta/`

Expand Down

0 comments on commit 3c7f5d6

Please sign in to comment.