github.com/itscaro/cli@v0.0.0-20190705081621-c9db0fe93829/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/docker/cli/e2e/internal/fixtures"
    11  	"github.com/kr/pty"
    12  	"gotest.tools/assert"
    13  	"gotest.tools/icmd"
    14  	"gotest.tools/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  	_, tty, err := pty.Open()
    21  	assert.NilError(t, err, "could not open pty")
    22  	defer func() { _ = tty.Close() }()
    23  
    24  	containerName := "repro-28872"
    25  	cmd := exec.Command("docker", "run", "-i", "-t", "--init", "--name", containerName, fixtures.BusyboxImage, "sleep", "30")
    26  	cmd.Stdin = tty
    27  	cmd.Stdout = tty
    28  	cmd.Stderr = tty
    29  
    30  	err = cmd.Start()
    31  	out, _ := cmd.CombinedOutput()
    32  	assert.NilError(t, err, "failed to start container: %s", out)
    33  	defer icmd.RunCommand("docker", "container", "rm", "-f", containerName)
    34  
    35  	poll.WaitOn(t, containerExistsWithStatus(t, containerName, "running"), poll.WithDelay(100*time.Millisecond), poll.WithTimeout(5*time.Second))
    36  
    37  	pid := cmd.Process.Pid
    38  	t.Logf("terminating PID %d", pid)
    39  	err = syscall.Kill(pid, syscall.SIGTERM)
    40  	assert.NilError(t, err)
    41  
    42  	poll.WaitOn(t, containerExistsWithStatus(t, containerName, "exited"), poll.WithDelay(100*time.Millisecond), poll.WithTimeout(5*time.Second))
    43  }
    44  
    45  func containerExistsWithStatus(t *testing.T, containerID, status string) func(poll.LogT) poll.Result {
    46  	return func(poll.LogT) poll.Result {
    47  		result := icmd.RunCommand("docker", "inspect", "-f", "{{ .State.Status }}", containerID)
    48  		// ignore initial failures as the container may not yet exist (i.e., don't result.Assert(t, icmd.Success))
    49  
    50  		actual := strings.TrimSpace(result.Stdout())
    51  		if actual == status {
    52  			return poll.Success()
    53  		}
    54  		return poll.Continue("expected status %s != %s", status, actual)
    55  	}
    56  }