2018-04-05 12:42:31 -04:00
|
|
|
package logging
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2020-02-19 17:03:45 -05:00
|
|
|
"runtime"
|
2018-04-05 12:42:31 -04:00
|
|
|
"strings"
|
2020-02-19 17:03:45 -05:00
|
|
|
"testing"
|
|
|
|
"time"
|
2018-04-05 12:42:31 -04:00
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/pkg/stdcopy"
|
|
|
|
"github.com/docker/docker/testutil/daemon"
|
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestReadPluginNoRead tests that reads are supported even if the plugin isn't capable.
|
|
|
|
func TestReadPluginNoRead(t *testing.T) {
|
2020-02-19 17:03:45 -05:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("no unix domain sockets on Windows")
|
|
|
|
}
|
2018-04-05 12:42:31 -04:00
|
|
|
t.Parallel()
|
|
|
|
d := daemon.New(t)
|
|
|
|
d.StartWithBusybox(t, "--iptables=false")
|
|
|
|
defer d.Stop(t)
|
|
|
|
|
|
|
|
client, err := d.NewClient()
|
|
|
|
assert.Assert(t, err)
|
|
|
|
createPlugin(t, client, "test", "discard", asLogDriver)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
err = client.PluginEnable(ctx, "test", types.PluginEnableOptions{Timeout: 30})
|
|
|
|
assert.Check(t, err)
|
2018-04-09 15:35:24 -04:00
|
|
|
d.Stop(t)
|
2018-04-05 12:42:31 -04:00
|
|
|
|
2018-04-09 15:35:24 -04:00
|
|
|
cfg := &container.Config{
|
|
|
|
Image: "busybox",
|
|
|
|
Cmd: []string{"/bin/echo", "hello world"},
|
|
|
|
}
|
|
|
|
for desc, test := range map[string]struct {
|
|
|
|
dOpts []string
|
|
|
|
logsSupported bool
|
|
|
|
}{
|
|
|
|
"default": {logsSupported: true},
|
|
|
|
"disabled caching": {[]string{"--log-opt=cache-disabled=true"}, false},
|
|
|
|
"explicitly enabled caching": {[]string{"--log-opt=cache-disabled=false"}, true},
|
|
|
|
} {
|
|
|
|
t.Run(desc, func(t *testing.T) {
|
|
|
|
d.Start(t, append([]string{"--iptables=false"}, test.dOpts...)...)
|
|
|
|
defer d.Stop(t)
|
|
|
|
c, err := client.ContainerCreate(ctx,
|
|
|
|
cfg,
|
|
|
|
&container.HostConfig{LogConfig: container.LogConfig{Type: "test"}},
|
|
|
|
nil,
|
2020-03-19 16:54:48 -04:00
|
|
|
nil,
|
2018-04-09 15:35:24 -04:00
|
|
|
"",
|
|
|
|
)
|
|
|
|
assert.Assert(t, err)
|
|
|
|
defer client.ContainerRemove(ctx, c.ID, types.ContainerRemoveOptions{Force: true})
|
2018-04-05 12:42:31 -04:00
|
|
|
|
2018-04-09 15:35:24 -04:00
|
|
|
err = client.ContainerStart(ctx, c.ID, types.ContainerStartOptions{})
|
|
|
|
assert.Assert(t, err)
|
2018-04-05 12:42:31 -04:00
|
|
|
|
2018-04-09 15:35:24 -04:00
|
|
|
logs, err := client.ContainerLogs(ctx, c.ID, types.ContainerLogsOptions{ShowStdout: true})
|
|
|
|
if !test.logsSupported {
|
|
|
|
assert.Assert(t, err != nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.Assert(t, err)
|
|
|
|
defer logs.Close()
|
2018-04-05 12:42:31 -04:00
|
|
|
|
2018-04-09 15:35:24 -04:00
|
|
|
buf := bytes.NewBuffer(nil)
|
2018-04-05 12:42:31 -04:00
|
|
|
|
2018-04-09 15:35:24 -04:00
|
|
|
errCh := make(chan error)
|
|
|
|
go func() {
|
|
|
|
_, err := stdcopy.StdCopy(buf, buf, logs)
|
|
|
|
errCh <- err
|
|
|
|
}()
|
2018-04-05 12:42:31 -04:00
|
|
|
|
2018-04-09 15:35:24 -04:00
|
|
|
select {
|
|
|
|
case <-time.After(60 * time.Second):
|
|
|
|
t.Fatal("timeout waiting for IO to complete")
|
|
|
|
case err := <-errCh:
|
|
|
|
assert.Assert(t, err)
|
|
|
|
}
|
|
|
|
assert.Assert(t, strings.TrimSpace(buf.String()) == "hello world", buf.Bytes())
|
|
|
|
})
|
2018-04-05 12:42:31 -04:00
|
|
|
}
|
2018-04-09 15:35:24 -04:00
|
|
|
|
2018-04-05 12:42:31 -04:00
|
|
|
}
|