Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions pkg/minikube/kubeconfig/kubeconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package kubeconfig
import (
"os"
"path/filepath"
"runtime"
"strconv"
"testing"

Expand Down Expand Up @@ -767,7 +768,20 @@ func contextEquals(aContext, bContext *api.Context) bool {
return true
}

func equalPaths(a, b string) bool {
return filepath.Clean(filepath.ToSlash(a)) == filepath.Clean(filepath.ToSlash(b))
}

func TestGetKubeConfigPath(t *testing.T) {
sep := string(os.PathListSeparator)

// Ensure $HOME expands sensibly on Windows where HOME may be unset.
if runtime.GOOS == "windows" && os.Getenv("HOME") == "" {
if h, err := os.UserHomeDir(); err == nil && h != "" {
_ = os.Setenv("HOME", h)
}
}

var tests = []struct {
input string
want string
Expand All @@ -777,15 +791,17 @@ func TestGetKubeConfigPath(t *testing.T) {
want: "/home/fake/.kube/.kubeconfig",
},
{
input: "/home/fake/.kube/.kubeconfig:/home/fake2/.kubeconfig",
// multiple entries, first should be chosen
input: "/home/fake/.kube/.kubeconfig" + sep + "/home/fake2/.kubeconfig",
want: "/home/fake/.kube/.kubeconfig",
},
{
input: ":/home/fake/.kube/.kubeconfig:/home/fake2/.kubeconfig",
// leading empty entry should be skipped
input: sep + "/home/fake/.kube/.kubeconfig" + sep + "/home/fake2/.kubeconfig",
want: "/home/fake/.kube/.kubeconfig",
},
{
input: ":",
input: sep,
want: "$HOME/.kube/config",
},
{
Expand All @@ -796,8 +812,10 @@ func TestGetKubeConfigPath(t *testing.T) {

for _, test := range tests {
t.Setenv(clientcmd.RecommendedConfigPathEnvVar, test.input)
if result := PathFromEnv(); result != os.ExpandEnv(test.want) {
t.Errorf("Expected first split chunk, got: %s", result)
result := PathFromEnv()
expandedWant := os.ExpandEnv(test.want)
if !equalPaths(result, expandedWant) {
t.Errorf("Expected first split chunk, got: %s (want %s)", result, expandedWant)
}
}
}