Replace os.Setenv with testing.T.Setenv in tests

T.Setenv ensures that the environment is returned to its prior state
when the test ends. It also panics when called from a parallel test to
prevent racy test interdependencies.
This commit is contained in:
Chris Bandy
2023-04-15 10:09:47 -05:00
parent c07c739f04
commit 2e76ac31fd
4 changed files with 9 additions and 14 deletions

View File

@@ -83,10 +83,8 @@ func TestComponentSecureServingAndAuth(t *testing.T) {
// Insulate this test from picking up in-cluster config when run inside a pod
// We can't assume we have permissions to write to /var/run/secrets/... from a unit test to mock in-cluster config for testing
originalHost := os.Getenv("KUBERNETES_SERVICE_HOST")
if len(originalHost) > 0 {
os.Setenv("KUBERNETES_SERVICE_HOST", "")
defer os.Setenv("KUBERNETES_SERVICE_HOST", originalHost)
if len(os.Getenv("KUBERNETES_SERVICE_HOST")) > 0 {
t.Setenv("KUBERNETES_SERVICE_HOST", "")
}
// authenticate to apiserver via bearer token