mirror of
https://github.com/ahmetb/kubectx.git
synced 2025-06-21 21:24:24 +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)
|
list := filepath.SplitList(v)
|
||||||
if len(list) > 1 {
|
if len(list) > 1 {
|
||||||
// TODO KUBECONFIG=file1:file2 currently not supported
|
// 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
|
return v, nil
|
||||||
}
|
}
|
||||||
@ -55,7 +55,6 @@ func kubeconfigPath() (string, error) {
|
|||||||
if home == "" {
|
if home == "" {
|
||||||
return "", errors.New("HOME or USERPROFILE environment variable not set")
|
return "", errors.New("HOME or USERPROFILE environment variable not set")
|
||||||
}
|
}
|
||||||
|
|
||||||
// return default path
|
// return default path
|
||||||
return filepath.Join(home, ".kube", "config"), nil
|
return filepath.Join(home, ".kube", "config"), nil
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,6 @@ func (k *Kubeconfig) Parse() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
k.f = f
|
k.f = f
|
||||||
|
|
||||||
var v yaml.Node
|
var v yaml.Node
|
||||||
if err := yaml.NewDecoder(f).Decode(&v); err != nil {
|
if err := yaml.NewDecoder(f).Decode(&v); err != nil {
|
||||||
return errors.Wrap(err, "failed to decode")
|
return errors.Wrap(err, "failed to decode")
|
||||||
|
@ -8,46 +8,84 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_kubeconfigPath_homePath(t *testing.T) {
|
func Test_homeDir(t *testing.T) {
|
||||||
origHome := os.Getenv("HOME")
|
type env struct{ k, v string }
|
||||||
os.Setenv("HOME", "/foo/bar")
|
cases := []struct {
|
||||||
defer os.Setenv("HOME", origHome)
|
name string
|
||||||
|
envs []env
|
||||||
got, err := kubeconfigPath()
|
want string
|
||||||
if err != nil {
|
}{
|
||||||
t.Fatal(err)
|
{
|
||||||
|
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 {
|
for _, c := range cases {
|
||||||
t.Fatalf("wrong value: expected=%s got=%s", expected, got)
|
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) {
|
func Test_kubeconfigPath(t *testing.T) {
|
||||||
origHome := os.Getenv("HOME")
|
defer withTestVar("HOME", "/x/y/z")()
|
||||||
os.Unsetenv("HOME")
|
|
||||||
os.Setenv("USERPROFILE", "/foo/bar")
|
|
||||||
defer os.Setenv("HOME", origHome)
|
|
||||||
|
|
||||||
|
expected := filepath.FromSlash("/x/y/z/.kube/config")
|
||||||
got, err := kubeconfigPath()
|
got, err := kubeconfigPath()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
expected := filepath.Join(filepath.FromSlash("/foo/bar"), ".kube", "config")
|
|
||||||
|
|
||||||
if got != expected {
|
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) {
|
func Test_kubeconfigPath_noEnvVars(t *testing.T) {
|
||||||
origHome := os.Getenv("HOME")
|
defer withTestVar("XDG_CACHE_HOME", "")()
|
||||||
origUserprofile := os.Getenv("USERPROFILE")
|
defer withTestVar("HOME", "")()
|
||||||
os.Unsetenv("HOME")
|
defer withTestVar("USERPROFILE", "")()
|
||||||
os.Unsetenv("USERPROFILE")
|
|
||||||
defer os.Setenv("HOME", origHome)
|
|
||||||
defer os.Setenv("USERPROFILE", origUserprofile)
|
|
||||||
|
|
||||||
_, err := kubeconfigPath()
|
_, err := kubeconfigPath()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@ -56,8 +94,7 @@ func Test_kubeconfigPath_noEnvVars(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_kubeconfigPath_envOvveride(t *testing.T) {
|
func Test_kubeconfigPath_envOvveride(t *testing.T) {
|
||||||
os.Setenv("KUBECONFIG", "foo")
|
defer withTestVar("KUBECONFIG", "foo")()
|
||||||
defer os.Unsetenv("KUBECONFIG")
|
|
||||||
|
|
||||||
v, err := kubeconfigPath()
|
v, err := kubeconfigPath()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -70,8 +107,7 @@ func Test_kubeconfigPath_envOvveride(t *testing.T) {
|
|||||||
|
|
||||||
func Test_kubeconfigPath_envOvverideDoesNotSupportPathSeparator(t *testing.T) {
|
func Test_kubeconfigPath_envOvverideDoesNotSupportPathSeparator(t *testing.T) {
|
||||||
path := strings.Join([]string{"file1", "file2"}, string(os.PathListSeparator))
|
path := strings.Join([]string{"file1", "file2"}, string(os.PathListSeparator))
|
||||||
os.Setenv("KUBECONFIG", path)
|
defer withTestVar("KUBECONFIG", path)()
|
||||||
defer os.Unsetenv("KUBECONFIG")
|
|
||||||
|
|
||||||
_, err := kubeconfigPath()
|
_, err := kubeconfigPath()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user