func startRedisSession(config restoreconfig.Config, instanceID, planName string) (redisSession *gexec.Session) { var testInstanceDir string testDataDir := config.InstanceDataDir(instanceID) if planName == dedicatedPlan { testInstanceDir = testDataDir os.RemoveAll(testDataDir) } else { testInstanceDir = filepath.Join(config.RedisDataDirectory, instanceID) os.RemoveAll(testInstanceDir) } os.MkdirAll(testDataDir, 0777) err := ioutil.WriteFile( filepath.Join(testInstanceDir, "redis.conf"), []byte("port 6379"), os.ModePerm, ) Expect(err).ToNot(HaveOccurred()) pidfilePath := config.InstancePidFilePath(instanceID) err = os.MkdirAll(config.PidfileDirectory, 0777) Expect(err).NotTo(HaveOccurred()) redisCmd := exec.Command("redis-server", "--dir", testInstanceDir, "--save", "900", "1", "--pidfile", pidfilePath, "--daemonize", "yes", ) redisSession, err = gexec.Start(redisCmd, GinkgoWriter, GinkgoWriter) Expect(err).ToNot(HaveOccurred()) pidFileWritten := make(chan bool) go func(c chan<- bool) { for { if _, err := os.Stat(pidfilePath); !os.IsNotExist(err) { c <- true break } time.Sleep(50 * time.Millisecond) } }(pidFileWritten) // wait for redis to write pid file select { case <-pidFileWritten: break case <-time.After(30 * time.Second): Fail("Test timed out waiting for redis to write PID file.") } return redisSession }
It("stops and then starts the process-watcher", func() { session, err := gexec.Start(restoreCommand, GinkgoWriter, GinkgoWriter) Expect(err).NotTo(HaveOccurred()) Eventually(session, "20s").Should(gexec.Exit(0)) monitLogBytes, err := ioutil.ReadFile(monitLogFile) Expect(err).ToNot(HaveOccurred()) Expect(string(monitLogBytes)).To(ContainSubstring("stopping process-watcher")) Expect(string(monitLogBytes)).To(ContainSubstring("starting process-watcher")) }) It("creates a new RDB file in the instance directory", func() { newRdbPath := filepath.Join(config.InstanceDataDir(instanceID), "dump.rdb") _, err := os.Stat(newRdbPath) Expect(os.IsNotExist(err)).To(BeTrue()) session, err := gexec.Start(restoreCommand, GinkgoWriter, GinkgoWriter) Expect(err).NotTo(HaveOccurred()) Eventually(session, "20s").Should(gexec.Exit(0)) copiedFileContents, err := ioutil.ReadFile(newRdbPath) Expect(err).NotTo(HaveOccurred()) sourceFileContents, err := ioutil.ReadFile(sourceRdbPath) Expect(err).NotTo(HaveOccurred()) Expect(copiedFileContents).To(Equal(sourceFileContents))
It("Returns instance PID", func() { Expect(restoreConfig.InstancePid("instance-id")).To(Equal(1234)) }) }) Describe("#InstancePidFilePath", func() { It("Returns the instance PID file path", func() { expected := "/tmp/pidfiles/instance-id.pid" Expect(restoreConfig.InstancePidFilePath("instance-id")).To(Equal(expected)) }) }) Describe("#InstanceDataDir", func() { It("Returns the instance data directory", func() { Expect(restoreConfig.InstanceDataDir("instance-id")).To(Equal("/tmp/redis/data/instance-id/db")) }) }) }) Context("Dedicated vm", func() { BeforeEach(func() { var err error restoreConfig, err = restoreconfig.Load(path.Join("assets", "restore-dedicated.yml")) Expect(err).ToNot(HaveOccurred()) }) Describe("#InstancePid", func() { var pidfilePath string BeforeEach(func() {