Skip to content

Commit 864ba8b

Browse files
committed
attach: Use ContainerWait instead of Inspect
Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
1 parent 026c6db commit 864ba8b

File tree

3 files changed

+32
-59
lines changed

3 files changed

+32
-59
lines changed

cli/command/container/attach.go

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ func runAttach(dockerCli command.Cli, opts *attachOptions) error {
6666
ctx := context.Background()
6767
client := dockerCli.Client()
6868

69+
// request channel to wait for client
70+
resultC, errC := client.ContainerWait(ctx, opts.container, "")
71+
6972
c, err := inspectContainerAndCheckState(ctx, client, opts.container)
7073
if err != nil {
7174
return err
@@ -140,7 +143,18 @@ func runAttach(dockerCli command.Cli, opts *attachOptions) error {
140143
if errAttach != nil {
141144
return errAttach
142145
}
143-
return getExitStatus(ctx, dockerCli.Client(), opts.container)
146+
147+
// get status code
148+
select {
149+
case result := <-resultC:
150+
if result.StatusCode != 0 {
151+
return cli.StatusError{StatusCode: int(result.StatusCode)}
152+
}
153+
case err := <-errC:
154+
return err
155+
}
156+
157+
return nil
144158
}
145159

146160
func resizeTTY(ctx context.Context, dockerCli command.Cli, containerID string) {
@@ -157,19 +171,3 @@ func resizeTTY(ctx context.Context, dockerCli command.Cli, containerID string) {
157171
logrus.Debugf("Error monitoring TTY size: %s", err)
158172
}
159173
}
160-
161-
func getExitStatus(ctx context.Context, apiclient client.ContainerAPIClient, containerID string) error {
162-
container, err := apiclient.ContainerInspect(ctx, containerID)
163-
if err != nil {
164-
// If we can't connect, then the daemon probably died.
165-
if !client.IsErrConnectionFailed(err) {
166-
return err
167-
}
168-
return cli.StatusError{StatusCode: -1}
169-
}
170-
status := container.State.ExitCode
171-
if status != 0 {
172-
return cli.StatusError{StatusCode: status}
173-
}
174-
return nil
175-
}

cli/command/container/attach_test.go

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@ import (
44
"io/ioutil"
55
"testing"
66

7-
"github.com/docker/cli/cli"
87
"github.com/docker/cli/internal/test"
98
"github.com/docker/cli/internal/test/testutil"
109
"github.com/docker/docker/api/types"
1110
"github.com/pkg/errors"
12-
"github.com/stretchr/testify/assert"
13-
"golang.org/x/net/context"
1411
)
1512

1613
func TestNewAttachCommandErrors(t *testing.T) {
@@ -76,42 +73,3 @@ func TestNewAttachCommandErrors(t *testing.T) {
7673
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
7774
}
7875
}
79-
80-
func TestGetExitStatus(t *testing.T) {
81-
containerID := "the exec id"
82-
expecatedErr := errors.New("unexpected error")
83-
84-
testcases := []struct {
85-
inspectError error
86-
exitCode int
87-
expectedError error
88-
}{
89-
{
90-
inspectError: nil,
91-
exitCode: 0,
92-
},
93-
{
94-
inspectError: expecatedErr,
95-
expectedError: expecatedErr,
96-
},
97-
{
98-
exitCode: 15,
99-
expectedError: cli.StatusError{StatusCode: 15},
100-
},
101-
}
102-
103-
for _, testcase := range testcases {
104-
client := &fakeClient{
105-
inspectFunc: func(id string) (types.ContainerJSON, error) {
106-
assert.Equal(t, containerID, id)
107-
return types.ContainerJSON{
108-
ContainerJSONBase: &types.ContainerJSONBase{
109-
State: &types.ContainerState{ExitCode: testcase.exitCode},
110-
},
111-
}, testcase.inspectError
112-
},
113-
}
114-
err := getExitStatus(context.Background(), client, containerID)
115-
assert.Equal(t, testcase.expectedError, err)
116-
}
117-
}

e2e/container/attach_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package container
2+
3+
import (
4+
"testing"
5+
6+
"github.com/gotestyourself/gotestyourself/icmd"
7+
)
8+
9+
func TestAttachExitCode(t *testing.T) {
10+
cName := "test-attach-exit-code"
11+
icmd.RunCommand("docker", "run", "-d", "--rm", "--name", cName,
12+
alpineImage, "sh", "-c", "sleep 5 ; exit 21").Assert(t, icmd.Success)
13+
cmd := icmd.Command("docker", "wait", cName)
14+
res := icmd.StartCmd(cmd)
15+
icmd.RunCommand("docker", "attach", cName).Assert(t, icmd.Expected{ExitCode: 21})
16+
icmd.WaitOnCmd(5, res).Assert(t, icmd.Success)
17+
}

0 commit comments

Comments
 (0)