Merge pull request #117423 from cbandy/test-testing-setenv

Replace os.Setenv with testing.T.Setenv in tests
This commit is contained in:
Kubernetes Prow Robot 2023-06-06 10:20:12 -07:00 committed by GitHub
commit fea314a89a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 9 additions and 14 deletions

View File

@ -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"},
}, {

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

View File

@ -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 {

View File

@ -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")
}
}