Move logic out of constructors

We can move all of the logic out of the constructors and psuh them into
`ensureInitialized()` of both APIServer and Etcd.
By doing so, the constructors are actually not needed anymore.

We however kept the constructor for the ControlPlane for convinience.
This commit is contained in:
Hannes Hörl 2017-12-13 13:54:08 +00:00
parent 519293f43b
commit 0df12db242
6 changed files with 25 additions and 51 deletions

View File

@ -29,24 +29,6 @@ type certDirManager interface {
//go:generate counterfeiter . certDirManager
// NewAPIServer creates a new APIServer Control Plane Process
func NewAPIServer() (*APIServer, error) {
starter := func(command *exec.Cmd, out, err io.Writer) (SimpleSession, error) {
return gexec.Start(command, out, err)
}
etcd, err := NewEtcd()
if err != nil {
return nil, err
}
return &APIServer{
ProcessStarter: starter,
CertDirManager: NewTempDirManager(),
Etcd: etcd,
}, nil
}
// URL returns the URL APIServer is listening on. Clients can use this to connect to APIServer.
func (s *APIServer) URL() (string, error) {
port, err := s.AddressManager.Port()
@ -125,6 +107,17 @@ func (s *APIServer) ensureInitialized() {
if s.AddressManager == nil {
s.AddressManager = &DefaultAddressManager{}
}
if s.ProcessStarter == nil {
s.ProcessStarter = func(command *exec.Cmd, out, err io.Writer) (SimpleSession, error) {
return gexec.Start(command, out, err)
}
}
if s.CertDirManager == nil {
s.CertDirManager = NewTempDirManager()
}
if s.Etcd == nil {
s.Etcd = &Etcd{}
}
s.stdOut = gbytes.NewBuffer()
s.stdErr = gbytes.NewBuffer()

View File

@ -19,17 +19,10 @@ type ControlPlaneProcess interface {
//go:generate counterfeiter . ControlPlaneProcess
// NewControlPlane will give you a ControlPlane struct that's properly wired together.
func NewControlPlane() (*ControlPlane, error) {
apiServer, err := NewAPIServer()
if err != nil {
return nil, err
func NewControlPlane() *ControlPlane {
return &ControlPlane{
APIServer: &APIServer{},
}
controlPlane := &ControlPlane{
APIServer: apiServer,
}
return controlPlane, nil
}
// Start will start your control plane. To stop it, call Stop().

View File

@ -11,11 +11,6 @@ import (
)
var _ = Describe("ControlPlane", func() {
It("can construct a properly wired ControlPlane struct", func() {
_, err := NewControlPlane()
Expect(err).NotTo(HaveOccurred())
})
Context("with a properly configured set of ControlPlane", func() {
var (
fakeAPIServerProcess *testfakes.FakeControlPlaneProcess

View File

@ -25,7 +25,7 @@ var _ = BeforeSuite(func() {
pathToDemoCommand, err = gexec.Build("k8s.io/kubectl/pkg/framework/test/democli/")
Expect(err).NotTo(HaveOccurred())
controlPlane, err = test.NewControlPlane()
controlPlane = test.NewControlPlane()
Expect(err).NotTo(HaveOccurred())
err = controlPlane.Start()

View File

@ -40,18 +40,6 @@ type SimpleSession interface {
type simpleSessionStarter func(command *exec.Cmd, out, err io.Writer) (SimpleSession, error)
// NewEtcd returns a Etcd process configured with sane defaults
func NewEtcd() (*Etcd, error) {
starter := func(command *exec.Cmd, out, err io.Writer) (SimpleSession, error) {
return gexec.Start(command, out, err)
}
return &Etcd{
ProcessStarter: starter,
DataDirManager: NewTempDirManager(),
}, nil
}
// URL returns the URL Etcd is listening on. Clients can use this to connect to Etcd.
func (e *Etcd) URL() (string, error) {
port, err := e.AddressManager.Port()
@ -114,6 +102,14 @@ func (e *Etcd) ensureInitialized() {
if e.AddressManager == nil {
e.AddressManager = &DefaultAddressManager{}
}
if e.ProcessStarter == nil {
e.ProcessStarter = func(command *exec.Cmd, out, err io.Writer) (SimpleSession, error) {
return gexec.Start(command, out, err)
}
}
if e.DataDirManager == nil {
e.DataDirManager = NewTempDirManager()
}
e.stdOut = gbytes.NewBuffer()
e.stdErr = gbytes.NewBuffer()

View File

@ -15,10 +15,8 @@ import (
var _ = Describe("The Testing Framework", func() {
It("Successfully manages the control plane lifecycle", func() {
var err error
var controlPlane *test.ControlPlane
controlPlane, err = test.NewControlPlane()
Expect(err).NotTo(HaveOccurred())
controlPlane := test.NewControlPlane()
By("Starting all the control plane processes")
err = controlPlane.Start()
@ -58,8 +56,7 @@ var _ = Describe("The Testing Framework", func() {
Measure("It should be fast to bring up and tear down the control plane", func(b Benchmarker) {
b.Time("lifecycle", func() {
controlPlane, err := test.NewControlPlane()
Expect(err).NotTo(HaveOccurred())
controlPlane := test.NewControlPlane()
controlPlane.Start()
controlPlane.Stop()