Skip to content

Commit

Permalink
Rewrite the test of the log cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
torwig committed Nov 17, 2024
1 parent 433ab2c commit f332596
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 26 deletions.
63 changes: 37 additions & 26 deletions tests/gocase/unit/log/logclean_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,47 +21,58 @@ package log

import (
"os"
"strings"
"testing"
"time"

"github.com/apache/kvrocks/tests/gocase/util"
"github.com/stretchr/testify/require"
)

func TestLogClean(t *testing.T) {
const infoLogFileNamePart = ".INFO."

func TestInfoLogClean(t *testing.T) {
logDir := "/tmp/kvrocks/logfile"
require.NoError(t, os.RemoveAll(logDir))
require.NoError(t, os.MkdirAll(logDir, os.ModePerm))

srv := util.StartServer(t, map[string]string{
"log-dir": logDir,
"log-retention-days": "0",
"log-retention-days": "-1",
})
srv.CloseWithoutCleanup()

logInfoFiles := getInfoLogFilesInDir(t, logDir)
require.NotEmpty(t, logInfoFiles)

srv = util.StartServer(t, map[string]string{
"log-dir": logDir,
"log-retention-days": "0", // all previous INFO level logs should be immediately removed
})
defer srv.Close()

files1, err := os.ReadDir(logDir)
logInfoFiles = getInfoLogFilesInDir(t, logDir)
require.Empty(t, logInfoFiles) // the log directory doesn't contain any INFO level log files
}

func getInfoLogFilesInDir(t *testing.T, dir string) []os.DirEntry {
t.Helper()

files, err := os.ReadDir(dir)
require.NoError(t, err)
if len(files1) == 0 {
return
}
require.Eventually(t, func() bool {
srv.Restart()

files2, err := os.ReadDir(logDir)
require.NoError(t, err)
for _, f1 := range files1 {
fileExists := false
for _, f2 := range files2 {
if f1.Name() == f2.Name() {
fileExists = true
break
}
}
// If the file does not exist, it means the file has been cleaned
if !fileExists {
return true
}

infoLogFiles := make([]os.DirEntry, 0, len(files))

for _, f := range files {
if !f.Type().IsRegular() {
continue
}

if !strings.Contains(f.Name(), infoLogFileNamePart) {
continue
}
return false
}, 10*time.Second, 200*time.Millisecond)

infoLogFiles = append(infoLogFiles, f)
}

return infoLogFiles
}
4 changes: 4 additions & 0 deletions tests/gocase/util/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ func (s *KvrocksServer) Close() {
s.close(false)
}

func (s *KvrocksServer) CloseWithoutCleanup() {
s.close(true)
}

func (s *KvrocksServer) close(keepDir bool) {
require.NoError(s.t, s.cmd.Process.Signal(syscall.SIGTERM))
f := func(err error) { require.NoError(s.t, err) }
Expand Down

0 comments on commit f332596

Please sign in to comment.