From 69c5a542262ea3f6260d52f1d0a5459441ce266f Mon Sep 17 00:00:00 2001 From: tsinow Date: Fri, 20 Sep 2024 13:54:40 +0800 Subject: [PATCH] fix bgsave to find dump --- tests/integration/server_test.go | 91 +++++++++++++++++--------------- 1 file changed, 47 insertions(+), 44 deletions(-) diff --git a/tests/integration/server_test.go b/tests/integration/server_test.go index ba2669749..851982e4b 100644 --- a/tests/integration/server_test.go +++ b/tests/integration/server_test.go @@ -4,7 +4,6 @@ import ( "context" "time" "os" - "fmt" "path/filepath" . "github.com/bsm/ginkgo/v2" . "github.com/bsm/gomega" @@ -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 @@ -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() {