diff --git a/rest/client_test.go b/rest/client_test.go index 6b359aa7..6ba9e738 100644 --- a/rest/client_test.go +++ b/rest/client_test.go @@ -23,7 +23,6 @@ import ( "net/http/httptest" "net/http/httputil" "net/url" - "os" "reflect" "testing" "time" @@ -340,8 +339,8 @@ func TestCreateBackoffManager(t *testing.T) { theUrl, _ := url.Parse("http://localhost") // 1 second base backoff + duration of 2 seconds -> exponential backoff for requests. - os.Setenv(envBackoffBase, "1") - os.Setenv(envBackoffDuration, "2") + t.Setenv(envBackoffBase, "1") + t.Setenv(envBackoffDuration, "2") backoff := readExpBackoffConfig() backoff.UpdateBackoff(theUrl, nil, 500) backoff.UpdateBackoff(theUrl, nil, 500) @@ -350,8 +349,8 @@ func TestCreateBackoffManager(t *testing.T) { } // 0 duration -> no backoff. - os.Setenv(envBackoffBase, "1") - os.Setenv(envBackoffDuration, "0") + t.Setenv(envBackoffBase, "1") + t.Setenv(envBackoffDuration, "0") backoff.UpdateBackoff(theUrl, nil, 500) backoff.UpdateBackoff(theUrl, nil, 500) backoff = readExpBackoffConfig() @@ -360,8 +359,8 @@ func TestCreateBackoffManager(t *testing.T) { } // No env -> No backoff. - os.Setenv(envBackoffBase, "") - os.Setenv(envBackoffDuration, "") + t.Setenv(envBackoffBase, "") + t.Setenv(envBackoffDuration, "") backoff = readExpBackoffConfig() backoff.UpdateBackoff(theUrl, nil, 500) backoff.UpdateBackoff(theUrl, nil, 500) diff --git a/rest/connection_test.go b/rest/connection_test.go index 323c9eec..8d705b78 100644 --- a/rest/connection_test.go +++ b/rest/connection_test.go @@ -24,7 +24,6 @@ import ( "net/http" "net/http/httptest" "net/url" - "os" "strconv" "strings" "sync/atomic" @@ -80,23 +79,15 @@ func newLB(t *testing.T, serverURL string) *tcpLB { return &lb } -func setEnv(key, value string) func() { - originalValue := os.Getenv(key) - os.Setenv(key, value) - return func() { - os.Setenv(key, originalValue) - } -} - const ( readIdleTimeout int = 1 pingTimeout int = 1 ) func TestReconnectBrokenTCP(t *testing.T) { - defer setEnv("HTTP2_READ_IDLE_TIMEOUT_SECONDS", strconv.Itoa(readIdleTimeout))() - defer setEnv("HTTP2_PING_TIMEOUT_SECONDS", strconv.Itoa(pingTimeout))() - defer setEnv("DISABLE_HTTP2", "")() + t.Setenv("HTTP2_READ_IDLE_TIMEOUT_SECONDS", strconv.Itoa(readIdleTimeout)) + t.Setenv("HTTP2_PING_TIMEOUT_SECONDS", strconv.Itoa(pingTimeout)) + t.Setenv("DISABLE_HTTP2", "") ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, %s", r.Proto) })) diff --git a/tools/clientcmd/loader_test.go b/tools/clientcmd/loader_test.go index ba42e56f..aa6741b8 100644 --- a/tools/clientcmd/loader_test.go +++ b/tools/clientcmd/loader_test.go @@ -897,12 +897,9 @@ func TestLoadingGetLoadingPrecedence(t *testing.T) { }, } - kubeconfig := os.Getenv("KUBECONFIG") - defer os.Setenv("KUBECONFIG", kubeconfig) - for name, test := range testCases { t.Run(name, func(t *testing.T) { - os.Setenv("KUBECONFIG", test.env) + t.Setenv("KUBECONFIG", test.env) rules := test.rules if rules == nil { rules = NewDefaultClientConfigLoadingRules()