Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[27.1 backport] attach: wait for exit code from ContainerWait #5302

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cli/command/container/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ func RunAttach(ctx context.Context, dockerCLI command.Cli, containerID string, o
apiClient := dockerCLI.Client()

// request channel to wait for client
resultC, errC := apiClient.ContainerWait(ctx, containerID, "")
waitCtx := context.WithoutCancel(ctx)
resultC, errC := apiClient.ContainerWait(waitCtx, containerID, "")

c, err := inspectContainerAndCheckState(ctx, apiClient, containerID)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions cli/command/container/attach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,7 @@ func TestNewAttachCommandErrors(t *testing.T) {
}

func TestGetExitStatus(t *testing.T) {
var (
expectedErr = errors.New("unexpected error")
errC = make(chan error, 1)
resultC = make(chan container.WaitResponse, 1)
)
expectedErr := errors.New("unexpected error")

testcases := []struct {
result *container.WaitResponse
Expand Down Expand Up @@ -121,13 +117,17 @@ func TestGetExitStatus(t *testing.T) {
}

for _, testcase := range testcases {
errC := make(chan error, 1)
resultC := make(chan container.WaitResponse, 1)
if testcase.err != nil {
errC <- testcase.err
}
if testcase.result != nil {
resultC <- *testcase.result
}

err := getExitStatus(errC, resultC)

if testcase.expectedError == nil {
assert.NilError(t, err)
} else {
Expand Down
14 changes: 11 additions & 3 deletions e2e/container/attach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/docker/cli/e2e/internal/fixtures"
"gotest.tools/v3/assert"
"gotest.tools/v3/icmd"
"gotest.tools/v3/skip"
)

func TestAttachExitCode(t *testing.T) {
Expand All @@ -32,7 +33,13 @@ func withStdinNewline(cmd *icmd.Cmd) {

// Regression test for https://github.com/docker/cli/issues/5294
func TestAttachInterrupt(t *testing.T) {
result := icmd.RunCommand("docker", "run", "-d", fixtures.AlpineImage, "sh", "-c", "sleep 5")
// this is a new test, it already did not work (inside dind) when over ssh
// todo(laurazard): make this test work w/ dind over ssh
skip.If(t, strings.Contains(os.Getenv("DOCKER_HOST"), "ssh://"))

// if
result := icmd.RunCommand("docker", "run", "-d", fixtures.AlpineImage,
"sh", "-c", "trap \"exit 33\" SIGINT; for i in $(seq 100); do sleep 0.1; done; exit 34")
result.Assert(t, icmd.Success)
containerID := strings.TrimSpace(result.Stdout())

Expand All @@ -49,6 +56,7 @@ func TestAttachInterrupt(t *testing.T) {
c.Process.Signal(os.Interrupt)

_ = c.Wait()
assert.Equal(t, c.ProcessState.ExitCode(), 0)
assert.Equal(t, d.String(), "")
// the CLI should exit with 33 (the SIGINT was forwarded to the container), and the
// CLI process waited for the container exit and properly captured/set the exit code
assert.Equal(t, c.ProcessState.ExitCode(), 33)
}
Loading