Files
docker-cli/components/engine/integration/plugin/logging/logging_test.go
Brian Goff 9488201604 Fix logging plugin crash unrecoverable
In cases where a logging plugin has crashed when the daemon tries to
copy the container stdio to the logging plugin it returns a broken pipe
error and any log entries that occurr while the plugin is down are lost.

Fix this by opening read+write in the daemon so logs are not lost while
the plugin is down.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
Upstream-commit: e7479e3ab8128f9e84cc640f0bed4e77b268a6e9
Component: engine
2018-05-14 16:51:56 -04:00

83 lines
2.4 KiB
Go

package logging
import (
"bufio"
"context"
"os"
"strings"
"testing"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/volume"
"github.com/docker/docker/integration/internal/container"
"github.com/docker/docker/internal/test/daemon"
"github.com/gotestyourself/gotestyourself/assert"
)
func TestContinueAfterPluginCrash(t *testing.T) {
t.Parallel()
d := daemon.New(t)
d.StartWithBusybox(t, "--iptables=false", "--init")
defer d.Stop(t)
client := d.NewClientT(t)
createPlugin(t, client, "test", "close_on_start", asLogDriver)
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
assert.Assert(t, client.PluginEnable(ctx, "test", types.PluginEnableOptions{Timeout: 30}))
cancel()
defer client.PluginRemove(context.Background(), "test", types.PluginRemoveOptions{Force: true})
v, err := client.VolumeCreate(context.Background(), volume.VolumesCreateBody{})
assert.Assert(t, err)
defer client.VolumeRemove(context.Background(), v.Name, true)
ctx, cancel = context.WithTimeout(context.Background(), 60*time.Second)
id := container.Run(t, ctx, client,
container.WithAutoRemove,
container.WithLogDriver("test"),
container.WithCmd(
"/bin/sh", "-c", "while true; do sleep 1; echo hello; done",
),
)
cancel()
defer client.ContainerRemove(context.Background(), id, types.ContainerRemoveOptions{Force: true})
// Attach to the container to make sure it's written a few times to stdout
attach, err := client.ContainerAttach(context.Background(), id, types.ContainerAttachOptions{Stream: true, Stdout: true})
assert.Assert(t, err)
chErr := make(chan error)
go func() {
defer close(chErr)
rdr := bufio.NewReader(attach.Reader)
for i := 0; i < 5; i++ {
_, _, err := rdr.ReadLine()
if err != nil {
chErr <- err
return
}
}
}()
select {
case err := <-chErr:
assert.Assert(t, err)
case <-time.After(60 * time.Second):
t.Fatal("timeout waiting for container i/o")
}
// check daemon logs for "broken pipe"
// TODO(@cpuguy83): This is horribly hacky but is the only way to really test this case right now.
// It would be nice if there was a way to know that a broken pipe has occurred without looking through the logs.
log, err := os.Open(d.LogFileName())
assert.Assert(t, err)
scanner := bufio.NewScanner(log)
for scanner.Scan() {
assert.Assert(t, !strings.Contains(scanner.Text(), "broken pipe"))
}
}