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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
package logger // import "github.com/docker/docker/daemon/logger"
import (
"encoding/binary"
"io"
"sync"
"testing"
"time"
"github.com/docker/docker/api/types/plugins/logdriver"
protoio "github.com/gogo/protobuf/io"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
// mockLoggingPlugin implements the loggingPlugin interface for testing purposes
// it only supports a single log stream
type mockLoggingPlugin struct {
io.WriteCloser
inStream io.Reader
logs []*logdriver.LogEntry
c *sync.Cond
err error
}
func newMockLoggingPlugin() *mockLoggingPlugin {
r, w := io.Pipe()
return &mockLoggingPlugin{
WriteCloser: w,
inStream: r,
logs: []*logdriver.LogEntry{},
c: sync.NewCond(new(sync.Mutex)),
}
}
func (l *mockLoggingPlugin) StartLogging(file string, info Info) error {
go func() {
dec := protoio.NewUint32DelimitedReader(l.inStream, binary.BigEndian, 1e6)
for {
var msg logdriver.LogEntry
if err := dec.ReadMsg(&msg); err != nil {
l.c.L.Lock()
if l.err == nil {
l.err = err
}
l.c.L.Unlock()
l.c.Broadcast()
return
}
l.c.L.Lock()
l.logs = append(l.logs, &msg)
l.c.L.Unlock()
l.c.Broadcast()
}
}()
return nil
}
func (l *mockLoggingPlugin) StopLogging(file string) error {
l.c.L.Lock()
if l.err == nil {
l.err = io.EOF
}
l.c.L.Unlock()
l.c.Broadcast()
return nil
}
func (l *mockLoggingPlugin) Capabilities() (cap Capability, err error) {
return Capability{ReadLogs: true}, nil
}
func (l *mockLoggingPlugin) ReadLogs(info Info, config ReadConfig) (io.ReadCloser, error) {
r, w := io.Pipe()
go func() {
var idx int
enc := logdriver.NewLogEntryEncoder(w)
l.c.L.Lock()
defer l.c.L.Unlock()
for {
if l.err != nil {
w.Close()
return
}
if idx >= len(l.logs) {
if !config.Follow {
w.Close()
return
}
l.c.Wait()
continue
}
if err := enc.Encode(l.logs[idx]); err != nil {
w.CloseWithError(err)
return
}
idx++
}
}()
return r, nil
}
func (l *mockLoggingPlugin) waitLen(i int) {
l.c.L.Lock()
defer l.c.L.Unlock()
for len(l.logs) < i {
l.c.Wait()
}
}
func (l *mockLoggingPlugin) check(t *testing.T) {
if l.err != nil && l.err != io.EOF {
t.Fatal(l.err)
}
}
func newMockPluginAdapter(plugin *mockLoggingPlugin) Logger {
enc := logdriver.NewLogEntryEncoder(plugin)
a := &pluginAdapterWithRead{
&pluginAdapter{
plugin: plugin,
stream: plugin,
enc: enc,
},
}
a.plugin.StartLogging("", Info{})
return a
}
func TestAdapterReadLogs(t *testing.T) {
plugin := newMockLoggingPlugin()
l := newMockPluginAdapter(plugin)
testMsg := []Message{
{Line: []byte("Are you the keymaker?"), Timestamp: time.Now()},
{Line: []byte("Follow the white rabbit"), Timestamp: time.Now()},
}
for _, msg := range testMsg {
m := msg.copy()
assert.Check(t, l.Log(m))
}
// Wait until messages are read into plugin
plugin.waitLen(len(testMsg))
lr, ok := l.(LogReader)
assert.Check(t, ok, "Logger does not implement LogReader")
lw := lr.ReadLogs(ReadConfig{})
for _, x := range testMsg {
select {
case msg := <-lw.Msg:
testMessageEqual(t, &x, msg)
case <-time.After(10 * time.Second):
t.Fatal("timeout reading logs")
}
}
select {
case _, ok := <-lw.Msg:
assert.Check(t, !ok, "expected message channel to be closed")
case <-time.After(10 * time.Second):
t.Fatal("timeout waiting for message channel to close")
}
lw.ProducerGone()
lw = lr.ReadLogs(ReadConfig{Follow: true})
for _, x := range testMsg {
select {
case msg := <-lw.Msg:
testMessageEqual(t, &x, msg)
case <-time.After(10 * time.Second):
t.Fatal("timeout reading logs")
}
}
x := Message{Line: []byte("Too infinity and beyond!"), Timestamp: time.Now()}
assert.Check(t, l.Log(x.copy()))
select {
case msg, ok := <-lw.Msg:
assert.Check(t, ok, "message channel unexpectedly closed")
testMessageEqual(t, &x, msg)
case <-time.After(10 * time.Second):
t.Fatal("timeout reading logs")
}
l.Close()
select {
case msg, ok := <-lw.Msg:
assert.Check(t, !ok, "expected message channel to be closed")
assert.Check(t, is.Nil(msg))
case <-time.After(10 * time.Second):
t.Fatal("timeout waiting for logger to close")
}
plugin.check(t)
}
func testMessageEqual(t *testing.T, a, b *Message) {
assert.Check(t, is.DeepEqual(a.Line, b.Line))
assert.Check(t, is.DeepEqual(a.Timestamp.UnixNano(), b.Timestamp.UnixNano()))
assert.Check(t, is.Equal(a.Source, b.Source))
}
|