From fc643d2e40c49791d41d134cdfbb4f1a68a47624 Mon Sep 17 00:00:00 2001 From: Chris Bandy Date: Sat, 15 Apr 2023 10:09:47 -0500 Subject: [PATCH] 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. --- cmd/kube-scheduler/app/options/options_test.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/cmd/kube-scheduler/app/options/options_test.go b/cmd/kube-scheduler/app/options/options_test.go index abce33634cc..4dfc68af8af 100644 --- a/cmd/kube-scheduler/app/options/options_test.go +++ b/cmd/kube-scheduler/app/options/options_test.go @@ -413,10 +413,8 @@ profiles: // 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", "") } defaultPodInitialBackoffSeconds := int64(1)