github.com/xeptore/docker-cli@v20.10.14+incompatible/cli/command/image/history_test.go (about) 1 package image 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "testing" 7 "time" 8 9 "github.com/docker/cli/internal/test" 10 "github.com/docker/docker/api/types/image" 11 "github.com/pkg/errors" 12 "gotest.tools/v3/assert" 13 "gotest.tools/v3/golden" 14 "gotest.tools/v3/skip" 15 ) 16 17 func TestNewHistoryCommandErrors(t *testing.T) { 18 testCases := []struct { 19 name string 20 args []string 21 expectedError string 22 imageHistoryFunc func(img string) ([]image.HistoryResponseItem, error) 23 }{ 24 { 25 name: "wrong-args", 26 args: []string{}, 27 expectedError: "requires exactly 1 argument.", 28 }, 29 { 30 name: "client-error", 31 args: []string{"image:tag"}, 32 expectedError: "something went wrong", 33 imageHistoryFunc: func(img string) ([]image.HistoryResponseItem, error) { 34 return []image.HistoryResponseItem{{}}, errors.Errorf("something went wrong") 35 }, 36 }, 37 } 38 for _, tc := range testCases { 39 cmd := NewHistoryCommand(test.NewFakeCli(&fakeClient{imageHistoryFunc: tc.imageHistoryFunc})) 40 cmd.SetOut(ioutil.Discard) 41 cmd.SetArgs(tc.args) 42 assert.ErrorContains(t, cmd.Execute(), tc.expectedError) 43 } 44 } 45 46 func notUTCTimezone() bool { 47 now := time.Now() 48 return now != now.UTC() 49 } 50 51 func TestNewHistoryCommandSuccess(t *testing.T) { 52 skip.If(t, notUTCTimezone, "expected output requires UTC timezone") 53 testCases := []struct { 54 name string 55 args []string 56 imageHistoryFunc func(img string) ([]image.HistoryResponseItem, error) 57 }{ 58 { 59 name: "simple", 60 args: []string{"image:tag"}, 61 imageHistoryFunc: func(img string) ([]image.HistoryResponseItem, error) { 62 return []image.HistoryResponseItem{{ 63 ID: "1234567890123456789", 64 Created: time.Now().Unix(), 65 }}, nil 66 }, 67 }, 68 { 69 name: "quiet", 70 args: []string{"--quiet", "image:tag"}, 71 }, 72 { 73 name: "non-human", 74 args: []string{"--human=false", "image:tag"}, 75 imageHistoryFunc: func(img string) ([]image.HistoryResponseItem, error) { 76 return []image.HistoryResponseItem{{ 77 ID: "abcdef", 78 Created: time.Date(2017, 1, 1, 12, 0, 3, 0, time.UTC).Unix(), 79 CreatedBy: "rose", 80 Comment: "new history item!", 81 }}, nil 82 }, 83 }, 84 { 85 name: "quiet-no-trunc", 86 args: []string{"--quiet", "--no-trunc", "image:tag"}, 87 imageHistoryFunc: func(img string) ([]image.HistoryResponseItem, error) { 88 return []image.HistoryResponseItem{{ 89 ID: "1234567890123456789", 90 Created: time.Now().Unix(), 91 }}, nil 92 }, 93 }, 94 } 95 for _, tc := range testCases { 96 cli := test.NewFakeCli(&fakeClient{imageHistoryFunc: tc.imageHistoryFunc}) 97 cmd := NewHistoryCommand(cli) 98 cmd.SetOut(ioutil.Discard) 99 cmd.SetArgs(tc.args) 100 err := cmd.Execute() 101 assert.NilError(t, err) 102 actual := cli.OutBuffer().String() 103 golden.Assert(t, actual, fmt.Sprintf("history-command-success.%s.golden", tc.name)) 104 } 105 }