From 468de49504009d7d4d0cddc24974bbb05697efbd Mon Sep 17 00:00:00 2001 From: vinllen Date: Wed, 9 Oct 2019 21:25:24 +0800 Subject: [PATCH] add integration dir --- src/integration-test/deploy/deploy.go | 73 ++++++++++++++++++++------- 1 file changed, 56 insertions(+), 17 deletions(-) diff --git a/src/integration-test/deploy/deploy.go b/src/integration-test/deploy/deploy.go index 2e0eed8..0577dcb 100644 --- a/src/integration-test/deploy/deploy.go +++ b/src/integration-test/deploy/deploy.go @@ -12,8 +12,36 @@ import ( const ( StandaloneScript = "deploy_standalone.sh" ClusterScript = "deploy_cluster.sh" + + RedisShake = "redis-shake" + RedisShakeConf = "redis-shake.conf" ) +func run(cmd *exec.Cmd) error { + if err := cmd.Start(); err != nil { + return fmt.Errorf("start failed[%v]", err) + } + + if err := cmd.Wait(); err != nil { + if exiterr, ok := err.(*exec.ExitError); ok { + // The program has exited with an exit code != 0 + + // This works on both Unix and Windows. Although package + // syscall is generally platform dependent, WaitStatus is + // defined for both Unix and Windows and in both cases has + // an ExitStatus() method with the same signature. + if status, ok := exiterr.Sys().(syscall.WaitStatus); ok { + if retCode := status.ExitStatus(); retCode != 0 { + return fmt.Errorf("run with exit code[%v]", retCode) + } + } + } else { + return fmt.Errorf("wait failed[%s]", err) + } + } + return nil +} + /* * tp: standalone, cluster * port: redis-server port @@ -41,27 +69,38 @@ func Deploy(tp string, port int, cmd string, node int) error { execCmd = exec.Command(path, portS, cmd, strconv.Itoa(node)) } - if err := execCmd.Start(); err != nil { - return fmt.Errorf("start failed[%v]", err) + return run(execCmd) +} + +// start redis-shake with given configuration, mode means sync/rump/dump/restore/decode +func StartShake(shakeDir, runDir string, conf map[string]interface{}, mode string) error { + if _, err := os.Stat(runDir); os.IsNotExist(err) { + if err := os.Mkdir(runDir, os.ModePerm); err != nil { + return fmt.Errorf("mkdir %v failed[%v]", runDir, err) + } } - if err := execCmd.Wait(); err != nil { - if exiterr, ok := err.(*exec.ExitError); ok { - // The program has exited with an exit code != 0 + from := fmt.Sprintf("%s/%s", shakeDir, RedisShake) + to := fmt.Sprintf("%s/%s", runDir, RedisShake) + cpCmd := exec.Command("cp", from, to) + if err := run(cpCmd); err != nil { + return fmt.Errorf("copy file from [%v] to [%v] failed[%v]", from, to, err) + } - // This works on both Unix and Windows. Although package - // syscall is generally platform dependent, WaitStatus is - // defined for both Unix and Windows and in both cases has - // an ExitStatus() method with the same signature. - if status, ok := exiterr.Sys().(syscall.WaitStatus); ok { - if retCode := status.ExitStatus(); retCode != 0 { - return fmt.Errorf("run with exit code[%v]", retCode) - } - } - } else { - return fmt.Errorf("wait failed[%s]", err) + f, err := os.Create(RedisShakeConf) + if err != nil { + return err + } + + // write conf + for key, val := range conf { + _, err := f.WriteString(fmt.Sprintf("%v = %v\n", key, val)) + if err != nil { + return err } } - return nil + // start redis-shake + execCmd := exec.Command(to, fmt.Sprintf("-type=%s", mode), fmt.Sprintf("-conf=%s", RedisShakeConf), "&") + return run(execCmd) } \ No newline at end of file