Skip to content

Commit

Permalink
Merge pull request #5285 from sharifelgamal/windows-process
Browse files Browse the repository at this point in the history
Kill all child processes when cleaning up integration tests
  • Loading branch information
sharifelgamal authored Sep 10, 2019
2 parents e589c37 + 0a0c527 commit ea83658
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion test/integration/fn_addons.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func testDashboard(t *testing.T) {
mk := NewMinikubeRunner(t, p, "--wait=false")
cmd, out := mk.RunDaemon("dashboard --url")
defer func() {
err := cmd.Process.Kill()
err := util.KillProcess(cmd.Process.Pid, t)
if err != nil {
t.Logf("Failed to kill dashboard command: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion test/integration/fn_cluster_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ func testClusterStatus(t *testing.T) {
}

if err := retry.Expo(healthy, 500*time.Millisecond, time.Minute); err != nil {
t.Fatalf("Cluster is not healthy: %v", err)
t.Errorf("Cluster is not healthy: %v", err)
}
}
2 changes: 1 addition & 1 deletion test/integration/fn_mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func testMounting(t *testing.T) {
mountCmd := getMountCmd(mk, tempDir)
cmd, _, _ := mk.RunDaemon2(mountCmd)
defer func() {
err := cmd.Process.Kill()
err = util.KillProcess(cmd.Process.Pid, t)
if err != nil {
t.Logf("Failed to kill mount command: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions test/integration/fn_tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func testTunnel(t *testing.T) {
go func() {
output, stderr := mk.MustRun("tunnel --alsologtostderr -v 8 --logtostderr")
if t.Failed() {
t.Errorf("tunnel stderr : %s", stderr)
t.Errorf("tunnel output : %s", output)
t.Logf("tunnel stderr : %s", stderr)
t.Logf("tunnel output : %s", output)
}
}()

Expand Down
28 changes: 28 additions & 0 deletions test/integration/util/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

"github.com/pkg/errors"
"github.com/shirou/gopsutil/process"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/minikube/pkg/kapi"
)
Expand All @@ -44,3 +45,30 @@ func Logf(str string, args ...interface{}) {
fmt.Printf(" %s | ", time.Now().Format("15:04:05"))
fmt.Println(fmt.Sprintf(str, args...))
}

// KillProcess kills the process associated with the given pid and all its children
func KillProcess(pid int, t *testing.T) error {
p, err := process.NewProcess(int32(pid))
if err != nil {
// Process doesn't exist
return err
}
children, err := p.Children()
if err != nil {
// No children, log the error, don't exit
t.Log(err)
}
for _, c := range children {
err = c.Kill()
if err != nil {
// Log the error, but don't exit
t.Log(err)
}
}
err = p.Kill()
if err != nil {
return err
}

return nil
}
2 changes: 1 addition & 1 deletion test/integration/util/minikube_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (m *MinikubeRunner) start(opts ...string) (stdout string, stderr string, er
return m.RunWithContext(ctx, cmd, true)
}

// StartWithFail starts the cluster and fail the test if error
// MustStart starts the cluster and fail the test if error
func (m *MinikubeRunner) MustStart(opts ...string) (stdout string, stderr string) {
stdout, stderr, err := m.start(opts...)
// the reason for this formatting is, the logs are very big but useful and also in parallel testing logs are harder to identify
Expand Down
2 changes: 1 addition & 1 deletion test/integration/z_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func testProxyDashboard(t *testing.T) {
mk := NewMinikubeRunner(t, p)
cmd, out := mk.RunDaemon("dashboard --url")
defer func() {
err := cmd.Process.Kill()
err := util.KillProcess(cmd.Process.Pid, t)
if err != nil {
t.Logf("Failed to kill dashboard command: %v", err)
}
Expand Down

0 comments on commit ea83658

Please sign in to comment.