github.com/AliyunContainerService/cli@v0.0.0-20181009023821-814ced4b30d0/cli/command/image/save_test.go (about)

     1  package image
     2  
     3  import (
     4  	"io"
     5  	"io/ioutil"
     6  	"os"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/docker/cli/internal/test"
    11  	"github.com/pkg/errors"
    12  	"gotest.tools/assert"
    13  	is "gotest.tools/assert/cmp"
    14  )
    15  
    16  func TestNewSaveCommandErrors(t *testing.T) {
    17  	testCases := []struct {
    18  		name          string
    19  		args          []string
    20  		isTerminal    bool
    21  		expectedError string
    22  		imageSaveFunc func(images []string) (io.ReadCloser, error)
    23  	}{
    24  		{
    25  			name:          "wrong args",
    26  			args:          []string{},
    27  			expectedError: "requires at least 1 argument.",
    28  		},
    29  		{
    30  			name:          "output to terminal",
    31  			args:          []string{"output", "file", "arg1"},
    32  			isTerminal:    true,
    33  			expectedError: "cowardly refusing to save to a terminal. Use the -o flag or redirect",
    34  		},
    35  		{
    36  			name:          "ImageSave fail",
    37  			args:          []string{"arg1"},
    38  			isTerminal:    false,
    39  			expectedError: "error saving image",
    40  			imageSaveFunc: func(images []string) (io.ReadCloser, error) {
    41  				return ioutil.NopCloser(strings.NewReader("")), errors.Errorf("error saving image")
    42  			},
    43  		},
    44  		{
    45  			name:          "output directory does not exist",
    46  			args:          []string{"-o", "fakedir/out.tar", "arg1"},
    47  			expectedError: "failed to save image: unable to validate output path: directory \"fakedir\" does not exist",
    48  		},
    49  	}
    50  	for _, tc := range testCases {
    51  		cli := test.NewFakeCli(&fakeClient{imageSaveFunc: tc.imageSaveFunc})
    52  		cli.Out().SetIsTerminal(tc.isTerminal)
    53  		cmd := NewSaveCommand(cli)
    54  		cmd.SetOutput(ioutil.Discard)
    55  		cmd.SetArgs(tc.args)
    56  		assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
    57  	}
    58  }
    59  
    60  func TestNewSaveCommandSuccess(t *testing.T) {
    61  	testCases := []struct {
    62  		args          []string
    63  		isTerminal    bool
    64  		imageSaveFunc func(images []string) (io.ReadCloser, error)
    65  		deferredFunc  func()
    66  	}{
    67  		{
    68  			args:       []string{"-o", "save_tmp_file", "arg1"},
    69  			isTerminal: true,
    70  			imageSaveFunc: func(images []string) (io.ReadCloser, error) {
    71  				assert.Assert(t, is.Len(images, 1))
    72  				assert.Check(t, is.Equal("arg1", images[0]))
    73  				return ioutil.NopCloser(strings.NewReader("")), nil
    74  			},
    75  			deferredFunc: func() {
    76  				os.Remove("save_tmp_file")
    77  			},
    78  		},
    79  		{
    80  			args:       []string{"arg1", "arg2"},
    81  			isTerminal: false,
    82  			imageSaveFunc: func(images []string) (io.ReadCloser, error) {
    83  				assert.Assert(t, is.Len(images, 2))
    84  				assert.Check(t, is.Equal("arg1", images[0]))
    85  				assert.Check(t, is.Equal("arg2", images[1]))
    86  				return ioutil.NopCloser(strings.NewReader("")), nil
    87  			},
    88  		},
    89  	}
    90  	for _, tc := range testCases {
    91  		cmd := NewSaveCommand(test.NewFakeCli(&fakeClient{
    92  			imageSaveFunc: func(images []string) (io.ReadCloser, error) {
    93  				return ioutil.NopCloser(strings.NewReader("")), nil
    94  			},
    95  		}))
    96  		cmd.SetOutput(ioutil.Discard)
    97  		cmd.SetArgs(tc.args)
    98  		assert.NilError(t, cmd.Execute())
    99  		if tc.deferredFunc != nil {
   100  			tc.deferredFunc()
   101  		}
   102  	}
   103  }