2017-03-30 20:21:14 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2024-01-24 08:32:07 -05:00
|
|
|
"github.com/docker/docker/api/types/image"
|
2017-03-30 20:21:14 -04:00
|
|
|
"github.com/pkg/errors"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2017-03-30 20:21:14 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewImportCommandErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
expectedError string
|
2024-06-09 07:54:37 -04:00
|
|
|
imageImportFunc func(source image.ImportSource, ref string, options image.ImportOptions) (io.ReadCloser, error)
|
2017-03-30 20:21:14 -04:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
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",
|
2024-06-09 07:54:37 -04:00
|
|
|
imageImportFunc: func(source image.ImportSource, ref string, options image.ImportOptions) (io.ReadCloser, error) {
|
2017-03-30 20:21:14 -04:00
|
|
|
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}))
|
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 TestNewImportCommandInvalidFile(t *testing.T) {
|
2017-07-05 14:43:39 -04:00
|
|
|
cmd := NewImportCommand(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{"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
|
2024-06-09 07:54:37 -04:00
|
|
|
imageImportFunc func(source image.ImportSource, ref string, options image.ImportOptions) (io.ReadCloser, error)
|
2017-03-30 20:21:14 -04:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "simple",
|
|
|
|
args: []string{"testdata/import-command-success.input.txt"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "terminal-source",
|
|
|
|
args: []string{"-"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "double",
|
|
|
|
args: []string{"-", "image:local"},
|
2024-06-09 07:54:37 -04:00
|
|
|
imageImportFunc: func(source image.ImportSource, ref string, options image.ImportOptions) (io.ReadCloser, error) {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("image:local", ref))
|
2022-02-25 07:10:34 -05:00
|
|
|
return io.NopCloser(strings.NewReader("")), nil
|
2017-03-30 20:21:14 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "message",
|
|
|
|
args: []string{"--message", "test message", "-"},
|
2024-06-09 07:54:37 -04:00
|
|
|
imageImportFunc: func(source image.ImportSource, ref string, options image.ImportOptions) (io.ReadCloser, error) {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("test message", options.Message))
|
2022-02-25 07:10:34 -05:00
|
|
|
return io.NopCloser(strings.NewReader("")), nil
|
2017-03-30 20:21:14 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "change",
|
2020-09-23 07:23:59 -04:00
|
|
|
args: []string{"--change", "ENV DEBUG=true", "-"},
|
2024-06-09 07:54:37 -04:00
|
|
|
imageImportFunc: func(source image.ImportSource, ref string, options image.ImportOptions) (io.ReadCloser, error) {
|
2020-09-23 07:23:59 -04:00
|
|
|
assert.Check(t, is.Equal("ENV DEBUG=true", options.Changes[0]))
|
2022-02-25 07:10:34 -05:00
|
|
|
return io.NopCloser(strings.NewReader("")), nil
|
2020-09-23 07:23:59 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "change legacy syntax",
|
2017-03-30 20:21:14 -04:00
|
|
|
args: []string{"--change", "ENV DEBUG true", "-"},
|
2024-06-09 07:54:37 -04:00
|
|
|
imageImportFunc: func(source image.ImportSource, ref string, options image.ImportOptions) (io.ReadCloser, error) {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("ENV DEBUG true", options.Changes[0]))
|
2022-02-25 07:10:34 -05:00
|
|
|
return io.NopCloser(strings.NewReader("")), nil
|
2017-03-30 20:21:14 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2017-08-16 12:19:53 -04:00
|
|
|
cmd := NewImportCommand(test.NewFakeCli(&fakeClient{imageImportFunc: tc.imageImportFunc}))
|
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 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|
|
|
|
}
|