mirror of
https://github.com/ahmetb/kubectx.git
synced 2025-08-14 13:33:37 +00:00
Extend test coverage
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
parent
562631ad2b
commit
73c1f268ee
@ -24,5 +24,5 @@ func (_op CurrentOp) Run(stdout, _ io.Writer) error {
|
||||
return errors.New("current-context is not set")
|
||||
}
|
||||
_, err := fmt.Fprintln(stdout, v)
|
||||
return err
|
||||
return errors.Wrap(err, "write error")
|
||||
}
|
||||
|
@ -55,5 +55,5 @@ func deleteContext(name string) (deleteName string, wasActiveContext bool, err e
|
||||
if err := kc.DeleteContextEntry(name); err != nil {
|
||||
return "", false, errors.Wrap(err, "failed to modify yaml doc")
|
||||
}
|
||||
return name, wasActiveContext, errors.Wrap(kc.Save(), "failed to save kubeconfig file")
|
||||
return name, wasActiveContext, errors.Wrap(kc.Save(), "failed to save modified kubeconfig file")
|
||||
}
|
||||
|
@ -3,6 +3,8 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// HelpOp describes printing help.
|
||||
@ -28,5 +30,5 @@ func printUsage(out io.Writer) error {
|
||||
kubectx -h,--help : show this message`
|
||||
|
||||
_, err := fmt.Fprintf(out, "%s\n", help)
|
||||
return err
|
||||
return errors.Wrap(err, "write error")
|
||||
}
|
||||
|
@ -95,6 +95,34 @@ func TestKubeconfig_ModifyCurrentContext_fieldMissing(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestKubeconfig_ModifyContextName_noContextsEntryError(t *testing.T) {
|
||||
// no context entries
|
||||
test := &testLoader{in: strings.NewReader(
|
||||
`a: b`)}
|
||||
kc := new(Kubeconfig).WithLoader(test)
|
||||
if err := kc.Parse(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := kc.ModifyContextName("c1", "c2"); err == nil {
|
||||
t.Fatal("was expecting error for no 'contexts' entry; got nil")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func TestKubeconfig_ModifyContextName_contextsEntryNotSequenceError(t *testing.T) {
|
||||
// no context entries
|
||||
test := &testLoader{in: strings.NewReader(
|
||||
`contexts: "hello"`)}
|
||||
kc := new(Kubeconfig).WithLoader(test)
|
||||
if err := kc.Parse(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := kc.ModifyContextName("c1", "c2"); err == nil {
|
||||
t.Fatal("was expecting error for 'context entry not a sequence'; got nil")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func TestKubeconfig_ModifyContextName_noChange(t *testing.T) {
|
||||
test := &testLoader{in: strings.NewReader(
|
||||
`contexts: [{name: c1}, {name: c2}, {name: c3}]`)}
|
||||
|
@ -29,6 +29,32 @@ contexts:
|
||||
}
|
||||
}
|
||||
|
||||
func TestKubeconfig_ContextNames_noContextsEntry(t *testing.T) {
|
||||
tl := &testLoader{in: strings.NewReader(`a: b`)}
|
||||
kc := new(Kubeconfig).WithLoader(tl)
|
||||
if err := kc.Parse(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx := kc.ContextNames()
|
||||
var expected []string = nil
|
||||
if diff := cmp.Diff(expected, ctx); diff != "" {
|
||||
t.Fatalf("%s", diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKubeconfig_ContextNames_nonArrayContextsEntry(t *testing.T) {
|
||||
tl := &testLoader{in: strings.NewReader(`contexts: "hello"`)}
|
||||
kc := new(Kubeconfig).WithLoader(tl)
|
||||
if err := kc.Parse(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx := kc.ContextNames()
|
||||
var expected []string = nil
|
||||
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
|
||||
|
@ -3,10 +3,12 @@ package kubeconfig
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
)
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
err := new(Kubeconfig).WithLoader(&testLoader{in: strings.NewReader(`a:b`)}).Parse()
|
||||
err := new(Kubeconfig).WithLoader(&testLoader{in: strings.NewReader(`a: [1, 2`)}).Parse()
|
||||
if err == nil {
|
||||
t.Fatal("expected error from bad yaml")
|
||||
}
|
||||
@ -21,3 +23,21 @@ func TestParse(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSave(t *testing.T) {
|
||||
in := `a: [1, 2, 3]` + "\n"
|
||||
test := &testLoader{in: strings.NewReader(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.Save(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
expected := in+"current-context: hello\n"
|
||||
if diff := cmp.Diff(expected, test.out.String()); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
}
|
||||
|
@ -27,5 +27,5 @@ func (_ UnsetOp) Run(_, stderr io.Writer) error {
|
||||
}
|
||||
|
||||
_, err := fmt.Fprintln(stderr, "Successfully unset the current context")
|
||||
return err
|
||||
return errors.Wrap(err, "write error")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user