client: Client.ContainerLogs: close reader on context cancellation

Use a cancelReadCloser to automatically close the reader when the context
is cancelled. Consumers are still recommended to manually close the reader,
but the cancelReadCloser makes the Close idempotent.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-10-30 10:38:42 +01:00
parent cc9969bfed
commit d35d8ec81b
3 changed files with 11 additions and 37 deletions

View File

@@ -100,14 +100,14 @@ func TestNetworkLoopbackNat(t *testing.T) {
poll.WaitOn(t, container.IsStopped(ctx, apiClient, cID))
body, err := apiClient.ContainerLogs(ctx, cID, client.ContainerLogsOptions{
logs, err := apiClient.ContainerLogs(ctx, cID, client.ContainerLogsOptions{
ShowStdout: true,
})
assert.NilError(t, err)
defer body.Close()
defer logs.Close()
var b bytes.Buffer
_, err = io.Copy(&b, body)
_, err = io.Copy(&b, logs)
assert.NilError(t, err)
assert.Check(t, is.Equal(msg, strings.TrimSpace(b.String())))