Unify fixture processes

Instead of the separate {Etcd,APIServer}StartStopper use the unified
interface FixtureProcess
This commit is contained in:
Gareth Smith 2017-11-24 13:59:03 +00:00
parent 940bec8b1c
commit ddd0a5683f
4 changed files with 35 additions and 134 deletions

View File

@ -8,8 +8,8 @@ import (
//
// Right now, that means Etcd and your APIServer. This is likely to increase in future.
type Fixtures struct {
Etcd EtcdStartStopper
APIServer APIServerStartStopper
Etcd FixtureProcess
APIServer FixtureProcess
}
// EtcdStartStopper knows how to start an Etcd. One good implementation is Etcd.
@ -28,6 +28,16 @@ type APIServerStartStopper interface {
//go:generate counterfeiter . APIServerStartStopper
// FixtureProcess knows how to start and stop a Fixture processes.
// This interface is potentially going to be expanded to e.g. allow access to the processes StdOut/StdErr
// and other internals.
type FixtureProcess interface {
Start() error
Stop()
}
//go:generate counterfeiter . FixtureProcess
// NewFixtures will give you a Fixtures struct that's properly wired together.
func NewFixtures(pathToEtcd, pathToAPIServer string) *Fixtures {
etcdURL := "http://127.0.0.1:2379"

View File

@ -19,16 +19,16 @@ var _ = Describe("Fixtures", func() {
Context("with a properly configured set of Fixtures", func() {
var (
fakeEtcdStartStopper *testfakes.FakeEtcdStartStopper
fakeAPIServerStartStopper *testfakes.FakeAPIServerStartStopper
fixtures Fixtures
fakeEtcdProcess *testfakes.FakeFixtureProcess
fakeAPIServerProcess *testfakes.FakeFixtureProcess
fixtures Fixtures
)
BeforeEach(func() {
fakeEtcdStartStopper = &testfakes.FakeEtcdStartStopper{}
fakeAPIServerStartStopper = &testfakes.FakeAPIServerStartStopper{}
fakeEtcdProcess = &testfakes.FakeFixtureProcess{}
fakeAPIServerProcess = &testfakes.FakeFixtureProcess{}
fixtures = Fixtures{
Etcd: fakeEtcdStartStopper,
APIServer: fakeAPIServerStartStopper,
Etcd: fakeEtcdProcess,
APIServer: fakeAPIServerProcess,
}
})
@ -37,17 +37,17 @@ var _ = Describe("Fixtures", func() {
Expect(err).NotTo(HaveOccurred())
By("starting Etcd")
Expect(fakeEtcdStartStopper.StartCallCount()).To(Equal(1),
Expect(fakeEtcdProcess.StartCallCount()).To(Equal(1),
"the EtcdStartStopper should be called exactly once")
By("starting APIServer")
Expect(fakeAPIServerStartStopper.StartCallCount()).To(Equal(1),
Expect(fakeAPIServerProcess.StartCallCount()).To(Equal(1),
"the APIServerStartStopper should be called exactly once")
})
Context("when starting etcd fails", func() {
It("wraps the error", func() {
fakeEtcdStartStopper.StartReturns(fmt.Errorf("some error"))
fakeEtcdProcess.StartReturns(fmt.Errorf("some error"))
err := fixtures.Start()
Expect(err).To(MatchError(ContainSubstring("some error")))
})
@ -55,7 +55,7 @@ var _ = Describe("Fixtures", func() {
Context("when starting APIServer fails", func() {
It("wraps the error", func() {
fakeAPIServerStartStopper.StartReturns(fmt.Errorf("another error"))
fakeAPIServerProcess.StartReturns(fmt.Errorf("another error"))
err := fixtures.Start()
Expect(err).To(MatchError(ContainSubstring("another error")))
})
@ -63,8 +63,8 @@ var _ = Describe("Fixtures", func() {
It("can can clean up the temporary directory and stop", func() {
fixtures.Stop()
Expect(fakeEtcdStartStopper.StopCallCount()).To(Equal(1))
Expect(fakeAPIServerStartStopper.StopCallCount()).To(Equal(1))
Expect(fakeEtcdProcess.StopCallCount()).To(Equal(1))
Expect(fakeAPIServerProcess.StopCallCount()).To(Equal(1))
})
})

View File

@ -1,109 +0,0 @@
// Code generated by counterfeiter. DO NOT EDIT.
package testfakes
import (
"sync"
"k8s.io/kubectl/pkg/framework/test"
)
type FakeAPIServerStartStopper struct {
StartStub func() error
startMutex sync.RWMutex
startArgsForCall []struct{}
startReturns struct {
result1 error
}
startReturnsOnCall map[int]struct {
result1 error
}
StopStub func()
stopMutex sync.RWMutex
stopArgsForCall []struct{}
invocations map[string][][]interface{}
invocationsMutex sync.RWMutex
}
func (fake *FakeAPIServerStartStopper) Start() error {
fake.startMutex.Lock()
ret, specificReturn := fake.startReturnsOnCall[len(fake.startArgsForCall)]
fake.startArgsForCall = append(fake.startArgsForCall, struct{}{})
fake.recordInvocation("Start", []interface{}{})
fake.startMutex.Unlock()
if fake.StartStub != nil {
return fake.StartStub()
}
if specificReturn {
return ret.result1
}
return fake.startReturns.result1
}
func (fake *FakeAPIServerStartStopper) StartCallCount() int {
fake.startMutex.RLock()
defer fake.startMutex.RUnlock()
return len(fake.startArgsForCall)
}
func (fake *FakeAPIServerStartStopper) StartReturns(result1 error) {
fake.StartStub = nil
fake.startReturns = struct {
result1 error
}{result1}
}
func (fake *FakeAPIServerStartStopper) StartReturnsOnCall(i int, result1 error) {
fake.StartStub = nil
if fake.startReturnsOnCall == nil {
fake.startReturnsOnCall = make(map[int]struct {
result1 error
})
}
fake.startReturnsOnCall[i] = struct {
result1 error
}{result1}
}
func (fake *FakeAPIServerStartStopper) Stop() {
fake.stopMutex.Lock()
fake.stopArgsForCall = append(fake.stopArgsForCall, struct{}{})
fake.recordInvocation("Stop", []interface{}{})
fake.stopMutex.Unlock()
if fake.StopStub != nil {
fake.StopStub()
}
}
func (fake *FakeAPIServerStartStopper) StopCallCount() int {
fake.stopMutex.RLock()
defer fake.stopMutex.RUnlock()
return len(fake.stopArgsForCall)
}
func (fake *FakeAPIServerStartStopper) Invocations() map[string][][]interface{} {
fake.invocationsMutex.RLock()
defer fake.invocationsMutex.RUnlock()
fake.startMutex.RLock()
defer fake.startMutex.RUnlock()
fake.stopMutex.RLock()
defer fake.stopMutex.RUnlock()
copiedInvocations := map[string][][]interface{}{}
for key, value := range fake.invocations {
copiedInvocations[key] = value
}
return copiedInvocations
}
func (fake *FakeAPIServerStartStopper) recordInvocation(key string, args []interface{}) {
fake.invocationsMutex.Lock()
defer fake.invocationsMutex.Unlock()
if fake.invocations == nil {
fake.invocations = map[string][][]interface{}{}
}
if fake.invocations[key] == nil {
fake.invocations[key] = [][]interface{}{}
}
fake.invocations[key] = append(fake.invocations[key], args)
}
var _ test.APIServerStartStopper = new(FakeAPIServerStartStopper)

View File

@ -7,7 +7,7 @@ import (
"k8s.io/kubectl/pkg/framework/test"
)
type FakeEtcdStartStopper struct {
type FakeFixtureProcess struct {
StartStub func() error
startMutex sync.RWMutex
startArgsForCall []struct{}
@ -24,7 +24,7 @@ type FakeEtcdStartStopper struct {
invocationsMutex sync.RWMutex
}
func (fake *FakeEtcdStartStopper) Start() error {
func (fake *FakeFixtureProcess) Start() error {
fake.startMutex.Lock()
ret, specificReturn := fake.startReturnsOnCall[len(fake.startArgsForCall)]
fake.startArgsForCall = append(fake.startArgsForCall, struct{}{})
@ -39,20 +39,20 @@ func (fake *FakeEtcdStartStopper) Start() error {
return fake.startReturns.result1
}
func (fake *FakeEtcdStartStopper) StartCallCount() int {
func (fake *FakeFixtureProcess) StartCallCount() int {
fake.startMutex.RLock()
defer fake.startMutex.RUnlock()
return len(fake.startArgsForCall)
}
func (fake *FakeEtcdStartStopper) StartReturns(result1 error) {
func (fake *FakeFixtureProcess) StartReturns(result1 error) {
fake.StartStub = nil
fake.startReturns = struct {
result1 error
}{result1}
}
func (fake *FakeEtcdStartStopper) StartReturnsOnCall(i int, result1 error) {
func (fake *FakeFixtureProcess) StartReturnsOnCall(i int, result1 error) {
fake.StartStub = nil
if fake.startReturnsOnCall == nil {
fake.startReturnsOnCall = make(map[int]struct {
@ -64,7 +64,7 @@ func (fake *FakeEtcdStartStopper) StartReturnsOnCall(i int, result1 error) {
}{result1}
}
func (fake *FakeEtcdStartStopper) Stop() {
func (fake *FakeFixtureProcess) Stop() {
fake.stopMutex.Lock()
fake.stopArgsForCall = append(fake.stopArgsForCall, struct{}{})
fake.recordInvocation("Stop", []interface{}{})
@ -74,13 +74,13 @@ func (fake *FakeEtcdStartStopper) Stop() {
}
}
func (fake *FakeEtcdStartStopper) StopCallCount() int {
func (fake *FakeFixtureProcess) StopCallCount() int {
fake.stopMutex.RLock()
defer fake.stopMutex.RUnlock()
return len(fake.stopArgsForCall)
}
func (fake *FakeEtcdStartStopper) Invocations() map[string][][]interface{} {
func (fake *FakeFixtureProcess) Invocations() map[string][][]interface{} {
fake.invocationsMutex.RLock()
defer fake.invocationsMutex.RUnlock()
fake.startMutex.RLock()
@ -94,7 +94,7 @@ func (fake *FakeEtcdStartStopper) Invocations() map[string][][]interface{} {
return copiedInvocations
}
func (fake *FakeEtcdStartStopper) recordInvocation(key string, args []interface{}) {
func (fake *FakeFixtureProcess) recordInvocation(key string, args []interface{}) {
fake.invocationsMutex.Lock()
defer fake.invocationsMutex.Unlock()
if fake.invocations == nil {
@ -106,4 +106,4 @@ func (fake *FakeEtcdStartStopper) recordInvocation(key string, args []interface{
fake.invocations[key] = append(fake.invocations[key], args)
}
var _ test.EtcdStartStopper = new(FakeEtcdStartStopper)
var _ test.FixtureProcess = new(FakeFixtureProcess)