2017-11-13 04:53:13 -05:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/cli/internal/test"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/container"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2017-11-13 04:53:13 -05:00
|
|
|
)
|
|
|
|
|
2017-11-14 10:43:35 -05:00
|
|
|
var logFn = func(expectedOut string) func(string, types.ContainerLogsOptions) (io.ReadCloser, error) {
|
|
|
|
return func(container string, opts types.ContainerLogsOptions) (io.ReadCloser, error) {
|
2022-02-25 07:05:59 -05:00
|
|
|
return io.NopCloser(strings.NewReader(expectedOut)), nil
|
2017-11-14 10:43:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-13 04:53:13 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-09-29 11:21:51 -04:00
|
|
|
testcases := []struct {
|
2017-11-13 04:53:13 -05:00
|
|
|
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 != "" {
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, err, testcase.expectedError)
|
2017-11-13 04:53:13 -05:00
|
|
|
} else {
|
2018-03-05 18:53:52 -05:00
|
|
|
if !assert.Check(t, err) {
|
2017-11-13 04:53:13 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(testcase.expectedOut, cli.OutBuffer().String()))
|
|
|
|
assert.Check(t, is.Equal(testcase.expectedErr, cli.ErrBuffer().String()))
|
2017-11-13 04:53:13 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|