github.com/xeptore/docker-cli@v20.10.14+incompatible/cli/command/image/import_test.go (about) 1 package image 2 3 import ( 4 "io" 5 "io/ioutil" 6 "strings" 7 "testing" 8 9 "github.com/docker/cli/internal/test" 10 "github.com/docker/docker/api/types" 11 "github.com/pkg/errors" 12 "gotest.tools/v3/assert" 13 is "gotest.tools/v3/assert/cmp" 14 ) 15 16 func TestNewImportCommandErrors(t *testing.T) { 17 testCases := []struct { 18 name string 19 args []string 20 expectedError string 21 imageImportFunc func(source types.ImageImportSource, ref string, options types.ImageImportOptions) (io.ReadCloser, error) 22 }{ 23 { 24 name: "wrong-args", 25 args: []string{}, 26 expectedError: "requires at least 1 argument.", 27 }, 28 { 29 name: "import-failed", 30 args: []string{"testdata/import-command-success.input.txt"}, 31 expectedError: "something went wrong", 32 imageImportFunc: func(source types.ImageImportSource, ref string, options types.ImageImportOptions) (io.ReadCloser, error) { 33 return nil, errors.Errorf("something went wrong") 34 }, 35 }, 36 } 37 for _, tc := range testCases { 38 cmd := NewImportCommand(test.NewFakeCli(&fakeClient{imageImportFunc: tc.imageImportFunc})) 39 cmd.SetOut(ioutil.Discard) 40 cmd.SetArgs(tc.args) 41 assert.ErrorContains(t, cmd.Execute(), tc.expectedError) 42 } 43 } 44 45 func TestNewImportCommandInvalidFile(t *testing.T) { 46 cmd := NewImportCommand(test.NewFakeCli(&fakeClient{})) 47 cmd.SetOut(ioutil.Discard) 48 cmd.SetArgs([]string{"testdata/import-command-success.unexistent-file"}) 49 assert.ErrorContains(t, cmd.Execute(), "testdata/import-command-success.unexistent-file") 50 } 51 52 func TestNewImportCommandSuccess(t *testing.T) { 53 testCases := []struct { 54 name string 55 args []string 56 imageImportFunc func(source types.ImageImportSource, ref string, options types.ImageImportOptions) (io.ReadCloser, error) 57 }{ 58 { 59 name: "simple", 60 args: []string{"testdata/import-command-success.input.txt"}, 61 }, 62 { 63 name: "terminal-source", 64 args: []string{"-"}, 65 }, 66 { 67 name: "double", 68 args: []string{"-", "image:local"}, 69 imageImportFunc: func(source types.ImageImportSource, ref string, options types.ImageImportOptions) (io.ReadCloser, error) { 70 assert.Check(t, is.Equal("image:local", ref)) 71 return ioutil.NopCloser(strings.NewReader("")), nil 72 }, 73 }, 74 { 75 name: "message", 76 args: []string{"--message", "test message", "-"}, 77 imageImportFunc: func(source types.ImageImportSource, ref string, options types.ImageImportOptions) (io.ReadCloser, error) { 78 assert.Check(t, is.Equal("test message", options.Message)) 79 return ioutil.NopCloser(strings.NewReader("")), nil 80 }, 81 }, 82 { 83 name: "change", 84 args: []string{"--change", "ENV DEBUG=true", "-"}, 85 imageImportFunc: func(source types.ImageImportSource, ref string, options types.ImageImportOptions) (io.ReadCloser, error) { 86 assert.Check(t, is.Equal("ENV DEBUG=true", options.Changes[0])) 87 return ioutil.NopCloser(strings.NewReader("")), nil 88 }, 89 }, 90 { 91 name: "change legacy syntax", 92 args: []string{"--change", "ENV DEBUG true", "-"}, 93 imageImportFunc: func(source types.ImageImportSource, ref string, options types.ImageImportOptions) (io.ReadCloser, error) { 94 assert.Check(t, is.Equal("ENV DEBUG true", options.Changes[0])) 95 return ioutil.NopCloser(strings.NewReader("")), nil 96 }, 97 }, 98 } 99 for _, tc := range testCases { 100 cmd := NewImportCommand(test.NewFakeCli(&fakeClient{imageImportFunc: tc.imageImportFunc})) 101 cmd.SetOut(ioutil.Discard) 102 cmd.SetArgs(tc.args) 103 assert.NilError(t, cmd.Execute()) 104 } 105 }