From a52c87cb5aac98d2220a46d5d9760839f723fda9 Mon Sep 17 00:00:00 2001 From: Sebastian Rivera Date: Thu, 31 Mar 2022 09:57:15 -0400 Subject: [PATCH] Fix a flaky test manipulating environment variables This test resolves an issue where variables were going unset causing leaks into other test cases and therefore resulting in failures. --- internal/cloud/backend_test.go | 79 ++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 33 deletions(-) diff --git a/internal/cloud/backend_test.go b/internal/cloud/backend_test.go index 48b77e558a..a29ca14ced 100644 --- a/internal/cloud/backend_test.go +++ b/internal/cloud/backend_test.go @@ -178,21 +178,27 @@ func TestCloud_PrepareConfigWithEnvVars(t *testing.T) { } for name, tc := range cases { - s := testServer(t) - b := New(testDisco(s)) + t.Run(name, func(t *testing.T) { + s := testServer(t) + b := New(testDisco(s)) - for k, v := range tc.vars { - os.Setenv(k, v) - defer os.Unsetenv(k) - } - - _, valDiags := b.PrepareConfig(tc.config) - if valDiags.Err() != nil && tc.expectedErr != "" { - actualErr := valDiags.Err().Error() - if !strings.Contains(actualErr, tc.expectedErr) { - t.Fatalf("%s: unexpected validation result: %v", name, valDiags.Err()) + for k, v := range tc.vars { + os.Setenv(k, v) } - } + t.Cleanup(func() { + for k := range tc.vars { + os.Unsetenv(k) + } + }) + + _, valDiags := b.PrepareConfig(tc.config) + if valDiags.Err() != nil && tc.expectedErr != "" { + actualErr := valDiags.Err().Error() + if !strings.Contains(actualErr, tc.expectedErr) { + t.Fatalf("%s: unexpected validation result: %v", name, valDiags.Err()) + } + } + }) } } @@ -266,31 +272,38 @@ func TestCloud_configWithEnvVars(t *testing.T) { } for name, tc := range cases { - s := testServer(t) - b := New(testDisco(s)) + t.Run(name, func(t *testing.T) { + s := testServer(t) + b := New(testDisco(s)) - for k, v := range tc.vars { - os.Setenv(k, v) - defer os.Unsetenv(k) - } + for k, v := range tc.vars { + os.Setenv(k, v) + } - _, valDiags := b.PrepareConfig(tc.config) - if valDiags.Err() != nil { - t.Fatalf("%s: unexpected validation result: %v", name, valDiags.Err()) - } + t.Cleanup(func() { + for k := range tc.vars { + os.Unsetenv(k) + } + }) - diags := b.Configure(tc.config) - if diags.Err() != nil { - t.Fatalf("%s: unexpected configuration result: %v", name, valDiags.Err()) - } + _, valDiags := b.PrepareConfig(tc.config) + if valDiags.Err() != nil { + t.Fatalf("%s: unexpected validation result: %v", name, valDiags.Err()) + } - if tc.expectedOrganization != "" && tc.expectedOrganization != b.organization { - t.Fatalf("%s: organization not valid: %s, expected: %s", name, b.organization, tc.expectedOrganization) - } + diags := b.Configure(tc.config) + if diags.Err() != nil { + t.Fatalf("%s: unexpected configuration result: %v", name, diags.Err()) + } - if tc.expectedHostname != "" && tc.expectedHostname != b.hostname { - t.Fatalf("%s: hostname not valid: %s, expected: %s", name, b.hostname, tc.expectedHostname) - } + if tc.expectedOrganization != "" && tc.expectedOrganization != b.organization { + t.Fatalf("%s: organization not valid: %s, expected: %s", name, b.organization, tc.expectedOrganization) + } + + if tc.expectedHostname != "" && tc.expectedHostname != b.hostname { + t.Fatalf("%s: hostname not valid: %s, expected: %s", name, b.hostname, tc.expectedHostname) + } + }) } }