mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
client: Client.ContainerExport: 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:
@@ -71,6 +71,7 @@ func TestExportContainerAfterDaemonRestart(t *testing.T) {
|
||||
|
||||
d.Restart(t)
|
||||
|
||||
_, err := c.ContainerExport(ctx, ctrID, client.ContainerExportOptions{})
|
||||
res, err := c.ContainerExport(ctx, ctrID, client.ContainerExportOptions{})
|
||||
assert.NilError(t, err)
|
||||
_ = res.Close()
|
||||
}
|
||||
|
||||
@@ -32,10 +32,10 @@ func TestNoOverlayfsWarningsAboutUndefinedBehaviors(t *testing.T) {
|
||||
return err
|
||||
}},
|
||||
{name: "export", operation: func(*testing.T) error {
|
||||
rc, err := apiClient.ContainerExport(ctx, cID, client.ContainerExportOptions{})
|
||||
res, err := apiClient.ContainerExport(ctx, cID, client.ContainerExportOptions{})
|
||||
if err == nil {
|
||||
defer rc.Close()
|
||||
_, err = io.Copy(io.Discard, rc)
|
||||
_, err = io.Copy(io.Discard, res)
|
||||
_ = res.Close()
|
||||
}
|
||||
return err
|
||||
}},
|
||||
@@ -48,8 +48,8 @@ func TestNoOverlayfsWarningsAboutUndefinedBehaviors(t *testing.T) {
|
||||
{name: "cp from container", operation: func(*testing.T) error {
|
||||
res, err := apiClient.CopyFromContainer(ctx, cID, client.CopyFromContainerOptions{SourcePath: "/file"})
|
||||
if err == nil {
|
||||
defer res.Content.Close()
|
||||
_, err = io.Copy(io.Discard, res.Content)
|
||||
_ = res.Content.Close()
|
||||
}
|
||||
|
||||
return err
|
||||
|
||||
@@ -363,13 +363,13 @@ func TestAuthZPluginEnsureLoadImportWorking(t *testing.T) {
|
||||
|
||||
cID := container.Run(ctx, t, c)
|
||||
|
||||
responseReader, err := c.ContainerExport(ctx, cID, client.ContainerExportOptions{})
|
||||
res, err := c.ContainerExport(ctx, cID, client.ContainerExportOptions{})
|
||||
assert.NilError(t, err)
|
||||
defer responseReader.Close()
|
||||
defer func() { _ = res.Close() }()
|
||||
file, err := os.Create(exportedImagePath)
|
||||
assert.NilError(t, err)
|
||||
defer file.Close()
|
||||
_, err = io.Copy(file, responseReader)
|
||||
defer func() { _ = file.Close() }()
|
||||
_, err = io.Copy(file, res)
|
||||
assert.NilError(t, err)
|
||||
|
||||
err = imageImport(ctx, c, exportedImagePath)
|
||||
|
||||
Reference in New Issue
Block a user