Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Apr 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 5 additions & 18 deletions staging/src/k8s.io/apimachinery/pkg/util/net/http_test.go
Expand Up @@ -26,7 +26,6 @@ import (
"net"
"net/http"
"net/url"
"os"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -205,7 +204,7 @@ func TestProxierWithNoProxyCIDR(t *testing.T) {
}

for _, test := range testCases {
os.Setenv("NO_PROXY", test.noProxy)
t.Setenv("NO_PROXY", test.noProxy)
actualDelegated := false
proxyFunc := NewProxierWithNoProxyCIDR(func(req *http.Request) (*url.URL, error) {
actualDelegated = true
Expand Down Expand Up @@ -917,40 +916,28 @@ func TestIsProbableEOF(t *testing.T) {
}
}

func setEnv(key, value string) func() {
originalValue := os.Getenv(key)
os.Setenv(key, value)
return func() {
os.Setenv(key, originalValue)
}
}

func TestReadIdleTimeoutSeconds(t *testing.T) {
reset := setEnv("HTTP2_READ_IDLE_TIMEOUT_SECONDS", "60")
t.Setenv("HTTP2_READ_IDLE_TIMEOUT_SECONDS", "60")
if e, a := 60, readIdleTimeoutSeconds(); e != a {
t.Errorf("expected %d, got %d", e, a)
}
reset()

reset = setEnv("HTTP2_READ_IDLE_TIMEOUT_SECONDS", "illegal value")
t.Setenv("HTTP2_READ_IDLE_TIMEOUT_SECONDS", "illegal value")
if e, a := 30, readIdleTimeoutSeconds(); e != a {
t.Errorf("expected %d, got %d", e, a)
}
reset()
}

func TestPingTimeoutSeconds(t *testing.T) {
reset := setEnv("HTTP2_PING_TIMEOUT_SECONDS", "60")
t.Setenv("HTTP2_PING_TIMEOUT_SECONDS", "60")
if e, a := 60, pingTimeoutSeconds(); e != a {
t.Errorf("expected %d, got %d", e, a)
}
reset()

reset = setEnv("HTTP2_PING_TIMEOUT_SECONDS", "illegal value")
t.Setenv("HTTP2_PING_TIMEOUT_SECONDS", "illegal value")
if e, a := 15, pingTimeoutSeconds(); e != a {
t.Errorf("expected %d, got %d", e, a)
}
reset()
}

func Benchmark_ParseQuotedString(b *testing.B) {
Expand Down