mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-07 19:23:40 +00:00
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.
This commit is contained in:
parent
c07c739f04
commit
2e76ac31fd
@ -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"},
|
||||
}, {
|
||||
|
@ -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
|
||||
|
@ -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 {
|
||||
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user