make e2e TestKillChildOnCancel exec properly on windows

This commit is contained in:
guillaume.tardif 2020-06-04 16:31:06 +02:00 committed by Guillaume Tardif
parent 427527d197
commit 7c6b04f28e
3 changed files with 22 additions and 6 deletions

View File

@ -109,7 +109,7 @@ func (s *E2eSuite) TestSetupError() {
func (s *E2eSuite) TestKillChildOnCancel() {
It("should kill docker-classic if parent command is cancelled", func() {
out := s.NewCommand("ps", "-x").ExecOrDie()
out := s.ListProcessesCommand().ExecOrDie()
Expect(out).NotTo(ContainSubstring("docker-classic"))
dir := s.ConfigDir
@ -122,16 +122,16 @@ RUN sleep 100`), 0644)).To(Succeed())
_, err := ctx.Exec()
errs <- err
}()
err := WaitFor(time.Second, 3*time.Second, errs, func() bool {
out := s.NewCommand("ps", "-x").ExecOrDie()
err := WaitFor(time.Second, 10*time.Second, errs, func() bool {
out := s.ListProcessesCommand().ExecOrDie()
return strings.Contains(out, "docker-classic")
})
Expect(err).NotTo(HaveOccurred())
log.Println("Killing docker process")
close(shutdown)
err = WaitFor(time.Second, 4*time.Second, nil, func() bool {
out := s.NewCommand("ps", "-x").ExecOrDie()
err = WaitFor(time.Second, 12*time.Second, nil, func() bool {
out := s.ListProcessesCommand().ExecOrDie()
return !strings.Contains(out, "docker-classic")
})
Expect(err).NotTo(HaveOccurred())

View File

@ -32,6 +32,7 @@ import (
"fmt"
"io"
"os/exec"
"runtime"
"strings"
"syscall"
"time"
@ -176,7 +177,7 @@ func Execute(cmd *exec.Cmd, timeout <-chan time.Time) (string, error) {
}
case <-timeout:
log.Debugf("%s %s timed-out", cmd.Path, strings.Join(cmd.Args[1:], " "))
if err := cmd.Process.Signal(syscall.SIGTERM); err != nil {
if err := terminateProcess(cmd); err != nil {
return "", err
}
return "", fmt.Errorf(
@ -189,6 +190,13 @@ func Execute(cmd *exec.Cmd, timeout <-chan time.Time) (string, error) {
return stdout.String(), nil
}
func terminateProcess(cmd *exec.Cmd) error {
if runtime.GOOS == "windows" {
return cmd.Process.Kill()
}
return cmd.Process.Signal(syscall.SIGTERM)
}
func mergeWriter(other io.Writer, buf io.Writer) io.Writer {
if other != nil {
return io.MultiWriter(other, buf)

View File

@ -107,6 +107,14 @@ func (s *Suite) AfterTest(suite, test string) {
_ = os.RemoveAll(s.ConfigDir)
}
// ListProcessesCommand creates a command to list processes, "tasklist" on windows, "ps" otherwise.
func (s *Suite) ListProcessesCommand() *CmdContext {
if runtime.GOOS == "windows" {
return s.NewCommand("tasklist")
}
return s.NewCommand("ps")
}
// NewCommand creates a command context.
func (s *Suite) NewCommand(command string, args ...string) *CmdContext {
return &CmdContext{