mirror of
https://github.com/ahmetb/kubectx.git
synced 2025-09-12 14:01:07 +00:00
Move kubeconfig utility to a shared pkg
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
77
internal/kubeconfig/contexts_test.go
Normal file
77
internal/kubeconfig/contexts_test.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package kubeconfig
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
)
|
||||
|
||||
func TestKubeconfig_ContextNames(t *testing.T) {
|
||||
tl := &testLoader{in: strings.NewReader(`
|
||||
contexts:
|
||||
- name: abc
|
||||
- name: def
|
||||
field1: value1
|
||||
- name: ghi
|
||||
foo:
|
||||
bar: zoo`)}
|
||||
|
||||
kc := new(Kubeconfig).WithLoader(tl)
|
||||
if err := kc.Parse(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
ctx := kc.ContextNames()
|
||||
expected := []string{"abc", "def", "ghi"}
|
||||
if diff := cmp.Diff(expected, ctx); diff != "" {
|
||||
t.Fatalf("%s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKubeconfig_ContextNames_noContextsEntry(t *testing.T) {
|
||||
tl := &testLoader{in: strings.NewReader(`a: b`)}
|
||||
kc := new(Kubeconfig).WithLoader(tl)
|
||||
if err := kc.Parse(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx := kc.ContextNames()
|
||||
var expected []string = nil
|
||||
if diff := cmp.Diff(expected, ctx); diff != "" {
|
||||
t.Fatalf("%s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKubeconfig_ContextNames_nonArrayContextsEntry(t *testing.T) {
|
||||
tl := &testLoader{in: strings.NewReader(`contexts: "hello"`)}
|
||||
kc := new(Kubeconfig).WithLoader(tl)
|
||||
if err := kc.Parse(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx := kc.ContextNames()
|
||||
var expected []string = nil
|
||||
if diff := cmp.Diff(expected, ctx); diff != "" {
|
||||
t.Fatalf("%s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKubeconfig_CheckContextExists(t *testing.T) {
|
||||
tl := &testLoader{in: strings.NewReader(`contexts:
|
||||
- name: c1
|
||||
- name: c2`)}
|
||||
|
||||
kc := new(Kubeconfig).WithLoader(tl)
|
||||
if err := kc.Parse(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !kc.ContextExists("c1") {
|
||||
t.Fatal("c1 actually exists; reported false")
|
||||
}
|
||||
if !kc.ContextExists("c2") {
|
||||
t.Fatal("c2 actually exists; reported false")
|
||||
}
|
||||
if kc.ContextExists("c3") {
|
||||
t.Fatal("c3 does not exist; but reported true")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user