2020-06-12 19:01:37 -04:00
|
|
|
package loggerutils // import "github.com/docker/docker/daemon/logger/loggerutils"
|
2018-04-05 12:39:28 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2020-06-12 19:01:37 -04:00
|
|
|
"bytes"
|
2018-04-05 12:39:28 -04:00
|
|
|
"context"
|
2020-06-12 19:01:37 -04:00
|
|
|
"fmt"
|
2018-04-05 12:39:28 -04:00
|
|
|
"io"
|
2018-08-27 16:53:23 -04:00
|
|
|
"os"
|
2020-05-14 17:25:20 -04:00
|
|
|
"path/filepath"
|
2018-04-05 12:39:28 -04:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2020-06-12 19:01:37 -04:00
|
|
|
"text/tabwriter"
|
2018-04-05 12:39:28 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/daemon/logger"
|
|
|
|
"github.com/docker/docker/pkg/tailfile"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2020-05-14 17:25:20 -04:00
|
|
|
"gotest.tools/v3/poll"
|
2018-04-05 12:39:28 -04:00
|
|
|
)
|
|
|
|
|
2020-04-08 15:24:31 -04:00
|
|
|
type testDecoder struct {
|
2021-12-24 01:03:39 -05:00
|
|
|
rdr io.Reader
|
|
|
|
scanner *bufio.Scanner
|
|
|
|
resetCount int
|
2020-04-08 15:24:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *testDecoder) Decode() (*logger.Message, error) {
|
|
|
|
if d.scanner == nil {
|
|
|
|
d.scanner = bufio.NewScanner(d.rdr)
|
|
|
|
}
|
|
|
|
if !d.scanner.Scan() {
|
|
|
|
return nil, d.scanner.Err()
|
|
|
|
}
|
|
|
|
// some comment
|
|
|
|
return &logger.Message{Line: d.scanner.Bytes(), Timestamp: time.Now()}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *testDecoder) Reset(rdr io.Reader) {
|
|
|
|
d.rdr = rdr
|
|
|
|
d.scanner = bufio.NewScanner(rdr)
|
2021-12-24 01:03:39 -05:00
|
|
|
d.resetCount++
|
2020-04-08 15:24:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *testDecoder) Close() {
|
|
|
|
d.rdr = nil
|
|
|
|
d.scanner = nil
|
|
|
|
}
|
|
|
|
|
2018-04-05 12:39:28 -04:00
|
|
|
func TestTailFiles(t *testing.T) {
|
|
|
|
s1 := strings.NewReader("Hello.\nMy name is Inigo Montoya.\n")
|
|
|
|
s2 := strings.NewReader("I'm serious.\nDon't call me Shirley!\n")
|
|
|
|
s3 := strings.NewReader("Roads?\nWhere we're going we don't need roads.\n")
|
|
|
|
|
|
|
|
files := []SizeReaderAt{s1, s2, s3}
|
|
|
|
watcher := logger.NewLogWatcher()
|
2020-07-14 20:13:26 -04:00
|
|
|
defer watcher.ConsumerGone()
|
2018-04-05 12:39:28 -04:00
|
|
|
|
|
|
|
tailReader := func(ctx context.Context, r SizeReaderAt, lines int) (io.Reader, int, error) {
|
|
|
|
return tailfile.NewTailReader(ctx, r, lines)
|
|
|
|
}
|
2020-04-08 15:24:31 -04:00
|
|
|
dec := &testDecoder{}
|
2018-04-05 12:39:28 -04:00
|
|
|
|
|
|
|
for desc, config := range map[string]logger.ReadConfig{} {
|
|
|
|
t.Run(desc, func(t *testing.T) {
|
|
|
|
started := make(chan struct{})
|
2022-02-10 14:47:24 -05:00
|
|
|
fwd := newForwarder(config)
|
2018-04-05 12:39:28 -04:00
|
|
|
go func() {
|
|
|
|
close(started)
|
2022-02-10 14:47:24 -05:00
|
|
|
tailFiles(files, watcher, dec, tailReader, config.Tail, fwd)
|
2018-04-05 12:39:28 -04:00
|
|
|
}()
|
|
|
|
<-started
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
config := logger.ReadConfig{Tail: 2}
|
2022-02-10 14:47:24 -05:00
|
|
|
fwd := newForwarder(config)
|
2018-04-05 12:39:28 -04:00
|
|
|
started := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
close(started)
|
2022-02-10 14:47:24 -05:00
|
|
|
tailFiles(files, watcher, dec, tailReader, config.Tail, fwd)
|
2018-04-05 12:39:28 -04:00
|
|
|
}()
|
|
|
|
<-started
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(60 * time.Second):
|
|
|
|
t.Fatal("timeout waiting for tail line")
|
|
|
|
case err := <-watcher.Err:
|
2019-01-21 07:16:02 -05:00
|
|
|
assert.NilError(t, err)
|
2018-04-05 12:39:28 -04:00
|
|
|
case msg := <-watcher.Msg:
|
|
|
|
assert.Assert(t, msg != nil)
|
|
|
|
assert.Assert(t, string(msg.Line) == "Roads?", string(msg.Line))
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(60 * time.Second):
|
|
|
|
t.Fatal("timeout waiting for tail line")
|
|
|
|
case err := <-watcher.Err:
|
2019-01-21 07:16:02 -05:00
|
|
|
assert.NilError(t, err)
|
2018-04-05 12:39:28 -04:00
|
|
|
case msg := <-watcher.Msg:
|
|
|
|
assert.Assert(t, msg != nil)
|
|
|
|
assert.Assert(t, string(msg.Line) == "Where we're going we don't need roads.", string(msg.Line))
|
|
|
|
}
|
|
|
|
}
|
2018-08-27 16:53:23 -04:00
|
|
|
|
2020-04-08 15:24:31 -04:00
|
|
|
type dummyDecoder struct{}
|
|
|
|
|
|
|
|
func (dummyDecoder) Decode() (*logger.Message, error) {
|
|
|
|
return &logger.Message{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dummyDecoder) Close() {}
|
|
|
|
func (dummyDecoder) Reset(io.Reader) {}
|
|
|
|
|
2020-05-06 12:35:13 -04:00
|
|
|
func TestCheckCapacityAndRotate(t *testing.T) {
|
2022-03-02 18:16:47 -05:00
|
|
|
dir := t.TempDir()
|
2020-05-06 12:35:13 -04:00
|
|
|
|
2022-03-02 18:16:47 -05:00
|
|
|
logPath := filepath.Join(dir, "log")
|
|
|
|
getTailReader := func(ctx context.Context, r SizeReaderAt, lines int) (io.Reader, int, error) {
|
|
|
|
return tailfile.NewTailReader(ctx, r, lines)
|
2020-05-06 12:35:13 -04:00
|
|
|
}
|
2022-03-02 18:16:47 -05:00
|
|
|
createDecoder := func(io.Reader) Decoder {
|
|
|
|
return dummyDecoder{}
|
|
|
|
}
|
|
|
|
l, err := NewLogFile(
|
|
|
|
logPath,
|
|
|
|
5, // capacity
|
|
|
|
3, // maxFiles
|
|
|
|
true, // compress
|
|
|
|
createDecoder,
|
|
|
|
0600, // perms
|
|
|
|
getTailReader,
|
|
|
|
)
|
|
|
|
assert.NilError(t, err)
|
2020-05-06 12:35:13 -04:00
|
|
|
defer l.Close()
|
|
|
|
|
2020-06-12 19:01:37 -04:00
|
|
|
ls := dirStringer{dir}
|
|
|
|
|
2022-05-27 09:07:07 -04:00
|
|
|
timestamp := time.Time{}
|
|
|
|
|
|
|
|
assert.NilError(t, l.WriteLogEntry(timestamp, []byte("hello world!")))
|
2022-03-02 18:16:47 -05:00
|
|
|
_, err = os.Stat(logPath + ".1")
|
2020-06-12 19:01:37 -04:00
|
|
|
assert.Assert(t, os.IsNotExist(err), ls)
|
2020-05-06 12:35:13 -04:00
|
|
|
|
2022-05-27 09:07:07 -04:00
|
|
|
assert.NilError(t, l.WriteLogEntry(timestamp, []byte("hello world!")))
|
2022-03-02 18:16:47 -05:00
|
|
|
poll.WaitOn(t, checkFileExists(logPath+".1.gz"), poll.WithDelay(time.Millisecond), poll.WithTimeout(30*time.Second))
|
2020-05-06 12:35:13 -04:00
|
|
|
|
2022-05-27 09:07:07 -04:00
|
|
|
assert.NilError(t, l.WriteLogEntry(timestamp, []byte("hello world!")))
|
2022-03-02 18:16:47 -05:00
|
|
|
poll.WaitOn(t, checkFileExists(logPath+".1.gz"), poll.WithDelay(time.Millisecond), poll.WithTimeout(30*time.Second))
|
|
|
|
poll.WaitOn(t, checkFileExists(logPath+".2.gz"), poll.WithDelay(time.Millisecond), poll.WithTimeout(30*time.Second))
|
2020-05-06 12:35:13 -04:00
|
|
|
|
2020-06-12 19:01:37 -04:00
|
|
|
t.Run("closed log file", func(t *testing.T) {
|
|
|
|
// Now let's simulate a failed rotation where the file was able to be closed but something else happened elsewhere
|
|
|
|
// down the line.
|
|
|
|
// We want to make sure that we can recover in the case that `l.f` was closed while attempting a rotation.
|
|
|
|
l.f.Close()
|
2022-05-27 09:07:07 -04:00
|
|
|
assert.NilError(t, l.WriteLogEntry(timestamp, []byte("hello world!")))
|
2022-03-02 18:16:47 -05:00
|
|
|
assert.NilError(t, os.Remove(logPath+".2.gz"))
|
2020-06-12 19:01:37 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("with log reader", func(t *testing.T) {
|
|
|
|
// Make sure rotate works with an active reader
|
2022-03-02 18:16:47 -05:00
|
|
|
lw := l.ReadLogs(logger.ReadConfig{Follow: true, Tail: 1000})
|
2020-06-12 19:01:37 -04:00
|
|
|
defer lw.ConsumerGone()
|
|
|
|
|
2022-05-27 09:07:07 -04:00
|
|
|
assert.NilError(t, l.WriteLogEntry(timestamp, []byte("hello world 0!")), ls)
|
2020-06-12 19:01:37 -04:00
|
|
|
// make sure the log reader is primed
|
|
|
|
waitForMsg(t, lw, 30*time.Second)
|
|
|
|
|
2022-05-27 09:07:07 -04:00
|
|
|
assert.NilError(t, l.WriteLogEntry(timestamp, []byte("hello world 1!")), ls)
|
|
|
|
assert.NilError(t, l.WriteLogEntry(timestamp, []byte("hello world 2!")), ls)
|
|
|
|
assert.NilError(t, l.WriteLogEntry(timestamp, []byte("hello world 3!")), ls)
|
|
|
|
assert.NilError(t, l.WriteLogEntry(timestamp, []byte("hello world 4!")), ls)
|
2022-03-02 18:16:47 -05:00
|
|
|
poll.WaitOn(t, checkFileExists(logPath+".2.gz"), poll.WithDelay(time.Millisecond), poll.WithTimeout(30*time.Second))
|
2020-06-12 19:01:37 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func waitForMsg(t *testing.T, lw *logger.LogWatcher, timeout time.Duration) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
timer := time.NewTimer(timeout)
|
|
|
|
defer timer.Stop()
|
|
|
|
|
|
|
|
select {
|
2022-03-02 18:38:16 -05:00
|
|
|
case _, ok := <-lw.Msg:
|
|
|
|
assert.Assert(t, ok, "log producer gone before log message arrived")
|
2020-06-12 19:01:37 -04:00
|
|
|
case err := <-lw.Err:
|
|
|
|
assert.NilError(t, err)
|
|
|
|
case <-timer.C:
|
|
|
|
t.Fatal("timeout waiting for log message")
|
|
|
|
}
|
2020-05-06 12:35:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type dirStringer struct {
|
|
|
|
d string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d dirStringer) String() string {
|
2021-08-24 06:10:50 -04:00
|
|
|
ls, err := os.ReadDir(d.d)
|
2020-05-06 12:35:13 -04:00
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
2020-06-12 19:01:37 -04:00
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
tw := tabwriter.NewWriter(buf, 1, 8, 1, '\t', 0)
|
|
|
|
buf.WriteString("\n")
|
|
|
|
|
|
|
|
btw := bufio.NewWriter(tw)
|
2020-05-06 12:35:13 -04:00
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
for _, entry := range ls {
|
|
|
|
fi, err := entry.Info()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-06-12 19:01:37 -04:00
|
|
|
btw.WriteString(fmt.Sprintf("%s\t%s\t%dB\t%s\n", fi.Name(), fi.Mode(), fi.Size(), fi.ModTime()))
|
2020-05-06 12:35:13 -04:00
|
|
|
}
|
2020-06-12 19:01:37 -04:00
|
|
|
btw.Flush()
|
|
|
|
tw.Flush()
|
|
|
|
return buf.String()
|
2020-05-06 12:35:13 -04:00
|
|
|
}
|
2020-05-14 17:25:20 -04:00
|
|
|
|
|
|
|
func checkFileExists(name string) poll.Check {
|
|
|
|
return func(t poll.LogT) poll.Result {
|
|
|
|
_, err := os.Stat(name)
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
return poll.Success()
|
|
|
|
case os.IsNotExist(err):
|
|
|
|
return poll.Continue("waiting for %s to exist", name)
|
|
|
|
default:
|
2020-07-14 20:13:26 -04:00
|
|
|
t.Logf("waiting for %s: %v: %s", name, err, dirStringer{filepath.Dir(name)})
|
2020-05-14 17:25:20 -04:00
|
|
|
return poll.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|