<commit_before>package backupintegration_test

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
	"os/exec"
	"path"
	"path/filepath"
	"strconv"
	"time"

	"code.google.com/p/go-uuid/uuid"

	"github.com/cloudfoundry-incubator/candiedyaml"
	"github.com/mitchellh/goamz/aws"
	"github.com/mitchellh/goamz/s3"
	"github.com/mitchellh/goamz/s3/s3test"
	"github.com/onsi/gomega/gexec"
	"github.com/pivotal-cf/cf-redis-broker/backupconfig"
	"github.com/pivotal-cf/cf-redis-broker/integration"
	"github.com/pivotal-cf/cf-redis-broker/integration/helpers"
	"github.com/pivotal-cf/cf-redis-broker/redis/client"
	"github.com/pivotal-cf/cf-redis-broker/redisconf"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("backups", func() {
	Context("when the backup configuration is empty", func() {
		It("exits with status code 0", func() {
			backupSession := runBackupWithConfig(backupExecutablePath, filepath.Join("assets", "empty-backup.yml"))
			backupExitStatusCode := backupSession.Wait(time.Second * 10).ExitCode()
			Ω(backupExitStatusCode).Should(Equal(0))
		})
	})

	Context("when the configuration is not empty", func() {
		var (
			backupConfigPath string
			backupConfig     *backupconfig.Config

			client         *s3.S3
			bucket         *s3.Bucket
			oldS3ServerURL string
			s3TestServer   *s3test.Server
		)

		BeforeEach(func() {
			s3TestServer = startS3TestServer()
		})

		JustBeforeEach(func() {
			err := bucket.PutBucket("")
			Ω(err).ShouldNot(HaveOccurred())
		})

		AfterEach(func() {
			swapS3UrlInBackupConfig(backupConfig, backupConfigPath, oldS3ServerURL)
		})

		Context("when its a dedicated instance to back up", func() {
			var redisRunner *integration.RedisRunner
			var confPath string
			var instanceID string

			BeforeEach(func() {
				backupConfigPath = filepath.Join("assets", "backup-dedicated.yml")
				backupConfig = loadBackupConfig(backupConfigPath)
				oldS3ServerURL = swapS3UrlInBackupConfig(backupConfig, backupConfigPath, s3TestServer.URL())
				client, bucket = configureS3ClientAndBucket(backupConfig)

				instanceID = uuid.NewRandom().String()
				confPath = filepath.Join(brokerConfig.RedisConfiguration.InstanceDataDirectory, "redis.conf")
				assetPath, _ := helpers.AssetPath("redis-dedicated.conf")
				redisConfContents, _ := ioutil.ReadFile(assetPath)
				ioutil.WriteFile(confPath, redisConfContents, 0777)
				redisRunner = &integration.RedisRunner{}
				redisRunner.Start([]string{confPath, "--port", "6480"})

				status, _ := brokerClient.ProvisionInstance(instanceID, "dedicated")
				Ω(status).To(Equal(http.StatusCreated))
				bindAndWriteTestData(instanceID)
			})

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

			It("causes redis to SAVE/BGSAVE before backing up", func() {
				lastSaveTime := getLastSaveTime(instanceID, confPath)

				backupSession := runBackupWithConfig(backupExecutablePath, backupConfigPath)
				backupExitStatusCode := backupSession.Wait(time.Second * 10).ExitCode()
				Expect(backupExitStatusCode).To(Equal(0))

				Expect(getLastSaveTime(instanceID, confPath)).To(BeNumerically(">", lastSaveTime))
			})

			It("uploads redis instance RDB file to the correct S3 bucket", func() {
				timestamp := getCurrentTimestamp()
				backupSession := runBackupWithConfig(backupExecutablePath, backupConfigPath)

				backupSession.Wait(time.Second * 10).ExitCode()
				retrievedBackupBytes, err := bucket.Get(backupFilename(backupConfig.S3Configuration.Path, timestamp, backupConfig.NodeID, "dedicated-vm"))
				Ω(err).NotTo(HaveOccurred())
				originalData, _ := ioutil.ReadFile(path.Join(backupConfig.RedisDataDirectory, "dump.rdb"))
				Ω(retrievedBackupBytes).To(Equal(originalData))
			})

			Context("when an instance backup fails", func() {
				It("returns non-zero exit code", func() {<commit_msg>backfill missing backup test for dedicated-vm plan<commit_after>package backupintegration_test

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
	"os/exec"
	"path"
	"path/filepath"
	"strconv"
	"time"

	"code.google.com/p/go-uuid/uuid"

	"github.com/cloudfoundry-incubator/candiedyaml"
	"github.com/mitchellh/goamz/aws"
	"github.com/mitchellh/goamz/s3"
	"github.com/mitchellh/goamz/s3/s3test"
	"github.com/onsi/gomega/gexec"
	"github.com/pivotal-cf/cf-redis-broker/backupconfig"
	"github.com/pivotal-cf/cf-redis-broker/integration"
	"github.com/pivotal-cf/cf-redis-broker/integration/helpers"
	"github.com/pivotal-cf/cf-redis-broker/redis/client"
	"github.com/pivotal-cf/cf-redis-broker/redisconf"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("backups", func() {
	Context("when the backup configuration is empty", func() {
		It("exits with status code 0", func() {
			backupSession := runBackupWithConfig(backupExecutablePath, filepath.Join("assets", "empty-backup.yml"))
			backupExitStatusCode := backupSession.Wait(time.Second * 10).ExitCode()
			Ω(backupExitStatusCode).Should(Equal(0))
		})
	})

	Context("when the configuration is not empty", func() {
		var (
			backupConfigPath string
			backupConfig     *backupconfig.Config

			client         *s3.S3
			bucket         *s3.Bucket
			oldS3ServerURL string
			s3TestServer   *s3test.Server
		)

		BeforeEach(func() {
			s3TestServer = startS3TestServer()
		})

		JustBeforeEach(func() {
			err := bucket.PutBucket("")
			Ω(err).ShouldNot(HaveOccurred())
		})

		AfterEach(func() {
			swapS3UrlInBackupConfig(backupConfig, backupConfigPath, oldS3ServerURL)
		})

		Context("when its a dedicated instance to back up", func() {
			var redisRunner *integration.RedisRunner
			var confPath string
			var instanceID string

			BeforeEach(func() {
				backupConfigPath = filepath.Join("assets", "backup-dedicated.yml")
				backupConfig = loadBackupConfig(backupConfigPath)
				oldS3ServerURL = swapS3UrlInBackupConfig(backupConfig, backupConfigPath, s3TestServer.URL())
				client, bucket = configureS3ClientAndBucket(backupConfig)

				instanceID = uuid.NewRandom().String()
				confPath = filepath.Join(brokerConfig.RedisConfiguration.InstanceDataDirectory, "redis.conf")
				assetPath, _ := helpers.AssetPath("redis-dedicated.conf")
				redisConfContents, _ := ioutil.ReadFile(assetPath)
				ioutil.WriteFile(confPath, redisConfContents, 0777)
				redisRunner = &integration.RedisRunner{}
				redisRunner.Start([]string{confPath, "--port", "6480"})

				status, _ := brokerClient.ProvisionInstance(instanceID, "dedicated")
				Ω(status).To(Equal(http.StatusCreated))
				bindAndWriteTestData(instanceID)
			})

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

			It("causes redis to SAVE/BGSAVE before backing up", func() {
				lastSaveTime := getLastSaveTime(instanceID, confPath)

				backupSession := runBackupWithConfig(backupExecutablePath, backupConfigPath)
				backupExitStatusCode := backupSession.Wait(time.Second * 10).ExitCode()
				Expect(backupExitStatusCode).To(Equal(0))

				Expect(getLastSaveTime(instanceID, confPath)).To(BeNumerically(">", lastSaveTime))
			})

			It("uploads redis instance RDB file to the correct S3 bucket", func() {
				timestamp := getCurrentTimestamp()
				runBackupWithConfig(backupExecutablePath, backupConfigPath).Wait(time.Second * 10)

				retrievedBackupBytes, err := bucket.Get(backupFilename(backupConfig.S3Configuration.Path, timestamp, backupConfig.NodeID, "dedicated-vm"))
				Ω(err).NotTo(HaveOccurred())
				originalData, _ := ioutil.ReadFile(path.Join(backupConfig.RedisDataDirectory, "dump.rdb"))
				Ω(retrievedBackupBytes).To(Equal(originalData))
			})

			It("creates the bucket if it does not exist and uploads a file for each instance", func() {
				err := bucket.DelBucket()
				Ω(err).NotTo(HaveOccurred())

				timestamp := getCurrentTimestamp()
				runBackupWithConfig(backupExecutablePath, backupConfigPath).Wait(time.Second * 10)

				retrievedBackupBytes, err := bucket.Get(backupFilename(backupConfig.S3Configuration.Path, timestamp, backupConfig.NodeID, "dedicated-vm"))
				Ω(err).NotTo(HaveOccurred())
				Ω(retrievedBackupBytes).ShouldNot(BeEmpty())
			})

			Context("when an instance backup fails", func() {
				It("returns non-zero exit code", func() {
{'fraction_non_alphanumeric': 0.1306508302023997, 'fraction_numerical': 0.009089807296085323, 'mean_word_length': 4.754532775453278, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6590671', 'n_tokens_mistral': 3131, 'n_tokens_neox': 2761, 'n_words': 432}