File: logging_linux_test.go

package info (click to toggle)
docker.io 27.5.1%2Bdfsg4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 67,384 kB
  • sloc: sh: 5,847; makefile: 1,146; ansic: 664; python: 162; asm: 133
file content (85 lines) | stat: -rw-r--r-- 2,422 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package logging

import (
	"bufio"
	"context"
	"os"
	"strings"
	"testing"
	"time"

	"github.com/docker/docker/api/types"
	containertypes "github.com/docker/docker/api/types/container"
	"github.com/docker/docker/integration/internal/container"
	"github.com/docker/docker/testutil"
	"github.com/docker/docker/testutil/daemon"
	"gotest.tools/v3/assert"
	"gotest.tools/v3/skip"
)

func TestContinueAfterPluginCrash(t *testing.T) {
	skip.If(t, testEnv.IsRemoteDaemon, "test requires daemon on the same host")
	t.Parallel()

	ctx := testutil.StartSpan(baseContext, t)

	d := daemon.New(t)
	d.StartWithBusybox(ctx, t, "--iptables=false", "--ip6tables=false", "--init")
	defer d.Stop(t)

	client := d.NewClientT(t)
	createPlugin(ctx, t, client, "test", "close_on_start", asLogDriver)

	ctxT, cancel := context.WithTimeout(ctx, 60*time.Second)
	defer cancel()
	assert.Assert(t, client.PluginEnable(ctxT, "test", types.PluginEnableOptions{Timeout: 30}))
	cancel()
	defer client.PluginRemove(ctx, "test", types.PluginRemoveOptions{Force: true})

	ctxT, cancel = context.WithTimeout(ctx, 60*time.Second)
	defer cancel()

	id := container.Run(ctxT, t, client,
		container.WithAutoRemove,
		container.WithLogDriver("test"),
		container.WithCmd(
			"/bin/sh", "-c", "while true; do sleep 1; echo hello; done",
		),
	)
	cancel()
	defer client.ContainerRemove(ctx, id, containertypes.RemoveOptions{Force: true})

	// Attach to the container to make sure it's written a few times to stdout
	attach, err := client.ContainerAttach(ctx, id, containertypes.AttachOptions{Stream: true, Stdout: true})
	assert.NilError(t, err)

	chErr := make(chan error, 1)
	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.NilError(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.NilError(t, err)
	scanner := bufio.NewScanner(log)
	for scanner.Scan() {
		assert.Assert(t, !strings.Contains(scanner.Text(), "broken pipe"))
	}
}