2018-09-18 12:29:56 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/cli/internal/test"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/fs"
|
2018-09-18 12:29:56 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestContainerExportOutputToFile(t *testing.T) {
|
|
|
|
dir := fs.NewDir(t, "export-test")
|
|
|
|
defer dir.Remove()
|
|
|
|
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
containerExportFunc: func(container string) (io.ReadCloser, error) {
|
2022-02-25 07:05:59 -05:00
|
|
|
return io.NopCloser(strings.NewReader("bar")), nil
|
2018-09-18 12:29:56 -04:00
|
|
|
},
|
|
|
|
})
|
|
|
|
cmd := NewExportCommand(cli)
|
2022-02-25 07:05:59 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2018-09-18 12:29:56 -04:00
|
|
|
cmd.SetArgs([]string{"-o", dir.Join("foo"), "container"})
|
|
|
|
assert.NilError(t, cmd.Execute())
|
|
|
|
|
|
|
|
expected := fs.Expected(t,
|
|
|
|
fs.WithFile("foo", "bar", fs.MatchAnyFileMode),
|
|
|
|
)
|
|
|
|
|
|
|
|
assert.Assert(t, fs.Equal(dir.Path(), expected))
|
|
|
|
}
|
2019-02-07 03:17:35 -05:00
|
|
|
|
|
|
|
func TestContainerExportOutputToIrregularFile(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
containerExportFunc: func(container string) (io.ReadCloser, error) {
|
2022-02-25 07:05:59 -05:00
|
|
|
return io.NopCloser(strings.NewReader("foo")), nil
|
2019-02-07 03:17:35 -05:00
|
|
|
},
|
|
|
|
})
|
|
|
|
cmd := NewExportCommand(cli)
|
2022-02-25 07:05:59 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2019-02-07 03:17:35 -05:00
|
|
|
cmd.SetArgs([]string{"-o", "/dev/random", "container"})
|
|
|
|
|
|
|
|
err := cmd.Execute()
|
|
|
|
assert.Assert(t, err != nil)
|
|
|
|
expected := `"/dev/random" must be a directory or a regular file`
|
|
|
|
assert.ErrorContains(t, err, expected)
|
|
|
|
}
|