2013-01-08 04:54:49 +08:00
|
|
|
// Copyright (c) 2013 ActiveState Software Inc. All rights reserved.
|
|
|
|
|
2012-10-14 03:50:27 +08:00
|
|
|
// TODO:
|
|
|
|
// * repeat all the tests with Poll:true
|
|
|
|
|
2012-10-13 05:32:04 +08:00
|
|
|
package tail
|
|
|
|
|
|
|
|
import (
|
2012-10-14 02:44:47 +08:00
|
|
|
_ "fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2014-02-19 07:46:36 +08:00
|
|
|
"strings"
|
2012-10-13 05:32:04 +08:00
|
|
|
"testing"
|
2012-10-14 02:20:04 +08:00
|
|
|
"time"
|
2015-07-03 22:51:41 +08:00
|
|
|
|
2015-09-30 22:48:17 +08:00
|
|
|
"github.com/hpcloud/tail/ratelimiter"
|
2016-04-08 07:41:16 +08:00
|
|
|
"github.com/hpcloud/tail/watch"
|
2012-10-13 05:32:04 +08:00
|
|
|
)
|
|
|
|
|
2012-10-14 02:20:04 +08:00
|
|
|
func init() {
|
2013-05-29 03:55:11 +08:00
|
|
|
// Clear the temporary test directory
|
2012-10-14 02:20:04 +08:00
|
|
|
err := os.RemoveAll(".test")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-09 02:47:25 +08:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
// Use a smaller poll duration for faster test runs. Keep it below
|
|
|
|
// 100ms (which value is used as common delays for tests)
|
|
|
|
watch.POLL_DURATION = 5 * time.Millisecond
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
2012-10-14 03:11:50 +08:00
|
|
|
func TestMustExist(t *testing.T) {
|
2012-10-14 02:20:04 +08:00
|
|
|
tail, err := TailFile("/no/such/file", Config{Follow: true, MustExist: true})
|
2012-10-13 05:32:04 +08:00
|
|
|
if err == nil {
|
|
|
|
t.Error("MustExist:true is violated")
|
2012-10-14 02:20:04 +08:00
|
|
|
tail.Stop()
|
2012-10-13 05:32:04 +08:00
|
|
|
}
|
2012-10-14 02:20:04 +08:00
|
|
|
tail, err = TailFile("/no/such/file", Config{Follow: true, MustExist: false})
|
2012-10-13 05:32:04 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Error("MustExist:false is violated")
|
|
|
|
}
|
2012-10-14 02:20:04 +08:00
|
|
|
tail.Stop()
|
2012-10-13 07:28:22 +08:00
|
|
|
_, err = TailFile("README.md", Config{Follow: true, MustExist: true})
|
|
|
|
if err != nil {
|
|
|
|
t.Error("MustExist:true on an existing file is violated")
|
|
|
|
}
|
2015-07-03 22:53:42 +08:00
|
|
|
tail.Cleanup()
|
2012-10-14 02:20:04 +08:00
|
|
|
}
|
|
|
|
|
2016-04-27 02:30:20 +08:00
|
|
|
func TestWaitsForFileToExist(t *testing.T) {
|
|
|
|
tailTest := NewTailTest("waits-for-file-to-exist", t)
|
|
|
|
tail := tailTest.StartTail("test.txt", Config{})
|
2016-04-27 06:21:54 +08:00
|
|
|
go tailTest.VerifyTailOutput(tail, []string{"hello", "world"}, false)
|
2015-09-29 16:01:06 +08:00
|
|
|
|
|
|
|
<-time.After(100 * time.Millisecond)
|
2016-04-27 02:30:20 +08:00
|
|
|
tailTest.CreateFile("test.txt", "hello\nworld\n")
|
|
|
|
tailTest.Cleanup(tail, true)
|
2015-09-29 16:01:06 +08:00
|
|
|
}
|
|
|
|
|
2016-04-27 02:30:20 +08:00
|
|
|
func TestWaitsForFileToExistRelativePath(t *testing.T) {
|
|
|
|
tailTest := NewTailTest("waits-for-file-to-exist-relative", t)
|
2015-09-29 16:01:06 +08:00
|
|
|
|
|
|
|
oldWD, err := os.Getwd()
|
|
|
|
if err != nil {
|
2016-04-27 02:30:20 +08:00
|
|
|
tailTest.Fatal(err)
|
2015-09-29 16:01:06 +08:00
|
|
|
}
|
2016-04-27 02:30:20 +08:00
|
|
|
os.Chdir(tailTest.path)
|
2015-09-29 16:01:06 +08:00
|
|
|
defer os.Chdir(oldWD)
|
|
|
|
|
|
|
|
tail, err := TailFile("test.txt", Config{})
|
|
|
|
if err != nil {
|
2016-04-27 02:30:20 +08:00
|
|
|
tailTest.Fatal(err)
|
2015-09-29 16:01:06 +08:00
|
|
|
}
|
|
|
|
|
2016-04-27 06:21:54 +08:00
|
|
|
go tailTest.VerifyTailOutput(tail, []string{"hello", "world"}, false)
|
2015-09-29 16:01:06 +08:00
|
|
|
|
|
|
|
<-time.After(100 * time.Millisecond)
|
|
|
|
if err := ioutil.WriteFile("test.txt", []byte("hello\nworld\n"), 0600); err != nil {
|
2016-04-27 02:30:20 +08:00
|
|
|
tailTest.Fatal(err)
|
2015-09-29 16:01:06 +08:00
|
|
|
}
|
2016-04-27 02:30:20 +08:00
|
|
|
tailTest.Cleanup(tail, true)
|
2015-09-29 16:01:06 +08:00
|
|
|
}
|
|
|
|
|
2013-09-23 15:03:51 +08:00
|
|
|
func TestStop(t *testing.T) {
|
|
|
|
tail, err := TailFile("_no_such_file", Config{Follow: true, MustExist: false})
|
|
|
|
if err != nil {
|
|
|
|
t.Error("MustExist:false is violated")
|
|
|
|
}
|
|
|
|
if tail.Stop() != nil {
|
|
|
|
t.Error("Should be stoped successfully")
|
|
|
|
}
|
2015-07-03 22:53:42 +08:00
|
|
|
tail.Cleanup()
|
2013-09-23 15:03:51 +08:00
|
|
|
}
|
|
|
|
|
2016-04-27 02:30:20 +08:00
|
|
|
func TestStopAtEOF(t *testing.T) {
|
|
|
|
tailTest := NewTailTest("maxlinesize", t)
|
|
|
|
tailTest.CreateFile("test.txt", "hello\nthere\nworld\n")
|
|
|
|
tail := tailTest.StartTail("test.txt", Config{Follow: true, Location: nil})
|
2014-12-25 05:48:29 +08:00
|
|
|
|
|
|
|
// read "hello"
|
2016-04-27 07:16:15 +08:00
|
|
|
line := <-tail.Lines
|
|
|
|
if line.Text != "hello" {
|
2016-04-27 07:30:13 +08:00
|
|
|
t.Errorf("Expected to get 'hello', got '%s' instead", line.Text)
|
2016-04-27 07:16:15 +08:00
|
|
|
}
|
2014-12-25 05:48:29 +08:00
|
|
|
|
2016-04-27 06:21:54 +08:00
|
|
|
tailTest.VerifyTailOutput(tail, []string{"there", "world"}, false)
|
2014-12-25 05:48:29 +08:00
|
|
|
tail.StopAtEOF()
|
2016-04-27 02:30:20 +08:00
|
|
|
tailTest.Cleanup(tail, true)
|
2014-12-25 05:48:29 +08:00
|
|
|
}
|
|
|
|
|
2016-04-27 02:30:20 +08:00
|
|
|
func TestMaxLineSizeFollow(t *testing.T) {
|
2015-06-17 22:59:08 +08:00
|
|
|
// As last file line does not end with newline, it will not be present in tail's output
|
2016-04-27 06:21:54 +08:00
|
|
|
maxLineSize(t, true, "hello\nworld\nfin\nhe", []string{"hel", "lo", "wor", "ld", "fin"})
|
2015-06-17 22:59:08 +08:00
|
|
|
}
|
|
|
|
|
2016-04-27 02:30:20 +08:00
|
|
|
func TestMaxLineSizeNoFollow(t *testing.T) {
|
2016-04-27 06:21:54 +08:00
|
|
|
maxLineSize(t, false, "hello\nworld\nfin\nhe", []string{"hel", "lo", "wor", "ld", "fin", "he"})
|
2015-06-17 22:59:08 +08:00
|
|
|
}
|
|
|
|
|
2016-04-27 02:30:20 +08:00
|
|
|
func TestOver4096ByteLine(t *testing.T) {
|
|
|
|
tailTest := NewTailTest("Over4096ByteLine", t)
|
2014-02-19 07:46:36 +08:00
|
|
|
testString := strings.Repeat("a", 4097)
|
2016-04-27 02:30:20 +08:00
|
|
|
tailTest.CreateFile("test.txt", "test\n"+testString+"\nhello\nworld\n")
|
|
|
|
tail := tailTest.StartTail("test.txt", Config{Follow: true, Location: nil})
|
2016-04-27 06:21:54 +08:00
|
|
|
go tailTest.VerifyTailOutput(tail, []string{"test", testString, "hello", "world"}, false)
|
2014-02-19 07:46:36 +08:00
|
|
|
|
|
|
|
// Delete after a reasonable delay, to give tail sufficient time
|
|
|
|
// to read all lines.
|
|
|
|
<-time.After(100 * time.Millisecond)
|
2016-04-27 02:30:20 +08:00
|
|
|
tailTest.RemoveFile("test.txt")
|
|
|
|
tailTest.Cleanup(tail, true)
|
2014-02-19 07:46:36 +08:00
|
|
|
}
|
2016-04-27 02:30:20 +08:00
|
|
|
func TestOver4096ByteLineWithSetMaxLineSize(t *testing.T) {
|
|
|
|
tailTest := NewTailTest("Over4096ByteLineMaxLineSize", t)
|
2014-02-19 07:46:36 +08:00
|
|
|
testString := strings.Repeat("a", 4097)
|
2016-04-27 02:30:20 +08:00
|
|
|
tailTest.CreateFile("test.txt", "test\n"+testString+"\nhello\nworld\n")
|
|
|
|
tail := tailTest.StartTail("test.txt", Config{Follow: true, Location: nil, MaxLineSize: 4097})
|
2016-04-27 06:21:54 +08:00
|
|
|
go tailTest.VerifyTailOutput(tail, []string{"test", testString, "hello", "world"}, false)
|
2014-02-19 07:46:36 +08:00
|
|
|
|
2012-10-14 03:50:27 +08:00
|
|
|
// Delete after a reasonable delay, to give tail sufficient time
|
|
|
|
// to read all lines.
|
|
|
|
<-time.After(100 * time.Millisecond)
|
2016-04-27 02:30:20 +08:00
|
|
|
tailTest.RemoveFile("test.txt")
|
|
|
|
tailTest.Cleanup(tail, true)
|
2012-10-14 03:50:27 +08:00
|
|
|
}
|
|
|
|
|
2016-04-27 02:30:20 +08:00
|
|
|
func TestLocationFull(t *testing.T) {
|
|
|
|
tailTest := NewTailTest("location-full", t)
|
|
|
|
tailTest.CreateFile("test.txt", "hello\nworld\n")
|
|
|
|
tail := tailTest.StartTail("test.txt", Config{Follow: true, Location: nil})
|
2016-04-27 06:21:54 +08:00
|
|
|
go tailTest.VerifyTailOutput(tail, []string{"hello", "world"}, false)
|
2012-10-14 02:20:04 +08:00
|
|
|
|
2012-10-14 02:44:47 +08:00
|
|
|
// Delete after a reasonable delay, to give tail sufficient time
|
|
|
|
// to read all lines.
|
2012-10-14 03:11:50 +08:00
|
|
|
<-time.After(100 * time.Millisecond)
|
2016-04-27 02:30:20 +08:00
|
|
|
tailTest.RemoveFile("test.txt")
|
|
|
|
tailTest.Cleanup(tail, true)
|
2012-10-14 02:20:04 +08:00
|
|
|
}
|
|
|
|
|
2016-04-27 02:30:20 +08:00
|
|
|
func TestLocationFullDontFollow(t *testing.T) {
|
|
|
|
tailTest := NewTailTest("location-full-dontfollow", t)
|
|
|
|
tailTest.CreateFile("test.txt", "hello\nworld\n")
|
|
|
|
tail := tailTest.StartTail("test.txt", Config{Follow: false, Location: nil})
|
2016-04-27 06:21:54 +08:00
|
|
|
go tailTest.VerifyTailOutput(tail, []string{"hello", "world"}, false)
|
2013-05-31 04:18:46 +08:00
|
|
|
|
|
|
|
// Add more data only after reasonable delay.
|
|
|
|
<-time.After(100 * time.Millisecond)
|
2016-04-27 02:30:20 +08:00
|
|
|
tailTest.AppendFile("test.txt", "more\ndata\n")
|
2013-05-31 04:18:46 +08:00
|
|
|
<-time.After(100 * time.Millisecond)
|
|
|
|
|
2016-04-27 02:30:20 +08:00
|
|
|
tailTest.Cleanup(tail, true)
|
2013-05-31 04:18:46 +08:00
|
|
|
}
|
|
|
|
|
2016-04-27 02:30:20 +08:00
|
|
|
func TestLocationEnd(t *testing.T) {
|
|
|
|
tailTest := NewTailTest("location-end", t)
|
|
|
|
tailTest.CreateFile("test.txt", "hello\nworld\n")
|
|
|
|
tail := tailTest.StartTail("test.txt", Config{Follow: true, Location: &SeekInfo{0, os.SEEK_END}})
|
2016-04-27 06:21:54 +08:00
|
|
|
go tailTest.VerifyTailOutput(tail, []string{"more", "data"}, false)
|
2012-10-14 03:02:38 +08:00
|
|
|
|
|
|
|
<-time.After(100 * time.Millisecond)
|
2016-04-27 02:30:20 +08:00
|
|
|
tailTest.AppendFile("test.txt", "more\ndata\n")
|
2012-10-14 03:02:38 +08:00
|
|
|
|
|
|
|
// Delete after a reasonable delay, to give tail sufficient time
|
|
|
|
// to read all lines.
|
2012-10-14 03:11:50 +08:00
|
|
|
<-time.After(100 * time.Millisecond)
|
2016-04-27 02:30:20 +08:00
|
|
|
tailTest.RemoveFile("test.txt")
|
|
|
|
tailTest.Cleanup(tail, true)
|
2012-10-14 03:11:50 +08:00
|
|
|
}
|
|
|
|
|
2016-04-27 02:30:20 +08:00
|
|
|
func TestLocationMiddle(t *testing.T) {
|
2013-08-10 05:57:38 +08:00
|
|
|
// Test reading from middle.
|
2016-04-27 02:30:20 +08:00
|
|
|
tailTest := NewTailTest("location-middle", t)
|
|
|
|
tailTest.CreateFile("test.txt", "hello\nworld\n")
|
|
|
|
tail := tailTest.StartTail("test.txt", Config{Follow: true, Location: &SeekInfo{-6, os.SEEK_END}})
|
2016-04-27 06:21:54 +08:00
|
|
|
go tailTest.VerifyTailOutput(tail, []string{"world", "more", "data"}, false)
|
2013-08-10 05:57:38 +08:00
|
|
|
|
|
|
|
<-time.After(100 * time.Millisecond)
|
2016-04-27 02:30:20 +08:00
|
|
|
tailTest.AppendFile("test.txt", "more\ndata\n")
|
2013-08-10 05:57:38 +08:00
|
|
|
|
|
|
|
// Delete after a reasonable delay, to give tail sufficient time
|
|
|
|
// to read all lines.
|
|
|
|
<-time.After(100 * time.Millisecond)
|
2016-04-27 02:30:20 +08:00
|
|
|
tailTest.RemoveFile("test.txt")
|
|
|
|
tailTest.Cleanup(tail, true)
|
2013-08-10 05:57:38 +08:00
|
|
|
}
|
|
|
|
|
2013-05-28 04:53:50 +08:00
|
|
|
// The use of polling file watcher could affect file rotation
|
|
|
|
// (detected via renames), so test these explicitly.
|
|
|
|
|
2016-04-27 02:30:20 +08:00
|
|
|
func TestReOpenInotify(t *testing.T) {
|
|
|
|
reOpen(t, false)
|
2013-05-29 02:46:49 +08:00
|
|
|
}
|
|
|
|
|
2016-04-27 02:30:20 +08:00
|
|
|
func TestReOpenPolling(t *testing.T) {
|
|
|
|
reOpen(t, true)
|
2013-05-28 04:53:50 +08:00
|
|
|
}
|
|
|
|
|
2013-05-29 02:46:49 +08:00
|
|
|
// The use of polling file watcher could affect file rotation
|
|
|
|
// (detected via renames), so test these explicitly.
|
|
|
|
|
2016-04-27 02:30:20 +08:00
|
|
|
func TestReSeekInotify(t *testing.T) {
|
|
|
|
reSeek(t, false)
|
2013-05-29 02:46:49 +08:00
|
|
|
}
|
|
|
|
|
2016-04-27 02:30:20 +08:00
|
|
|
func TestReSeekPolling(t *testing.T) {
|
|
|
|
reSeek(t, true)
|
2013-05-28 04:53:50 +08:00
|
|
|
}
|
2012-10-14 03:11:50 +08:00
|
|
|
|
2016-04-27 02:30:20 +08:00
|
|
|
func TestRateLimiting(t *testing.T) {
|
|
|
|
tailTest := NewTailTest("rate-limiting", t)
|
|
|
|
tailTest.CreateFile("test.txt", "hello\nworld\nagain\nextra\n")
|
2013-07-12 08:28:18 +08:00
|
|
|
config := Config{
|
2014-04-30 07:39:57 +08:00
|
|
|
Follow: true,
|
|
|
|
RateLimiter: ratelimiter.NewLeakyBucket(2, time.Second)}
|
|
|
|
leakybucketFull := "Too much log activity; waiting a second before resuming tailing"
|
2016-04-27 02:30:20 +08:00
|
|
|
tail := tailTest.StartTail("test.txt", config)
|
2014-04-29 05:46:42 +08:00
|
|
|
|
2013-07-12 08:28:18 +08:00
|
|
|
// TODO: also verify that tail resumes after the cooloff period.
|
2016-04-27 02:30:20 +08:00
|
|
|
go tailTest.VerifyTailOutput(tail, []string{
|
2014-04-30 07:39:57 +08:00
|
|
|
"hello", "world", "again",
|
|
|
|
leakybucketFull,
|
|
|
|
"more", "data",
|
2016-04-27 06:21:54 +08:00
|
|
|
leakybucketFull}, false)
|
2014-04-29 05:46:42 +08:00
|
|
|
|
|
|
|
// Add more data only after reasonable delay.
|
|
|
|
<-time.After(1200 * time.Millisecond)
|
2016-04-27 02:30:20 +08:00
|
|
|
tailTest.AppendFile("test.txt", "more\ndata\n")
|
2013-07-12 08:28:18 +08:00
|
|
|
|
|
|
|
// Delete after a reasonable delay, to give tail sufficient time
|
|
|
|
// to read all lines.
|
|
|
|
<-time.After(100 * time.Millisecond)
|
2016-04-27 02:30:20 +08:00
|
|
|
tailTest.RemoveFile("test.txt")
|
2014-04-29 05:46:42 +08:00
|
|
|
|
2016-04-27 02:30:20 +08:00
|
|
|
tailTest.Cleanup(tail, true)
|
2013-07-12 08:28:18 +08:00
|
|
|
}
|
|
|
|
|
2016-04-27 02:30:20 +08:00
|
|
|
func TestTell(t *testing.T) {
|
|
|
|
tailTest := NewTailTest("tell-position", t)
|
|
|
|
tailTest.CreateFile("test.txt", "hello\nworld\nagain\nmore\n")
|
2013-08-24 15:43:25 +08:00
|
|
|
config := Config{
|
|
|
|
Follow: false,
|
|
|
|
Location: &SeekInfo{0, os.SEEK_SET}}
|
2016-04-27 02:30:20 +08:00
|
|
|
tail := tailTest.StartTail("test.txt", config)
|
2013-08-24 15:43:25 +08:00
|
|
|
// read noe line
|
|
|
|
<-tail.Lines
|
|
|
|
offset, err := tail.Tell()
|
|
|
|
if err != nil {
|
2016-04-27 02:30:20 +08:00
|
|
|
tailTest.Errorf("Tell return error: %s", err.Error())
|
2013-08-24 15:43:25 +08:00
|
|
|
}
|
|
|
|
tail.Done()
|
|
|
|
// tail.close()
|
|
|
|
|
|
|
|
config = Config{
|
|
|
|
Follow: false,
|
|
|
|
Location: &SeekInfo{offset, os.SEEK_SET}}
|
2016-04-27 02:30:20 +08:00
|
|
|
tail = tailTest.StartTail("test.txt", config)
|
2013-08-24 15:43:25 +08:00
|
|
|
for l := range tail.Lines {
|
|
|
|
// it may readed one line in the chan(tail.Lines),
|
|
|
|
// so it may lost one line.
|
|
|
|
if l.Text != "world" && l.Text != "again" {
|
2016-04-27 02:30:20 +08:00
|
|
|
tailTest.Fatalf("mismatch; expected world or again, but got %s",
|
2013-08-24 15:43:25 +08:00
|
|
|
l.Text)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2016-04-27 02:30:20 +08:00
|
|
|
tailTest.RemoveFile("test.txt")
|
2013-08-24 15:43:25 +08:00
|
|
|
tail.Done()
|
2015-07-03 22:53:42 +08:00
|
|
|
tail.Cleanup()
|
2013-08-24 15:43:25 +08:00
|
|
|
}
|
|
|
|
|
2016-04-27 02:30:20 +08:00
|
|
|
func TestBlockUntilExists(t *testing.T) {
|
|
|
|
tailTest := NewTailTest("block-until-file-exists", t)
|
2015-12-09 15:29:06 +08:00
|
|
|
config := Config{
|
|
|
|
Follow: true,
|
|
|
|
}
|
2016-04-27 02:30:20 +08:00
|
|
|
tail := tailTest.StartTail("test.txt", config)
|
2015-12-09 15:29:06 +08:00
|
|
|
go func() {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2016-04-27 02:30:20 +08:00
|
|
|
tailTest.CreateFile("test.txt", "hello world\n")
|
2015-12-09 15:29:06 +08:00
|
|
|
}()
|
|
|
|
for l := range tail.Lines {
|
|
|
|
if l.Text != "hello world" {
|
2016-04-27 02:30:20 +08:00
|
|
|
tailTest.Fatalf("mismatch; expected hello world, but got %s",
|
2015-12-09 15:29:06 +08:00
|
|
|
l.Text)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2016-04-27 02:30:20 +08:00
|
|
|
tailTest.RemoveFile("test.txt")
|
2015-12-09 15:29:06 +08:00
|
|
|
tail.Stop()
|
|
|
|
tail.Cleanup()
|
|
|
|
}
|
|
|
|
|
2016-04-27 06:21:54 +08:00
|
|
|
func maxLineSize(t *testing.T, follow bool, fileContent string, expected []string) {
|
|
|
|
tailTest := NewTailTest("maxlinesize", t)
|
|
|
|
tailTest.CreateFile("test.txt", fileContent)
|
|
|
|
tail := tailTest.StartTail("test.txt", Config{Follow: follow, Location: nil, MaxLineSize: 3})
|
|
|
|
go tailTest.VerifyTailOutput(tail, expected, false)
|
|
|
|
|
|
|
|
// Delete after a reasonable delay, to give tail sufficient time
|
|
|
|
// to read all lines.
|
|
|
|
<-time.After(100 * time.Millisecond)
|
|
|
|
tailTest.RemoveFile("test.txt")
|
|
|
|
tailTest.Cleanup(tail, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func reOpen(t *testing.T, poll bool) {
|
|
|
|
var name string
|
|
|
|
var delay time.Duration
|
|
|
|
if poll {
|
|
|
|
name = "reopen-polling"
|
|
|
|
delay = 300 * time.Millisecond // account for POLL_DURATION
|
|
|
|
} else {
|
|
|
|
name = "reopen-inotify"
|
|
|
|
delay = 100 * time.Millisecond
|
|
|
|
}
|
|
|
|
tailTest := NewTailTest(name, t)
|
|
|
|
tailTest.CreateFile("test.txt", "hello\nworld\n")
|
|
|
|
tail := tailTest.StartTail(
|
|
|
|
"test.txt",
|
|
|
|
Config{Follow: true, ReOpen: true, Poll: poll})
|
|
|
|
content := []string{"hello", "world", "more", "data", "endofworld"}
|
|
|
|
go tailTest.ReadLines(tail, content)
|
|
|
|
|
|
|
|
// deletion must trigger reopen
|
|
|
|
<-time.After(delay)
|
|
|
|
tailTest.RemoveFile("test.txt")
|
|
|
|
<-time.After(delay)
|
|
|
|
tailTest.CreateFile("test.txt", "more\ndata\n")
|
|
|
|
|
|
|
|
// rename must trigger reopen
|
|
|
|
<-time.After(delay)
|
|
|
|
tailTest.RenameFile("test.txt", "test.txt.rotated")
|
|
|
|
<-time.After(delay)
|
|
|
|
tailTest.CreateFile("test.txt", "endofworld\n")
|
|
|
|
|
|
|
|
// Delete after a reasonable delay, to give tail sufficient time
|
|
|
|
// to read all lines.
|
|
|
|
<-time.After(delay)
|
|
|
|
tailTest.RemoveFile("test.txt")
|
|
|
|
<-time.After(delay)
|
|
|
|
|
|
|
|
// Do not bother with stopping as it could kill the tomb during
|
|
|
|
// the reading of data written above. Timings can vary based on
|
|
|
|
// test environment.
|
|
|
|
tail.Cleanup()
|
|
|
|
}
|
|
|
|
|
|
|
|
func reSeek(t *testing.T, poll bool) {
|
|
|
|
var name string
|
|
|
|
if poll {
|
|
|
|
name = "reseek-polling"
|
|
|
|
} else {
|
|
|
|
name = "reseek-inotify"
|
|
|
|
}
|
|
|
|
tailTest := NewTailTest(name, t)
|
|
|
|
tailTest.CreateFile("test.txt", "a really long string goes here\nhello\nworld\n")
|
|
|
|
tail := tailTest.StartTail(
|
|
|
|
"test.txt",
|
|
|
|
Config{Follow: true, ReOpen: false, Poll: poll})
|
|
|
|
|
|
|
|
go tailTest.VerifyTailOutput(tail, []string{
|
|
|
|
"a really long string goes here", "hello", "world", "h311o", "w0r1d", "endofworld"}, false)
|
|
|
|
|
|
|
|
// truncate now
|
|
|
|
<-time.After(100 * time.Millisecond)
|
|
|
|
tailTest.TruncateFile("test.txt", "h311o\nw0r1d\nendofworld\n")
|
|
|
|
|
|
|
|
// Delete after a reasonable delay, to give tail sufficient time
|
|
|
|
// to read all lines.
|
|
|
|
<-time.After(100 * time.Millisecond)
|
|
|
|
tailTest.RemoveFile("test.txt")
|
|
|
|
|
|
|
|
// Do not bother with stopping as it could kill the tomb during
|
|
|
|
// the reading of data written above. Timings can vary based on
|
|
|
|
// test environment.
|
|
|
|
tailTest.Cleanup(tail, false)
|
|
|
|
}
|
|
|
|
|
2012-10-14 03:11:50 +08:00
|
|
|
// Test library
|
|
|
|
|
2012-10-14 03:54:12 +08:00
|
|
|
type TailTest struct {
|
2012-10-14 02:20:04 +08:00
|
|
|
Name string
|
|
|
|
path string
|
2015-09-29 16:01:06 +08:00
|
|
|
done chan struct{}
|
2012-10-14 03:54:12 +08:00
|
|
|
*testing.T
|
2012-10-14 02:20:04 +08:00
|
|
|
}
|
|
|
|
|
2012-10-14 03:54:12 +08:00
|
|
|
func NewTailTest(name string, t *testing.T) TailTest {
|
2015-09-29 16:01:06 +08:00
|
|
|
tt := TailTest{name, ".test/" + name, make(chan struct{}), t}
|
2012-10-14 03:54:12 +08:00
|
|
|
err := os.MkdirAll(tt.path, os.ModeTemporary|0700)
|
2012-10-14 02:44:47 +08:00
|
|
|
if err != nil {
|
2012-10-14 03:54:12 +08:00
|
|
|
tt.Fatal(err)
|
2012-10-14 02:44:47 +08:00
|
|
|
}
|
2013-05-29 03:55:11 +08:00
|
|
|
|
2012-10-14 03:54:12 +08:00
|
|
|
return tt
|
2012-10-14 02:20:04 +08:00
|
|
|
}
|
|
|
|
|
2012-10-14 03:54:12 +08:00
|
|
|
func (t TailTest) CreateFile(name string, contents string) {
|
|
|
|
err := ioutil.WriteFile(t.path+"/"+name, []byte(contents), 0600)
|
2012-10-14 02:44:47 +08:00
|
|
|
if err != nil {
|
2012-10-14 03:54:12 +08:00
|
|
|
t.Fatal(err)
|
2012-10-14 02:44:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-14 03:54:12 +08:00
|
|
|
func (t TailTest) RemoveFile(name string) {
|
|
|
|
err := os.Remove(t.path + "/" + name)
|
2012-10-14 02:44:47 +08:00
|
|
|
if err != nil {
|
2012-10-14 03:54:12 +08:00
|
|
|
t.Fatal(err)
|
2012-10-14 02:44:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-14 03:54:12 +08:00
|
|
|
func (t TailTest) RenameFile(oldname string, newname string) {
|
|
|
|
oldname = t.path + "/" + oldname
|
|
|
|
newname = t.path + "/" + newname
|
2012-10-14 03:11:50 +08:00
|
|
|
err := os.Rename(oldname, newname)
|
|
|
|
if err != nil {
|
2012-10-14 03:54:12 +08:00
|
|
|
t.Fatal(err)
|
2012-10-14 03:11:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-14 03:54:12 +08:00
|
|
|
func (t TailTest) AppendFile(name string, contents string) {
|
|
|
|
f, err := os.OpenFile(t.path+"/"+name, os.O_APPEND|os.O_WRONLY, 0600)
|
2012-10-14 03:02:38 +08:00
|
|
|
if err != nil {
|
2012-10-14 03:54:12 +08:00
|
|
|
t.Fatal(err)
|
2012-10-14 03:02:38 +08:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
_, err = f.WriteString(contents)
|
|
|
|
if err != nil {
|
2012-10-14 03:54:12 +08:00
|
|
|
t.Fatal(err)
|
2012-10-14 03:02:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-29 02:46:49 +08:00
|
|
|
func (t TailTest) TruncateFile(name string, contents string) {
|
2013-05-29 03:28:56 +08:00
|
|
|
f, err := os.OpenFile(t.path+"/"+name, os.O_TRUNC|os.O_WRONLY, 0600)
|
2013-05-29 02:46:49 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
_, err = f.WriteString(contents)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-14 03:54:12 +08:00
|
|
|
func (t TailTest) StartTail(name string, config Config) *Tail {
|
|
|
|
tail, err := TailFile(t.path+"/"+name, config)
|
2012-10-14 02:44:47 +08:00
|
|
|
if err != nil {
|
2012-10-14 03:54:12 +08:00
|
|
|
t.Fatal(err)
|
2012-10-14 02:44:47 +08:00
|
|
|
}
|
|
|
|
return tail
|
|
|
|
}
|
|
|
|
|
2016-04-27 06:21:54 +08:00
|
|
|
func (t TailTest) VerifyTailOutput(tail *Tail, lines []string, expectEOF bool) {
|
2015-09-29 16:01:06 +08:00
|
|
|
defer close(t.done)
|
2016-04-27 06:21:54 +08:00
|
|
|
t.ReadLines(tail, lines)
|
|
|
|
// It is important to do this if only EOF is expected
|
|
|
|
// otherwise we could block on <-tail.Lines
|
|
|
|
if expectEOF {
|
|
|
|
line, ok := <-tail.Lines
|
|
|
|
if ok {
|
|
|
|
t.Fatalf("more content from tail: %+v", line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t TailTest) ReadLines(tail *Tail, lines []string) {
|
2012-10-14 03:11:50 +08:00
|
|
|
for idx, line := range lines {
|
2012-10-14 02:20:04 +08:00
|
|
|
tailedLine, ok := <-tail.Lines
|
|
|
|
if !ok {
|
2014-04-29 05:46:18 +08:00
|
|
|
// tail.Lines is closed and empty.
|
2013-05-29 10:33:52 +08:00
|
|
|
err := tail.Err()
|
2013-05-28 06:21:02 +08:00
|
|
|
if err != nil {
|
2014-04-29 05:46:18 +08:00
|
|
|
t.Fatalf("tail ended with error: %v", err)
|
2013-05-28 06:21:02 +08:00
|
|
|
}
|
2013-05-29 10:33:52 +08:00
|
|
|
t.Fatalf("tail ended early; expecting more: %v", lines[idx:])
|
2012-10-14 02:20:04 +08:00
|
|
|
}
|
|
|
|
if tailedLine == nil {
|
2012-10-14 03:54:12 +08:00
|
|
|
t.Fatalf("tail.Lines returned nil; not possible")
|
2012-10-14 02:20:04 +08:00
|
|
|
}
|
2014-04-29 05:15:34 +08:00
|
|
|
// Note: not checking .Err as the `lines` argument is designed
|
|
|
|
// to match error strings as well.
|
2012-10-14 02:20:04 +08:00
|
|
|
if tailedLine.Text != line {
|
2014-05-17 09:01:10 +08:00
|
|
|
t.Fatalf(
|
|
|
|
"unexpected line/err from tail: "+
|
|
|
|
"expecting <<%s>>>, but got <<<%s>>>",
|
2014-04-29 05:15:34 +08:00
|
|
|
line, tailedLine.Text)
|
2012-10-14 02:20:04 +08:00
|
|
|
}
|
|
|
|
}
|
2012-10-13 05:32:04 +08:00
|
|
|
}
|
2016-04-27 01:56:05 +08:00
|
|
|
|
2016-04-27 02:30:20 +08:00
|
|
|
func (t TailTest) Cleanup(tail *Tail, stop bool) {
|
2016-04-27 03:23:22 +08:00
|
|
|
<-t.done
|
2016-04-27 02:30:20 +08:00
|
|
|
if stop {
|
|
|
|
tail.Stop()
|
|
|
|
}
|
2016-04-27 01:56:05 +08:00
|
|
|
tail.Cleanup()
|
|
|
|
}
|