mirror of
https://github.com/ahmetb/kubectx.git
synced 2025-07-16 16:23:08 +00:00
Create test utils for crafting kubeconfig strings
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
parent
62d8dad7d5
commit
342d21683b
@ -4,6 +4,8 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
|
||||
"github.com/ahmetb/kubectx/internal/testutil"
|
||||
)
|
||||
|
||||
func TestKubeconfig_DeleteContextEntry_errors(t *testing.T) {
|
||||
@ -31,7 +33,10 @@ func TestKubeconfig_DeleteContextEntry_errors(t *testing.T) {
|
||||
|
||||
func TestKubeconfig_DeleteContextEntry(t *testing.T) {
|
||||
test := WithMockKubeconfigLoader(
|
||||
`contexts: [{name: c1}, {name: c2}, {name: c3}]`)
|
||||
testutil.KC().WithCtxs(
|
||||
testutil.Ctx("c1"),
|
||||
testutil.Ctx("c2"),
|
||||
testutil.Ctx("c3")).ToYAML(t))
|
||||
kc := new(Kubeconfig).WithLoader(test)
|
||||
if err := kc.Parse(); err != nil {
|
||||
t.Fatal(err)
|
||||
@ -43,7 +48,9 @@ func TestKubeconfig_DeleteContextEntry(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
expected := "contexts: [{name: c2}, {name: c3}]\n"
|
||||
expected := testutil.KC().WithCtxs(
|
||||
testutil.Ctx("c2"),
|
||||
testutil.Ctx("c3")).ToYAML(t)
|
||||
out := test.Output()
|
||||
if diff := cmp.Diff(expected, out); diff != "" {
|
||||
t.Fatalf("diff: %s", diff)
|
||||
@ -51,8 +58,8 @@ func TestKubeconfig_DeleteContextEntry(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestKubeconfig_ModifyCurrentContext_fieldExists(t *testing.T) {
|
||||
test := WithMockKubeconfigLoader(`current-context: abc
|
||||
field1: value1`)
|
||||
test := WithMockKubeconfigLoader(
|
||||
testutil.KC().WithCurrentCtx("abc").Set("field1", "value1").ToYAML(t))
|
||||
kc := new(Kubeconfig).WithLoader(test)
|
||||
if err := kc.Parse(); err != nil {
|
||||
t.Fatal(err)
|
||||
@ -64,8 +71,7 @@ field1: value1`)
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
expected := `current-context: foo
|
||||
field1: value1` + "\n"
|
||||
expected := testutil.KC().WithCurrentCtx("foo").Set("field1", "value1").ToYAML(t)
|
||||
out := test.Output()
|
||||
if diff := cmp.Diff(expected, out); diff != "" {
|
||||
t.Fatalf("diff: %s", diff)
|
||||
@ -73,8 +79,7 @@ field1: value1` + "\n"
|
||||
}
|
||||
|
||||
func TestKubeconfig_ModifyCurrentContext_fieldMissing(t *testing.T) {
|
||||
test := WithMockKubeconfigLoader(
|
||||
`field1: value1`)
|
||||
test := WithMockKubeconfigLoader(`f1: v1`)
|
||||
kc := new(Kubeconfig).WithLoader(test)
|
||||
if err := kc.Parse(); err != nil {
|
||||
t.Fatal(err)
|
||||
@ -86,7 +91,9 @@ func TestKubeconfig_ModifyCurrentContext_fieldMissing(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
expected := `field1: value1` + "\n" + "current-context: foo\n"
|
||||
expected := `f1: v1
|
||||
current-context: foo
|
||||
`
|
||||
out := test.Output()
|
||||
if diff := cmp.Diff(expected, out); diff != "" {
|
||||
t.Fatalf("diff: %s", diff)
|
||||
@ -95,8 +102,7 @@ func TestKubeconfig_ModifyCurrentContext_fieldMissing(t *testing.T) {
|
||||
|
||||
func TestKubeconfig_ModifyContextName_noContextsEntryError(t *testing.T) {
|
||||
// no context entries
|
||||
test := WithMockKubeconfigLoader(
|
||||
`a: b`)
|
||||
test := WithMockKubeconfigLoader(`a: b`)
|
||||
kc := new(Kubeconfig).WithLoader(test)
|
||||
if err := kc.Parse(); err != nil {
|
||||
t.Fatal(err)
|
||||
@ -106,7 +112,6 @@ func TestKubeconfig_ModifyContextName_noContextsEntryError(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func TestKubeconfig_ModifyContextName_contextsEntryNotSequenceError(t *testing.T) {
|
||||
// no context entries
|
||||
test := WithMockKubeconfigLoader(
|
||||
@ -120,10 +125,11 @@ func TestKubeconfig_ModifyContextName_contextsEntryNotSequenceError(t *testing.T
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func TestKubeconfig_ModifyContextName_noChange(t *testing.T) {
|
||||
test := WithMockKubeconfigLoader(
|
||||
`contexts: [{name: c1}, {name: c2}, {name: c3}]`)
|
||||
test := WithMockKubeconfigLoader(testutil.KC().WithCtxs(
|
||||
testutil.Ctx("c1"),
|
||||
testutil.Ctx("c2"),
|
||||
testutil.Ctx("c3")).ToYAML(t))
|
||||
kc := new(Kubeconfig).WithLoader(test)
|
||||
if err := kc.Parse(); err != nil {
|
||||
t.Fatal(err)
|
||||
@ -134,8 +140,10 @@ func TestKubeconfig_ModifyContextName_noChange(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestKubeconfig_ModifyContextName(t *testing.T) {
|
||||
test := WithMockKubeconfigLoader(
|
||||
`contexts: [{name: c1}, {name: c2}, {name: c3}]`)
|
||||
test := WithMockKubeconfigLoader(testutil.KC().WithCtxs(
|
||||
testutil.Ctx("c1"),
|
||||
testutil.Ctx("c2"),
|
||||
testutil.Ctx("c3")).ToYAML(t))
|
||||
kc := new(Kubeconfig).WithLoader(test)
|
||||
if err := kc.Parse(); err != nil {
|
||||
t.Fatal(err)
|
||||
@ -147,7 +155,10 @@ func TestKubeconfig_ModifyContextName(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
expected := "contexts: [{name: ccc}, {name: c2}, {name: c3}]\n"
|
||||
expected := testutil.KC().WithCtxs(
|
||||
testutil.Ctx("ccc"),
|
||||
testutil.Ctx("c2"),
|
||||
testutil.Ctx("c3")).ToYAML(t)
|
||||
out := test.Output()
|
||||
if diff := cmp.Diff(expected, out); diff != "" {
|
||||
t.Fatalf("diff: %s", diff)
|
||||
|
@ -4,18 +4,16 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
|
||||
"github.com/ahmetb/kubectx/internal/testutil"
|
||||
)
|
||||
|
||||
func TestKubeconfig_ContextNames(t *testing.T) {
|
||||
tl := WithMockKubeconfigLoader(`
|
||||
contexts:
|
||||
- name: abc
|
||||
- name: def
|
||||
field1: value1
|
||||
- name: ghi
|
||||
foo:
|
||||
bar: zoo`)
|
||||
|
||||
tl := WithMockKubeconfigLoader(
|
||||
testutil.KC().WithCtxs(
|
||||
testutil.Ctx("abc"),
|
||||
testutil.Ctx("def"),
|
||||
testutil.Ctx("ghi")).Set("field1", map[string]string{"bar": "zoo"}).ToYAML(t))
|
||||
kc := new(Kubeconfig).WithLoader(tl)
|
||||
if err := kc.Parse(); err != nil {
|
||||
t.Fatal(err)
|
||||
@ -55,9 +53,10 @@ func TestKubeconfig_ContextNames_nonArrayContextsEntry(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestKubeconfig_CheckContextExists(t *testing.T) {
|
||||
tl := WithMockKubeconfigLoader(`contexts:
|
||||
- name: c1
|
||||
- name: c2`)
|
||||
tl := WithMockKubeconfigLoader(
|
||||
testutil.KC().WithCtxs(
|
||||
testutil.Ctx("c1"),
|
||||
testutil.Ctx("c2")).ToYAML(t))
|
||||
|
||||
kc := new(Kubeconfig).WithLoader(tl)
|
||||
if err := kc.Parse(); err != nil {
|
||||
|
@ -2,6 +2,8 @@ package kubeconfig
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/ahmetb/kubectx/internal/testutil"
|
||||
)
|
||||
|
||||
func TestKubeconfig_GetCurrentContext(t *testing.T) {
|
||||
@ -33,7 +35,7 @@ func TestKubeconfig_GetCurrentContext_missingField(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestKubeconfig_UnsetCurrentContext(t *testing.T) {
|
||||
tl := WithMockKubeconfigLoader(`current-context: foo`)
|
||||
tl := WithMockKubeconfigLoader(testutil.KC().WithCurrentCtx("foo").ToYAML(t))
|
||||
kc := new(Kubeconfig).WithLoader(tl)
|
||||
if err := kc.Parse(); err != nil {
|
||||
t.Fatal(err)
|
||||
@ -46,8 +48,7 @@ func TestKubeconfig_UnsetCurrentContext(t *testing.T) {
|
||||
}
|
||||
|
||||
out := tl.Output()
|
||||
expected := `current-context: ""
|
||||
`
|
||||
expected := testutil.KC().WithCurrentCtx("").ToYAML(t)
|
||||
if out != expected {
|
||||
t.Fatalf("expected=%q; got=%q", expected, out)
|
||||
}
|
||||
|
@ -60,6 +60,6 @@ func (k *Kubeconfig) Save() error {
|
||||
return errors.Wrap(err, "failed to reset file")
|
||||
}
|
||||
enc := yaml.NewEncoder(k.f)
|
||||
enc.SetIndent(2)
|
||||
enc.SetIndent(0)
|
||||
return enc.Encode(k.rootNode)
|
||||
}
|
||||
|
@ -4,6 +4,8 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
|
||||
"github.com/ahmetb/kubectx/internal/testutil"
|
||||
)
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
@ -21,21 +23,30 @@ func TestParse(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = new(Kubeconfig).WithLoader(WithMockKubeconfigLoader(testutil.KC().
|
||||
WithCurrentCtx("foo").
|
||||
WithCtxs().ToYAML(t))).Parse()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSave(t *testing.T) {
|
||||
in := `a: [1, 2, 3]` + "\n"
|
||||
in := "a: [1, 2, 3]\n"
|
||||
test := WithMockKubeconfigLoader(in)
|
||||
kc := new(Kubeconfig).WithLoader(test)
|
||||
defer kc.Close()
|
||||
if err := kc.Parse(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := kc.ModifyCurrentContext("hello"); err != nil {t.Fatal(err)}
|
||||
if err := kc.ModifyCurrentContext("hello"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := kc.Save(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
expected := in+"current-context: hello\n"
|
||||
expected := "a: [1, 2, 3]\ncurrent-context: hello\n"
|
||||
if diff := cmp.Diff(expected, test.Output()); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
|
36
internal/testutil/kubeconfigbuilder.go
Normal file
36
internal/testutil/kubeconfigbuilder.go
Normal file
@ -0,0 +1,36 @@
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type Context struct {
|
||||
Name string `yaml:"name,omitempty"`
|
||||
Namespace string `yaml:"namespace,omitempty"`
|
||||
}
|
||||
|
||||
func Ctx(name string) *Context { return &Context{Name: name} }
|
||||
|
||||
type Kubeconfig map[string]interface{}
|
||||
|
||||
func KC() *Kubeconfig {
|
||||
return &Kubeconfig{
|
||||
"apiVersion": "v1",
|
||||
"kind": "Config"}
|
||||
}
|
||||
|
||||
func (k *Kubeconfig) Set(key string, v interface{}) *Kubeconfig { (*k)[key] = v; return k }
|
||||
func (k *Kubeconfig) WithCurrentCtx(s string) *Kubeconfig { (*k)["current-context"] = s; return k }
|
||||
func (k *Kubeconfig) WithCtxs(c ...*Context) *Kubeconfig { (*k)["contexts"] = c; return k }
|
||||
|
||||
func (k *Kubeconfig) ToYAML(t *testing.T) string {
|
||||
t.Helper()
|
||||
var v strings.Builder
|
||||
if err := yaml.NewEncoder(&v).Encode(*k); err != nil {
|
||||
t.Fatalf("failed to encode mock kubeconfig: %v", err)
|
||||
}
|
||||
return v.String()
|
||||
}
|
Loading…
Reference in New Issue
Block a user