From 08d9b85f9954b65e011044e57a9ad96049159c4b Mon Sep 17 00:00:00 2001 From: Adam Reese Date: Wed, 27 Apr 2016 14:31:26 -0700 Subject: [PATCH] fix(kube): use a fake client in unit tests --- pkg/kube/client.go | 11 +++-------- pkg/kube/client_test.go | 14 +++++++++++--- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/pkg/kube/client.go b/pkg/kube/client.go index a536505e3..7f6acf029 100644 --- a/pkg/kube/client.go +++ b/pkg/kube/client.go @@ -19,16 +19,11 @@ type ResourceActorFunc func(*resource.Info) error // Namespace will set the namespace // Config allows for overiding values from kubectl func Create(namespace string, reader io.Reader, config clientcmd.ClientConfig) error { - return perform(namespace, reader, createResource, config) -} - -func perform(namespace string, reader io.Reader, fn ResourceActorFunc, config clientcmd.ClientConfig) error { f := cmdutil.NewFactory(config) - //schema, err := f.Validator(true, "") - //if err != nil { - //return err - //} + return perform(f, namespace, reader, createResource) +} +func perform(f *cmdutil.Factory, namespace string, reader io.Reader, fn ResourceActorFunc) error { r := f.NewBuilder(includeThirdPartyAPIs). ContinueOnError(). NamespaceParam(namespace). diff --git a/pkg/kube/client_test.go b/pkg/kube/client_test.go index 15de2d2ac..daeb3dee4 100644 --- a/pkg/kube/client_test.go +++ b/pkg/kube/client_test.go @@ -4,15 +4,18 @@ import ( "os" "testing" + "k8s.io/kubernetes/pkg/api/meta" + "k8s.io/kubernetes/pkg/client/unversioned/fake" + cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" "k8s.io/kubernetes/pkg/kubectl/resource" ) func TestPerform(t *testing.T) { - f, err := os.Open("./testdata/guestbook-all-in-one.yaml") + input, err := os.Open("./testdata/guestbook-all-in-one.yaml") if err != nil { t.Fatal(err) } - defer f.Close() + defer input.Close() results := []*resource.Info{} @@ -26,7 +29,12 @@ func TestPerform(t *testing.T) { return nil } - if err := perform("test", f, fn, nil); err != nil { + f := cmdutil.NewFactory(nil) + f.ClientForMapping = func(mapping *meta.RESTMapping) (resource.RESTClient, error) { + return &fake.RESTClient{}, nil + } + + if err := perform(f, "test", input, fn); err != nil { t.Fatalf("Unexpected error: %s", err) }