mirror of
https://github.com/ahmetb/kubectx.git
synced 2025-06-21 05:02:31 +00:00
Update tests for homeDir and kubeconfigPath()
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
parent
e5a09017d0
commit
195e6315da
@ -46,7 +46,7 @@ func kubeconfigPath() (string, error) {
|
||||
list := filepath.SplitList(v)
|
||||
if len(list) > 1 {
|
||||
// TODO KUBECONFIG=file1:file2 currently not supported
|
||||
return "", errors.New("multiple files in KUBECONFIG currently not supported")
|
||||
return "", errors.New("multiple files in KUBECONFIG are currently not supported")
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
@ -55,7 +55,6 @@ func kubeconfigPath() (string, error) {
|
||||
if home == "" {
|
||||
return "", errors.New("HOME or USERPROFILE environment variable not set")
|
||||
}
|
||||
|
||||
// return default path
|
||||
return filepath.Join(home, ".kube", "config"), nil
|
||||
}
|
||||
|
@ -44,7 +44,6 @@ func (k *Kubeconfig) Parse() error {
|
||||
}
|
||||
|
||||
k.f = f
|
||||
|
||||
var v yaml.Node
|
||||
if err := yaml.NewDecoder(f).Decode(&v); err != nil {
|
||||
return errors.Wrap(err, "failed to decode")
|
||||
|
@ -8,46 +8,84 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_kubeconfigPath_homePath(t *testing.T) {
|
||||
origHome := os.Getenv("HOME")
|
||||
os.Setenv("HOME", "/foo/bar")
|
||||
defer os.Setenv("HOME", origHome)
|
||||
|
||||
got, err := kubeconfigPath()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
func Test_homeDir(t *testing.T) {
|
||||
type env struct{ k, v string }
|
||||
cases := []struct {
|
||||
name string
|
||||
envs []env
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "XDG_CACHE_HOME precedence",
|
||||
envs: []env{
|
||||
{"XDG_CACHE_HOME", "xdg"},
|
||||
{"HOME", "home"},
|
||||
},
|
||||
want: "xdg",
|
||||
},
|
||||
{
|
||||
name: "HOME over USERPROFILE",
|
||||
envs: []env{
|
||||
{"HOME", "home"},
|
||||
{"USERPROFILE", "up"},
|
||||
},
|
||||
want: "home",
|
||||
},
|
||||
{
|
||||
name: "only USERPROFILE available",
|
||||
envs: []env{
|
||||
{"XDG_CACHE_HOME", ""},
|
||||
{"HOME", ""},
|
||||
{"USERPROFILE", "up"},
|
||||
},
|
||||
want: "up",
|
||||
},
|
||||
{
|
||||
name: "none available",
|
||||
envs: []env{
|
||||
{"XDG_CACHE_HOME", ""},
|
||||
{"HOME", ""},
|
||||
{"USERPROFILE", ""},
|
||||
},
|
||||
want: "",
|
||||
},
|
||||
}
|
||||
expected := filepath.Join(filepath.FromSlash("/foo/bar"), ".kube", "config")
|
||||
|
||||
if got != expected {
|
||||
t.Fatalf("wrong value: expected=%s got=%s", expected, got)
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(tt *testing.T) {
|
||||
var unsets []func()
|
||||
for _, e := range c.envs {
|
||||
unsets = append(unsets, withTestVar(e.k, e.v))
|
||||
}
|
||||
|
||||
got := homeDir()
|
||||
if got != c.want {
|
||||
t.Errorf("expected:%q got:%q", c.want, got)
|
||||
}
|
||||
for _, u := range unsets {
|
||||
u()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_kubeconfigPath_userprofile(t *testing.T) {
|
||||
origHome := os.Getenv("HOME")
|
||||
os.Unsetenv("HOME")
|
||||
os.Setenv("USERPROFILE", "/foo/bar")
|
||||
defer os.Setenv("HOME", origHome)
|
||||
func Test_kubeconfigPath(t *testing.T) {
|
||||
defer withTestVar("HOME", "/x/y/z")()
|
||||
|
||||
expected := filepath.FromSlash("/x/y/z/.kube/config")
|
||||
got, err := kubeconfigPath()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
expected := filepath.Join(filepath.FromSlash("/foo/bar"), ".kube", "config")
|
||||
|
||||
if got != expected {
|
||||
t.Fatalf("wrong value: expected=%s got=%s", expected, got)
|
||||
t.Fatalf("got=%q expected=%q", got, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_kubeconfigPath_noEnvVars(t *testing.T) {
|
||||
origHome := os.Getenv("HOME")
|
||||
origUserprofile := os.Getenv("USERPROFILE")
|
||||
os.Unsetenv("HOME")
|
||||
os.Unsetenv("USERPROFILE")
|
||||
defer os.Setenv("HOME", origHome)
|
||||
defer os.Setenv("USERPROFILE", origUserprofile)
|
||||
defer withTestVar("XDG_CACHE_HOME", "")()
|
||||
defer withTestVar("HOME", "")()
|
||||
defer withTestVar("USERPROFILE", "")()
|
||||
|
||||
_, err := kubeconfigPath()
|
||||
if err == nil {
|
||||
@ -56,8 +94,7 @@ func Test_kubeconfigPath_noEnvVars(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_kubeconfigPath_envOvveride(t *testing.T) {
|
||||
os.Setenv("KUBECONFIG", "foo")
|
||||
defer os.Unsetenv("KUBECONFIG")
|
||||
defer withTestVar("KUBECONFIG", "foo")()
|
||||
|
||||
v, err := kubeconfigPath()
|
||||
if err != nil {
|
||||
@ -70,8 +107,7 @@ func Test_kubeconfigPath_envOvveride(t *testing.T) {
|
||||
|
||||
func Test_kubeconfigPath_envOvverideDoesNotSupportPathSeparator(t *testing.T) {
|
||||
path := strings.Join([]string{"file1", "file2"}, string(os.PathListSeparator))
|
||||
os.Setenv("KUBECONFIG", path)
|
||||
defer os.Unsetenv("KUBECONFIG")
|
||||
defer withTestVar("KUBECONFIG", path)()
|
||||
|
||||
_, err := kubeconfigPath()
|
||||
if err == nil {
|
||||
|
Loading…
Reference in New Issue
Block a user