mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-04 04:08:16 +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:
		@@ -249,18 +249,14 @@ func TestNewCmdToken(t *testing.T) {
 | 
			
		||||
			if _, err = f.WriteString(tc.configToWrite); err != nil {
 | 
			
		||||
				t.Errorf("Unable to write test file %q: %v", fullPath, err)
 | 
			
		||||
			}
 | 
			
		||||
			// store the current value of the environment variable.
 | 
			
		||||
			storedEnv := os.Getenv(clientcmd.RecommendedConfigPathEnvVar)
 | 
			
		||||
			if tc.kubeConfigEnv != "" {
 | 
			
		||||
				os.Setenv(clientcmd.RecommendedConfigPathEnvVar, tc.kubeConfigEnv)
 | 
			
		||||
				t.Setenv(clientcmd.RecommendedConfigPathEnvVar, tc.kubeConfigEnv)
 | 
			
		||||
			}
 | 
			
		||||
			cmd.SetArgs(tc.args)
 | 
			
		||||
			err := cmd.Execute()
 | 
			
		||||
			if (err != nil) != tc.expectedError {
 | 
			
		||||
				t.Errorf("Test case %q: newCmdToken expected error: %v, saw: %v", tc.name, tc.expectedError, (err != nil))
 | 
			
		||||
			}
 | 
			
		||||
			// restore the environment variable.
 | 
			
		||||
			os.Setenv(clientcmd.RecommendedConfigPathEnvVar, storedEnv)
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -27,11 +27,9 @@ import (
 | 
			
		||||
	"k8s.io/apimachinery/pkg/util/version"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func runKubeadmInit(args ...string) (string, string, int, error) {
 | 
			
		||||
	const dryRunDir = "KUBEADM_INIT_DRYRUN_DIR"
 | 
			
		||||
	if err := os.Setenv(dryRunDir, os.TempDir()); err != nil {
 | 
			
		||||
		panic(fmt.Sprintf("could not set the %s environment variable", dryRunDir))
 | 
			
		||||
	}
 | 
			
		||||
func runKubeadmInit(t testing.TB, args ...string) (string, string, int, error) {
 | 
			
		||||
	t.Helper()
 | 
			
		||||
	t.Setenv("KUBEADM_INIT_DRYRUN_DIR", os.TempDir())
 | 
			
		||||
	kubeadmPath := getKubeadmPath()
 | 
			
		||||
	kubeadmArgs := []string{"init", "--dry-run", "--ignore-preflight-errors=all"}
 | 
			
		||||
	kubeadmArgs = append(kubeadmArgs, args...)
 | 
			
		||||
@@ -73,7 +71,7 @@ func TestCmdInitToken(t *testing.T) {
 | 
			
		||||
 | 
			
		||||
	for _, rt := range initTest {
 | 
			
		||||
		t.Run(rt.name, func(t *testing.T) {
 | 
			
		||||
			_, _, _, err := runKubeadmInit(rt.args)
 | 
			
		||||
			_, _, _, err := runKubeadmInit(t, rt.args)
 | 
			
		||||
			if (err == nil) != rt.expected {
 | 
			
		||||
				t.Fatalf(dedent.Dedent(`
 | 
			
		||||
					CmdInitToken test case %q failed with an error: %v
 | 
			
		||||
@@ -112,7 +110,7 @@ func TestCmdInitKubernetesVersion(t *testing.T) {
 | 
			
		||||
 | 
			
		||||
	for _, rt := range initTest {
 | 
			
		||||
		t.Run(rt.name, func(t *testing.T) {
 | 
			
		||||
			_, _, _, err := runKubeadmInit(rt.args)
 | 
			
		||||
			_, _, _, err := runKubeadmInit(t, rt.args)
 | 
			
		||||
			if (err == nil) != rt.expected {
 | 
			
		||||
				t.Fatalf(dedent.Dedent(`
 | 
			
		||||
					CmdInitKubernetesVersion test case %q failed with an error: %v
 | 
			
		||||
@@ -176,7 +174,7 @@ func TestCmdInitConfig(t *testing.T) {
 | 
			
		||||
 | 
			
		||||
	for _, rt := range initTest {
 | 
			
		||||
		t.Run(rt.name, func(t *testing.T) {
 | 
			
		||||
			_, _, _, err := runKubeadmInit(rt.args)
 | 
			
		||||
			_, _, _, err := runKubeadmInit(t, rt.args)
 | 
			
		||||
			if (err == nil) != rt.expected {
 | 
			
		||||
				t.Fatalf(dedent.Dedent(`
 | 
			
		||||
						CmdInitConfig test case %q failed with an error: %v
 | 
			
		||||
@@ -225,7 +223,7 @@ func TestCmdInitAPIPort(t *testing.T) {
 | 
			
		||||
 | 
			
		||||
	for _, rt := range initTest {
 | 
			
		||||
		t.Run(rt.name, func(t *testing.T) {
 | 
			
		||||
			_, _, _, err := runKubeadmInit(rt.args)
 | 
			
		||||
			_, _, _, err := runKubeadmInit(t, rt.args)
 | 
			
		||||
			if (err == nil) != rt.expected {
 | 
			
		||||
				t.Fatalf(dedent.Dedent(`
 | 
			
		||||
							CmdInitAPIPort test case %q failed with an error: %v
 | 
			
		||||
@@ -267,7 +265,7 @@ func TestCmdInitFeatureGates(t *testing.T) {
 | 
			
		||||
 | 
			
		||||
	for _, rt := range initTest {
 | 
			
		||||
		t.Run(rt.name, func(t *testing.T) {
 | 
			
		||||
			_, _, exitcode, err := runKubeadmInit(rt.args)
 | 
			
		||||
			_, _, exitcode, err := runKubeadmInit(t, rt.args)
 | 
			
		||||
			if exitcode == PanicExitcode {
 | 
			
		||||
				t.Fatalf(dedent.Dedent(`
 | 
			
		||||
							CmdInitFeatureGates test case %q failed with an error: %v
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user