mirror of https://github.com/docker/cli.git
Merge pull request #5297 from laurazard/fix-context-cancel-attach-exit-code
attach: wait for exit code from `ContainerWait`
This commit is contained in:
commit
bc7e64d425
|
@ -72,7 +72,8 @@ func RunAttach(ctx context.Context, dockerCLI command.Cli, containerID string, o
|
||||||
apiClient := dockerCLI.Client()
|
apiClient := dockerCLI.Client()
|
||||||
|
|
||||||
// request channel to wait for 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)
|
c, err := inspectContainerAndCheckState(ctx, apiClient, containerID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -163,9 +164,6 @@ func getExitStatus(errC <-chan error, resultC <-chan container.WaitResponse) err
|
||||||
return cli.StatusError{StatusCode: int(result.StatusCode)}
|
return cli.StatusError{StatusCode: int(result.StatusCode)}
|
||||||
}
|
}
|
||||||
case err := <-errC:
|
case err := <-errC:
|
||||||
if errors.Is(err, context.Canceled) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package container
|
package container
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"io"
|
"io"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -86,11 +85,7 @@ func TestNewAttachCommandErrors(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetExitStatus(t *testing.T) {
|
func TestGetExitStatus(t *testing.T) {
|
||||||
var (
|
expectedErr := errors.New("unexpected error")
|
||||||
expectedErr = errors.New("unexpected error")
|
|
||||||
errC = make(chan error, 1)
|
|
||||||
resultC = make(chan container.WaitResponse, 1)
|
|
||||||
)
|
|
||||||
|
|
||||||
testcases := []struct {
|
testcases := []struct {
|
||||||
result *container.WaitResponse
|
result *container.WaitResponse
|
||||||
|
@ -118,20 +113,20 @@ func TestGetExitStatus(t *testing.T) {
|
||||||
},
|
},
|
||||||
expectedError: cli.StatusError{StatusCode: 15},
|
expectedError: cli.StatusError{StatusCode: 15},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
err: context.Canceled,
|
|
||||||
expectedError: nil,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, testcase := range testcases {
|
for _, testcase := range testcases {
|
||||||
|
errC := make(chan error, 1)
|
||||||
|
resultC := make(chan container.WaitResponse, 1)
|
||||||
if testcase.err != nil {
|
if testcase.err != nil {
|
||||||
errC <- testcase.err
|
errC <- testcase.err
|
||||||
}
|
}
|
||||||
if testcase.result != nil {
|
if testcase.result != nil {
|
||||||
resultC <- *testcase.result
|
resultC <- *testcase.result
|
||||||
}
|
}
|
||||||
|
|
||||||
err := getExitStatus(errC, resultC)
|
err := getExitStatus(errC, resultC)
|
||||||
|
|
||||||
if testcase.expectedError == nil {
|
if testcase.expectedError == nil {
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -13,6 +13,7 @@ import (
|
||||||
"github.com/docker/cli/e2e/internal/fixtures"
|
"github.com/docker/cli/e2e/internal/fixtures"
|
||||||
"gotest.tools/v3/assert"
|
"gotest.tools/v3/assert"
|
||||||
"gotest.tools/v3/icmd"
|
"gotest.tools/v3/icmd"
|
||||||
|
"gotest.tools/v3/skip"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAttachExitCode(t *testing.T) {
|
func TestAttachExitCode(t *testing.T) {
|
||||||
|
@ -32,7 +33,13 @@ func withStdinNewline(cmd *icmd.Cmd) {
|
||||||
|
|
||||||
// Regression test for https://github.com/docker/cli/issues/5294
|
// Regression test for https://github.com/docker/cli/issues/5294
|
||||||
func TestAttachInterrupt(t *testing.T) {
|
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)
|
result.Assert(t, icmd.Success)
|
||||||
containerID := strings.TrimSpace(result.Stdout())
|
containerID := strings.TrimSpace(result.Stdout())
|
||||||
|
|
||||||
|
@ -49,6 +56,8 @@ func TestAttachInterrupt(t *testing.T) {
|
||||||
c.Process.Signal(os.Interrupt)
|
c.Process.Signal(os.Interrupt)
|
||||||
|
|
||||||
_ = c.Wait()
|
_ = c.Wait()
|
||||||
assert.Equal(t, c.ProcessState.ExitCode(), 0)
|
// the CLI should exit with 33 (the SIGINT was forwarded to the container), and the
|
||||||
assert.Equal(t, d.String(), "")
|
// CLI process waited for the container exit and properly captured/set the exit code
|
||||||
|
assert.Equal(t, c.ProcessState.ExitCode(), 33)
|
||||||
|
assert.Equal(t, d.String(), "exit status 33\n")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue