From 7c6b04f28e901410a2c839a3b0063760acc1cb6b Mon Sep 17 00:00:00 2001 From: "guillaume.tardif" Date: Thu, 4 Jun 2020 16:31:06 +0200 Subject: [PATCH] make e2e TestKillChildOnCancel exec properly on windows --- tests/e2e/e2e_test.go | 10 +++++----- tests/framework/exec.go | 10 +++++++++- tests/framework/suite.go | 8 ++++++++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/tests/e2e/e2e_test.go b/tests/e2e/e2e_test.go index 67e528f21..e968212b1 100644 --- a/tests/e2e/e2e_test.go +++ b/tests/e2e/e2e_test.go @@ -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()) diff --git a/tests/framework/exec.go b/tests/framework/exec.go index b0494e899..899dab2fb 100644 --- a/tests/framework/exec.go +++ b/tests/framework/exec.go @@ -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) diff --git a/tests/framework/suite.go b/tests/framework/suite.go index 076e9a44a..b27e9a553 100644 --- a/tests/framework/suite.go +++ b/tests/framework/suite.go @@ -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{