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
|
|
|
|
|
|
|
"./watch"
|
2015-09-30 22:48:17 +08:00
|
|
|
"github.com/hpcloud/tail/ratelimiter"
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
2012-10-14 02:20:04 +08:00
|
|
|
tail.Stop()
|
2015-07-03 22:53:42 +08:00
|
|
|
tail.Cleanup()
|
2012-10-14 02:20:04 +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
|
|
|
}
|
|
|
|
|
2015-06-17 22:59:08 +08:00
|
|
|
func MaxLineSizeT(_t *testing.T, follow bool, fileContent string, expected []string) {
|
2012-10-14 03:54:12 +08:00
|
|
|
t := NewTailTest("maxlinesize", _t)
|
2015-06-17 22:59:08 +08:00
|
|
|
t.CreateFile("test.txt", fileContent)
|
|
|
|
tail := t.StartTail("test.txt", Config{Follow: follow, Location: nil, MaxLineSize: 3})
|
|
|
|
go t.VerifyTailOutput(tail, expected)
|
2012-10-14 03:50:27 +08:00
|
|
|
|
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)
|
|
|
|
t.RemoveFile("test.txt")
|
|
|
|
tail.Stop()
|
2015-07-03 22:53:42 +08:00
|
|
|
tail.Cleanup()
|
2014-02-19 07:46:36 +08:00
|
|
|
}
|
|
|
|
|
2015-06-17 22:59:08 +08:00
|
|
|
func TestMaxLineSizeFollow(_t *testing.T) {
|
|
|
|
// As last file line does not end with newline, it will not be present in tail's output
|
|
|
|
MaxLineSizeT(_t, true, "hello\nworld\nfin\nhe", []string{"hel", "lo", "wor", "ld", "fin"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMaxLineSizeNoFollow(_t *testing.T) {
|
|
|
|
MaxLineSizeT(_t, false, "hello\nworld\nfin\nhe", []string{"hel", "lo", "wor", "ld", "fin", "he"})
|
|
|
|
}
|
|
|
|
|
2014-02-19 07:46:36 +08:00
|
|
|
func TestOver4096ByteLine(_t *testing.T) {
|
|
|
|
t := NewTailTest("Over4096ByteLine", _t)
|
|
|
|
testString := strings.Repeat("a", 4097)
|
|
|
|
t.CreateFile("test.txt", "test\n"+testString+"\nhello\nworld\n")
|
|
|
|
tail := t.StartTail("test.txt", Config{Follow: true, Location: nil})
|
|
|
|
go t.VerifyTailOutput(tail, []string{"test", testString, "hello", "world"})
|
|
|
|
|
|
|
|
// Delete after a reasonable delay, to give tail sufficient time
|
|
|
|
// to read all lines.
|
|
|
|
<-time.After(100 * time.Millisecond)
|
|
|
|
t.RemoveFile("test.txt")
|
|
|
|
tail.Stop()
|
2015-07-03 22:53:42 +08:00
|
|
|
tail.Cleanup()
|
2014-02-19 07:46:36 +08:00
|
|
|
}
|
|
|
|
func TestOver4096ByteLineWithSetMaxLineSize(_t *testing.T) {
|
|
|
|
t := NewTailTest("Over4096ByteLineMaxLineSize", _t)
|
|
|
|
testString := strings.Repeat("a", 4097)
|
2014-05-17 09:01:10 +08:00
|
|
|
t.CreateFile("test.txt", "test\n"+testString+"\nhello\nworld\n")
|
2014-02-19 07:46:36 +08:00
|
|
|
tail := t.StartTail("test.txt", Config{Follow: true, Location: nil, MaxLineSize: 4097})
|
|
|
|
go t.VerifyTailOutput(tail, []string{"test", testString, "hello", "world"})
|
|
|
|
|
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)
|
2012-10-14 03:54:12 +08:00
|
|
|
t.RemoveFile("test.txt")
|
2014-05-17 09:01:10 +08:00
|
|
|
// tail.Stop()
|
2015-07-03 22:53:42 +08:00
|
|
|
tail.Cleanup()
|
2012-10-14 03:50:27 +08:00
|
|
|
}
|
|
|
|
|
2012-10-14 03:54:12 +08:00
|
|
|
func TestLocationFull(_t *testing.T) {
|
|
|
|
t := NewTailTest("location-full", _t)
|
|
|
|
t.CreateFile("test.txt", "hello\nworld\n")
|
2013-08-10 05:57:38 +08:00
|
|
|
tail := t.StartTail("test.txt", Config{Follow: true, Location: nil})
|
2012-10-14 03:54:12 +08:00
|
|
|
go t.VerifyTailOutput(tail, []string{"hello", "world"})
|
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)
|
2012-10-14 03:54:12 +08:00
|
|
|
t.RemoveFile("test.txt")
|
2012-10-14 02:20:04 +08:00
|
|
|
tail.Stop()
|
2015-07-03 22:53:42 +08:00
|
|
|
tail.Cleanup()
|
2012-10-14 02:20:04 +08:00
|
|
|
}
|
|
|
|
|
2013-05-31 04:18:46 +08:00
|
|
|
func TestLocationFullDontFollow(_t *testing.T) {
|
|
|
|
t := NewTailTest("location-full-dontfollow", _t)
|
|
|
|
t.CreateFile("test.txt", "hello\nworld\n")
|
2013-08-10 05:57:38 +08:00
|
|
|
tail := t.StartTail("test.txt", Config{Follow: false, Location: nil})
|
2013-05-31 04:18:46 +08:00
|
|
|
go t.VerifyTailOutput(tail, []string{"hello", "world"})
|
|
|
|
|
|
|
|
// Add more data only after reasonable delay.
|
|
|
|
<-time.After(100 * time.Millisecond)
|
|
|
|
t.AppendFile("test.txt", "more\ndata\n")
|
|
|
|
<-time.After(100 * time.Millisecond)
|
|
|
|
|
|
|
|
tail.Stop()
|
2015-07-03 22:53:42 +08:00
|
|
|
tail.Cleanup()
|
2013-05-31 04:18:46 +08:00
|
|
|
}
|
|
|
|
|
2012-10-14 03:54:12 +08:00
|
|
|
func TestLocationEnd(_t *testing.T) {
|
|
|
|
t := NewTailTest("location-end", _t)
|
|
|
|
t.CreateFile("test.txt", "hello\nworld\n")
|
2013-08-10 05:57:38 +08:00
|
|
|
tail := t.StartTail("test.txt", Config{Follow: true, Location: &SeekInfo{0, os.SEEK_END}})
|
2012-10-14 03:54:12 +08:00
|
|
|
go t.VerifyTailOutput(tail, []string{"more", "data"})
|
2012-10-14 03:02:38 +08:00
|
|
|
|
|
|
|
<-time.After(100 * time.Millisecond)
|
2012-10-14 03:54:12 +08:00
|
|
|
t.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)
|
2012-10-14 03:54:12 +08:00
|
|
|
t.RemoveFile("test.txt")
|
2012-10-14 03:11:50 +08:00
|
|
|
tail.Stop()
|
2015-07-03 22:53:42 +08:00
|
|
|
tail.Cleanup()
|
2012-10-14 03:11:50 +08:00
|
|
|
}
|
|
|
|
|
2013-08-10 05:57:38 +08:00
|
|
|
func TestLocationMiddle(_t *testing.T) {
|
|
|
|
// Test reading from middle.
|
|
|
|
t := NewTailTest("location-end", _t)
|
|
|
|
t.CreateFile("test.txt", "hello\nworld\n")
|
|
|
|
tail := t.StartTail("test.txt", Config{Follow: true, Location: &SeekInfo{-6, os.SEEK_END}})
|
|
|
|
go t.VerifyTailOutput(tail, []string{"world", "more", "data"})
|
|
|
|
|
|
|
|
<-time.After(100 * time.Millisecond)
|
|
|
|
t.AppendFile("test.txt", "more\ndata\n")
|
|
|
|
|
|
|
|
// Delete after a reasonable delay, to give tail sufficient time
|
|
|
|
// to read all lines.
|
|
|
|
<-time.After(100 * time.Millisecond)
|
|
|
|
t.RemoveFile("test.txt")
|
|
|
|
tail.Stop()
|
2015-07-03 22:53:42 +08:00
|
|
|
tail.Cleanup()
|
2013-08-10 05:57:38 +08:00
|
|
|
}
|
|
|
|
|
2013-05-28 04:53:50 +08:00
|
|
|
func _TestReOpen(_t *testing.T, poll bool) {
|
2013-05-28 06:21:02 +08:00
|
|
|
var name string
|
2014-03-22 13:32:56 +08:00
|
|
|
var delay time.Duration
|
2013-05-28 06:21:02 +08:00
|
|
|
if poll {
|
|
|
|
name = "reopen-polling"
|
2014-03-22 13:32:56 +08:00
|
|
|
delay = 300 * time.Millisecond // account for POLL_DURATION
|
2013-05-29 04:53:19 +08:00
|
|
|
} else {
|
2013-05-28 06:21:02 +08:00
|
|
|
name = "reopen-inotify"
|
2014-03-22 13:32:56 +08:00
|
|
|
delay = 100 * time.Millisecond
|
2013-05-28 06:21:02 +08:00
|
|
|
}
|
|
|
|
t := NewTailTest(name, _t)
|
2012-10-14 03:54:12 +08:00
|
|
|
t.CreateFile("test.txt", "hello\nworld\n")
|
2013-05-28 04:53:50 +08:00
|
|
|
tail := t.StartTail(
|
|
|
|
"test.txt",
|
2013-08-10 05:57:38 +08:00
|
|
|
Config{Follow: true, ReOpen: true, Poll: poll})
|
2013-05-29 04:53:19 +08:00
|
|
|
|
2012-10-14 03:54:12 +08:00
|
|
|
go t.VerifyTailOutput(tail, []string{"hello", "world", "more", "data", "endofworld"})
|
2012-10-14 03:11:50 +08:00
|
|
|
|
|
|
|
// deletion must trigger reopen
|
2014-03-22 13:32:56 +08:00
|
|
|
<-time.After(delay)
|
2012-10-14 03:54:12 +08:00
|
|
|
t.RemoveFile("test.txt")
|
2014-03-22 13:32:56 +08:00
|
|
|
<-time.After(delay)
|
2012-10-14 03:54:12 +08:00
|
|
|
t.CreateFile("test.txt", "more\ndata\n")
|
2012-10-14 03:11:50 +08:00
|
|
|
|
|
|
|
// rename must trigger reopen
|
2014-03-22 13:32:56 +08:00
|
|
|
<-time.After(delay)
|
2012-10-14 03:54:12 +08:00
|
|
|
t.RenameFile("test.txt", "test.txt.rotated")
|
2014-03-22 13:32:56 +08:00
|
|
|
<-time.After(delay)
|
2013-05-28 06:28:50 +08:00
|
|
|
t.CreateFile("test.txt", "endofworld")
|
2012-10-14 03:11:50 +08:00
|
|
|
|
|
|
|
// Delete after a reasonable delay, to give tail sufficient time
|
|
|
|
// to read all lines.
|
2014-03-22 13:32:56 +08:00
|
|
|
<-time.After(delay)
|
2012-10-14 03:54:12 +08:00
|
|
|
t.RemoveFile("test.txt")
|
2014-03-22 13:32:56 +08:00
|
|
|
<-time.After(delay)
|
2013-05-28 06:21:02 +08:00
|
|
|
|
2013-05-29 10:33:52 +08:00
|
|
|
// 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.Stop()
|
2015-07-03 22:53:42 +08:00
|
|
|
tail.Cleanup()
|
2012-10-14 03:02: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.
|
|
|
|
|
2013-05-29 03:28:56 +08:00
|
|
|
func TestReOpenInotify(_t *testing.T) {
|
2013-05-29 02:46:49 +08:00
|
|
|
_TestReOpen(_t, false)
|
|
|
|
}
|
|
|
|
|
2013-05-29 03:28:56 +08:00
|
|
|
func TestReOpenPolling(_t *testing.T) {
|
2013-05-28 04:53:50 +08:00
|
|
|
_TestReOpen(_t, true)
|
|
|
|
}
|
|
|
|
|
2013-05-29 02:46:49 +08:00
|
|
|
func _TestReSeek(_t *testing.T, poll bool) {
|
|
|
|
var name string
|
|
|
|
if poll {
|
|
|
|
name = "reseek-polling"
|
2013-05-29 04:53:19 +08:00
|
|
|
} else {
|
2013-05-29 02:46:49 +08:00
|
|
|
name = "reseek-inotify"
|
|
|
|
}
|
|
|
|
t := NewTailTest(name, _t)
|
2013-05-29 03:28:56 +08:00
|
|
|
t.CreateFile("test.txt", "a really long string goes here\nhello\nworld\n")
|
2013-05-29 02:46:49 +08:00
|
|
|
tail := t.StartTail(
|
|
|
|
"test.txt",
|
2013-08-10 05:57:38 +08:00
|
|
|
Config{Follow: true, ReOpen: false, Poll: poll})
|
2013-05-29 04:53:19 +08:00
|
|
|
|
2013-05-29 03:28:56 +08:00
|
|
|
go t.VerifyTailOutput(tail, []string{
|
|
|
|
"a really long string goes here", "hello", "world", "h311o", "w0r1d", "endofworld"})
|
2013-05-29 02:46:49 +08:00
|
|
|
|
|
|
|
// truncate now
|
|
|
|
<-time.After(100 * time.Millisecond)
|
|
|
|
t.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)
|
2013-05-29 03:55:11 +08:00
|
|
|
t.RemoveFile("test.txt")
|
2013-05-29 02:46:49 +08:00
|
|
|
|
2013-05-29 10:33:52 +08:00
|
|
|
// 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.Stop()
|
2015-07-03 22:53:42 +08:00
|
|
|
tail.Cleanup()
|
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.
|
|
|
|
|
2013-05-29 03:28:56 +08:00
|
|
|
func TestReSeekInotify(_t *testing.T) {
|
2013-05-29 02:46:49 +08:00
|
|
|
_TestReSeek(_t, false)
|
|
|
|
}
|
|
|
|
|
2013-05-29 03:28:56 +08:00
|
|
|
func TestReSeekPolling(_t *testing.T) {
|
2013-05-29 02:46:49 +08:00
|
|
|
_TestReSeek(_t, true)
|
2013-05-28 04:53:50 +08:00
|
|
|
}
|
2012-10-14 03:11:50 +08:00
|
|
|
|
2013-07-12 08:28:18 +08:00
|
|
|
func TestRateLimiting(_t *testing.T) {
|
|
|
|
t := NewTailTest("rate-limiting", _t)
|
2014-04-29 05:15:17 +08:00
|
|
|
t.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"
|
2013-07-12 08:28:18 +08:00
|
|
|
tail := t.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.
|
2014-04-30 07:39:57 +08:00
|
|
|
go t.VerifyTailOutput(tail, []string{
|
|
|
|
"hello", "world", "again",
|
|
|
|
leakybucketFull,
|
|
|
|
"more", "data",
|
|
|
|
leakybucketFull})
|
2014-04-29 05:46:42 +08:00
|
|
|
|
|
|
|
// Add more data only after reasonable delay.
|
|
|
|
<-time.After(1200 * time.Millisecond)
|
|
|
|
t.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)
|
|
|
|
t.RemoveFile("test.txt")
|
2014-04-29 05:46:42 +08:00
|
|
|
|
|
|
|
// tail.Stop()
|
2015-07-03 22:53:42 +08:00
|
|
|
tail.Cleanup()
|
2013-07-12 08:28:18 +08:00
|
|
|
}
|
|
|
|
|
2013-08-24 15:43:25 +08:00
|
|
|
func TestTell(_t *testing.T) {
|
|
|
|
t := NewTailTest("tell-position", _t)
|
|
|
|
t.CreateFile("test.txt", "hello\nworld\nagain\nmore\n")
|
|
|
|
config := Config{
|
|
|
|
Follow: false,
|
|
|
|
Location: &SeekInfo{0, os.SEEK_SET}}
|
|
|
|
tail := t.StartTail("test.txt", config)
|
|
|
|
// read noe line
|
|
|
|
<-tail.Lines
|
|
|
|
offset, err := tail.Tell()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Tell return error: %s", err.Error())
|
|
|
|
}
|
|
|
|
tail.Done()
|
|
|
|
// tail.close()
|
|
|
|
|
|
|
|
config = Config{
|
|
|
|
Follow: false,
|
|
|
|
Location: &SeekInfo{offset, os.SEEK_SET}}
|
|
|
|
tail = t.StartTail("test.txt", config)
|
|
|
|
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" {
|
|
|
|
t.Fatalf("mismatch; expected world or again, but got %s",
|
|
|
|
l.Text)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
t.RemoveFile("test.txt")
|
|
|
|
tail.Done()
|
2015-07-03 22:53:42 +08:00
|
|
|
tail.Cleanup()
|
2013-08-24 15:43:25 +08:00
|
|
|
}
|
|
|
|
|
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
|
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 {
|
|
|
|
tt := TailTest{name, ".test/" + name, t}
|
|
|
|
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
|
|
|
|
2013-05-29 10:33:52 +08:00
|
|
|
// 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
|
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
|
|
|
|
}
|
|
|
|
|
2012-10-14 03:54:12 +08:00
|
|
|
func (t TailTest) VerifyTailOutput(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
|
|
|
}
|
|
|
|
}
|
|
|
|
line, ok := <-tail.Lines
|
|
|
|
if ok {
|
2013-07-12 08:28:18 +08:00
|
|
|
t.Fatalf("more content from tail: %+v", line)
|
2012-10-14 02:20:04 +08:00
|
|
|
}
|
2012-10-13 05:32:04 +08:00
|
|
|
}
|