From 8784c53fb32ff9ee972a9f878c5d1253d94ad455 Mon Sep 17 00:00:00 2001 From: The Magician Date: Wed, 25 May 2022 11:29:05 -0700 Subject: [PATCH] Fix eventarc test template (#6066) (#723) * Fix eventarc test template * Update steps * Final one Signed-off-by: Modular Magician --- converters/google/resources/getconfig.go | 14 ++++- converters/google/resources/getconfig_test.go | 38 +++---------- test/read_test.go | 2 +- ...dRunDomainMappingBasicExample_offline.json | 55 ------------------ ...e_cloudRunServiceBasicExample_offline.json | 37 ------------ ...leEnvironmentVariablesExample_offline.json | 50 ----------------- ...ice_cloudRunServiceSqlExample_offline.json | 56 ------------------- 7 files changed, 21 insertions(+), 231 deletions(-) delete mode 100644 testdata/generatedconvert/CloudRunDomainMapping_cloudRunDomainMappingBasicExample_offline.json delete mode 100644 testdata/generatedconvert/CloudRunService_cloudRunServiceBasicExample_offline.json delete mode 100644 testdata/generatedconvert/CloudRunService_cloudRunServiceMultipleEnvironmentVariablesExample_offline.json delete mode 100644 testdata/generatedconvert/CloudRunService_cloudRunServiceSqlExample_offline.json diff --git a/converters/google/resources/getconfig.go b/converters/google/resources/getconfig.go index 106aa5e1e..88383ba2d 100644 --- a/converters/google/resources/getconfig.go +++ b/converters/google/resources/getconfig.go @@ -2,8 +2,12 @@ package google import ( "context" + "fmt" + "os" "github.com/pkg/errors" + + "github.com/GoogleCloudPlatform/terraform-validator/version" ) // Return the value of the private userAgent field @@ -11,10 +15,10 @@ func (c *Config) UserAgent() string { return c.userAgent } -func GetConfig(ctx context.Context, project string, offline bool, userAgent string) (*Config, error) { +func GetConfig(ctx context.Context, project string, offline bool) (*Config, error) { cfg := &Config{ Project: project, - userAgent: userAgent, + userAgent: fmt.Sprintf("config-validator-tf/%s", version.BuildVersion()), } // Search for default credentials @@ -44,6 +48,12 @@ func GetConfig(ctx context.Context, project string, offline bool, userAgent stri "CLOUDSDK_COMPUTE_REGION", }) + // opt in extension for adding to the User-Agent header + if ext := os.Getenv("GOOGLE_TERRAFORM_VALIDATOR_USERAGENT_EXTENSION"); ext != "" { + ua := cfg.userAgent + cfg.userAgent = fmt.Sprintf("%s %s", ua, ext) + } + if !offline { ConfigureBasePaths(cfg) if err := cfg.LoadAndValidate(ctx); err != nil { diff --git a/converters/google/resources/getconfig_test.go b/converters/google/resources/getconfig_test.go index a776984e9..3e17c7d60 100644 --- a/converters/google/resources/getconfig_test.go +++ b/converters/google/resources/getconfig_test.go @@ -116,6 +116,13 @@ func TestGetConfigExtractsEnvVars(t *testing.T) { expected: "whatever", getConfigValue: getRegionValue, }, + { + name: "GOOGLE_TERRAFORM_VALIDATOR_USERAGENT_EXTENSION", + envKey: "GOOGLE_TERRAFORM_VALIDATOR_USERAGENT_EXTENSION", + envValue: "whatever", + expected: "config-validator-tf/dev whatever", + getConfigValue: getUserAgent, + }, } for _, c := range cases { @@ -126,7 +133,7 @@ func TestGetConfigExtractsEnvVars(t *testing.T) { t.Fatalf("error setting env var %s=%s: %s", c.envKey, c.envValue, err) } - cfg, err := GetConfig(ctx, "project", offline, "") + cfg, err := GetConfig(ctx, "project", offline) if err != nil { t.Fatalf("error building converter: %s", err) } @@ -147,32 +154,3 @@ func TestGetConfigExtractsEnvVars(t *testing.T) { }) } } - -func TestGetConfigUserAgent(t *testing.T) { - ctx := context.Background() - offline := true - cases := []struct { - userAgent string - expected string - }{ - { - userAgent: "", - expected: "", - }, - { - userAgent: "whatever", - expected: "whatever", - }, - } - - for _, c := range cases { - t.Run(c.userAgent, func(t *testing.T) { - cfg, err := GetConfig(ctx, "project", offline, c.userAgent) - if err != nil { - t.Fatalf("error building converter: %s", err) - } - - assert.Equal(t, c.expected, getUserAgent(cfg)) - }) - } -} diff --git a/test/read_test.go b/test/read_test.go index 9619f603a..865b0679d 100644 --- a/test/read_test.go +++ b/test/read_test.go @@ -143,7 +143,7 @@ func TestReadPlannedAssetsCoverage(t *testing.T) { ancestryCache := map[string]string{ data.Provider["project"]: data.Ancestry, } - got, err := tfgcv.ReadPlannedAssets(ctx, planfile, data.Provider["project"], ancestryCache, true, false, zaptest.NewLogger(t), "") + got, err := tfgcv.ReadPlannedAssets(ctx, planfile, data.Provider["project"], ancestryCache, true, false, zaptest.NewLogger(t)) if err != nil { t.Fatalf("ReadPlannedAssets(%s, %s, %s, %t): %v", planfile, data.Provider["project"], ancestryCache, true, err) } diff --git a/testdata/generatedconvert/CloudRunDomainMapping_cloudRunDomainMappingBasicExample_offline.json b/testdata/generatedconvert/CloudRunDomainMapping_cloudRunDomainMappingBasicExample_offline.json deleted file mode 100644 index d884adf50..000000000 --- a/testdata/generatedconvert/CloudRunDomainMapping_cloudRunDomainMappingBasicExample_offline.json +++ /dev/null @@ -1,55 +0,0 @@ -[ - { - "name": "//run.googleapis.com/projects/foobar/locations/us-central1/DomainMappings/tf-test-domainmeepmerp.gcp.tfacc.hashicorptest.com", - "asset_type": "run.googleapis.com/DomainMapping", - "ancestry_path": "organization/12345/folder/67890/project/foobar", - "resource": { - "version": "v1", - "discovery_document_uri": "https://www.googleapis.com/discovery/v1/apis/run/v1/rest", - "discovery_name": "DomainMapping", - "parent": "//cloudresourcemanager.googleapis.com/projects/foobar", - "data": { - "apiVersion": "domains.cloudrun.com/v1", - "kind": "DomainMapping", - "metadata": { - "name": "tf-test-domainmeepmerp.gcp.tfacc.hashicorptest.com", - "namespace": "foobar" - }, - "spec": { - "certificateMode": "AUTOMATIC", - "routeName": "tf-test-cloudrun-srvmeepmerp" - } - } - } - }, - { - "name": "//run.googleapis.com/projects/foobar/locations/us-central1/services/tf-test-cloudrun-srvmeepmerp", - "asset_type": "run.googleapis.com/Service", - "ancestry_path": "organization/12345/folder/67890/project/foobar", - "resource": { - "version": "v1", - "discovery_document_uri": "https://www.googleapis.com/discovery/v1/apis/run/v1/rest", - "discovery_name": "Service", - "parent": "//cloudresourcemanager.googleapis.com/projects/foobar", - "data": { - "apiVersion": "serving.knative.dev/v1", - "kind": "Service", - "metadata": { - "name": "tf-test-cloudrun-srvmeepmerp", - "namespace": "foobar" - }, - "spec": { - "template": { - "spec": { - "containers": [ - { - "image": "us-docker.pkg.dev/cloudrun/container/hello" - } - ] - } - } - } - } - } - } -] diff --git a/testdata/generatedconvert/CloudRunService_cloudRunServiceBasicExample_offline.json b/testdata/generatedconvert/CloudRunService_cloudRunServiceBasicExample_offline.json deleted file mode 100644 index df78f8123..000000000 --- a/testdata/generatedconvert/CloudRunService_cloudRunServiceBasicExample_offline.json +++ /dev/null @@ -1,37 +0,0 @@ -[ - { - "name": "//run.googleapis.com/projects/foobar/locations/us-central1/services/tf-test-cloudrun-srvmeepmerp", - "asset_type": "run.googleapis.com/Service", - "ancestry_path": "organization/12345/folder/67890/project/foobar", - "resource": { - "version": "v1", - "discovery_document_uri": "https://www.googleapis.com/discovery/v1/apis/run/v1/rest", - "discovery_name": "Service", - "parent": "//cloudresourcemanager.googleapis.com/projects/foobar", - "data": { - "apiVersion": "serving.knative.dev/v1", - "kind": "Service", - "metadata": { - "name": "tf-test-cloudrun-srvmeepmerp" - }, - "spec": { - "template": { - "spec": { - "containers": [ - { - "image": "us-docker.pkg.dev/cloudrun/container/hello" - } - ] - } - }, - "traffic": [ - { - "latestRevision": true, - "percent": 100 - } - ] - } - } - } - } -] diff --git a/testdata/generatedconvert/CloudRunService_cloudRunServiceMultipleEnvironmentVariablesExample_offline.json b/testdata/generatedconvert/CloudRunService_cloudRunServiceMultipleEnvironmentVariablesExample_offline.json deleted file mode 100644 index 69a4d5b32..000000000 --- a/testdata/generatedconvert/CloudRunService_cloudRunServiceMultipleEnvironmentVariablesExample_offline.json +++ /dev/null @@ -1,50 +0,0 @@ -[ - { - "name": "//run.googleapis.com/projects/foobar/locations/us-central1/services/tf-test-cloudrun-srvmeepmerp", - "asset_type": "run.googleapis.com/Service", - "ancestry_path": "organization/12345/folder/67890/project/foobar", - "resource": { - "version": "v1", - "discovery_document_uri": "https://www.googleapis.com/discovery/v1/apis/run/v1/rest", - "discovery_name": "Service", - "parent": "//cloudresourcemanager.googleapis.com/projects/foobar", - "data": { - "apiVersion": "serving.knative.dev/v1", - "kind": "Service", - "metadata": { - "annotations": { - "generated-by": "magic-modules" - }, - "name": "tf-test-cloudrun-srvmeepmerp" - }, - "spec": { - "template": { - "spec": { - "containers": [ - { - "env": [ - { - "name": "SOURCE", - "value": "remote" - }, - { - "name": "TARGET", - "value": "home" - } - ], - "image": "us-docker.pkg.dev/cloudrun/container/hello" - } - ] - } - }, - "traffic": [ - { - "latestRevision": true, - "percent": 100 - } - ] - } - } - } - } -] diff --git a/testdata/generatedconvert/CloudRunService_cloudRunServiceSqlExample_offline.json b/testdata/generatedconvert/CloudRunService_cloudRunServiceSqlExample_offline.json deleted file mode 100644 index 6120dcdc6..000000000 --- a/testdata/generatedconvert/CloudRunService_cloudRunServiceSqlExample_offline.json +++ /dev/null @@ -1,56 +0,0 @@ -[ - { - "name": "//cloudsql.googleapis.com/projects/foobar/instances/tf-test-cloudrun-sqlmeepmerp", - "asset_type": "sqladmin.googleapis.com/Instance", - "ancestry_path": "organization/12345/folder/67890/project/foobar", - "resource": { - "version": "v1beta4", - "discovery_document_uri": "https://www.googleapis.com/discovery/v1/apis/sqladmin/v1beta4/rest", - "discovery_name": "DatabaseInstance", - "parent": "//cloudresourcemanager.googleapis.com/projects/foobar", - "data": { - "databaseVersion": "MYSQL_5_7", - "name": "tf-test-cloudrun-sqlmeepmerp", - "project": "foobar", - "region": "us-east1", - "settings": { - "activationPolicy": "ALWAYS", - "availabilityType": "ZONAL", - "dataDiskType": "PD_SSD", - "pricingPlan": "PER_USE", - "storageAutoResize": true, - "tier": "db-f1-micro" - } - } - } - }, - { - "name": "//run.googleapis.com/projects/foobar/locations/us-central1/services/tf-test-cloudrun-srvmeepmerp", - "asset_type": "run.googleapis.com/Service", - "ancestry_path": "organization/12345/folder/67890/project/foobar", - "resource": { - "version": "v1", - "discovery_document_uri": "https://www.googleapis.com/discovery/v1/apis/run/v1/rest", - "discovery_name": "Service", - "parent": "//cloudresourcemanager.googleapis.com/projects/foobar", - "data": { - "apiVersion": "serving.knative.dev/v1", - "kind": "Service", - "metadata": { - "name": "tf-test-cloudrun-srvmeepmerp" - }, - "spec": { - "template": { - "spec": { - "containers": [ - { - "image": "us-docker.pkg.dev/cloudrun/container/hello" - } - ] - } - } - } - } - } - } -]