From 342d21683b732cfb86df17a560434604da650a18 Mon Sep 17 00:00:00 2001 From: Ahmet Alp Balkan Date: Thu, 16 Apr 2020 21:37:29 -0700 Subject: [PATCH] Create test utils for crafting kubeconfig strings Signed-off-by: Ahmet Alp Balkan --- internal/kubeconfig/contextmodify_test.go | 47 +++++++++++++--------- internal/kubeconfig/contexts_test.go | 23 +++++------ internal/kubeconfig/currentcontext_test.go | 7 ++-- internal/kubeconfig/kubeconfig.go | 2 +- internal/kubeconfig/kubeconfig_test.go | 17 ++++++-- internal/testutil/kubeconfigbuilder.go | 36 +++++++++++++++++ 6 files changed, 95 insertions(+), 37 deletions(-) create mode 100644 internal/testutil/kubeconfigbuilder.go diff --git a/internal/kubeconfig/contextmodify_test.go b/internal/kubeconfig/contextmodify_test.go index f468e4c..e66226e 100644 --- a/internal/kubeconfig/contextmodify_test.go +++ b/internal/kubeconfig/contextmodify_test.go @@ -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) diff --git a/internal/kubeconfig/contexts_test.go b/internal/kubeconfig/contexts_test.go index 192580c..f1be0c0 100644 --- a/internal/kubeconfig/contexts_test.go +++ b/internal/kubeconfig/contexts_test.go @@ -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 { diff --git a/internal/kubeconfig/currentcontext_test.go b/internal/kubeconfig/currentcontext_test.go index 2673884..b99768d 100644 --- a/internal/kubeconfig/currentcontext_test.go +++ b/internal/kubeconfig/currentcontext_test.go @@ -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) } diff --git a/internal/kubeconfig/kubeconfig.go b/internal/kubeconfig/kubeconfig.go index 62f1ca8..051a295 100644 --- a/internal/kubeconfig/kubeconfig.go +++ b/internal/kubeconfig/kubeconfig.go @@ -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) } diff --git a/internal/kubeconfig/kubeconfig_test.go b/internal/kubeconfig/kubeconfig_test.go index 8125983..0be3a4d 100644 --- a/internal/kubeconfig/kubeconfig_test.go +++ b/internal/kubeconfig/kubeconfig_test.go @@ -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) } diff --git a/internal/testutil/kubeconfigbuilder.go b/internal/testutil/kubeconfigbuilder.go new file mode 100644 index 0000000..47cfe15 --- /dev/null +++ b/internal/testutil/kubeconfigbuilder.go @@ -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() +}