mirror of
https://github.com/ahmetb/kubectx.git
synced 2025-09-29 00:46:02 +00:00
53 lines
990 B
Go
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")
|
|
}
|
|
}
|