mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-08 03:33:56 +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",
|
desc: "OS env",
|
||||||
env: &osEnv{},
|
env: &osEnv{},
|
||||||
preHook: func() {
|
preHook: func() {
|
||||||
os.Setenv("key1", "1")
|
t.Setenv("key1", "1")
|
||||||
},
|
},
|
||||||
expect: map[string]string{"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
|
// 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
|
// 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(os.Getenv("KUBERNETES_SERVICE_HOST")) > 0 {
|
||||||
if len(originalHost) > 0 {
|
t.Setenv("KUBERNETES_SERVICE_HOST", "")
|
||||||
os.Setenv("KUBERNETES_SERVICE_HOST", "")
|
|
||||||
defer os.Setenv("KUBERNETES_SERVICE_HOST", originalHost)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// authenticate to apiserver via bearer token
|
// authenticate to apiserver via bearer token
|
||||||
|
@ -21,7 +21,6 @@ package volumescheduling
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"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
|
// Set max volume limit to the number of PVCs the test will create
|
||||||
// TODO: remove when max volume limit allows setting through storageclass
|
// TODO: remove when max volume limit allows setting through storageclass
|
||||||
if err := os.Setenv(nodevolumelimits.KubeMaxPDVols, fmt.Sprintf("%v", podLimit*volsPerPod)); err != nil {
|
t.Setenv(nodevolumelimits.KubeMaxPDVols, fmt.Sprintf("%v", podLimit*volsPerPod))
|
||||||
t.Fatalf("failed to set max pd limit: %v", err)
|
|
||||||
}
|
|
||||||
defer os.Unsetenv(nodevolumelimits.KubeMaxPDVols)
|
|
||||||
|
|
||||||
scName := &classWait
|
scName := &classWait
|
||||||
if dynamic {
|
if dynamic {
|
||||||
|
@ -32,7 +32,7 @@ var goBinary = flag.String("go", "", "path to a `go` binary")
|
|||||||
func TestVerify(t *testing.T) {
|
func TestVerify(t *testing.T) {
|
||||||
// x/tools/packages is going to literally exec `go`, so it needs some
|
// x/tools/packages is going to literally exec `go`, so it needs some
|
||||||
// setup.
|
// setup.
|
||||||
setEnvVars()
|
setEnvVars(t)
|
||||||
|
|
||||||
tcs := []struct {
|
tcs := []struct {
|
||||||
path string
|
path string
|
||||||
@ -57,17 +57,18 @@ func TestVerify(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func setEnvVars() {
|
func setEnvVars(t testing.TB) {
|
||||||
|
t.Helper()
|
||||||
if *goBinary != "" {
|
if *goBinary != "" {
|
||||||
newPath := filepath.Dir(*goBinary)
|
newPath := filepath.Dir(*goBinary)
|
||||||
curPath := os.Getenv("PATH")
|
curPath := os.Getenv("PATH")
|
||||||
if curPath != "" {
|
if curPath != "" {
|
||||||
newPath = newPath + ":" + curPath
|
newPath = newPath + ":" + curPath
|
||||||
}
|
}
|
||||||
os.Setenv("PATH", newPath)
|
t.Setenv("PATH", newPath)
|
||||||
}
|
}
|
||||||
if os.Getenv("HOME") == "" {
|
if os.Getenv("HOME") == "" {
|
||||||
os.Setenv("HOME", "/tmp")
|
t.Setenv("HOME", "/tmp")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user