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. Kubernetes-commit: 2181eea48435310d1b6e366ea8db2968c4941b93
This commit is contained in:
parent
5ef32b94c0
commit
facdb16b53
|
@ -342,7 +342,7 @@ func TestKubectlCommandHeadersHooks(t *testing.T) {
|
||||||
if kubeConfigFlags.WrapConfigFn != nil {
|
if kubeConfigFlags.WrapConfigFn != nil {
|
||||||
t.Fatal("expected initial nil WrapConfigFn")
|
t.Fatal("expected initial nil WrapConfigFn")
|
||||||
}
|
}
|
||||||
os.Setenv(kubectlCmdHeaders, testCase.envVar)
|
t.Setenv(kubectlCmdHeaders, testCase.envVar)
|
||||||
addCmdHeaderHooks(cmds, kubeConfigFlags)
|
addCmdHeaderHooks(cmds, kubeConfigFlags)
|
||||||
// Valdidate whether the hooks were added.
|
// Valdidate whether the hooks were added.
|
||||||
if testCase.addsHooks && kubeConfigFlags.WrapConfigFn == nil {
|
if testCase.addsHooks && kubeConfigFlags.WrapConfigFn == nil {
|
||||||
|
|
|
@ -293,8 +293,8 @@ func TestCreateConfigMap(t *testing.T) {
|
||||||
"create_get_env_from_env_file_configmap": {
|
"create_get_env_from_env_file_configmap": {
|
||||||
configMapName: "get_env",
|
configMapName: "get_env",
|
||||||
setup: func() func(t *testing.T, configMapOptions *ConfigMapOptions) func() {
|
setup: func() func(t *testing.T, configMapOptions *ConfigMapOptions) func() {
|
||||||
os.Setenv("g_key1", "1")
|
t.Setenv("g_key1", "1")
|
||||||
os.Setenv("g_key2", "2")
|
t.Setenv("g_key2", "2")
|
||||||
return setupEnvFile([][]string{{"g_key1", "g_key2="}})
|
return setupEnvFile([][]string{{"g_key1", "g_key2="}})
|
||||||
}(),
|
}(),
|
||||||
fromEnvFile: []string{"file.env"},
|
fromEnvFile: []string{"file.env"},
|
||||||
|
@ -316,8 +316,8 @@ func TestCreateConfigMap(t *testing.T) {
|
||||||
"create_get_env_from_env_file_hash_configmap": {
|
"create_get_env_from_env_file_hash_configmap": {
|
||||||
configMapName: "get_env",
|
configMapName: "get_env",
|
||||||
setup: func() func(t *testing.T, configMapOptions *ConfigMapOptions) func() {
|
setup: func() func(t *testing.T, configMapOptions *ConfigMapOptions) func() {
|
||||||
os.Setenv("g_key1", "1")
|
t.Setenv("g_key1", "1")
|
||||||
os.Setenv("g_key2", "2")
|
t.Setenv("g_key2", "2")
|
||||||
return setupEnvFile([][]string{{"g_key1", "g_key2="}})
|
return setupEnvFile([][]string{{"g_key1", "g_key2="}})
|
||||||
}(),
|
}(),
|
||||||
fromEnvFile: []string{"file.env"},
|
fromEnvFile: []string{"file.env"},
|
||||||
|
|
|
@ -340,8 +340,8 @@ func TestCreateSecretGeneric(t *testing.T) {
|
||||||
"create_secret_get_env_from_env_file": {
|
"create_secret_get_env_from_env_file": {
|
||||||
secretName: "get_env",
|
secretName: "get_env",
|
||||||
setup: func() func(t *testing.T, secretGenericOptions *CreateSecretOptions) func() {
|
setup: func() func(t *testing.T, secretGenericOptions *CreateSecretOptions) func() {
|
||||||
os.Setenv("g_key1", "1")
|
t.Setenv("g_key1", "1")
|
||||||
os.Setenv("g_key2", "2")
|
t.Setenv("g_key2", "2")
|
||||||
return setupSecretEnvFile([][]string{{"g_key1", "g_key2="}})
|
return setupSecretEnvFile([][]string{{"g_key1", "g_key2="}})
|
||||||
}(),
|
}(),
|
||||||
fromEnvFile: []string{"file.env"},
|
fromEnvFile: []string{"file.env"},
|
||||||
|
@ -362,8 +362,8 @@ func TestCreateSecretGeneric(t *testing.T) {
|
||||||
"create_secret_get_env_from_env_file_hash": {
|
"create_secret_get_env_from_env_file_hash": {
|
||||||
secretName: "get_env",
|
secretName: "get_env",
|
||||||
setup: func() func(t *testing.T, secretGenericOptions *CreateSecretOptions) func() {
|
setup: func() func(t *testing.T, secretGenericOptions *CreateSecretOptions) func() {
|
||||||
os.Setenv("g_key1", "1")
|
t.Setenv("g_key1", "1")
|
||||||
os.Setenv("g_key2", "2")
|
t.Setenv("g_key2", "2")
|
||||||
return setupSecretEnvFile([][]string{{"g_key1", "g_key2="}})
|
return setupSecretEnvFile([][]string{{"g_key1", "g_key2="}})
|
||||||
}(),
|
}(),
|
||||||
fromEnvFile: []string{"file.env"},
|
fromEnvFile: []string{"file.env"},
|
||||||
|
|
|
@ -64,13 +64,10 @@ func (f *FakeObject) Live() runtime.Object {
|
||||||
func TestDiffProgram(t *testing.T) {
|
func TestDiffProgram(t *testing.T) {
|
||||||
externalDiffCommands := [3]string{"diff", "diff -ruN", "diff --report-identical-files"}
|
externalDiffCommands := [3]string{"diff", "diff -ruN", "diff --report-identical-files"}
|
||||||
|
|
||||||
if oriLang := os.Getenv("LANG"); oriLang != "C" {
|
t.Setenv("LANG", "C")
|
||||||
os.Setenv("LANG", "C")
|
|
||||||
defer os.Setenv("LANG", oriLang)
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, c := range externalDiffCommands {
|
for i, c := range externalDiffCommands {
|
||||||
os.Setenv("KUBECTL_EXTERNAL_DIFF", c)
|
t.Setenv("KUBECTL_EXTERNAL_DIFF", c)
|
||||||
streams, _, stdout, _ := genericiooptions.NewTestIOStreams()
|
streams, _, stdout, _ := genericiooptions.NewTestIOStreams()
|
||||||
diff := DiffProgram{
|
diff := DiffProgram{
|
||||||
IOStreams: streams,
|
IOStreams: streams,
|
||||||
|
|
|
@ -170,8 +170,8 @@ func TestEdit(t *testing.T) {
|
||||||
server := httptest.NewServer(handler)
|
server := httptest.NewServer(handler)
|
||||||
defer server.Close()
|
defer server.Close()
|
||||||
|
|
||||||
os.Setenv("KUBE_EDITOR", "testdata/test_editor.sh")
|
t.Setenv("KUBE_EDITOR", "testdata/test_editor.sh")
|
||||||
os.Setenv("KUBE_EDITOR_CALLBACK", server.URL+"/callback")
|
t.Setenv("KUBE_EDITOR_CALLBACK", server.URL+"/callback")
|
||||||
|
|
||||||
testcases := sets.NewString()
|
testcases := sets.NewString()
|
||||||
filepath.Walk("testdata", func(path string, info os.FileInfo, err error) error {
|
filepath.Walk("testdata", func(path string, info os.FileInfo, err error) error {
|
||||||
|
|
|
@ -17,7 +17,6 @@ limitations under the License.
|
||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
@ -85,12 +84,7 @@ func Test_processEnvFileLine_readEnvironment(t *testing.T) {
|
||||||
const realKey = "k8s_test_env_file_key"
|
const realKey = "k8s_test_env_file_key"
|
||||||
const realValue = `my_value`
|
const realValue = `my_value`
|
||||||
|
|
||||||
// Just in case, these two lines ensure the environment is restored to
|
t.Setenv(realKey, `my_value`)
|
||||||
// its original state.
|
|
||||||
original := os.Getenv(realKey)
|
|
||||||
defer func() { os.Setenv(realKey, original) }()
|
|
||||||
|
|
||||||
os.Setenv(realKey, `my_value`)
|
|
||||||
|
|
||||||
key, value, err := processEnvFileLine([]byte(realKey), `filename`, 3)
|
key, value, err := processEnvFileLine([]byte(realKey), `filename`, 3)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue