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