Remove Start/Stop funcs in favour of methods

This commit is contained in:
Gareth Smith 2018-01-15 14:07:22 +00:00 committed by Hannes Hörl
parent 5f60d519e9
commit 01c1b7e190
2 changed files with 44 additions and 80 deletions

View File

@ -87,64 +87,34 @@ func DoDefaulting(
return defaults, nil
}
//type ProcessState2 struct {
// ProcessInput
// Args []string
// StartString string
//}
//
//func NewProcessState(input ProcessInput, args []string, startthing string) ProcessState {
// return ProcessState2{input, args, startthing}
//}
func (ps *ProcessState) Start() (err error) {
ps.Session, err = Start(
ps.Path,
ps.Args,
ps.StartMessage,
ps.StartTimeout,
)
return
}
func (ps *ProcessState) Stop() error {
return Stop(
ps.Session,
ps.StopTimeout,
ps.Dir,
ps.DirNeedsCleaning,
)
}
func Start(path string, args []string, startMessage string, startTimeout time.Duration) (*gexec.Session, error) {
command := exec.Command(path, args...)
command := exec.Command(ps.Path, ps.Args...)
stdErr := gbytes.NewBuffer()
detectedStart := stdErr.Detect(startMessage)
timedOut := time.After(startTimeout)
detectedStart := stdErr.Detect(ps.StartMessage)
timedOut := time.After(ps.StartTimeout)
session, err := gexec.Start(command, nil, stdErr)
ps.Session, err = gexec.Start(command, nil, stdErr)
if err != nil {
return session, err
return err
}
select {
case <-detectedStart:
return session, nil
return nil
case <-timedOut:
session.Terminate()
return session, fmt.Errorf("timeout waiting for process to start serving")
ps.Session.Terminate()
return fmt.Errorf("timeout waiting for process to start serving")
}
}
func Stop(session *gexec.Session, stopTimeout time.Duration, dirToClean string, dirNeedsCleaning bool) error {
if session == nil {
func (ps *ProcessState) Stop() error {
if ps.Session == nil {
return nil
}
detectedStop := session.Terminate().Exited
timedOut := time.After(stopTimeout)
detectedStop := ps.Session.Terminate().Exited
timedOut := time.After(ps.StopTimeout)
select {
case <-detectedStop:
@ -153,8 +123,8 @@ func Stop(session *gexec.Session, stopTimeout time.Duration, dirToClean string,
return fmt.Errorf("timeout waiting for process to stop")
}
if dirNeedsCleaning {
return os.RemoveAll(dirToClean)
if ps.DirNeedsCleaning {
return os.RemoveAll(ps.Dir)
}
return nil

View File

@ -54,52 +54,46 @@ var _ = Describe("Start method", func() {
})
})
var _ = Describe("Stop", func() {
var (
session *gexec.Session
timeout time.Duration
)
BeforeEach(func() {
var _ = Describe("Stop method", func() {
It("can stop a process", func() {
var err error
command := getSimpleCommand()
timeout = 10 * time.Second
session, err = gexec.Start(command, nil, nil)
processState := &ProcessState{}
processState.Session, err = gexec.Start(getSimpleCommand(), nil, nil)
Expect(err).NotTo(HaveOccurred())
processState.StopTimeout = 10 * time.Second
Expect(processState.Stop()).To(Succeed())
})
It("can stop a session", func() {
err := Stop(session, timeout, "", false)
Expect(err).NotTo(HaveOccurred())
Expect(session.ExitCode()).To(Equal(143))
})
Context("when command cannot be stopped", func() {
BeforeEach(func() {
session.Exited = make(chan struct{})
timeout = 200 * time.Millisecond
})
It("runs into a timeout", func() {
err := Stop(session, timeout, "", false)
Expect(err).To(MatchError(ContainSubstring("timeout")))
})
})
Context("when directory needs cleanup", func() {
var (
tmpDir string
)
BeforeEach(func() {
Context("when the command cannot be stopped", func() {
It("returns a timeout error", func() {
var err error
tmpDir, err = ioutil.TempDir("", "k8s_test_framework_tests_")
processState := &ProcessState{}
processState.Session, err = gexec.Start(getSimpleCommand(), nil, nil)
Expect(err).NotTo(HaveOccurred())
Expect(tmpDir).To(BeAnExistingFile())
processState.Session.Exited = make(chan struct{})
processState.StopTimeout = 200 * time.Millisecond
Expect(processState.Stop()).To(MatchError(ContainSubstring("timeout")))
})
})
Context("when the directory needs to be cleaned up", func() {
It("removes the directory", func() {
err := Stop(session, timeout, tmpDir, true)
var err error
processState := &ProcessState{}
processState.Session, err = gexec.Start(getSimpleCommand(), nil, nil)
Expect(err).NotTo(HaveOccurred())
Expect(tmpDir).NotTo(BeAnExistingFile())
processState.Dir, err = ioutil.TempDir("", "k8s_test_framework_")
Expect(err).NotTo(HaveOccurred())
processState.DirNeedsCleaning = true
processState.StopTimeout = 200 * time.Millisecond
Expect(processState.Stop()).To(Succeed())
Expect(processState.Dir).NotTo(BeAnExistingFile())
})
})
})