2017-03-30 20:21:14 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"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"
|
2018-03-05 18:53:52 -05:00
|
|
|
"github.com/gotestyourself/gotestyourself/assert"
|
|
|
|
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
2017-03-30 20:21:14 -04:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewImportCommandErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
expectedError string
|
|
|
|
imageImportFunc func(source types.ImageImportSource, ref string, options types.ImageImportOptions) (io.ReadCloser, error)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "wrong-args",
|
|
|
|
args: []string{},
|
2017-08-12 12:25:38 -04:00
|
|
|
expectedError: "requires at least 1 argument.",
|
2017-03-30 20:21:14 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "import-failed",
|
|
|
|
args: []string{"testdata/import-command-success.input.txt"},
|
|
|
|
expectedError: "something went wrong",
|
|
|
|
imageImportFunc: func(source types.ImageImportSource, ref string, options types.ImageImportOptions) (io.ReadCloser, error) {
|
|
|
|
return nil, errors.Errorf("something went wrong")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2017-08-16 12:19:53 -04:00
|
|
|
cmd := NewImportCommand(test.NewFakeCli(&fakeClient{imageImportFunc: tc.imageImportFunc}))
|
2017-03-30 20:21:14 -04:00
|
|
|
cmd.SetOutput(ioutil.Discard)
|
|
|
|
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 TestNewImportCommandInvalidFile(t *testing.T) {
|
2017-07-05 14:43:39 -04:00
|
|
|
cmd := NewImportCommand(test.NewFakeCli(&fakeClient{}))
|
2017-03-30 20:21:14 -04:00
|
|
|
cmd.SetOutput(ioutil.Discard)
|
|
|
|
cmd.SetArgs([]string{"testdata/import-command-success.unexistent-file"})
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), "testdata/import-command-success.unexistent-file")
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewImportCommandSuccess(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
imageImportFunc func(source types.ImageImportSource, ref string, options types.ImageImportOptions) (io.ReadCloser, error)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "simple",
|
|
|
|
args: []string{"testdata/import-command-success.input.txt"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "terminal-source",
|
|
|
|
args: []string{"-"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "double",
|
|
|
|
args: []string{"-", "image:local"},
|
|
|
|
imageImportFunc: func(source types.ImageImportSource, ref string, options types.ImageImportOptions) (io.ReadCloser, error) {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("image:local", ref))
|
2017-03-30 20:21:14 -04:00
|
|
|
return ioutil.NopCloser(strings.NewReader("")), nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "message",
|
|
|
|
args: []string{"--message", "test message", "-"},
|
|
|
|
imageImportFunc: func(source types.ImageImportSource, ref string, options types.ImageImportOptions) (io.ReadCloser, error) {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("test message", options.Message))
|
2017-03-30 20:21:14 -04:00
|
|
|
return ioutil.NopCloser(strings.NewReader("")), nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "change",
|
|
|
|
args: []string{"--change", "ENV DEBUG true", "-"},
|
|
|
|
imageImportFunc: func(source types.ImageImportSource, ref string, options types.ImageImportOptions) (io.ReadCloser, error) {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("ENV DEBUG true", options.Changes[0]))
|
2017-03-30 20:21:14 -04:00
|
|
|
return ioutil.NopCloser(strings.NewReader("")), nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2017-08-16 12:19:53 -04:00
|
|
|
cmd := NewImportCommand(test.NewFakeCli(&fakeClient{imageImportFunc: tc.imageImportFunc}))
|
2017-03-30 20:21:14 -04:00
|
|
|
cmd.SetOutput(ioutil.Discard)
|
|
|
|
cmd.SetArgs(tc.args)
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|
|
|
|
}
|