"io/ioutil"
	"os/exec"
	"strconv"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gexec"
	"github.com/pivotal-cf/cf-redis-broker/integration"

	"testing"
)

var (
	redisHost     = "127.0.0.1"
	redisPort     = strconv.Itoa(integration.RedisPort)
	redisPassword = "******"

	pathToBinary string
	redisRunner  *integration.RedisRunner
)

func TestIntegration(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "Integration Suite")
}

var _ = BeforeSuite(func() {
	var err error
	pathToBinary, err = gexec.Build("github.com/pivotal-cf/redis-cpu")
	Expect(err).ToNot(HaveOccurred())

	redisRunner = new(integration.RedisRunner)
	"github.com/pivotal-cf/cf-redis-broker/integration"
	"github.com/pivotal-cf/cf-redis-broker/redis/backup"
	redis "github.com/pivotal-cf/cf-redis-broker/redis/client"
	"github.com/pivotal-golang/lager"
)

func TestIntegration(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "Backup Integration Suite")
}

var (
	s3Server         *s3test.Server
	redisRunner      *integration.RedisRunner
	backupErr        error
	uploadedObject   []byte
	localDumpRdbPath string
	tmpDir           string
)

var _ = BeforeSuite(func() {
	redisHost := "127.0.0.1"
	redisPort := integration.RedisPort

	redisRunner = &integration.RedisRunner{}
	redisRunner.Start([]string{"--bind", redisHost, "--port", fmt.Sprintf("%d", redisPort)})

	localDumpRdbPath = filepath.Join(redisRunner.Dir, "dump.rdb")

	var err error
	s3Server, err = s3test.NewServer(&s3test.Config{
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/pivotal-cf/cf-redis-broker/integration"
	"github.com/pivotal-cf/cf-redis-broker/redis/client"

	redisclient "github.com/garyburd/redigo/redis"
)

var host = "localhost"
var port = 6480
var password = ""
var pidFilePath string

var _ = Describe("Client", func() {
	var redisArgs []string
	var redisRunner *integration.RedisRunner

	BeforeEach(func() {
		pidFile, err := ioutil.TempFile("", "pid")
		Ω(err).ShouldNot(HaveOccurred())
		pidFilePath = pidFile.Name()
		redisArgs = []string{"--port", fmt.Sprintf("%d", port), "--pidfile", pidFilePath}
	})

	AfterEach(func() {
		os.Remove(pidFilePath)
	})

	Describe("connecting to a redis server", func() {
		Context("when the server is not running", func() {
			It("returns an error", func() {