github.com/ali-iotechsys/cli@v20.10.0+incompatible/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/v3/assert" 13 is "gotest.tools/v3/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: invalid output path: directory \"fakedir\" does not exist", 48 }, 49 { 50 name: "output file is irregular", 51 args: []string{"-o", "/dev/null", "arg1"}, 52 expectedError: "failed to save image: invalid output path: \"/dev/null\" must be a directory or a regular file", 53 }, 54 } 55 for _, tc := range testCases { 56 cli := test.NewFakeCli(&fakeClient{imageSaveFunc: tc.imageSaveFunc}) 57 cli.Out().SetIsTerminal(tc.isTerminal) 58 cmd := NewSaveCommand(cli) 59 cmd.SetOut(ioutil.Discard) 60 cmd.SetArgs(tc.args) 61 assert.ErrorContains(t, cmd.Execute(), tc.expectedError) 62 } 63 } 64 65 func TestNewSaveCommandSuccess(t *testing.T) { 66 testCases := []struct { 67 args []string 68 isTerminal bool 69 imageSaveFunc func(images []string) (io.ReadCloser, error) 70 deferredFunc func() 71 }{ 72 { 73 args: []string{"-o", "save_tmp_file", "arg1"}, 74 isTerminal: true, 75 imageSaveFunc: func(images []string) (io.ReadCloser, error) { 76 assert.Assert(t, is.Len(images, 1)) 77 assert.Check(t, is.Equal("arg1", images[0])) 78 return ioutil.NopCloser(strings.NewReader("")), nil 79 }, 80 deferredFunc: func() { 81 os.Remove("save_tmp_file") 82 }, 83 }, 84 { 85 args: []string{"arg1", "arg2"}, 86 isTerminal: false, 87 imageSaveFunc: func(images []string) (io.ReadCloser, error) { 88 assert.Assert(t, is.Len(images, 2)) 89 assert.Check(t, is.Equal("arg1", images[0])) 90 assert.Check(t, is.Equal("arg2", images[1])) 91 return ioutil.NopCloser(strings.NewReader("")), nil 92 }, 93 }, 94 } 95 for _, tc := range testCases { 96 cmd := NewSaveCommand(test.NewFakeCli(&fakeClient{ 97 imageSaveFunc: func(images []string) (io.ReadCloser, error) { 98 return ioutil.NopCloser(strings.NewReader("")), nil 99 }, 100 })) 101 cmd.SetOut(ioutil.Discard) 102 cmd.SetArgs(tc.args) 103 assert.NilError(t, cmd.Execute()) 104 if tc.deferredFunc != nil { 105 tc.deferredFunc() 106 } 107 } 108 }