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"
|
2012-10-13 05:32:04 +08:00
|
|
|
"testing"
|
2012-10-14 02:20:04 +08:00
|
|
|
"time"
|
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()
|
|
|
|
}
|
|
|
|
|
2012-10-14 03:54:12 +08:00
|
|
|
func TestMaxLineSize(_t *testing.T) {
|
|
|
|
t := NewTailTest("maxlinesize", _t)
|
|
|
|
t.CreateFile("test.txt", "hello\nworld\nfin\nhe")
|
|
|
|
tail := t.StartTail("test.txt", Config{Follow: true, Location: -1, MaxLineSize: 3})
|
|
|
|
go t.VerifyTailOutput(tail, []string{"hel", "lo", "wor", "ld", "fin", "he"})
|
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")
|
2012-10-14 03:50:27 +08:00
|
|
|
tail.Stop()
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
tail := t.StartTail("test.txt", Config{Follow: true, Location: -1})
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
tail := t.StartTail("test.txt", Config{Follow: true, Location: 0})
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
if poll {
|
|
|
|
name = "reopen-polling"
|
|
|
|
}else {
|
|
|
|
name = "reopen-inotify"
|
|
|
|
}
|
|
|
|
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",
|
|
|
|
Config{Follow: true, ReOpen: true, Poll: poll, Location: -1})
|
2013-05-28 06:21:02 +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
|
|
|
|
<-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
|
|
|
<-time.After(100 * time.Millisecond)
|
2012-10-14 03:54:12 +08:00
|
|
|
t.CreateFile("test.txt", "more\ndata\n")
|
2013-05-28 06:21:02 +08:00
|
|
|
if poll {
|
2013-05-28 06:28:50 +08:00
|
|
|
// Give polling a chance to read the just-written lines (more;
|
|
|
|
// data), before we recreate the file again below.
|
2013-05-28 06:21:02 +08:00
|
|
|
<-time.After(POLL_DURATION)
|
|
|
|
}
|
2012-10-14 03:11:50 +08:00
|
|
|
|
|
|
|
// rename must trigger reopen
|
|
|
|
<-time.After(100 * time.Millisecond)
|
2012-10-14 03:54:12 +08:00
|
|
|
t.RenameFile("test.txt", "test.txt.rotated")
|
2012-10-14 03:11:50 +08:00
|
|
|
<-time.After(100 * time.Millisecond)
|
2013-05-28 06:21:02 +08:00
|
|
|
if poll {
|
2013-05-28 06:28:50 +08:00
|
|
|
// This time, wait a bit before creating the file to test
|
|
|
|
// PollingFileWatcher's BlockUntilExists.
|
2013-05-28 06:21:02 +08:00
|
|
|
<-time.After(POLL_DURATION)
|
|
|
|
}
|
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.
|
|
|
|
<-time.After(100 * time.Millisecond)
|
2012-10-14 03:54:12 +08:00
|
|
|
t.RemoveFile("test.txt")
|
2013-05-28 06:21:02 +08:00
|
|
|
|
2012-10-14 03:02:38 +08:00
|
|
|
tail.Stop()
|
|
|
|
}
|
|
|
|
|
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"
|
|
|
|
}else {
|
|
|
|
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",
|
|
|
|
Config{Follow: true, ReOpen: true, Poll: poll, Location: -1})
|
|
|
|
|
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)
|
2013-05-29 03:28:56 +08:00
|
|
|
if poll {
|
|
|
|
// Give polling a chance to read the just-written lines (more;
|
|
|
|
// data), before we truncate the file again below.
|
|
|
|
<-time.After(POLL_DURATION)
|
|
|
|
}
|
|
|
|
println("truncating..")
|
2013-05-29 02:46:49 +08:00
|
|
|
t.TruncateFile("test.txt", "h311o\nw0r1d\nendofworld\n")
|
|
|
|
// XXX: is this required for this test function?
|
|
|
|
if poll {
|
|
|
|
// Give polling a chance to read the just-written lines (more;
|
|
|
|
// data), before we recreate the file again below.
|
|
|
|
<-time.After(POLL_DURATION)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 03:28:56 +08:00
|
|
|
println("Stopping...")
|
2013-05-29 02:46:49 +08:00
|
|
|
tail.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-05-29 02:46:49 +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
|
|
|
|
|
|
|
// Use a smaller poll duration for faster test runs.
|
|
|
|
POLL_DURATION = 25 * time.Millisecond
|
|
|
|
|
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 {
|
2013-05-28 06:21:02 +08:00
|
|
|
err := tail.Wait()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("tail ended early with error: %v", err)
|
|
|
|
}else{
|
|
|
|
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
|
|
|
}
|
|
|
|
if tailedLine.Text != line {
|
2013-05-29 02:46:49 +08:00
|
|
|
t.Fatalf("mismatch; %s (actual) != %s (expected)",
|
|
|
|
tailedLine.Text, line)
|
2012-10-14 02:20:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
line, ok := <-tail.Lines
|
|
|
|
if ok {
|
2012-10-14 03:54:12 +08:00
|
|
|
t.Fatalf("more content from tail: %s", line.Text)
|
2012-10-14 02:20:04 +08:00
|
|
|
}
|
2012-10-13 05:32:04 +08:00
|
|
|
}
|