open-nomad/client/driver/logs_test.go

249 lines
5.5 KiB
Go
Raw Normal View History

2016-01-14 01:16:25 +00:00
package driver
import (
"io"
2016-01-15 06:36:55 +00:00
"io/ioutil"
2016-01-15 19:18:02 +00:00
"log"
2016-01-14 01:16:25 +00:00
"os"
"path/filepath"
"testing"
)
2016-01-15 19:18:02 +00:00
var (
2016-01-21 00:17:49 +00:00
logger = log.New(os.Stdout, "", log.LstdFlags)
pathPrefix = "logrotator"
2016-01-15 19:18:02 +00:00
)
2016-01-20 23:55:41 +00:00
func TestLogRotator_InvalidPath(t *testing.T) {
invalidPath := "/foo"
2016-01-14 01:16:25 +00:00
2016-01-20 23:55:41 +00:00
if _, err := NewLogRotator(invalidPath, "redis.stdout", 10, 10, logger); err == nil {
2016-01-14 01:16:25 +00:00
t.Fatal("expected err")
}
}
func TestLogRotator_FindCorrectIndex(t *testing.T) {
2016-01-21 00:17:49 +00:00
var path string
var err error
if path, err = ioutil.TempDir("", pathPrefix); err != nil {
2016-01-14 01:16:25 +00:00
t.Fatalf("test setup err: %v", err)
}
2016-01-14 19:41:31 +00:00
defer os.RemoveAll(path)
2016-01-14 01:16:25 +00:00
fname := filepath.Join(path, "redis.stdout.1")
if f, err := os.Create(fname); err == nil {
f.Close()
}
fname = filepath.Join(path, "redis.stdout.2")
if f, err := os.Create(fname); err == nil {
f.Close()
}
2016-01-15 19:18:02 +00:00
r, err := NewLogRotator(path, "redis.stdout", 10, 10, logger)
2016-01-14 01:16:25 +00:00
if err != nil {
t.Fatalf("test setup err: %v", err)
}
if r.logFileIdx != 2 {
t.Fatalf("Expected log file idx: %v, actual: %v", 2, r.logFileIdx)
}
}
2016-01-14 19:41:31 +00:00
func TestLogRotator_AppendToCurrentFile(t *testing.T) {
2016-01-21 00:17:49 +00:00
var path string
var err error
defer os.RemoveAll(path)
2016-01-21 00:17:49 +00:00
if path, err = ioutil.TempDir("", pathPrefix); err != nil {
2016-01-14 19:41:31 +00:00
t.Fatalf("test setup err: %v", err)
}
fname := filepath.Join(path, "redis.stdout.0")
2016-01-14 19:41:31 +00:00
if f, err := os.Create(fname); err == nil {
f.WriteString("abcde")
2016-01-14 19:41:31 +00:00
f.Close()
}
2016-01-15 19:18:02 +00:00
l, err := NewLogRotator(path, "redis.stdout", 10, 6, logger)
2016-01-15 06:36:55 +00:00
if err != nil && err != io.EOF {
2016-01-14 19:41:31 +00:00
t.Fatalf("test setup err: %v", err)
}
r, w := io.Pipe()
go func() {
w.Write([]byte("fg"))
w.Close()
}()
err = l.Start(r)
2016-01-15 06:36:55 +00:00
if err != nil && err != io.EOF {
t.Fatal(err)
}
finfo, err := os.Stat(fname)
if err != nil {
t.Fatal(err)
}
if finfo.Size() != 6 {
t.Fatalf("Expected size of file: %v, actual: %v", 6, finfo.Size())
}
2016-01-14 19:41:31 +00:00
}
2016-01-15 06:36:55 +00:00
func TestLogRotator_RotateFiles(t *testing.T) {
2016-01-21 00:17:49 +00:00
var path string
var err error
2016-01-15 06:36:55 +00:00
defer os.RemoveAll(path)
2016-01-21 00:17:49 +00:00
if path, err = ioutil.TempDir("", pathPrefix); err != nil {
2016-01-15 06:36:55 +00:00
t.Fatalf("test setup err: %v", err)
}
fname := filepath.Join(path, "redis.stdout.0")
if f, err := os.Create(fname); err == nil {
f.WriteString("abcde")
f.Close()
}
2016-01-15 19:18:02 +00:00
l, err := NewLogRotator(path, "redis.stdout", 10, 6, logger)
2016-01-15 06:36:55 +00:00
if err != nil {
t.Fatalf("test setup err: %v", err)
}
r, w := io.Pipe()
go func() {
2016-01-21 00:30:55 +00:00
// This should make the current log file rotate
2016-01-15 06:36:55 +00:00
w.Write([]byte("fg"))
w.Close()
}()
err = l.Start(r)
if err != nil && err != io.EOF {
t.Fatalf("Failure in logrotator start %v", err)
}
2016-01-21 00:25:51 +00:00
if finfo, err := os.Stat(filepath.Join(path, "redis.stdout.1")); err == nil {
if finfo.Size() != 1 {
t.Fatalf("expected number of bytes: %v, actual: %v", 1, finfo.Size())
2016-01-15 06:36:55 +00:00
}
2016-01-21 00:25:51 +00:00
} else {
t.Fatal("expected file redis.stdout.1")
2016-01-15 06:36:55 +00:00
}
2016-01-21 00:25:51 +00:00
if finfo, err := os.Stat(filepath.Join(path, "redis.stdout.0")); err == nil {
if finfo.Size() != 6 {
t.Fatalf("expected number of bytes: %v, actual: %v", 1, finfo.Size())
}
} else {
t.Fatal("expected file redis.stdout.0")
2016-01-15 06:36:55 +00:00
}
}
func TestLogRotator_StartFromEmptyDir(t *testing.T) {
2016-01-21 00:17:49 +00:00
var path string
var err error
defer os.RemoveAll(path)
2016-01-21 00:17:49 +00:00
if path, err = ioutil.TempDir("", pathPrefix); err != nil {
t.Fatalf("test setup err: %v", err)
}
2016-01-15 19:18:02 +00:00
l, err := NewLogRotator(path, "redis.stdout", 10, 10, logger)
if err != nil {
t.Fatalf("test setup err: %v", err)
}
r, w := io.Pipe()
go func() {
w.Write([]byte("abcdefg"))
w.Close()
}()
err = l.Start(r)
if err != nil && err != io.EOF {
t.Fatalf("Failure in logrotator start %v", err)
}
finfo, err := os.Stat(filepath.Join(path, "redis.stdout.0"))
if err != nil {
t.Fatal(err)
}
if finfo.Size() != 7 {
t.Fatalf("expected size of file: %v, actual: %v", 7, finfo.Size())
}
}
func TestLogRotator_SetPathAsFile(t *testing.T) {
2016-01-21 00:17:49 +00:00
var f *os.File
var err error
var path string
defer os.RemoveAll(path)
2016-01-21 00:17:49 +00:00
if f, err = ioutil.TempFile("", pathPrefix); err != nil {
t.Fatalf("test setup problem: %v", err)
}
2016-01-21 00:17:49 +00:00
path = f.Name()
if _, err = NewLogRotator(f.Name(), "redis.stdout", 10, 10, logger); err == nil {
t.Fatal("expected error")
}
}
func TestLogRotator_ExcludeDirs(t *testing.T) {
2016-01-21 00:17:49 +00:00
var path string
var err error
defer os.RemoveAll(path)
2016-01-21 00:17:49 +00:00
if path, err = ioutil.TempDir("", pathPrefix); err != nil {
t.Fatalf("test setup err: %v", err)
}
if err := os.Mkdir(filepath.Join(path, "redis.stdout.0"), os.ModeDir|os.ModePerm); err != nil {
t.Fatalf("test setup err: %v", err)
}
2016-01-15 19:18:02 +00:00
l, err := NewLogRotator(path, "redis.stdout", 10, 6, logger)
if err != nil {
t.Fatalf("test setup err: %v", err)
}
r, w := io.Pipe()
go func() {
w.Write([]byte("fg"))
w.Close()
}()
err = l.Start(r)
if err != nil && err != io.EOF {
t.Fatalf("Failure in logrotator start %v", err)
}
2016-01-17 03:19:52 +00:00
finfo, err := os.Stat(filepath.Join(path, "redis.stdout.1"))
if err != nil {
t.Fatal("expected rotator to create redis.stdout.1")
}
if finfo.Size() != 2 {
t.Fatalf("expected size: %v, actual: %v", 2, finfo.Size())
}
}
2016-01-17 03:19:52 +00:00
func TestLogRotator_PurgeDirs(t *testing.T) {
2016-01-21 00:17:49 +00:00
var path string
var err error
2016-01-17 03:19:52 +00:00
defer os.RemoveAll(path)
2016-01-21 00:17:49 +00:00
if path, err = ioutil.TempDir("", pathPrefix); err != nil {
2016-01-17 03:19:52 +00:00
t.Fatalf("test setup err: %v", err)
}
l, err := NewLogRotator(path, "redis.stdout", 2, 4, logger)
if err != nil {
t.Fatalf("test setup err: %v", err)
}
r, w := io.Pipe()
go func() {
w.Write([]byte("abcdefghijklmno"))
w.Close()
}()
err = l.Start(r)
if err != nil && err != io.EOF {
t.Fatalf("failure in logrotator start: %v", err)
}
l.PurgeOldFiles()
files, err := ioutil.ReadDir(path)
if err != nil {
t.Fatalf("err: %v", err)
}
if len(files) != 2 {
t.Fatalf("expected number of files: %v, actual: %v", 2, len(files))
}
}