Files
kubectx/cmd/kubectx/kubeconfig/contexts_test.go
Ahmet Alp Balkan 492e3e7053 Move ctx-related YAML parse methods to pkg
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
2020-04-29 12:52:55 -07:00

53 lines
990 B
Go

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)
_, err := kc.ParseRaw()
if 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_CheckContextExists(t *testing.T) {
tl := &testLoader{in: strings.NewReader(`contexts:
- name: c1
- name: c2`)}
kc := new(Kubeconfig).WithLoader(tl)
_, err := kc.ParseRaw()
if 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")
}
}