Skip to content

Commit a35c363

Browse files
authored
Merge pull request #5302 from laurazard/27-attach-exit-code
[27.1 backport] attach: wait for exit code from `ContainerWait`
2 parents fd3157b + 1cf3637 commit a35c363

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

cli/command/container/attach.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ func RunAttach(ctx context.Context, dockerCLI command.Cli, containerID string, o
7373
apiClient := dockerCLI.Client()
7474

7575
// request channel to wait for client
76-
resultC, errC := apiClient.ContainerWait(ctx, containerID, "")
76+
waitCtx := context.WithoutCancel(ctx)
77+
resultC, errC := apiClient.ContainerWait(waitCtx, containerID, "")
7778

7879
c, err := inspectContainerAndCheckState(ctx, apiClient, containerID)
7980
if err != nil {

cli/command/container/attach_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,7 @@ func TestNewAttachCommandErrors(t *testing.T) {
8282
}
8383

8484
func TestGetExitStatus(t *testing.T) {
85-
var (
86-
expectedErr = errors.New("unexpected error")
87-
errC = make(chan error, 1)
88-
resultC = make(chan container.WaitResponse, 1)
89-
)
85+
expectedErr := errors.New("unexpected error")
9086

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

123119
for _, testcase := range testcases {
120+
errC := make(chan error, 1)
121+
resultC := make(chan container.WaitResponse, 1)
124122
if testcase.err != nil {
125123
errC <- testcase.err
126124
}
127125
if testcase.result != nil {
128126
resultC <- *testcase.result
129127
}
128+
130129
err := getExitStatus(errC, resultC)
130+
131131
if testcase.expectedError == nil {
132132
assert.NilError(t, err)
133133
} else {

e2e/container/attach_test.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/docker/cli/e2e/internal/fixtures"
1414
"gotest.tools/v3/assert"
1515
"gotest.tools/v3/icmd"
16+
"gotest.tools/v3/skip"
1617
)
1718

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

3334
// Regression test for https://github.com/docker/cli/issues/5294
3435
func TestAttachInterrupt(t *testing.T) {
35-
result := icmd.RunCommand("docker", "run", "-d", fixtures.AlpineImage, "sh", "-c", "sleep 5")
36+
// this is a new test, it already did not work (inside dind) when over ssh
37+
// todo(laurazard): make this test work w/ dind over ssh
38+
skip.If(t, strings.Contains(os.Getenv("DOCKER_HOST"), "ssh://"))
39+
40+
// if
41+
result := icmd.RunCommand("docker", "run", "-d", fixtures.AlpineImage,
42+
"sh", "-c", "trap \"exit 33\" SIGINT; for i in $(seq 100); do sleep 0.1; done; exit 34")
3643
result.Assert(t, icmd.Success)
3744
containerID := strings.TrimSpace(result.Stdout())
3845

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

5158
_ = c.Wait()
52-
assert.Equal(t, c.ProcessState.ExitCode(), 0)
53-
assert.Equal(t, d.String(), "")
59+
// the CLI should exit with 33 (the SIGINT was forwarded to the container), and the
60+
// CLI process waited for the container exit and properly captured/set the exit code
61+
assert.Equal(t, c.ProcessState.ExitCode(), 33)
5462
}

0 commit comments

Comments
 (0)