wal: add a test for wal cleanup, improve comments

To add test coverage of wal cleanup.
This commit is contained in:
Joshua Coutinho 2019-05-10 20:37:08 +01:00
parent f7f7e9c762
commit a0c889d14b
2 changed files with 35 additions and 2 deletions

View File

@ -234,9 +234,9 @@ func (w *WAL) cleanupWAL(lg *zap.Logger) {
var err error
if err = w.Close(); err != nil {
if lg != nil {
lg.Panic("failed to closeup WAL during cleanup", zap.Error(err))
lg.Panic("failed to close WAL during cleanup", zap.Error(err))
} else {
plog.Panicf("failed to closeup WAL during cleanup: %v", err)
plog.Panicf("failed to close WAL during cleanup: %v", err)
}
}
brokenDirName := fmt.Sprintf("%s.broken.%v", w.dir, time.Now().Format("20060102.150405.999999"))

View File

@ -16,6 +16,7 @@ package wal
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"math"
@ -23,6 +24,7 @@ import (
"path"
"path/filepath"
"reflect"
"regexp"
"testing"
"go.etcd.io/etcd/v3/pkg/fileutil"
@ -101,6 +103,37 @@ func TestCreateFailFromPollutedDir(t *testing.T) {
}
}
func TestWalCleanup(t *testing.T) {
testRoot, err := ioutil.TempDir(os.TempDir(), "waltestroot")
if err != nil {
t.Fatal(err)
}
p, err := ioutil.TempDir(testRoot, "waltest")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(testRoot)
logger := zap.NewExample()
w, err := Create(logger, p, []byte(""))
if err != nil {
t.Fatalf("err = %v, want nil", err)
}
w.cleanupWAL(logger)
fnames, err := fileutil.ReadDir(testRoot)
if err != nil {
t.Fatalf("err = %v, want nil", err)
}
if len(fnames) != 1 {
t.Fatalf("expected 1 file under %v, got %v", testRoot, len(fnames))
}
pattern := fmt.Sprintf(`%s.broken\.[\d]{8}\.[\d]{6}\.[\d]{1,6}?`, filepath.Base(p))
match, _ := regexp.MatchString(pattern, fnames[0])
if !match {
t.Errorf("match = false, expected true for %v with pattern %v", fnames[0], pattern)
}
}
func TestCreateFailFromNoSpaceLeft(t *testing.T) {
p, err := ioutil.TempDir(os.TempDir(), "waltest")
if err != nil {