From be71ab06a8eb6437751cfc5d218863d5378f0175 Mon Sep 17 00:00:00 2001 From: Nick Fagerlund Date: Fri, 6 Oct 2023 16:47:36 -0700 Subject: [PATCH] Add a first test for resolveCloudConfig --- internal/cloud/backend_test.go | 68 ++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/internal/cloud/backend_test.go b/internal/cloud/backend_test.go index 6a48c9004f..4110268a74 100644 --- a/internal/cloud/backend_test.go +++ b/internal/cloud/backend_test.go @@ -8,6 +8,8 @@ import ( "fmt" "net/http" "os" + "reflect" + "sort" "strings" "testing" @@ -826,6 +828,72 @@ func TestCloud_setUnavailableTerraformVersion(t *testing.T) { } } +func TestCloud_resolveCloudConfig(t *testing.T) { + cases := map[string]struct { + config cty.Value + vars map[string]string + expectedResult cloudConfig + expectedErr string + }{ + "hostname/org/name/token/project all set": { + config: cty.ObjectVal(map[string]cty.Value{ + "hostname": cty.StringVal("app.staging.terraform.io"), + "organization": cty.StringVal("examplecorp"), + "token": cty.StringVal("password123"), + "workspaces": cty.ObjectVal(map[string]cty.Value{ + "name": cty.StringVal("prod"), + "tags": cty.NullVal(cty.Set(cty.String)), + "project": cty.StringVal("networking"), + }), + }), + expectedResult: cloudConfig{ + hostname: "app.staging.terraform.io", + organization: "examplecorp", + token: "password123", + workspaceMapping: WorkspaceMapping{ + Name: "prod", + Project: "networking", + }, + }, + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + for k, v := range tc.vars { + os.Setenv(k, v) + } + t.Cleanup(func() { + for k := range tc.vars { + os.Unsetenv(k) + } + }) + + result, diags := resolveCloudConfig(tc.config) + + // Validate either expected error or result, not both + if tc.expectedErr != "" { + if diags.Err() == nil { + t.Fatalf("%s: expected validation error %q but didn't get one", name, tc.expectedErr) + } else { + actualErr := diags.Err().Error() + if !strings.Contains(actualErr, tc.expectedErr) { + t.Fatalf("%s: expected validation error %q but instead got %q", name, tc.expectedErr, actualErr) + } + } + } else { // check result + // Sort tags first to avoid flaking, since the slice order from + // a cty.Set isn't guaranteed: + sort.Strings(tc.expectedResult.workspaceMapping.Tags) + sort.Strings(result.workspaceMapping.Tags) + if !reflect.DeepEqual(tc.expectedResult, result) { + t.Fatalf("%s: expected final config of %#v but instead got %#v", name, tc.expectedResult, result) + } + } + }) + } +} + func TestCloud_setConfigurationFields(t *testing.T) { originalForceBackendEnv := os.Getenv("TF_FORCE_LOCAL_BACKEND")