Merge pull request #117426 from cbandy/kubectl-testing-setenv

Replace os.Setenv with testing.T.Setenv in tests
This commit is contained in:
Kubernetes Prow Robot 2023-05-06 07:23:15 -07:00 committed by GitHub
commit 54da29dd56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 35 additions and 44 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -65,71 +65,70 @@ func TestTranslationUsingEnvVar(t *testing.T) {
testCases := []struct { testCases := []struct {
name string name string
setenvFn func() setenv map[string]string
expectedStr string expectedStr string
}{ }{
{ {
name: "Only LC_ALL is set", name: "Only LC_ALL is set",
setenvFn: func() { os.Setenv("LC_ALL", knownTestLocale) }, setenv: map[string]string{"LC_ALL": knownTestLocale},
expectedStr: expectedStrEnUSLocale, expectedStr: expectedStrEnUSLocale,
}, },
{ {
name: "Only LC_MESSAGES is set", name: "Only LC_MESSAGES is set",
setenvFn: func() { os.Setenv("LC_MESSAGES", knownTestLocale) }, setenv: map[string]string{"LC_MESSAGES": knownTestLocale},
expectedStr: expectedStrEnUSLocale, expectedStr: expectedStrEnUSLocale,
}, },
{ {
name: "Only LANG", name: "Only LANG",
setenvFn: func() { os.Setenv("LANG", knownTestLocale) }, setenv: map[string]string{"LANG": knownTestLocale},
expectedStr: expectedStrEnUSLocale, expectedStr: expectedStrEnUSLocale,
}, },
{ {
name: "LC_MESSAGES overrides LANG", name: "LC_MESSAGES overrides LANG",
setenvFn: func() { setenv: map[string]string{
os.Setenv("LANG", "be_BY.UTF-8") // Unknown locale "LANG": "be_BY.UTF-8", // Unknown locale
os.Setenv("LC_MESSAGES", knownTestLocale) "LC_MESSAGES": knownTestLocale,
}, },
expectedStr: expectedStrEnUSLocale, expectedStr: expectedStrEnUSLocale,
}, },
{ {
name: "LC_ALL overrides LANG", name: "LC_ALL overrides LANG",
setenvFn: func() { setenv: map[string]string{
os.Setenv("LANG", "be_BY.UTF-8") // Unknown locale "LANG": "be_BY.UTF-8", // Unknown locale
os.Setenv("LC_ALL", knownTestLocale) "LC_ALL": knownTestLocale,
}, },
expectedStr: expectedStrEnUSLocale, expectedStr: expectedStrEnUSLocale,
}, },
{ {
name: "LC_ALL overrides LC_MESSAGES", name: "LC_ALL overrides LC_MESSAGES",
setenvFn: func() { setenv: map[string]string{
os.Setenv("LC_MESSAGES", "be_BY.UTF-8") // Unknown locale "LC_MESSAGES": "be_BY.UTF-8", // Unknown locale
os.Setenv("LC_ALL", knownTestLocale) "LC_ALL": knownTestLocale,
}, },
expectedStr: expectedStrEnUSLocale, expectedStr: expectedStrEnUSLocale,
}, },
{ {
name: "Unknown locale in LANG", name: "Unknown locale in LANG",
setenvFn: func() { os.Setenv("LANG", "be_BY.UTF-8") }, setenv: map[string]string{"LANG": "be_BY.UTF-8"},
expectedStr: expectedStrFallback, expectedStr: expectedStrFallback,
}, },
{ {
name: "Unknown locale in LC_MESSAGES", name: "Unknown locale in LC_MESSAGES",
setenvFn: func() { os.Setenv("LC_MESSAGES", "be_BY.UTF-8") }, setenv: map[string]string{"LC_MESSAGES": "be_BY.UTF-8"},
expectedStr: expectedStrFallback, expectedStr: expectedStrFallback,
}, },
{ {
name: "Unknown locale in LC_ALL", name: "Unknown locale in LC_ALL",
setenvFn: func() { os.Setenv("LC_ALL", "be_BY.UTF-8") }, setenv: map[string]string{"LC_ALL": "be_BY.UTF-8"},
expectedStr: expectedStrFallback, expectedStr: expectedStrFallback,
}, },
{ {
name: "Invalid env var", name: "Invalid env var",
setenvFn: func() { os.Setenv("LC_MESSAGES", "fake.locale.UTF-8") }, setenv: map[string]string{"LC_MESSAGES": "fake.locale.UTF-8"},
expectedStr: expectedStrFallback, expectedStr: expectedStrFallback,
}, },
{ {
name: "No env vars", name: "No env vars",
setenvFn: func() {},
expectedStr: expectedStrFallback, expectedStr: expectedStrFallback,
}, },
} }
@ -139,13 +138,14 @@ func TestTranslationUsingEnvVar(t *testing.T) {
for _, envVar := range envVarsToBackup { for _, envVar := range envVarsToBackup {
if envVarValue := os.Getenv(envVar); envVarValue != "" { if envVarValue := os.Getenv(envVar); envVarValue != "" {
envVarValue, envVar := envVarValue, envVar envVarValue, envVar := envVarValue, envVar
t.Cleanup(func() { os.Setenv(envVar, envVarValue) })
os.Unsetenv(envVar) os.Unsetenv(envVar)
// Restore env var at the end
defer func() { os.Setenv(envVar, envVarValue) }()
} }
} }
test.setenvFn() for envVar, envVarValue := range test.setenv {
t.Setenv(envVar, envVarValue)
}
err := LoadTranslations("test", nil) err := LoadTranslations("test", nil)
if err != nil { if err != nil {