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:
Chris Bandy 2023-04-15 10:09:47 -05:00 committed by Kubernetes Publisher
parent 5ef32b94c0
commit facdb16b53
6 changed files with 14 additions and 23 deletions

View File

@ -342,7 +342,7 @@ func TestKubectlCommandHeadersHooks(t *testing.T) {
if kubeConfigFlags.WrapConfigFn != nil {
t.Fatal("expected initial nil WrapConfigFn")
}
os.Setenv(kubectlCmdHeaders, testCase.envVar)
t.Setenv(kubectlCmdHeaders, testCase.envVar)
addCmdHeaderHooks(cmds, kubeConfigFlags)
// Valdidate whether the hooks were added.
if testCase.addsHooks && kubeConfigFlags.WrapConfigFn == nil {

View File

@ -293,8 +293,8 @@ func TestCreateConfigMap(t *testing.T) {
"create_get_env_from_env_file_configmap": {
configMapName: "get_env",
setup: func() func(t *testing.T, configMapOptions *ConfigMapOptions) func() {
os.Setenv("g_key1", "1")
os.Setenv("g_key2", "2")
t.Setenv("g_key1", "1")
t.Setenv("g_key2", "2")
return setupEnvFile([][]string{{"g_key1", "g_key2="}})
}(),
fromEnvFile: []string{"file.env"},
@ -316,8 +316,8 @@ func TestCreateConfigMap(t *testing.T) {
"create_get_env_from_env_file_hash_configmap": {
configMapName: "get_env",
setup: func() func(t *testing.T, configMapOptions *ConfigMapOptions) func() {
os.Setenv("g_key1", "1")
os.Setenv("g_key2", "2")
t.Setenv("g_key1", "1")
t.Setenv("g_key2", "2")
return setupEnvFile([][]string{{"g_key1", "g_key2="}})
}(),
fromEnvFile: []string{"file.env"},

View File

@ -340,8 +340,8 @@ func TestCreateSecretGeneric(t *testing.T) {
"create_secret_get_env_from_env_file": {
secretName: "get_env",
setup: func() func(t *testing.T, secretGenericOptions *CreateSecretOptions) func() {
os.Setenv("g_key1", "1")
os.Setenv("g_key2", "2")
t.Setenv("g_key1", "1")
t.Setenv("g_key2", "2")
return setupSecretEnvFile([][]string{{"g_key1", "g_key2="}})
}(),
fromEnvFile: []string{"file.env"},
@ -362,8 +362,8 @@ func TestCreateSecretGeneric(t *testing.T) {
"create_secret_get_env_from_env_file_hash": {
secretName: "get_env",
setup: func() func(t *testing.T, secretGenericOptions *CreateSecretOptions) func() {
os.Setenv("g_key1", "1")
os.Setenv("g_key2", "2")
t.Setenv("g_key1", "1")
t.Setenv("g_key2", "2")
return setupSecretEnvFile([][]string{{"g_key1", "g_key2="}})
}(),
fromEnvFile: []string{"file.env"},

View File

@ -64,13 +64,10 @@ func (f *FakeObject) Live() runtime.Object {
func TestDiffProgram(t *testing.T) {
externalDiffCommands := [3]string{"diff", "diff -ruN", "diff --report-identical-files"}
if oriLang := os.Getenv("LANG"); oriLang != "C" {
os.Setenv("LANG", "C")
defer os.Setenv("LANG", oriLang)
}
t.Setenv("LANG", "C")
for i, c := range externalDiffCommands {
os.Setenv("KUBECTL_EXTERNAL_DIFF", c)
t.Setenv("KUBECTL_EXTERNAL_DIFF", c)
streams, _, stdout, _ := genericiooptions.NewTestIOStreams()
diff := DiffProgram{
IOStreams: streams,

View File

@ -170,8 +170,8 @@ func TestEdit(t *testing.T) {
server := httptest.NewServer(handler)
defer server.Close()
os.Setenv("KUBE_EDITOR", "testdata/test_editor.sh")
os.Setenv("KUBE_EDITOR_CALLBACK", server.URL+"/callback")
t.Setenv("KUBE_EDITOR", "testdata/test_editor.sh")
t.Setenv("KUBE_EDITOR_CALLBACK", server.URL+"/callback")
testcases := sets.NewString()
filepath.Walk("testdata", func(path string, info os.FileInfo, err error) error {

View File

@ -17,7 +17,6 @@ limitations under the License.
package util
import (
"os"
"strings"
"testing"
)
@ -85,12 +84,7 @@ func Test_processEnvFileLine_readEnvironment(t *testing.T) {
const realKey = "k8s_test_env_file_key"
const realValue = `my_value`
// Just in case, these two lines ensure the environment is restored to
// its original state.
original := os.Getenv(realKey)
defer func() { os.Setenv(realKey, original) }()
os.Setenv(realKey, `my_value`)
t.Setenv(realKey, `my_value`)
key, value, err := processEnvFileLine([]byte(realKey), `filename`, 3)
if err != nil {