Fixing some unit test issues for k8s.io/client-go/tools/clientcmd on Windows

Signed-off-by: Mark Rossetti <marosset@microsoft.com>

Kubernetes-commit: f67d5965c9a58910685c196b91275e24b7e35b30
This commit is contained in:
Mark Rossetti
2026-07-20 10:27:17 -07:00
committed by Kubernetes Publisher
parent 37dca455f1
commit ec89f19ca5
2 changed files with 35 additions and 33 deletions

View File

@@ -31,11 +31,11 @@ func TestModifyConfigWritesToFirstKubeconfigFile(t *testing.T) {
)
tempdir := t.TempDir()
configFile1, _ := os.Create(filepath.Join(tempdir, "kubeconfig-a"))
configFile2, _ := os.Create(filepath.Join(tempdir, "kubeconfig-b"))
configFile1 := filepath.Join(tempdir, "kubeconfig-a")
configFile2 := filepath.Join(tempdir, "kubeconfig-b")
// The first kubeconfig has everything.
err := os.WriteFile(configFile1.Name(), []byte(`
err := os.WriteFile(configFile1, []byte(`
kind: Config
apiVersion: v1
clusters:
@@ -62,7 +62,7 @@ users:
}
// The second kubeconfig declares a new context and activates it.
err = os.WriteFile(configFile2.Name(), []byte(`
err = os.WriteFile(configFile2, []byte(`
kind: Config
apiVersion: v1
contexts:
@@ -80,7 +80,7 @@ current-context: `+contextNameB+`
// Set KUBECONFIG to the files, in descending alphabetical order.
// This will be used to check that they don't get sorted.
envVarValue := fmt.Sprintf("%s%c%s", configFile2.Name(), filepath.ListSeparator, configFile1.Name())
envVarValue := fmt.Sprintf("%s%c%s", configFile2, filepath.ListSeparator, configFile1)
t.Setenv(RecommendedConfigPathEnvVar, envVarValue)
// Load the kubeconfigs, change the active context, and call ModifyConfig.
@@ -99,7 +99,7 @@ current-context: `+contextNameB+`
}
// Load the files again and check that only configFile2 was changed.
config1, err := LoadFromFile(configFile1.Name()) // file sorts first, but was specified last
config1, err := LoadFromFile(configFile1) // file sorts first, but was specified last
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
@@ -108,7 +108,7 @@ current-context: `+contextNameB+`
t.Errorf("Config should not be modified, but was. Expected %q, got %q", contextNameA, config1.CurrentContext)
}
config2, err := LoadFromFile(configFile2.Name()) // file sorts last, but was specified first
config2, err := LoadFromFile(configFile2) // file sorts last, but was specified first
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

View File

@@ -34,6 +34,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
clientcmdlatest "k8s.io/client-go/tools/clientcmd/api/latest"
"k8s.io/client-go/util/homedir"
"k8s.io/klog/v2"
)
@@ -514,27 +515,28 @@ extensions:
}
func TestResolveRelativePaths(t *testing.T) {
absoluteDir := filepath.Join(t.TempDir(), "absolute")
pathResolutionConfig1 := clientcmdapi.Config{
AuthInfos: map[string]*clientcmdapi.AuthInfo{
"relative-user-1": {ClientCertificate: "relative/client/cert", ClientKey: "../relative/client/key"},
"absolute-user-1": {ClientCertificate: "/absolute/client/cert", ClientKey: "/absolute/client/key"},
"relative-cmd-1": {Exec: &clientcmdapi.ExecConfig{Command: "../relative/client/cmd"}},
"absolute-cmd-1": {Exec: &clientcmdapi.ExecConfig{Command: "/absolute/client/cmd"}},
"relative-user-1": {ClientCertificate: filepath.Join("relative", "client", "cert"), ClientKey: filepath.Join("..", "relative", "client", "key")},
"absolute-user-1": {ClientCertificate: filepath.Join(absoluteDir, "client", "cert"), ClientKey: filepath.Join(absoluteDir, "client", "key")},
"relative-cmd-1": {Exec: &clientcmdapi.ExecConfig{Command: filepath.Join("..", "relative", "client", "cmd")}},
"absolute-cmd-1": {Exec: &clientcmdapi.ExecConfig{Command: filepath.Join(absoluteDir, "client", "cmd")}},
"PATH-cmd-1": {Exec: &clientcmdapi.ExecConfig{Command: "cmd"}},
},
Clusters: map[string]*clientcmdapi.Cluster{
"relative-server-1": {CertificateAuthority: "../relative/ca"},
"absolute-server-1": {CertificateAuthority: "/absolute/ca"},
"relative-server-1": {CertificateAuthority: filepath.Join("..", "relative", "ca")},
"absolute-server-1": {CertificateAuthority: filepath.Join(absoluteDir, "ca")},
},
}
pathResolutionConfig2 := clientcmdapi.Config{
AuthInfos: map[string]*clientcmdapi.AuthInfo{
"relative-user-2": {ClientCertificate: "relative/client/cert2", ClientKey: "../relative/client/key2"},
"absolute-user-2": {ClientCertificate: "/absolute/client/cert2", ClientKey: "/absolute/client/key2"},
"relative-user-2": {ClientCertificate: filepath.Join("relative", "client", "cert2"), ClientKey: filepath.Join("..", "relative", "client", "key2")},
"absolute-user-2": {ClientCertificate: filepath.Join(absoluteDir, "client", "cert2"), ClientKey: filepath.Join(absoluteDir, "client", "key2")},
},
Clusters: map[string]*clientcmdapi.Cluster{
"relative-server-2": {CertificateAuthority: "../relative/ca2"},
"absolute-server-2": {CertificateAuthority: "/absolute/ca2"},
"relative-server-2": {CertificateAuthority: filepath.Join("..", "relative", "ca2")},
"absolute-server-2": {CertificateAuthority: filepath.Join(absoluteDir, "ca2")},
},
}
@@ -626,29 +628,26 @@ func TestResolveRelativePaths(t *testing.T) {
}
func TestMigratingFile(t *testing.T) {
sourceFile, _ := os.CreateTemp("", "")
defer utiltesting.CloseAndRemove(t, sourceFile)
destinationFile, _ := os.CreateTemp("", "")
// delete the file so that we'll write to it
os.Remove(destinationFile.Name())
tempDir := t.TempDir()
sourceFile := filepath.Join(tempDir, "source")
destinationFile := filepath.Join(tempDir, "destination")
WriteToFile(testConfigAlfa, sourceFile.Name())
if err := WriteToFile(testConfigAlfa, sourceFile); err != nil {
t.Fatalf("unexpected error %v", err)
}
loadingRules := ClientConfigLoadingRules{
MigrationRules: map[string]string{destinationFile.Name(): sourceFile.Name()},
MigrationRules: map[string]string{destinationFile: sourceFile},
}
if _, err := loadingRules.Load(); err != nil {
t.Errorf("unexpected error %v", err)
}
// the load should have recreated this file
defer utiltesting.CloseAndRemove(t, destinationFile)
sourceContent, err := os.ReadFile(sourceFile.Name())
sourceContent, err := os.ReadFile(sourceFile)
if err != nil {
t.Errorf("unexpected error %v", err)
}
destinationContent, err := os.ReadFile(destinationFile.Name())
destinationContent, err := os.ReadFile(destinationFile)
if err != nil {
t.Errorf("unexpected error %v", err)
}
@@ -658,11 +657,11 @@ func TestMigratingFile(t *testing.T) {
}
// destination file permissions should be the same as the source file permissions
sourceInfo, err := os.Stat(sourceFile.Name())
sourceInfo, err := os.Stat(sourceFile)
if err != nil {
t.Errorf("unexpected error %v", err)
}
destinationInfo, err := os.Stat(destinationFile.Name())
destinationInfo, err := os.Stat(destinationFile)
if err != nil {
t.Errorf("unexpected error %v", err)
}
@@ -949,7 +948,10 @@ func TestLoadingGetLoadingPrecedence(t *testing.T) {
precedence []string
}{
"default": {
precedence: []string{filepath.Join(os.Getenv("HOME"), ".kube/config")},
// Reconstruct the expected default path independently from the
// home directory so the assertion validates how RecommendedHomeFile
// is assembled rather than comparing it against itself.
precedence: []string{filepath.Join(homedir.HomeDir(), RecommendedHomeDir, RecommendedFileName)},
},
"explicit": {
rules: &ClientConfigLoadingRules{
@@ -962,7 +964,7 @@ func TestLoadingGetLoadingPrecedence(t *testing.T) {
precedence: []string{"/env/kubeconfig"},
},
"envvar-multiple": {
env: "/env/kubeconfig:/other/kubeconfig",
env: strings.Join([]string{"/env/kubeconfig", "/other/kubeconfig"}, string(filepath.ListSeparator)),
precedence: []string{"/env/kubeconfig", "/other/kubeconfig"},
},
}