github.com/cspotcode/docker-cli@v20.10.0-rc1.0.20201201121459-3faad7acc5b8+incompatible/cli/command/container/logs_test.go (about) 1 package container 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/docker/docker/api/types/container" 12 "gotest.tools/v3/assert" 13 is "gotest.tools/v3/assert/cmp" 14 ) 15 16 var logFn = func(expectedOut string) func(string, types.ContainerLogsOptions) (io.ReadCloser, error) { 17 return func(container string, opts types.ContainerLogsOptions) (io.ReadCloser, error) { 18 return ioutil.NopCloser(strings.NewReader(expectedOut)), nil 19 } 20 } 21 22 func TestRunLogs(t *testing.T) { 23 inspectFn := func(containerID string) (types.ContainerJSON, error) { 24 return types.ContainerJSON{ 25 Config: &container.Config{Tty: true}, 26 ContainerJSONBase: &types.ContainerJSONBase{State: &types.ContainerState{Running: false}}, 27 }, nil 28 } 29 30 var testcases = []struct { 31 doc string 32 options *logsOptions 33 client fakeClient 34 expectedError string 35 expectedOut string 36 expectedErr string 37 }{ 38 { 39 doc: "successful logs", 40 expectedOut: "foo", 41 options: &logsOptions{}, 42 client: fakeClient{logFunc: logFn("foo"), inspectFunc: inspectFn}, 43 }, 44 } 45 46 for _, testcase := range testcases { 47 t.Run(testcase.doc, func(t *testing.T) { 48 cli := test.NewFakeCli(&testcase.client) 49 50 err := runLogs(cli, testcase.options) 51 if testcase.expectedError != "" { 52 assert.ErrorContains(t, err, testcase.expectedError) 53 } else { 54 if !assert.Check(t, err) { 55 return 56 } 57 } 58 assert.Check(t, is.Equal(testcase.expectedOut, cli.OutBuffer().String())) 59 assert.Check(t, is.Equal(testcase.expectedErr, cli.ErrBuffer().String())) 60 }) 61 } 62 }