github.1git.de/docker/cli@v26.1.3+incompatible/cli/command/container/logs_test.go (about) 1 package container 2 3 import ( 4 "context" 5 "io" 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, container.LogsOptions) (io.ReadCloser, error) { 17 return func(container string, opts container.LogsOptions) (io.ReadCloser, error) { 18 return io.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 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(context.TODO(), cli, testcase.options) 51 if testcase.expectedError != "" { 52 assert.ErrorContains(t, err, testcase.expectedError) 53 } else if !assert.Check(t, err) { 54 return 55 } 56 assert.Check(t, is.Equal(testcase.expectedOut, cli.OutBuffer().String())) 57 assert.Check(t, is.Equal(testcase.expectedErr, cli.ErrBuffer().String())) 58 }) 59 } 60 }