Add entrypoint tests

This commit is contained in:
Antoni Rościszewski 2017-02-01 23:07:24 +01:00
parent 23aff4b2c8
commit 2a57fb841e
8 changed files with 124 additions and 11 deletions

10
Godeps/Godeps.json generated
View File

@ -799,6 +799,16 @@
"ImportPath": "k8s.io/client-go/transport", "ImportPath": "k8s.io/client-go/transport",
"Comment": "v2.0.0-alpha.0", "Comment": "v2.0.0-alpha.0",
"Rev": "b22087a53becae45931ed72d5e0f12e0031d771a" "Rev": "b22087a53becae45931ed72d5e0f12e0031d771a"
},
{
"ImportPath": "github.com/onsi/gomega",
"Comment": "v1.2.0",
"Rev": "e85f63af4302dc0e4277c6008ecf803f1d80e4f0"
},
{
"ImportPath": "github.com/onsi/ginkgo",
"Comment": "v1.2.0",
"Rev": "bb93381d543b0e5725244abe752214a110791d01"
} }
] ]
} }

View File

@ -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")
}

View File

@ -1,9 +1,27 @@
package entrypoint package entrypoint
import ( 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 { type dummyResolver struct {
name string name string
namespace string namespace string
@ -16,10 +34,77 @@ func (d dummyResolver) GetName() (name string) {
return d.name return d.name
} }
func TestRegisterNewDependency(t *testing.T) { func init() {
dummy := dummyResolver{name: "dummy"} 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) Register(dummy)
if len(dependencies) != 1 { Expect(len(dependencies)).To(Equal(1))
t.Errorf("Expecting dependencies len to be 1 got %v", len(dependencies)) })
}
} 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)))
})
})

View File

@ -2,6 +2,7 @@ package mocks
import ( import (
"fmt" "fmt"
v1beta1extensions "k8s.io/client-go/kubernetes/typed/extensions/v1beta1" v1beta1extensions "k8s.io/client-go/kubernetes/typed/extensions/v1beta1"
api "k8s.io/client-go/pkg/api" api "k8s.io/client-go/pkg/api"
unversioned "k8s.io/client-go/pkg/api/unversioned" unversioned "k8s.io/client-go/pkg/api/unversioned"

View File

@ -2,6 +2,7 @@ package mocks
import ( import (
"fmt" "fmt"
v1core "k8s.io/client-go/kubernetes/typed/core/v1" v1core "k8s.io/client-go/kubernetes/typed/core/v1"
api "k8s.io/client-go/pkg/api" api "k8s.io/client-go/pkg/api"
v1 "k8s.io/client-go/pkg/api/v1" v1 "k8s.io/client-go/pkg/api/v1"

View File

@ -1,7 +1,6 @@
package mocks package mocks
import ( import (
// "github.com/stackanetes/kubernetes-entrypoint/entrypoint"
cli "github.com/stackanetes/kubernetes-entrypoint/client" cli "github.com/stackanetes/kubernetes-entrypoint/client"
) )
@ -16,12 +15,14 @@ func (m MockEntrypoint) Resolve() {
func (m MockEntrypoint) Client() (client cli.ClientInterface) { func (m MockEntrypoint) Client() (client cli.ClientInterface) {
return m.client return m.client
} }
func (m MockEntrypoint) GetNamespace() (namespace string) { func (m MockEntrypoint) GetNamespace() (namespace string) {
return m.namespace return m.namespace
} }
func NewEntrypoint() MockEntrypoint {
func NewEntrypoint(client cli.ClientInterface, namespace string) MockEntrypoint {
return MockEntrypoint{ return MockEntrypoint{
client: NewClient(), client: client,
namespace: "test", namespace: namespace,
} }
} }

View File

@ -2,6 +2,7 @@ package mocks
import ( import (
"fmt" "fmt"
v1batch "k8s.io/client-go/kubernetes/typed/batch/v1" v1batch "k8s.io/client-go/kubernetes/typed/batch/v1"
api "k8s.io/client-go/pkg/api" api "k8s.io/client-go/pkg/api"
v1 "k8s.io/client-go/pkg/api/v1" v1 "k8s.io/client-go/pkg/api/v1"

View File

@ -2,6 +2,7 @@ package mocks
import ( import (
"fmt" "fmt"
v1core "k8s.io/client-go/kubernetes/typed/core/v1" v1core "k8s.io/client-go/kubernetes/typed/core/v1"
api "k8s.io/client-go/pkg/api" api "k8s.io/client-go/pkg/api"
v1 "k8s.io/client-go/pkg/api/v1" v1 "k8s.io/client-go/pkg/api/v1"