From 9c9303e11350cc58078c1fcc59cd1ef1b4b0ed88 Mon Sep 17 00:00:00 2001 From: Jamie Hannaford Date: Mon, 13 Nov 2017 10:53:13 +0100 Subject: [PATCH] add test for logs Signed-off-by: Jamie Hannaford --- cli/command/container/client_test.go | 8 ++++ cli/command/container/logs_test.go | 62 ++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 cli/command/container/logs_test.go diff --git a/cli/command/container/client_test.go b/cli/command/container/client_test.go index 9a17875c2d..692ed3115a 100644 --- a/cli/command/container/client_test.go +++ b/cli/command/container/client_test.go @@ -20,6 +20,7 @@ type fakeClient struct { infoFunc func() (types.Info, error) containerStatPathFunc func(container, path string) (types.ContainerPathStat, error) containerCopyFromFunc func(container, srcPath string) (io.ReadCloser, types.ContainerPathStat, error) + logFunc func(string, types.ContainerLogsOptions) (io.ReadCloser, error) } func (f *fakeClient) ContainerInspect(_ context.Context, containerID string) (types.ContainerJSON, error) { @@ -87,3 +88,10 @@ func (f *fakeClient) CopyFromContainer(_ context.Context, container, srcPath str } return nil, types.ContainerPathStat{}, nil } + +func (f *fakeClient) ContainerLogs(_ context.Context, container string, options types.ContainerLogsOptions) (io.ReadCloser, error) { + if f.logFunc != nil { + return f.logFunc(container, options) + } + return nil, nil +} diff --git a/cli/command/container/logs_test.go b/cli/command/container/logs_test.go new file mode 100644 index 0000000000..592989da4d --- /dev/null +++ b/cli/command/container/logs_test.go @@ -0,0 +1,62 @@ +package container + +import ( + "io" + "io/ioutil" + "strings" + "testing" + + "github.com/docker/cli/internal/test" + "github.com/docker/cli/internal/test/testutil" + "github.com/docker/docker/api/types" + "github.com/docker/docker/api/types/container" + "github.com/stretchr/testify/assert" +) + +func TestRunLogs(t *testing.T) { + inspectFn := func(containerID string) (types.ContainerJSON, error) { + return types.ContainerJSON{ + Config: &container.Config{Tty: true}, + ContainerJSONBase: &types.ContainerJSONBase{State: &types.ContainerState{Running: false}}, + }, nil + } + + logFn := func(expectedOut string) func(string, types.ContainerLogsOptions) (io.ReadCloser, error) { + return func(container string, opts types.ContainerLogsOptions) (io.ReadCloser, error) { + return ioutil.NopCloser(strings.NewReader(expectedOut)), nil + } + } + + var testcases = []struct { + doc string + options *logsOptions + client fakeClient + expectedError string + expectedOut string + expectedErr string + }{ + { + doc: "successful logs", + expectedOut: "foo", + options: &logsOptions{}, + client: fakeClient{logFunc: logFn("foo"), inspectFunc: inspectFn}, + }, + } + + for _, testcase := range testcases { + t.Run(testcase.doc, func(t *testing.T) { + cli := test.NewFakeCli(&testcase.client) + + err := runLogs(cli, testcase.options) + if testcase.expectedError != "" { + testutil.ErrorContains(t, err, testcase.expectedError) + } else { + if !assert.NoError(t, err) { + return + } + } + assert.Equal(t, testcase.expectedOut, cli.OutBuffer().String()) + assert.Equal(t, testcase.expectedErr, cli.ErrBuffer().String()) + }) + } +}