2017-03-30 20:21:14 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2017-03-30 20:21:14 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/pkg/errors"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/golden"
|
2017-03-30 20:21:14 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewLoadCommandErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
isTerminalIn bool
|
|
|
|
expectedError string
|
|
|
|
imageLoadFunc func(input io.Reader, quiet bool) (types.ImageLoadResponse, error)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "wrong-args",
|
|
|
|
args: []string{"arg"},
|
2017-08-12 12:25:38 -04:00
|
|
|
expectedError: "accepts no arguments.",
|
2017-03-30 20:21:14 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "input-to-terminal",
|
|
|
|
isTerminalIn: true,
|
|
|
|
expectedError: "requested load from stdin, but stdin is empty",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "pull-error",
|
|
|
|
expectedError: "something went wrong",
|
|
|
|
imageLoadFunc: func(input io.Reader, quiet bool) (types.ImageLoadResponse, error) {
|
|
|
|
return types.ImageLoadResponse{}, errors.Errorf("something went wrong")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2017-07-05 14:43:39 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{imageLoadFunc: tc.imageLoadFunc})
|
2017-03-30 20:21:14 -04:00
|
|
|
cli.In().SetIsTerminal(tc.isTerminalIn)
|
|
|
|
cmd := NewLoadCommand(cli)
|
2022-02-25 07:10:34 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2017-03-30 20:21:14 -04:00
|
|
|
cmd.SetArgs(tc.args)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewLoadCommandInvalidInput(t *testing.T) {
|
|
|
|
expectedError := "open *"
|
2017-07-05 14:43:39 -04:00
|
|
|
cmd := NewLoadCommand(test.NewFakeCli(&fakeClient{}))
|
2022-02-25 07:10:34 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2017-03-30 20:21:14 -04:00
|
|
|
cmd.SetArgs([]string{"--input", "*"})
|
|
|
|
err := cmd.Execute()
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, err, expectedError)
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewLoadCommandSuccess(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
imageLoadFunc func(input io.Reader, quiet bool) (types.ImageLoadResponse, error)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "simple",
|
|
|
|
imageLoadFunc: func(input io.Reader, quiet bool) (types.ImageLoadResponse, error) {
|
2022-02-25 07:10:34 -05:00
|
|
|
return types.ImageLoadResponse{Body: io.NopCloser(strings.NewReader("Success"))}, nil
|
2017-03-30 20:21:14 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "json",
|
|
|
|
imageLoadFunc: func(input io.Reader, quiet bool) (types.ImageLoadResponse, error) {
|
|
|
|
json := "{\"ID\": \"1\"}"
|
|
|
|
return types.ImageLoadResponse{
|
2022-02-25 07:10:34 -05:00
|
|
|
Body: io.NopCloser(strings.NewReader(json)),
|
2017-03-30 20:21:14 -04:00
|
|
|
JSON: true,
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "input-file",
|
|
|
|
args: []string{"--input", "testdata/load-command-success.input.txt"},
|
|
|
|
imageLoadFunc: func(input io.Reader, quiet bool) (types.ImageLoadResponse, error) {
|
2022-02-25 07:10:34 -05:00
|
|
|
return types.ImageLoadResponse{Body: io.NopCloser(strings.NewReader("Success"))}, nil
|
2017-03-30 20:21:14 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2017-08-16 12:19:53 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{imageLoadFunc: tc.imageLoadFunc})
|
|
|
|
cmd := NewLoadCommand(cli)
|
2022-02-25 07:10:34 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2017-03-30 20:21:14 -04:00
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
err := cmd.Execute()
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2017-08-16 12:19:53 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("load-command-success.%s.golden", tc.name))
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|
|
|
|
}
|