2017-03-30 20:21:14 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"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/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 TestNewSaveCommandErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
isTerminal bool
|
|
|
|
expectedError string
|
|
|
|
imageSaveFunc func(images []string) (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: "output to terminal",
|
|
|
|
args: []string{"output", "file", "arg1"},
|
|
|
|
isTerminal: true,
|
2017-05-03 22:26:45 -04:00
|
|
|
expectedError: "cowardly refusing to save to a terminal. Use the -o flag or redirect",
|
2017-03-30 20:21:14 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ImageSave fail",
|
|
|
|
args: []string{"arg1"},
|
|
|
|
isTerminal: false,
|
|
|
|
expectedError: "error saving image",
|
|
|
|
imageSaveFunc: func(images []string) (io.ReadCloser, error) {
|
|
|
|
return ioutil.NopCloser(strings.NewReader("")), errors.Errorf("error saving image")
|
|
|
|
},
|
|
|
|
},
|
2017-07-26 15:11:02 -04:00
|
|
|
{
|
|
|
|
name: "output directory does not exist",
|
|
|
|
args: []string{"-o", "fakedir/out.tar", "arg1"},
|
2019-02-07 03:17:35 -05:00
|
|
|
expectedError: "failed to save image: invalid output path: directory \"fakedir\" does not exist",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "output file is irregular",
|
|
|
|
args: []string{"-o", "/dev/null", "arg1"},
|
|
|
|
expectedError: "failed to save image: invalid output path: \"/dev/null\" must be a directory or a regular file",
|
2017-07-26 15:11:02 -04:00
|
|
|
},
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2017-07-05 14:43:39 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{imageSaveFunc: tc.imageSaveFunc})
|
2017-03-30 20:21:14 -04:00
|
|
|
cli.Out().SetIsTerminal(tc.isTerminal)
|
|
|
|
cmd := NewSaveCommand(cli)
|
|
|
|
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 TestNewSaveCommandSuccess(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
args []string
|
|
|
|
isTerminal bool
|
|
|
|
imageSaveFunc func(images []string) (io.ReadCloser, error)
|
|
|
|
deferredFunc func()
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
args: []string{"-o", "save_tmp_file", "arg1"},
|
|
|
|
isTerminal: true,
|
|
|
|
imageSaveFunc: func(images []string) (io.ReadCloser, error) {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Assert(t, is.Len(images, 1))
|
|
|
|
assert.Check(t, is.Equal("arg1", images[0]))
|
2017-03-30 20:21:14 -04:00
|
|
|
return ioutil.NopCloser(strings.NewReader("")), nil
|
|
|
|
},
|
|
|
|
deferredFunc: func() {
|
|
|
|
os.Remove("save_tmp_file")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"arg1", "arg2"},
|
|
|
|
isTerminal: false,
|
|
|
|
imageSaveFunc: func(images []string) (io.ReadCloser, error) {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Assert(t, is.Len(images, 2))
|
|
|
|
assert.Check(t, is.Equal("arg1", images[0]))
|
|
|
|
assert.Check(t, is.Equal("arg2", images[1]))
|
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 := NewSaveCommand(test.NewFakeCli(&fakeClient{
|
2017-03-30 20:21:14 -04:00
|
|
|
imageSaveFunc: func(images []string) (io.ReadCloser, error) {
|
|
|
|
return ioutil.NopCloser(strings.NewReader("")), nil
|
|
|
|
},
|
2017-08-16 12:19:53 -04:00
|
|
|
}))
|
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
|
|
|
if tc.deferredFunc != nil {
|
|
|
|
tc.deferredFunc()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|