Make the start message a member of the ProcessState

This commit is contained in:
Gareth Smith 2018-01-15 12:37:14 +00:00 committed by Hannes Hörl
parent 29f17a4569
commit 5f60d519e9
6 changed files with 51 additions and 203 deletions

View File

@ -52,12 +52,15 @@ func (s *APIServer) Start() error {
return err
}
s.processState, err = internal.NewProcessState(
s.processState = &internal.ProcessState{}
s.processState.DefaultedProcessInput, err = internal.DoDefaulting(
"kube-apiserver",
s.Path,
s.URL,
s.CertDir,
s.StartTimeout, s.StopTimeout,
s.Path,
s.StartTimeout,
s.StopTimeout,
)
if err != nil {
return err
@ -80,7 +83,9 @@ func (s *APIServer) Start() error {
return err
}
s.processState.Start(fmt.Sprintf("Serving insecurely on %s", s.processState.URL.Host))
s.processState.StartMessage = fmt.Sprintf("Serving insecurely on %s", s.processState.URL.Host)
s.processState.Start()
return err
}

View File

@ -30,6 +30,6 @@ func (f *ControlPlane) Stop() error {
}
// APIURL returns the URL you should connect to to talk to your API.
func (f *ControlPlane) APIURL() *url.URL {
func (f *ControlPlane) APIURL() url.URL {
return f.APIServer.processState.URL
}

View File

@ -27,12 +27,15 @@ type Etcd struct {
func (e *Etcd) Start() error {
var err error
e.processState, err = internal.NewProcessState(
e.processState = &internal.ProcessState{}
e.processState.DefaultedProcessInput, err = internal.DoDefaulting(
"etcd",
e.Path,
e.URL,
e.DataDir,
e.StartTimeout, e.StopTimeout,
e.Path,
e.StartTimeout,
e.StopTimeout,
)
if err != nil {
return err
@ -41,12 +44,14 @@ func (e *Etcd) Start() error {
e.processState.Args = []string{
"--debug",
"--listen-peer-urls=http://localhost:0",
fmt.Sprintf("--advertise-client-urls=%s", e.processState.URL),
fmt.Sprintf("--listen-client-urls=%s", e.processState.URL),
fmt.Sprintf("--advertise-client-urls=%s", e.processState.URL.String()),
fmt.Sprintf("--listen-client-urls=%s", e.processState.URL.String()),
fmt.Sprintf("--data-dir=%s", e.processState.Dir),
}
return e.processState.Start(fmt.Sprintf("serving insecure client requests on %s", e.processState.URL.Hostname()))
e.processState.StartMessage = fmt.Sprintf("serving insecure client requests on %s", e.processState.URL.Hostname())
return e.processState.Start()
}
// Stop stops this process gracefully, waits for its termination, and cleans up the data directory.

View File

@ -22,7 +22,7 @@ var _ = Describe("The Testing Framework", func() {
err = controlPlane.Start()
Expect(err).NotTo(HaveOccurred(), "Expected controlPlane to start successfully")
var apiServerURL *url.URL
var apiServerURL url.URL
//var etcdClientURL *url.URL
//etcdClientURL = controlPlane.APIServer.Etcd.URL
apiServerURL = controlPlane.APIURL()

View File

@ -15,13 +15,9 @@ import (
)
type ProcessState struct {
URL *url.URL
Dir string
DirNeedsCleaning bool
Path string
StopTimeout time.Duration
StartTimeout time.Duration
DefaultedProcessInput
Session *gexec.Session // TODO private?
StartMessage string
Args []string
}
@ -101,11 +97,11 @@ func DoDefaulting(
// return ProcessState2{input, args, startthing}
//}
func (ps *ProcessState) Start(startMessage string) (err error) {
func (ps *ProcessState) Start() (err error) {
ps.Session, err = Start(
ps.Path,
ps.Args,
startMessage,
ps.StartMessage,
ps.StartTimeout,
)
return
@ -163,60 +159,3 @@ func Stop(session *gexec.Session, stopTimeout time.Duration, dirToClean string,
return nil
}
func NewProcessState(
symbolicName string,
path string,
listenURL *url.URL,
dir string,
startTimeout time.Duration,
stopTimeout time.Duration,
) (*ProcessState, error) {
if path == "" && symbolicName == "" {
return nil, fmt.Errorf("Either a path or a symbolic name need to be set")
}
state := &ProcessState{
Path: path,
URL: listenURL,
Dir: dir,
DirNeedsCleaning: false,
StartTimeout: startTimeout,
StopTimeout: stopTimeout,
}
if path == "" {
state.Path = BinPathFinder(symbolicName)
}
if listenURL == nil {
am := &AddressManager{}
port, host, err := am.Initialize()
if err != nil {
return nil, err
}
state.URL = &url.URL{
Scheme: "http",
Host: fmt.Sprintf("%s:%d", host, port),
}
}
if dir == "" {
newDir, err := ioutil.TempDir("", "k8s_test_framework_")
if err != nil {
return nil, err
}
state.Dir = newDir
state.DirNeedsCleaning = true
}
if stopTimeout == 0 {
state.StopTimeout = 20 * time.Second
}
if startTimeout == 0 {
state.StartTimeout = 20 * time.Second
}
return state, nil
}

View File

@ -13,34 +13,41 @@ import (
. "k8s.io/kubectl/pkg/framework/test/internal"
)
var _ = Describe("Start", func() {
var (
timeout time.Duration
)
BeforeEach(func() {
timeout = 200 * time.Millisecond
})
var _ = Describe("Start method", func() {
It("can start a process", func() {
timeout = 5 * time.Second
session, err := Start("bash", simpleBashScript, "loop 3", timeout)
processState := &ProcessState{}
processState.Path = "bash"
processState.Args = simpleBashScript
processState.StartTimeout = 10 * time.Second
processState.StartMessage = "loop 5"
err := processState.Start()
Expect(err).NotTo(HaveOccurred())
Consistently(session.ExitCode).Should(BeNumerically("==", -1))
Consistently(processState.Session.ExitCode).Should(BeNumerically("==", -1))
})
Context("when process takes too long to start", func() {
It("returns an timeout error", func() {
session, err := Start("bash", simpleBashScript, "loop 3000", timeout)
It("returns a timeout error", func() {
processState := &ProcessState{}
processState.Path = "bash"
processState.Args = simpleBashScript
processState.StartTimeout = 200 * time.Millisecond
processState.StartMessage = "loop 5000"
err := processState.Start()
Expect(err).To(MatchError(ContainSubstring("timeout")))
Eventually(session.ExitCode, 10).Should(BeNumerically("==", 143))
Eventually(processState.Session.ExitCode).Should(Equal(143))
})
})
Context("when command cannot be started", func() {
Context("when the command cannot be started", func() {
It("propagates the error", func() {
_, err := Start("/notexeistent", []string{}, "does not matter", timeout)
processState := &ProcessState{}
processState.Path = "/nonexistent"
err := processState.Start()
Expect(os.IsNotExist(err)).To(BeTrue())
})
@ -97,114 +104,6 @@ var _ = Describe("Stop", func() {
})
})
var _ = Describe("NewProcessState", func() {
var (
name string
path string
listenURL *url.URL
dir string
startTimeout time.Duration
stopTimeout time.Duration
)
BeforeEach(func() {
name = "some name"
path = "some path"
listenURL = &url.URL{Host: "some host"}
dir = "some dir"
startTimeout = 1 * time.Second
stopTimeout = 1 * time.Second
})
It("creates a new ProcessState struct", func() {
procState, err := NewProcessState(name, path, listenURL, dir, startTimeout, stopTimeout)
Expect(err).NotTo(HaveOccurred())
Expect(procState.Path).To(Equal(path))
Expect(procState.URL).To(Equal(listenURL))
Expect(procState.Dir).To(Equal(dir))
Expect(procState.StartTimeout).To(Equal(startTimeout))
Expect(procState.StopTimeout).To(Equal(stopTimeout))
Expect(procState.DirNeedsCleaning).To(BeFalse())
})
Context("When path is empty but symbolic name is set", func() {
BeforeEach(func() {
path = ""
})
It("defaults the path", func() {
procState, err := NewProcessState(name, path, listenURL, dir, startTimeout, stopTimeout)
Expect(err).NotTo(HaveOccurred())
Expect(procState.Path).To(ContainSubstring("assets/bin/some name"))
})
})
Context("When symbolic name is empty but path is set", func() {
BeforeEach(func() {
name = ""
})
It("defaults the path", func() {
procState, err := NewProcessState(name, path, listenURL, dir, startTimeout, stopTimeout)
Expect(err).NotTo(HaveOccurred())
Expect(procState.Path).To(ContainSubstring("some path"))
})
})
Context("When symbolic name and the path are empty", func() {
BeforeEach(func() {
name = ""
path = ""
})
It("errors", func() {
_, err := NewProcessState(name, path, listenURL, dir, startTimeout, stopTimeout)
Expect(err).To(MatchError("Either a path or a symbolic name need to be set"))
})
})
Context("When listen URL is not set", func() {
BeforeEach(func() {
listenURL = nil
})
It("defaults the URL", func() {
procState, err := NewProcessState(name, path, listenURL, dir, startTimeout, stopTimeout)
Expect(err).NotTo(HaveOccurred())
Expect(procState.URL).NotTo(BeNil())
Expect(procState.URL.Host).NotTo(BeEmpty())
})
})
Context("When the directory is not set", func() {
BeforeEach(func() {
dir = ""
})
It("defaults to and creates a new directory and sets the directory cleanup flag", func() {
procState, err := NewProcessState(name, path, listenURL, dir, startTimeout, stopTimeout)
Expect(err).NotTo(HaveOccurred())
Expect(procState.Dir).To(BeADirectory())
Expect(procState.DirNeedsCleaning).To(BeTrue())
Expect(os.RemoveAll(procState.Dir)).To(Succeed())
})
})
Context("When timeouts are not set", func() {
BeforeEach(func() {
startTimeout = 0
stopTimeout = 0
})
It("defaults both timeouts", func() {
procState, err := NewProcessState(name, path, listenURL, dir, startTimeout, stopTimeout)
Expect(err).NotTo(HaveOccurred())
Expect(procState.StartTimeout).NotTo(BeZero())
Expect(procState.StopTimeout).NotTo(BeZero())
})
})
})
var _ = Describe("DoDefaulting", func() {
Context("when all inputs are provided", func() {
It("passes them through", func() {