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() {
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2012-10-14 03:54:12 +08:00
|
|
|
func TestReOpen(_t *testing.T) {
|
|
|
|
t := NewTailTest("reopen", _t)
|
|
|
|
t.CreateFile("test.txt", "hello\nworld\n")
|
|
|
|
tail := t.StartTail("test.txt", Config{Follow: true, ReOpen: true, Location: -1})
|
|
|
|
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")
|
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)
|
2012-10-14 03:54:12 +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")
|
2012-10-14 03:11:50 +08:00
|
|
|
|
2012-10-14 03:02:38 +08:00
|
|
|
tail.Stop()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2012-10-14 03:54:12 +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
|
|
|
}
|
|
|
|
if tailedLine.Text != line {
|
2012-10-14 03:54:12 +08:00
|
|
|
t.Fatalf("mismatch; %s != %s", 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
|
|
|
}
|