Using tmpdir and tmpfile in tests

This commit is contained in:
Diptanu Choudhury 2016-01-20 16:17:49 -08:00
parent 6b9dcd06c6
commit 92cc950398
2 changed files with 29 additions and 12 deletions

View File

@ -81,8 +81,9 @@ func (l *LogRotator) Start(r io.Reader) error {
return err return err
} }
l.logger.Printf("[DEBUG] client.logrotator: opened a new file: %s", logFileName) l.logger.Printf("[DEBUG] client.logrotator: opened a new file: %s", logFileName)
// closing the current log file if it doesn't have any more capacity // closing the current log file if it doesn't have any more capacity
if remainingSize < 1 { if remainingSize <= 0 {
l.logFileIdx = l.logFileIdx + 1 l.logFileIdx = l.logFileIdx + 1
f.Close() f.Close()
continue continue

View File

@ -11,8 +11,8 @@ import (
) )
var ( var (
logger = log.New(os.Stdout, "", log.LstdFlags) logger = log.New(os.Stdout, "", log.LstdFlags)
path = "/tmp/logrotator" pathPrefix = "logrotator"
) )
func TestLogRotator_InvalidPath(t *testing.T) { func TestLogRotator_InvalidPath(t *testing.T) {
@ -24,7 +24,9 @@ func TestLogRotator_InvalidPath(t *testing.T) {
} }
func TestLogRotator_FindCorrectIndex(t *testing.T) { func TestLogRotator_FindCorrectIndex(t *testing.T) {
if err := os.Mkdir(path, os.ModeDir|os.ModePerm); err != nil { var path string
var err error
if path, err = ioutil.TempDir("", pathPrefix); err != nil {
t.Fatalf("test setup err: %v", err) t.Fatalf("test setup err: %v", err)
} }
defer os.RemoveAll(path) defer os.RemoveAll(path)
@ -49,8 +51,10 @@ func TestLogRotator_FindCorrectIndex(t *testing.T) {
} }
func TestLogRotator_AppendToCurrentFile(t *testing.T) { func TestLogRotator_AppendToCurrentFile(t *testing.T) {
var path string
var err error
defer os.RemoveAll(path) defer os.RemoveAll(path)
if err := os.Mkdir(path, os.ModeDir|os.ModePerm); err != nil { if path, err = ioutil.TempDir("", pathPrefix); err != nil {
t.Fatalf("test setup err: %v", err) t.Fatalf("test setup err: %v", err)
} }
fname := filepath.Join(path, "redis.stdout.0") fname := filepath.Join(path, "redis.stdout.0")
@ -83,8 +87,10 @@ func TestLogRotator_AppendToCurrentFile(t *testing.T) {
} }
func TestLogRotator_RotateFiles(t *testing.T) { func TestLogRotator_RotateFiles(t *testing.T) {
var path string
var err error
defer os.RemoveAll(path) defer os.RemoveAll(path)
if err := os.Mkdir(path, os.ModeDir|os.ModePerm); err != nil { if path, err = ioutil.TempDir("", pathPrefix); err != nil {
t.Fatalf("test setup err: %v", err) t.Fatalf("test setup err: %v", err)
} }
fname := filepath.Join(path, "redis.stdout.0") fname := filepath.Join(path, "redis.stdout.0")
@ -125,8 +131,10 @@ func TestLogRotator_RotateFiles(t *testing.T) {
} }
func TestLogRotator_StartFromEmptyDir(t *testing.T) { func TestLogRotator_StartFromEmptyDir(t *testing.T) {
var path string
var err error
defer os.RemoveAll(path) defer os.RemoveAll(path)
if err := os.Mkdir(path, os.ModeDir|os.ModePerm); err != nil { if path, err = ioutil.TempDir("", pathPrefix); err != nil {
t.Fatalf("test setup err: %v", err) t.Fatalf("test setup err: %v", err)
} }
@ -156,20 +164,26 @@ func TestLogRotator_StartFromEmptyDir(t *testing.T) {
} }
func TestLogRotator_SetPathAsFile(t *testing.T) { func TestLogRotator_SetPathAsFile(t *testing.T) {
var f *os.File
var err error
var path string
defer os.RemoveAll(path) defer os.RemoveAll(path)
if _, err := os.Create(path); err != nil { if f, err = ioutil.TempFile("", pathPrefix); err != nil {
t.Fatalf("test setup problem: %v", err) t.Fatalf("test setup problem: %v", err)
} }
_, err := NewLogRotator(path, "redis.stdout", 10, 10, logger) path = f.Name()
if err == nil {
if _, err = NewLogRotator(f.Name(), "redis.stdout", 10, 10, logger); err == nil {
t.Fatal("expected error") t.Fatal("expected error")
} }
} }
func TestLogRotator_ExcludeDirs(t *testing.T) { func TestLogRotator_ExcludeDirs(t *testing.T) {
var path string
var err error
defer os.RemoveAll(path) defer os.RemoveAll(path)
if err := os.Mkdir(path, os.ModeDir|os.ModePerm); err != nil { if path, err = ioutil.TempDir("", pathPrefix); err != nil {
t.Fatalf("test setup err: %v", err) t.Fatalf("test setup err: %v", err)
} }
if err := os.Mkdir(filepath.Join(path, "redis.stdout.0"), os.ModeDir|os.ModePerm); err != nil { if err := os.Mkdir(filepath.Join(path, "redis.stdout.0"), os.ModeDir|os.ModePerm); err != nil {
@ -201,8 +215,10 @@ func TestLogRotator_ExcludeDirs(t *testing.T) {
} }
func TestLogRotator_PurgeDirs(t *testing.T) { func TestLogRotator_PurgeDirs(t *testing.T) {
var path string
var err error
defer os.RemoveAll(path) defer os.RemoveAll(path)
if err := os.Mkdir(path, os.ModeDir|os.ModePerm); err != nil { if path, err = ioutil.TempDir("", pathPrefix); err != nil {
t.Fatalf("test setup err: %v", err) t.Fatalf("test setup err: %v", err)
} }