Create test utils for crafting kubeconfig strings

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
Ahmet Alp Balkan 2020-04-16 21:37:29 -07:00
parent 62d8dad7d5
commit 342d21683b
No known key found for this signature in database
GPG Key ID: 441833503E604E2C
6 changed files with 95 additions and 37 deletions

View File

@ -4,6 +4,8 @@ import (
"testing" "testing"
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
"github.com/ahmetb/kubectx/internal/testutil"
) )
func TestKubeconfig_DeleteContextEntry_errors(t *testing.T) { func TestKubeconfig_DeleteContextEntry_errors(t *testing.T) {
@ -31,7 +33,10 @@ func TestKubeconfig_DeleteContextEntry_errors(t *testing.T) {
func TestKubeconfig_DeleteContextEntry(t *testing.T) { func TestKubeconfig_DeleteContextEntry(t *testing.T) {
test := WithMockKubeconfigLoader( 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) kc := new(Kubeconfig).WithLoader(test)
if err := kc.Parse(); err != nil { if err := kc.Parse(); err != nil {
t.Fatal(err) t.Fatal(err)
@ -43,7 +48,9 @@ func TestKubeconfig_DeleteContextEntry(t *testing.T) {
t.Fatal(err) 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() out := test.Output()
if diff := cmp.Diff(expected, out); diff != "" { if diff := cmp.Diff(expected, out); diff != "" {
t.Fatalf("diff: %s", diff) t.Fatalf("diff: %s", diff)
@ -51,8 +58,8 @@ func TestKubeconfig_DeleteContextEntry(t *testing.T) {
} }
func TestKubeconfig_ModifyCurrentContext_fieldExists(t *testing.T) { func TestKubeconfig_ModifyCurrentContext_fieldExists(t *testing.T) {
test := WithMockKubeconfigLoader(`current-context: abc test := WithMockKubeconfigLoader(
field1: value1`) testutil.KC().WithCurrentCtx("abc").Set("field1", "value1").ToYAML(t))
kc := new(Kubeconfig).WithLoader(test) kc := new(Kubeconfig).WithLoader(test)
if err := kc.Parse(); err != nil { if err := kc.Parse(); err != nil {
t.Fatal(err) t.Fatal(err)
@ -64,8 +71,7 @@ field1: value1`)
t.Fatal(err) t.Fatal(err)
} }
expected := `current-context: foo expected := testutil.KC().WithCurrentCtx("foo").Set("field1", "value1").ToYAML(t)
field1: value1` + "\n"
out := test.Output() out := test.Output()
if diff := cmp.Diff(expected, out); diff != "" { if diff := cmp.Diff(expected, out); diff != "" {
t.Fatalf("diff: %s", diff) t.Fatalf("diff: %s", diff)
@ -73,8 +79,7 @@ field1: value1` + "\n"
} }
func TestKubeconfig_ModifyCurrentContext_fieldMissing(t *testing.T) { func TestKubeconfig_ModifyCurrentContext_fieldMissing(t *testing.T) {
test := WithMockKubeconfigLoader( test := WithMockKubeconfigLoader(`f1: v1`)
`field1: value1`)
kc := new(Kubeconfig).WithLoader(test) kc := new(Kubeconfig).WithLoader(test)
if err := kc.Parse(); err != nil { if err := kc.Parse(); err != nil {
t.Fatal(err) t.Fatal(err)
@ -86,7 +91,9 @@ func TestKubeconfig_ModifyCurrentContext_fieldMissing(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
expected := `field1: value1` + "\n" + "current-context: foo\n" expected := `f1: v1
current-context: foo
`
out := test.Output() out := test.Output()
if diff := cmp.Diff(expected, out); diff != "" { if diff := cmp.Diff(expected, out); diff != "" {
t.Fatalf("diff: %s", diff) t.Fatalf("diff: %s", diff)
@ -95,8 +102,7 @@ func TestKubeconfig_ModifyCurrentContext_fieldMissing(t *testing.T) {
func TestKubeconfig_ModifyContextName_noContextsEntryError(t *testing.T) { func TestKubeconfig_ModifyContextName_noContextsEntryError(t *testing.T) {
// no context entries // no context entries
test := WithMockKubeconfigLoader( test := WithMockKubeconfigLoader(`a: b`)
`a: b`)
kc := new(Kubeconfig).WithLoader(test) kc := new(Kubeconfig).WithLoader(test)
if err := kc.Parse(); err != nil { if err := kc.Parse(); err != nil {
t.Fatal(err) t.Fatal(err)
@ -106,7 +112,6 @@ func TestKubeconfig_ModifyContextName_noContextsEntryError(t *testing.T) {
} }
} }
func TestKubeconfig_ModifyContextName_contextsEntryNotSequenceError(t *testing.T) { func TestKubeconfig_ModifyContextName_contextsEntryNotSequenceError(t *testing.T) {
// no context entries // no context entries
test := WithMockKubeconfigLoader( test := WithMockKubeconfigLoader(
@ -120,10 +125,11 @@ func TestKubeconfig_ModifyContextName_contextsEntryNotSequenceError(t *testing.T
} }
} }
func TestKubeconfig_ModifyContextName_noChange(t *testing.T) { func TestKubeconfig_ModifyContextName_noChange(t *testing.T) {
test := WithMockKubeconfigLoader( test := WithMockKubeconfigLoader(testutil.KC().WithCtxs(
`contexts: [{name: c1}, {name: c2}, {name: c3}]`) testutil.Ctx("c1"),
testutil.Ctx("c2"),
testutil.Ctx("c3")).ToYAML(t))
kc := new(Kubeconfig).WithLoader(test) kc := new(Kubeconfig).WithLoader(test)
if err := kc.Parse(); err != nil { if err := kc.Parse(); err != nil {
t.Fatal(err) t.Fatal(err)
@ -134,8 +140,10 @@ func TestKubeconfig_ModifyContextName_noChange(t *testing.T) {
} }
func TestKubeconfig_ModifyContextName(t *testing.T) { func TestKubeconfig_ModifyContextName(t *testing.T) {
test := WithMockKubeconfigLoader( test := WithMockKubeconfigLoader(testutil.KC().WithCtxs(
`contexts: [{name: c1}, {name: c2}, {name: c3}]`) testutil.Ctx("c1"),
testutil.Ctx("c2"),
testutil.Ctx("c3")).ToYAML(t))
kc := new(Kubeconfig).WithLoader(test) kc := new(Kubeconfig).WithLoader(test)
if err := kc.Parse(); err != nil { if err := kc.Parse(); err != nil {
t.Fatal(err) t.Fatal(err)
@ -147,7 +155,10 @@ func TestKubeconfig_ModifyContextName(t *testing.T) {
t.Fatal(err) 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() out := test.Output()
if diff := cmp.Diff(expected, out); diff != "" { if diff := cmp.Diff(expected, out); diff != "" {
t.Fatalf("diff: %s", diff) t.Fatalf("diff: %s", diff)

View File

@ -4,18 +4,16 @@ import (
"testing" "testing"
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
"github.com/ahmetb/kubectx/internal/testutil"
) )
func TestKubeconfig_ContextNames(t *testing.T) { func TestKubeconfig_ContextNames(t *testing.T) {
tl := WithMockKubeconfigLoader(` tl := WithMockKubeconfigLoader(
contexts: testutil.KC().WithCtxs(
- name: abc testutil.Ctx("abc"),
- name: def testutil.Ctx("def"),
field1: value1 testutil.Ctx("ghi")).Set("field1", map[string]string{"bar": "zoo"}).ToYAML(t))
- name: ghi
foo:
bar: zoo`)
kc := new(Kubeconfig).WithLoader(tl) kc := new(Kubeconfig).WithLoader(tl)
if err := kc.Parse(); err != nil { if err := kc.Parse(); err != nil {
t.Fatal(err) t.Fatal(err)
@ -55,9 +53,10 @@ func TestKubeconfig_ContextNames_nonArrayContextsEntry(t *testing.T) {
} }
func TestKubeconfig_CheckContextExists(t *testing.T) { func TestKubeconfig_CheckContextExists(t *testing.T) {
tl := WithMockKubeconfigLoader(`contexts: tl := WithMockKubeconfigLoader(
- name: c1 testutil.KC().WithCtxs(
- name: c2`) testutil.Ctx("c1"),
testutil.Ctx("c2")).ToYAML(t))
kc := new(Kubeconfig).WithLoader(tl) kc := new(Kubeconfig).WithLoader(tl)
if err := kc.Parse(); err != nil { if err := kc.Parse(); err != nil {

View File

@ -2,6 +2,8 @@ package kubeconfig
import ( import (
"testing" "testing"
"github.com/ahmetb/kubectx/internal/testutil"
) )
func TestKubeconfig_GetCurrentContext(t *testing.T) { func TestKubeconfig_GetCurrentContext(t *testing.T) {
@ -33,7 +35,7 @@ func TestKubeconfig_GetCurrentContext_missingField(t *testing.T) {
} }
func TestKubeconfig_UnsetCurrentContext(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) kc := new(Kubeconfig).WithLoader(tl)
if err := kc.Parse(); err != nil { if err := kc.Parse(); err != nil {
t.Fatal(err) t.Fatal(err)
@ -46,8 +48,7 @@ func TestKubeconfig_UnsetCurrentContext(t *testing.T) {
} }
out := tl.Output() out := tl.Output()
expected := `current-context: "" expected := testutil.KC().WithCurrentCtx("").ToYAML(t)
`
if out != expected { if out != expected {
t.Fatalf("expected=%q; got=%q", expected, out) t.Fatalf("expected=%q; got=%q", expected, out)
} }

View File

@ -60,6 +60,6 @@ func (k *Kubeconfig) Save() error {
return errors.Wrap(err, "failed to reset file") return errors.Wrap(err, "failed to reset file")
} }
enc := yaml.NewEncoder(k.f) enc := yaml.NewEncoder(k.f)
enc.SetIndent(2) enc.SetIndent(0)
return enc.Encode(k.rootNode) return enc.Encode(k.rootNode)
} }

View File

@ -4,6 +4,8 @@ import (
"testing" "testing"
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
"github.com/ahmetb/kubectx/internal/testutil"
) )
func TestParse(t *testing.T) { func TestParse(t *testing.T) {
@ -21,21 +23,30 @@ func TestParse(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) 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) { func TestSave(t *testing.T) {
in := `a: [1, 2, 3]` + "\n" in := "a: [1, 2, 3]\n"
test := WithMockKubeconfigLoader(in) test := WithMockKubeconfigLoader(in)
kc := new(Kubeconfig).WithLoader(test) kc := new(Kubeconfig).WithLoader(test)
defer kc.Close() defer kc.Close()
if err := kc.Parse(); err != nil { if err := kc.Parse(); err != nil {
t.Fatal(err) 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 { if err := kc.Save(); err != nil {
t.Fatal(err) 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 != "" { if diff := cmp.Diff(expected, test.Output()); diff != "" {
t.Fatal(diff) t.Fatal(diff)
} }

View 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()
}