Refactor Etcd and APIServer, esp. Start() and Stop()

- Start() should only return when the process is actually up and
  listening
- It may take some time to tear down a process, so we increased the
  timeout for Stop() (to some random number)
- We make sure Std{Out,Err} is properly initialized, we should not rely
  on Ginkgo/Gomega to do that for us
This commit is contained in:
Gareth Smith 2017-11-27 13:55:49 +00:00
parent 5d670c4625
commit d6f4cc6054
5 changed files with 41 additions and 8 deletions

View File

@ -1,9 +1,9 @@
package test
import (
"os/exec"
"fmt"
"os/exec"
"time"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"
@ -21,6 +21,9 @@ type APIServer struct {
// Start starts the apiserver, and returns a gexec.Session. To stop it again, call Terminate and Wait on that session.
func (s *APIServer) Start() error {
s.stdOut = gbytes.NewBuffer()
s.stdErr = gbytes.NewBuffer()
args := []string{
"--authorization-mode=Node,RBAC",
"--runtime-config=admissionregistration.k8s.io/v1alpha1",
@ -34,16 +37,28 @@ func (s *APIServer) Start() error {
fmt.Sprintf("--etcd-servers=%s", s.EtcdURL),
}
detectedStart := s.stdErr.Detect("Serving insecurely on 127.0.0.1:8080")
timedOut := time.After(20 * time.Second)
command := exec.Command(s.Path, args...)
var err error
s.session, err = gexec.Start(command, s.stdOut, s.stdErr)
if err != nil {
return err
}
select {
case <-detectedStart:
return nil
case <-timedOut:
return fmt.Errorf("timeout waiting for apiserver to start serving")
}
}
// Stop stops this process gracefully.
func (s *APIServer) Stop() {
if s.session != nil {
s.session.Terminate().Wait()
s.session.Terminate().Wait(20 * time.Second)
}
}

View File

@ -35,6 +35,7 @@ func main() {
}
}
fmt.Println("Everything is fine")
fmt.Fprintln(os.Stderr, "Serving insecurely on 127.0.0.1:8080")
time.Sleep(10 * time.Minute)
}

View File

@ -31,6 +31,7 @@ func main() {
}
}
fmt.Println("Everything is dandy")
fmt.Fprintln(os.Stderr, "serving insecure client requests on 127.0.0.1:2379")
time.Sleep(10 * time.Minute)
}

View File

@ -1,7 +1,9 @@
package test
import (
"fmt"
"os/exec"
"time"
"github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
@ -28,6 +30,8 @@ type DataDirManager interface {
// Start starts the etcd, and returns a gexec.Session. To stop it again, call Terminate and Wait on that session.
func (e *Etcd) Start() error {
e.dataDirManager = NewTempDirManager()
e.stdOut = gbytes.NewBuffer()
e.stdErr = gbytes.NewBuffer()
dataDir, err := e.dataDirManager.Create()
if err != nil {
@ -44,15 +48,27 @@ func (e *Etcd) Start() error {
dataDir,
}
detectedStart := e.stdErr.Detect("serving insecure client requests on 127.0.0.1:2379")
timedOut := time.After(20 * time.Second)
command := exec.Command(e.Path, args...)
e.session, err = gexec.Start(command, e.stdOut, e.stdErr)
if err != nil {
return err
}
select {
case <-detectedStart:
return nil
case <-timedOut:
return fmt.Errorf("timeout waiting for etcd to start serving")
}
}
// Stop stops this process gracefully.
func (e *Etcd) Stop() {
if e.session != nil {
e.session.Terminate().Wait()
e.session.Terminate().Wait(20 * time.Second)
err := e.dataDirManager.Destroy()
gomega.Expect(err).NotTo(gomega.HaveOccurred())
}

View File

@ -28,9 +28,9 @@ var _ = Describe("Integration", func() {
isEtcdListening := isSomethingListeningOnPort(2379)
isAPIServerListening := isSomethingListeningOnPort(8080)
Eventually(isEtcdListening, 25*time.Second).Should(BeTrue(), "Expected Etcd to listen on 2379")
Expect(isEtcdListening()).To(BeTrue(), "Expected Etcd to listen on 2379")
Eventually(isAPIServerListening, 25*time.Second).Should(BeTrue(), "Expected APIServer to listen on 8080")
Expect(isAPIServerListening()).To(BeTrue(), "Expected APIServer to listen on 8080")
err = fixtures.Stop()
Expect(err).NotTo(HaveOccurred(), "Expected fixtures to stop successfully")