Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions cli/command/image/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func runLoad(ctx context.Context, dockerCli command.Cli, opts loadOptions) error
if err != nil {
return err
}
defer file.Close()
defer func() { _ = file.Close() }()
input = file
}

Expand All @@ -95,12 +95,7 @@ func runLoad(ctx context.Context, dockerCli command.Cli, opts loadOptions) error
if err != nil {
return err
}
defer res.Close()
defer func() { _ = res.Close() }()

if res.JSON {
return jsonstream.Display(ctx, res, dockerCli.Out())
}

_, err = io.Copy(dockerCli.Out(), res)
return err
return jsonstream.Display(ctx, res, dockerCli.Out())
}
34 changes: 12 additions & 22 deletions cli/command/image/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func TestNewLoadCommandInvalidInput(t *testing.T) {
assert.ErrorContains(t, err, expectedError)
}

func mockImageLoadResult(content string, json bool) client.ImageLoadResult {
out := client.ImageLoadResult{JSON: json}
func mockImageLoadResult(content string) client.ImageLoadResult {
out := client.ImageLoadResult{}

// Set unexported field "body"
v := reflect.ValueOf(&out).Elem()
Expand All @@ -94,31 +94,21 @@ func TestNewLoadCommandSuccess(t *testing.T) {
{
name: "simple",
args: []string{},
imageLoadFunc: func(input io.Reader, options ...client.ImageLoadOption) (client.ImageLoadResult, error) {
// FIXME(thaJeztah): how to mock this?
// return client.ImageLoadResult{Body: io.NopCloser(strings.NewReader("Success"))}, nil
return mockImageLoadResult(`Success`, false), nil
},
},
{
name: "json",
args: []string{},
imageLoadFunc: func(input io.Reader, options ...client.ImageLoadOption) (client.ImageLoadResult, error) {
// FIXME(thaJeztah): how to mock this?
// return client.ImageLoadResult{
// Body: io.NopCloser(strings.NewReader(`{"ID": "1"}`)),
// JSON: true,
// Body: io.NopCloser(strings.NewReader(`{"ID":"simple","Status":"success"}`)),
// }, nil
return mockImageLoadResult(`{"ID":"1"}`, true), nil
return mockImageLoadResult(`{"ID":"simple","Status":"success"}`), nil
},
},
{
name: "input-file",
args: []string{"--input", "testdata/load-command-success.input.txt"},
imageLoadFunc: func(input io.Reader, options ...client.ImageLoadOption) (client.ImageLoadResult, error) {
// FIXME(thaJeztah): how to mock this?
// return client.ImageLoadResult{Body: io.NopCloser(strings.NewReader("Success"))}, nil
return mockImageLoadResult(`Success`, false), nil
// return client.ImageLoadResult{Body: io.NopCloser(strings.NewReader(`{"ID":"input-file","Status":"success"}`))}, nil
return mockImageLoadResult(`{"ID":"input-file","Status":"success"}`), nil
},
},
{
Expand All @@ -129,8 +119,8 @@ func TestNewLoadCommandSuccess(t *testing.T) {
assert.Check(t, len(options) > 0) // can be 1 or two depending on whether a terminal is attached :/
// assert.Check(t, is.Contains(options, client.ImageHistoryWithPlatform(ocispec.Platform{OS: "linux", Architecture: "amd64"})))
// FIXME(thaJeztah): how to mock this?
// return client.ImageLoadResult{Body: io.NopCloser(strings.NewReader("Success"))}, nil
return mockImageLoadResult(`Success`, false), nil
// return client.ImageLoadResult{Body: io.NopCloser(strings.NewReader(`{"ID":"single-platform","Status":"success"}`))}, nil
return mockImageLoadResult(`{"ID":"single-platform","Status":"success"}`), nil
},
},
{
Expand All @@ -139,8 +129,8 @@ func TestNewLoadCommandSuccess(t *testing.T) {
imageLoadFunc: func(input io.Reader, options ...client.ImageLoadOption) (client.ImageLoadResult, error) {
assert.Check(t, len(options) > 0) // can be 1 or two depending on whether a terminal is attached :/
// FIXME(thaJeztah): how to mock this?
// return client.ImageLoadResult{Body: io.NopCloser(strings.NewReader("Success"))}, nil
return mockImageLoadResult(`Success`, false), nil
// return client.ImageLoadResult{Body: io.NopCloser(strings.NewReader(`{"ID":"with-comma-separated-platforms","Status":"success"}`))}, nil
return mockImageLoadResult(`{"ID":"with-comma-separated-platforms","Status":"success"}`), nil
},
},
{
Expand All @@ -149,8 +139,8 @@ func TestNewLoadCommandSuccess(t *testing.T) {
imageLoadFunc: func(input io.Reader, options ...client.ImageLoadOption) (client.ImageLoadResult, error) {
assert.Check(t, len(options) > 0) // can be 1 or two depending on whether a terminal is attached :/
// FIXME(thaJeztah): how to mock this?
// return client.ImageLoadResult{Body: io.NopCloser(strings.NewReader("Success"))}, nil
return mockImageLoadResult(`Success`, false), nil
// return client.ImageLoadResult{Body: io.NopCloser(strings.NewReader(`{"ID":"with-multiple-platform-options","Status":"success"}`))}, nil
return mockImageLoadResult(`{"ID":"with-multiple-platform-options","Status":"success"}`), nil
},
},
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Success
input-file: success

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
Success
simple: success
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Success
with-comma-separated-platforms: success
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Success
with-multiple-platform-options: success
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Success
single-platform: success
Loading