From 30e1b17497205fefab0c8863e81122b1b37f5944 Mon Sep 17 00:00:00 2001 From: Joshua Coutinho Date: Fri, 10 May 2019 20:37:08 +0100 Subject: [PATCH] wal: add a test for wal cleanup, improve comments To add test coverage of wal cleanup. --- wal/wal.go | 4 ++-- wal/wal_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/wal/wal.go b/wal/wal.go index a86a48800278..c01e3696412c 100644 --- a/wal/wal.go +++ b/wal/wal.go @@ -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")) diff --git a/wal/wal_test.go b/wal/wal_test.go index 7ada82d4db0c..f541fc261930 100644 --- a/wal/wal_test.go +++ b/wal/wal_test.go @@ -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]{6}`, filepath.Base(p)) + match, _ := regexp.MatchString(pattern, fnames[0]) + if !match { + t.Fatalf("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 {