Merge pull request #46993 from thaJeztah/24.0_backport_46621-container_wait

[24.0 backport] Ensure that non-JSON-parsing errors are returned to the caller
This commit is contained in:
Sebastiaan van Stijn 2023-12-28 19:40:17 +01:00 committed by GitHub
commit 1c9cd6a5f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 2 deletions

View file

@ -66,8 +66,12 @@ func (cli *Client) ContainerWait(ctx context.Context, containerID string, condit
//
// If there's a JSON parsing error, read the real error message
// off the body and send it to the client.
_, _ = io.ReadAll(io.LimitReader(stream, containerWaitErrorMsgLimit))
errC <- errors.New(responseText.String())
if errors.As(err, new(*json.SyntaxError)) {
_, _ = io.ReadAll(io.LimitReader(stream, containerWaitErrorMsgLimit))
errC <- errors.New(responseText.String())
} else {
errC <- err
}
return
}

View file

@ -9,11 +9,14 @@ import (
"log"
"net/http"
"strings"
"syscall"
"testing"
"testing/iotest"
"time"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/errdefs"
"github.com/pkg/errors"
)
func TestContainerWaitError(t *testing.T) {
@ -117,6 +120,46 @@ func TestContainerWaitProxyInterruptLong(t *testing.T) {
}
}
func TestContainerWaitErrorHandling(t *testing.T) {
for _, test := range []struct {
name string
rdr io.Reader
exp error
}{
{name: "invalid json", rdr: strings.NewReader(`{]`), exp: errors.New("{]")},
{name: "context canceled", rdr: iotest.ErrReader(context.Canceled), exp: context.Canceled},
{name: "context deadline exceeded", rdr: iotest.ErrReader(context.DeadlineExceeded), exp: context.DeadlineExceeded},
{name: "connection reset", rdr: iotest.ErrReader(syscall.ECONNRESET), exp: syscall.ECONNRESET},
} {
t.Run(test.name, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
client := &Client{
version: "1.30",
client: newMockClient(func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(test.rdr),
}, nil
}),
}
resultC, errC := client.ContainerWait(ctx, "container_id", "")
select {
case err := <-errC:
if err.Error() != test.exp.Error() {
t.Fatalf("ContainerWait() errC = %v; want %v", err, test.exp)
}
return
case result := <-resultC:
t.Fatalf("expected to not get a wait result, got %d", result.StatusCode)
return
}
// Unexpected - we should not reach this line
})
}
}
func ExampleClient_ContainerWait_withTimeout() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()