diff --git a/test/conformance/image/go-runner/env_test.go b/test/conformance/image/go-runner/env_test.go index 4229ebe3037..ac9e771fbd4 100644 --- a/test/conformance/image/go-runner/env_test.go +++ b/test/conformance/image/go-runner/env_test.go @@ -32,7 +32,7 @@ func TestEnv(t *testing.T) { desc: "OS env", env: &osEnv{}, preHook: func() { - os.Setenv("key1", "1") + t.Setenv("key1", "1") }, expect: map[string]string{"key1": "1"}, }, { diff --git a/test/integration/serving/serving_test.go b/test/integration/serving/serving_test.go index 27b038cc218..b6aff38b7bf 100644 --- a/test/integration/serving/serving_test.go +++ b/test/integration/serving/serving_test.go @@ -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 diff --git a/test/integration/volumescheduling/volume_binding_test.go b/test/integration/volumescheduling/volume_binding_test.go index d91554b7f7e..0f10b190ed2 100644 --- a/test/integration/volumescheduling/volume_binding_test.go +++ b/test/integration/volumescheduling/volume_binding_test.go @@ -21,7 +21,6 @@ package volumescheduling import ( "context" "fmt" - "os" "strconv" "strings" "testing" @@ -427,10 +426,7 @@ func testVolumeBindingStress(t *testing.T, schedulerResyncPeriod time.Duration, // Set max volume limit to the number of PVCs the test will create // TODO: remove when max volume limit allows setting through storageclass - if err := os.Setenv(nodevolumelimits.KubeMaxPDVols, fmt.Sprintf("%v", podLimit*volsPerPod)); err != nil { - t.Fatalf("failed to set max pd limit: %v", err) - } - defer os.Unsetenv(nodevolumelimits.KubeMaxPDVols) + t.Setenv(nodevolumelimits.KubeMaxPDVols, fmt.Sprintf("%v", podLimit*volsPerPod)) scName := &classWait if dynamic { diff --git a/test/typecheck/main_test.go b/test/typecheck/main_test.go index 163e4aded62..817f43f9061 100644 --- a/test/typecheck/main_test.go +++ b/test/typecheck/main_test.go @@ -32,7 +32,7 @@ var goBinary = flag.String("go", "", "path to a `go` binary") func TestVerify(t *testing.T) { // x/tools/packages is going to literally exec `go`, so it needs some // setup. - setEnvVars() + setEnvVars(t) tcs := []struct { path string @@ -57,17 +57,18 @@ func TestVerify(t *testing.T) { } } -func setEnvVars() { +func setEnvVars(t testing.TB) { + t.Helper() if *goBinary != "" { newPath := filepath.Dir(*goBinary) curPath := os.Getenv("PATH") if curPath != "" { newPath = newPath + ":" + curPath } - os.Setenv("PATH", newPath) + t.Setenv("PATH", newPath) } if os.Getenv("HOME") == "" { - os.Setenv("HOME", "/tmp") + t.Setenv("HOME", "/tmp") } }