Skip to content

Commit

Permalink
fix bgsave to find dump
Browse files Browse the repository at this point in the history
  • Loading branch information
tsinow committed Sep 20, 2024
1 parent 663489e commit 69c5a54
Showing 1 changed file with 47 additions and 44 deletions.
91 changes: 47 additions & 44 deletions tests/integration/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"time"
"os"
"fmt"
"path/filepath"
. "github.com/bsm/ginkgo/v2"
. "github.com/bsm/gomega"
Expand Down Expand Up @@ -172,10 +171,55 @@ var _ = Describe("Server", func() {
lastSaveBefore, err := client.LastSave(ctx).Result()
Expect(err).NotTo(HaveOccurred())

currentDir, err := os.Getwd()
Expect(err).NotTo(HaveOccurred())

var pikaDir string
for currentDir != "/" {
if filepath.Base(currentDir) == "pika" {
pikaDir = currentDir
break
}
currentDir = filepath.Dir(currentDir)
}
Expect(pikaDir).NotTo(BeEmpty(), "Could not find 'pika' directory")

var dumpDirs []string
err = filepath.WalkDir(pikaDir, func(path string, info os.DirEntry, err error) error {
if err != nil {
return err
}
if info.IsDir() && info.Name() == "dump" {
absPath, err := filepath.Abs(path)
if err != nil {
return err
}
dumpDirs = append(dumpDirs, absPath)
}
return nil
})
Expect(err).NotTo(HaveOccurred(), "Error while searching for 'dump' directories")
Expect(len(dumpDirs)).To(BeNumerically(">", 0), "No 'dump' directories found in 'pika'")

shortestDumpDir := dumpDirs[0]
for _, dir := range dumpDirs {
if len(dir) < len(shortestDumpDir) {
shortestDumpDir = dir
}
}

dumpConfig := client.ConfigGet(ctx, "dump-path")
Expect(dumpConfig.Err()).NotTo(HaveOccurred())
originalDumpPath, ok := dumpConfig.Val()["dump-path"]
Expect(ok).To(BeTrue())

dumpPath := filepath.Join(filepath.Dir(shortestDumpDir), originalDumpPath)

res2, err2 := client.BgSave(ctx).Result()
Expect(err2).NotTo(HaveOccurred())
Expect(res.Err()).NotTo(HaveOccurred())
Expect(res2).To(ContainSubstring("Background saving started"))

startTime := time.Now()
maxWaitTime := 10 * time.Second

Expand All @@ -194,51 +238,10 @@ var _ = Describe("Server", func() {
time.Sleep(500 * time.Millisecond)
}

dumpConfig := client.ConfigGet(ctx, "dump-path")
Expect(dumpConfig.Err()).NotTo(HaveOccurred())
dumpPath, ok := dumpConfig.Val()["dump-path"]
Expect(ok).To(BeTrue())

// Convert dumpPath to absolute path
baseDir := "/home/runner/work/pika/"
absDumpPath := filepath.Join(baseDir, dumpPath)

// Check if dumpPath exists and is a directory
info, err := os.Stat(absDumpPath)
if os.IsNotExist(err) {
// If dumpPath does not exist, search for 'dump' directories
fmt.Println("Dump path does not exist. Searching for 'dump' directories...")

err = filepath.WalkDir(baseDir, func(path string, info os.DirEntry, err error) error {
if err != nil {
return err
}

if info.IsDir() && info.Name() == "dump" {
absPath, err := filepath.Abs(path)
if err != nil {
return err
}
fmt.Println("Found dump directory:", absPath)
}

return nil
})

if err != nil {
Fail("Error while searching for dump directories: " + err.Error())
}
} else if err != nil {
Fail("Error accessing dump path: " + err.Error())
} else {
Expect(info.IsDir()).To(BeTrue(), "dump path should be a directory")
}
files, err := os.ReadDir(dumpPath)
Expect(err).NotTo(HaveOccurred())
//need to delete test bgsave file?

Expect(len(files)).To(BeNumerically(">", 0), "dump directory is empty, bgsave create file failed")

Expect(err).NotTo(HaveOccurred(), "Failed to read dump_path")
Expect(len(files)).To(BeNumerically(">", 0), "dump_tmp directory is empty, BGSAVE failed to create a file")
})

It("should FlushDb", func() {
Expand Down

0 comments on commit 69c5a54

Please sign in to comment.