github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/cli/command/container/export_test.go (about)

     1  package container
     2  
     3  import (
     4  	"io"
     5  	"io/ioutil"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/docker/cli/internal/test"
    10  	"gotest.tools/v3/assert"
    11  	"gotest.tools/v3/fs"
    12  )
    13  
    14  func TestContainerExportOutputToFile(t *testing.T) {
    15  	dir := fs.NewDir(t, "export-test")
    16  	defer dir.Remove()
    17  
    18  	cli := test.NewFakeCli(&fakeClient{
    19  		containerExportFunc: func(container string) (io.ReadCloser, error) {
    20  			return ioutil.NopCloser(strings.NewReader("bar")), nil
    21  		},
    22  	})
    23  	cmd := NewExportCommand(cli)
    24  	cmd.SetOut(ioutil.Discard)
    25  	cmd.SetArgs([]string{"-o", dir.Join("foo"), "container"})
    26  	assert.NilError(t, cmd.Execute())
    27  
    28  	expected := fs.Expected(t,
    29  		fs.WithFile("foo", "bar", fs.MatchAnyFileMode),
    30  	)
    31  
    32  	assert.Assert(t, fs.Equal(dir.Path(), expected))
    33  }
    34  
    35  func TestContainerExportOutputToIrregularFile(t *testing.T) {
    36  	cli := test.NewFakeCli(&fakeClient{
    37  		containerExportFunc: func(container string) (io.ReadCloser, error) {
    38  			return ioutil.NopCloser(strings.NewReader("foo")), nil
    39  		},
    40  	})
    41  	cmd := NewExportCommand(cli)
    42  	cmd.SetOut(ioutil.Discard)
    43  	cmd.SetArgs([]string{"-o", "/dev/random", "container"})
    44  
    45  	err := cmd.Execute()
    46  	assert.Assert(t, err != nil)
    47  	expected := `"/dev/random" must be a directory or a regular file`
    48  	assert.ErrorContains(t, err, expected)
    49  }