2019-04-19 07:03:37 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-07-29 19:43:22 -04:00
|
|
|
"github.com/creack/pty"
|
2019-04-19 07:03:37 -04:00
|
|
|
"github.com/docker/cli/e2e/internal/fixtures"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/icmd"
|
|
|
|
"gotest.tools/v3/poll"
|
2019-04-19 07:03:37 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestSigProxyWithTTY tests that killing the docker CLI forwards the signal to
|
|
|
|
// the container, and kills the container's process. Test-case for moby/moby#28872
|
|
|
|
func TestSigProxyWithTTY(t *testing.T) {
|
2019-07-23 20:12:16 -04:00
|
|
|
cmd := exec.Command("docker", "run", "-i", "-t", "--init", "--name", t.Name(), fixtures.BusyboxImage, "sleep", "30")
|
|
|
|
p, err := pty.Start(cmd)
|
|
|
|
defer func() {
|
|
|
|
_ = cmd.Wait()
|
|
|
|
_ = p.Close()
|
|
|
|
}()
|
|
|
|
assert.NilError(t, err, "failed to start container")
|
|
|
|
defer icmd.RunCommand("docker", "container", "rm", "-f", t.Name())
|
2019-04-19 07:03:37 -04:00
|
|
|
|
2019-10-28 20:44:16 -04:00
|
|
|
poll.WaitOn(t, containerExistsWithStatus(t.Name(), "running"), poll.WithDelay(100*time.Millisecond), poll.WithTimeout(5*time.Second))
|
2019-04-19 07:03:37 -04:00
|
|
|
|
|
|
|
pid := cmd.Process.Pid
|
|
|
|
t.Logf("terminating PID %d", pid)
|
|
|
|
err = syscall.Kill(pid, syscall.SIGTERM)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2019-10-28 20:44:16 -04:00
|
|
|
poll.WaitOn(t, containerExistsWithStatus(t.Name(), "exited"), poll.WithDelay(100*time.Millisecond), poll.WithTimeout(5*time.Second))
|
2019-04-19 07:03:37 -04:00
|
|
|
}
|
|
|
|
|
2019-10-28 20:44:16 -04:00
|
|
|
func containerExistsWithStatus(containerID, status string) func(poll.LogT) poll.Result {
|
2019-04-19 07:03:37 -04:00
|
|
|
return func(poll.LogT) poll.Result {
|
|
|
|
result := icmd.RunCommand("docker", "inspect", "-f", "{{ .State.Status }}", containerID)
|
|
|
|
// ignore initial failures as the container may not yet exist (i.e., don't result.Assert(t, icmd.Success))
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(result.Stdout())
|
|
|
|
if actual == status {
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
return poll.Continue("expected status %s != %s", status, actual)
|
|
|
|
}
|
|
|
|
}
|