statusCode, body := brokerClient.BindInstance(instanceID, bindingID)
			Ω(statusCode).To(Equal(201))

			var parsedJSON map[string]interface{}
			json.Unmarshal(body, &parsedJSON)

			credentials := parsedJSON["credentials"].(map[string]interface{})
			port = uint(credentials["port"].(float64))
			host = credentials["host"].(string)
			password = credentials["password"].(string)

			client = helpers.BuildRedisClient(port, host, password)
		})

		AfterEach(func() {
			helpers.KillProcess(monitorSession)
			client.Close()
			brokerClient.DeprovisionInstance(instanceID)
		})

		It("is restarted", func() {
			_, err := client.Do("SET", "foo", "bar")
			Ω(err).ShouldNot(HaveOccurred())

			helpers.KillRedisProcess(instanceID, brokerConfig)

			Ω(helpers.ServiceAvailable(port)).Should(BeTrue())

			client = helpers.BuildRedisClient(port, host, password)

			value, err := redisclient.String(client.Do("GET", "foo"))
func relaunchProcessMonitorWithConfig(processMonitorPath, brokerConfigName string) {
	helpers.KillProcess(monitorSession)
	monitorSession = integration.LaunchProcessWithBrokerConfig(processMonitorPath, brokerConfigName)
}
func stopAgent(session *gexec.Session) {
	helpers.KillProcess(session)
	helpers.ResetTestDirs()
}
	"github.com/onsi/gomega/gbytes"
	"github.com/onsi/gomega/gexec"
	"github.com/pivotal-cf/cf-redis-broker/integration"
	"github.com/pivotal-cf/cf-redis-broker/integration/helpers"
)

var _ = Describe("Broker Integration", func() {
	var broker *gexec.Session

	BeforeEach(func() {
		broker = integration.LaunchProcessWithBrokerConfig(brokerExecutablePath, "broker.yml-colocated")
		Eventually(broker.Out).Should(gbytes.Say("shared Redis instance"))
	})

	AfterEach(func() {
		helpers.KillProcess(broker)
	})

	JustBeforeEach(func() {
		helpers.KillProcess(broker)
	})

	Context("when the Redis broker is shutdown", func() {
		It("logs a shutdown preparation message", func() {
			Expect(broker.Out).To(gbytes.Say("Starting Redis Broker shutdown"))
		})

		It("logs that it has identified zero shared instances", func() {
			Expect(broker.Out).To(gbytes.Say("0 shared Redis instances found"))
		})
var _ = Describe("Provision dedicated instance", func() {

	var instanceID string

	BeforeEach(func() {
		instanceID = uuid.NewRandom().String()
		brokerClient.ProvisionInstance(instanceID, "dedicated")
	})

	AfterEach(func() {
		brokerClient.DeprovisionInstance(instanceID)
	})

	Context("when the broker is restarted", func() {
		BeforeEach(func() {
			helpers.KillProcess(brokerSession)
			brokerSession = integration.LaunchProcessWithBrokerConfig(brokerExecutablePath, "broker.yml")
			Ω(helpers.ServiceAvailable(brokerPort)).Should(BeTrue())
		})

		It("retains state", func() {
			debugInfo := getDebugInfo()

			Ω(debugInfo.Allocated.Count).Should(Equal(1))
			Ω(len(debugInfo.Allocated.Clusters)).Should(Equal(1))

			host := debugInfo.Allocated.Clusters[0].Hosts[0]
			Ω(host).Should(MatchRegexp("server[1-3]\\.127\\.0\\.0\\.1\\.xip\\.io"))

			Ω(debugInfo.Pool.Clusters).ShouldNot(ContainElement([]string{host}))
		})
			conf, err := redisconf.Load(redisConfPath)
			Expect(err).ToNot(HaveOccurred())
			return conf
		}

		It("copies a redis.conf from the default path and adds a password", func() {
			conf := loadRedisConfFileWhenItExists()

			Expect(conf.Get("daemonize")).To(Equal("no"))
			Expect(conf.HasKey("requirepass")).To(BeTrue())
		})

		It("creates a new password each time", func() {
			initialConf := loadRedisConfFileWhenItExists()

			helpers.KillProcess(agentSession)
			err := os.Remove(redisConfPath)
			Expect(err).ToNot(HaveOccurred())

			agentSession = startAgent()
			newConf := loadRedisConfFileWhenItExists()
			Expect(initialConf.Get("requirepass")).NotTo(Equal(newConf.Get("requirepass")))
		})
	})

	Context("when there is a redis.conf already", func() {
		BeforeEach(func() {
			err := redisconf.New(
				redisconf.Param{Key: "daemonize", Value: "yes"},
				redisconf.Param{Key: "requirepass", Value: "someotherpassword"},
				redisconf.Param{Key: "shouldbedeleted", Value: "yes"},
func switchBroker(config string) {
	helpers.KillProcess(brokerSession)
	helpers.ResetTestDirs()
	brokerSession = integration.LaunchProcessWithBrokerConfig(brokerExecutablePath, config)
	Ω(helpers.ServiceAvailable(brokerPort)).Should(BeTrue())
}