From 2a57fb841e99956f2660ce0e0bc8679799d220f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoni=20Ro=C5=9Bciszewski?= Date: Wed, 1 Feb 2017 23:07:24 +0100 Subject: [PATCH] Add entrypoint tests --- Godeps/Godeps.json | 10 +++ entrypoint/entrypoint_suite_test.go | 13 ++++ entrypoint/entrypoint_test.go | 99 +++++++++++++++++++++++++++-- mocks/daemonset.go | 1 + mocks/endpoints.go | 1 + mocks/entrypoint.go | 9 +-- mocks/job.go | 1 + mocks/pod.go | 1 + 8 files changed, 124 insertions(+), 11 deletions(-) create mode 100644 entrypoint/entrypoint_suite_test.go diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 808dead..54bbc2b 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -799,6 +799,16 @@ "ImportPath": "k8s.io/client-go/transport", "Comment": "v2.0.0-alpha.0", "Rev": "b22087a53becae45931ed72d5e0f12e0031d771a" + }, + { + "ImportPath": "github.com/onsi/gomega", + "Comment": "v1.2.0", + "Rev": "e85f63af4302dc0e4277c6008ecf803f1d80e4f0" + }, + { + "ImportPath": "github.com/onsi/ginkgo", + "Comment": "v1.2.0", + "Rev": "bb93381d543b0e5725244abe752214a110791d01" } ] } diff --git a/entrypoint/entrypoint_suite_test.go b/entrypoint/entrypoint_suite_test.go new file mode 100644 index 0000000..d153748 --- /dev/null +++ b/entrypoint/entrypoint_suite_test.go @@ -0,0 +1,13 @@ +package entrypoint_test + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + + "testing" +) + +func TestEntrypoint(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Entrypoint Suite") +} diff --git a/entrypoint/entrypoint_test.go b/entrypoint/entrypoint_test.go index a9f213f..1ccdcc8 100644 --- a/entrypoint/entrypoint_test.go +++ b/entrypoint/entrypoint_test.go @@ -1,9 +1,27 @@ package entrypoint import ( - "testing" + "fmt" + "io/ioutil" + "os" + "time" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + cli "github.com/stackanetes/kubernetes-entrypoint/client" + "github.com/stackanetes/kubernetes-entrypoint/logger" + "github.com/stackanetes/kubernetes-entrypoint/mocks" ) +const ( + testNamespace = "test" + dummyResolverName = "dummy" + loggerInfoText = "Entrypoint INFO: " +) + +var testEntrypoint EntrypointInterface +var testClient cli.ClientInterface + type dummyResolver struct { name string namespace string @@ -16,10 +34,77 @@ func (d dummyResolver) GetName() (name string) { return d.name } -func TestRegisterNewDependency(t *testing.T) { - dummy := dummyResolver{name: "dummy"} - Register(dummy) - if len(dependencies) != 1 { - t.Errorf("Expecting dependencies len to be 1 got %v", len(dependencies)) - } +func init() { + testClient = mocks.NewClient() + testEntrypoint = mocks.NewEntrypoint(testClient, testNamespace) + } + +func registerNilResolver() { + Register(nil) +} + +var _ = Describe("Entrypoint", func() { + + dummy := dummyResolver{name: dummyResolverName} + + BeforeEach(func() { + logger.Info.SetFlags(0) + logger.Warning.SetFlags(0) + logger.Error.SetFlags(0) + }) + + AfterEach(func() { + // Clear dependencies + dependencies = make([]Resolver, 0) + }) + + It("registers new nil resolver", func() { + defer GinkgoRecover() + + Ω(registerNilResolver).Should(Panic()) + }) + + It("registers new non-nil resolver", func() { + defer GinkgoRecover() + Register(dummy) + Expect(len(dependencies)).To(Equal(1)) + }) + + It("checks Client() method", func() { + client := testEntrypoint.Client() + Expect(client).To(Equal(testClient)) + }) + + It("checks Namespace() method", func() { + ns := testEntrypoint.GetNamespace() + Expect(ns).To(Equal(testNamespace)) + }) + + It("resolves main entrypoint with a dummy dependency", func() { + defer GinkgoRecover() + + // Set output logger to our reader + r, w, _ := os.Pipe() + tmp := os.Stdout + defer func() { + os.Stdout = tmp + }() + + logger.Info.SetOutput(w) + + os.Stdout = w + go func() { + mainEntrypoint := Entrypoint{client: mocks.NewClient(), namespace: "main"} + Register(dummy) + mainEntrypoint.Resolve() + w.Close() + }() + + // Wait for resolver to finish + time.Sleep(5 * time.Second) + + stdout, _ := ioutil.ReadAll(r) + Expect(string(stdout)).To(Equal(fmt.Sprintf("%sResolving %s\n%sDependency %s is resolved\n", loggerInfoText, dummyResolverName, loggerInfoText, dummyResolverName))) + }) +}) diff --git a/mocks/daemonset.go b/mocks/daemonset.go index 10bdc2b..674df60 100644 --- a/mocks/daemonset.go +++ b/mocks/daemonset.go @@ -2,6 +2,7 @@ package mocks import ( "fmt" + v1beta1extensions "k8s.io/client-go/kubernetes/typed/extensions/v1beta1" api "k8s.io/client-go/pkg/api" unversioned "k8s.io/client-go/pkg/api/unversioned" diff --git a/mocks/endpoints.go b/mocks/endpoints.go index 7d1634a..81fa03e 100644 --- a/mocks/endpoints.go +++ b/mocks/endpoints.go @@ -2,6 +2,7 @@ package mocks import ( "fmt" + v1core "k8s.io/client-go/kubernetes/typed/core/v1" api "k8s.io/client-go/pkg/api" v1 "k8s.io/client-go/pkg/api/v1" diff --git a/mocks/entrypoint.go b/mocks/entrypoint.go index a083efe..7fba390 100644 --- a/mocks/entrypoint.go +++ b/mocks/entrypoint.go @@ -1,7 +1,6 @@ package mocks import ( - // "github.com/stackanetes/kubernetes-entrypoint/entrypoint" cli "github.com/stackanetes/kubernetes-entrypoint/client" ) @@ -16,12 +15,14 @@ func (m MockEntrypoint) Resolve() { func (m MockEntrypoint) Client() (client cli.ClientInterface) { return m.client } + func (m MockEntrypoint) GetNamespace() (namespace string) { return m.namespace } -func NewEntrypoint() MockEntrypoint { + +func NewEntrypoint(client cli.ClientInterface, namespace string) MockEntrypoint { return MockEntrypoint{ - client: NewClient(), - namespace: "test", + client: client, + namespace: namespace, } } diff --git a/mocks/job.go b/mocks/job.go index ffc7d2f..d3816b2 100644 --- a/mocks/job.go +++ b/mocks/job.go @@ -2,6 +2,7 @@ package mocks import ( "fmt" + v1batch "k8s.io/client-go/kubernetes/typed/batch/v1" api "k8s.io/client-go/pkg/api" v1 "k8s.io/client-go/pkg/api/v1" diff --git a/mocks/pod.go b/mocks/pod.go index a597fdc..2c35476 100644 --- a/mocks/pod.go +++ b/mocks/pod.go @@ -2,6 +2,7 @@ package mocks import ( "fmt" + v1core "k8s.io/client-go/kubernetes/typed/core/v1" api "k8s.io/client-go/pkg/api" v1 "k8s.io/client-go/pkg/api/v1"