github.com/pwn-term/docker@v0.0.0-20210616085119-6e977cce2565/cli/e2e/container/proxy_signal_test.go (about) 1 package container 2 3 import ( 4 "os/exec" 5 "strings" 6 "syscall" 7 "testing" 8 "time" 9 10 "github.com/creack/pty" 11 "github.com/docker/cli/e2e/internal/fixtures" 12 "gotest.tools/v3/assert" 13 "gotest.tools/v3/icmd" 14 "gotest.tools/v3/poll" 15 ) 16 17 // TestSigProxyWithTTY tests that killing the docker CLI forwards the signal to 18 // the container, and kills the container's process. Test-case for moby/moby#28872 19 func TestSigProxyWithTTY(t *testing.T) { 20 cmd := exec.Command("docker", "run", "-i", "-t", "--init", "--name", t.Name(), fixtures.BusyboxImage, "sleep", "30") 21 p, err := pty.Start(cmd) 22 defer func() { 23 _ = cmd.Wait() 24 _ = p.Close() 25 }() 26 assert.NilError(t, err, "failed to start container") 27 defer icmd.RunCommand("docker", "container", "rm", "-f", t.Name()) 28 29 poll.WaitOn(t, containerExistsWithStatus(t.Name(), "running"), poll.WithDelay(100*time.Millisecond), poll.WithTimeout(5*time.Second)) 30 31 pid := cmd.Process.Pid 32 t.Logf("terminating PID %d", pid) 33 err = syscall.Kill(pid, syscall.SIGTERM) 34 assert.NilError(t, err) 35 36 poll.WaitOn(t, containerExistsWithStatus(t.Name(), "exited"), poll.WithDelay(100*time.Millisecond), poll.WithTimeout(5*time.Second)) 37 } 38 39 func containerExistsWithStatus(containerID, status string) func(poll.LogT) poll.Result { 40 return func(poll.LogT) poll.Result { 41 result := icmd.RunCommand("docker", "inspect", "-f", "{{ .State.Status }}", containerID) 42 // ignore initial failures as the container may not yet exist (i.e., don't result.Assert(t, icmd.Success)) 43 44 actual := strings.TrimSpace(result.Stdout()) 45 if actual == status { 46 return poll.Success() 47 } 48 return poll.Continue("expected status %s != %s", status, actual) 49 } 50 }