Example #1
0
	"io"
	"io/ioutil"
	"net/http"
	"path"

	"github.com/cloudfoundry-community/go-cfenv"
	"github.com/pivotalservices/cfbackup"
	"github.com/pivotalservices/gtils/command"
	ghttp "github.com/pivotalservices/gtils/http"
	"github.com/pivotalservices/gtils/log"
	"github.com/xchapter7x/lo"
)

// NewOpsManager initializes an OpsManager instance
var NewOpsManager = func(opsManagerHostname string, adminUsername string, adminPassword string, opsManagerUsername string, opsManagerPassword string, target string) (context *OpsManager, err error) {
	backupContext := cfbackup.NewBackupContext(target, cfenv.CurrentEnv())
	settingsHTTPRequestor := ghttp.NewHttpGateway()
	settingsMultiHTTPRequestor := httpUploader(cfbackup.GetUploader(backupContext))
	assetsHTTPRequestor := ghttp.NewHttpGateway()
	assetsMultiHTTPRequestor := httpUploader(cfbackup.GetUploader(backupContext))

	context = &OpsManager{
		SettingsUploader:    settingsMultiHTTPRequestor,
		AssetsUploader:      assetsMultiHTTPRequestor,
		SettingsRequestor:   settingsHTTPRequestor,
		AssetsRequestor:     assetsHTTPRequestor,
		DeploymentDir:       path.Join(target, OpsMgrBackupDir, OpsMgrDeploymentsDir),
		Hostname:            opsManagerHostname,
		Username:            adminUsername,
		Password:            adminPassword,
		BackupContext:       backupContext,
Example #2
0
//NewFakeBackupContext --
func NewFakeBackupContext(target string, env map[string]string, storageProvider cfbackup.StorageProvider) (backupContext cfbackup.BackupContext) {
	backupContext = cfbackup.NewBackupContext(target, env)
	backupContext.StorageProvider = storageProvider
	return
}
func testERWithVersionSpecificFile(installationSettingsFilePath string) {

	Describe("Backup / Restore", func() {

		oldNewDirector := cfbackup.NewDirector

		BeforeEach(func() {
			oldNewDirector = cfbackup.NewDirector
			cfbackup.NewDirector = fakes.NewFakeDirector
		})

		AfterEach(func() {
			cfbackup.NewDirector = oldNewDirector
		})
		Context("with valid properties (DirectorInfo)", func() {
			var (
				product   = cfbackup.BoshName()
				component = "director"
				username  = "******"
				target    string
				er        ElasticRuntime
				info      = cfbackup.SystemsInfo{
					SystemDumps: map[string]cfbackup.SystemDump{
						"DirectorInfo": &cfbackup.SystemInfo{
							Product:   product,
							Component: component,
							Identity:  username,
						},
						"ConsoledbInfo": &PgInfoMock{
							SystemInfo: cfbackup.SystemInfo{
								Product:   product,
								Component: component,
								Identity:  username,
							},
						},
					},
				}
				ps = []cfbackup.SystemDump{info.SystemDumps["ConsoledbInfo"]}
			)

			BeforeEach(func() {
				target, _ = ioutil.TempDir("/tmp", "spec")
				er = ElasticRuntime{
					JSONFile:          installationSettingsFilePath,
					HTTPGateway:       &fakes.MockHTTPGateway{},
					BackupContext:     cfbackup.NewBackupContext(target, cfenv.CurrentEnv()),
					SystemsInfo:       info,
					PersistentSystems: ps,
				}
			})

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

			Context("With valid list of stores", func() {
				Context("Backup", func() {
					It("Should return nil error", func() {
						err := er.Backup()
						Ω(err).Should(BeNil())
					})
				})

				Context("Restore", func() {
					var filename = fmt.Sprintf("%s.backup", component)

					BeforeEach(func() {
						file, _ := os.Create(path.Join(target, filename))
						file.Close()
					})

					AfterEach(func() {
						os.Remove(path.Join(target, filename))
					})

					It("Should return nil error ", func() {
						err := er.Restore()
						Ω(err).Should(BeNil())
					})
				})
			})

			Context("With empty list of stores", func() {
				var psOrig []cfbackup.SystemDump
				BeforeEach(func() {
					psOrig = ps
					er.PersistentSystems = []cfbackup.SystemDump{}
				})

				AfterEach(func() {
					er.PersistentSystems = psOrig
				})
				Context("Backup", func() {
					It("Should return error on empty list of persistence stores", func() {
						err := er.Backup()
						Ω(err).ShouldNot(BeNil())
						Ω(err).Should(Equal(ErrEREmptyDBList))
					})
				})

				Context("Restore", func() {
					It("Should return error on empty list of persistence stores", func() {
						err := er.Restore()
						Ω(err).ShouldNot(BeNil())
						Ω(err).Should(Equal(ErrEREmptyDBList))
					})
				})
			})

			Context("When db backup fails", func() {
				var psOrig []cfbackup.SystemDump
				BeforeEach(func() {
					psOrig = ps
					er.PersistentSystems = []cfbackup.SystemDump{
						&PgInfoMock{
							SystemInfo: cfbackup.SystemInfo{
								Product:   product,
								Component: component,
								Identity:  username,
							},
						},
						&PgInfoMock{
							SystemInfo: cfbackup.SystemInfo{
								Product:   product,
								Component: component,
								Identity:  username,
							},
							failDump: true,
						},
					}
				})

				AfterEach(func() {
					er.PersistentSystems = psOrig
				})
				Context("Backup", func() {
					It("should return error if db backup fails", func() {
						err := er.Backup()
						Ω(err).ShouldNot(BeNil())
						Ω(err).Should(Equal(ErrERDBBackup))
					})
				})

				Context("Restore", func() {
					It("should return error if db backup fails", func() {
						err := er.Backup()
						Ω(err).ShouldNot(BeNil())
						Ω(err).Should(Equal(ErrERDBBackup))
					})
				})
			})

		})

		Context("with invalid properties", func() {
			var (
				product   = "cf"
				component = "consoledb"
				username  = "******"
				target    string
				er        ElasticRuntime
				info      = cfbackup.SystemsInfo{
					SystemDumps: map[string]cfbackup.SystemDump{
						"ConsoledbInfo": &cfbackup.SystemInfo{
							Product:   product,
							Component: component,
							Identity:  username,
						},
					},
				}
			)

			BeforeEach(func() {
				target, _ = ioutil.TempDir("/tmp", "spec")
				er = ElasticRuntime{
					JSONFile:      installationSettingsFilePath,
					HTTPGateway:   &fakes.MockHTTPGateway{true, 500, `{"state":"notdone"}`},
					BackupContext: cfbackup.NewBackupContext(target, cfenv.CurrentEnv()),
					SystemsInfo:   info,
				}
			})

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

			Context("Backup", func() {

				It("Should not return nil error", func() {
					err := er.Backup()
					Ω(err).ShouldNot(BeNil())
					Ω(err).Should(Equal(ErrERDirectorCreds))
				})

				It("Should not panic", func() {
					var err error
					Ω(func() {
						err = er.Backup()
					}).ShouldNot(Panic())
				})
			})

			Context("Restore", func() {

				It("Should not return nil error", func() {
					err := er.Restore()
					Ω(err).ShouldNot(BeNil())
					Ω(err).Should(Equal(ErrERDirectorCreds))
				})

				It("Should not panic", func() {
					var err error
					Ω(func() {
						err = er.Restore()
					}).ShouldNot(Panic())
				})
			})
		})
	})

	Describe("RunDbBackups function", func() {
		Context("with a valid product and component for ccdb", func() {
			var (
				product   = "cf"
				component = "consoledb"
				username  = "******"
				target    string
				er        ElasticRuntime
				info      = cfbackup.SystemsInfo{
					SystemDumps: map[string]cfbackup.SystemDump{
						"ConsoledbInfo": &PgInfoMock{
							SystemInfo: cfbackup.SystemInfo{
								Product:   product,
								Component: component,
								Identity:  username,
							},
						},
					},
				}
			)

			BeforeEach(func() {
				target, _ = ioutil.TempDir("/tmp", "spec")
				er = ElasticRuntime{
					JSONFile:      installationSettingsFilePath,
					HTTPGateway:   &fakes.MockHTTPGateway{},
					BackupContext: cfbackup.NewBackupContext(target, cfenv.CurrentEnv()),
					SystemsInfo:   info,
				}
				er.ReadAllUserCredentials()
			})

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

			Context("Backup", func() {
				It("Should write the dumped output to a file in the databaseDir", func() {
					er.RunDbAction([]cfbackup.SystemDump{info.SystemDumps["ConsoledbInfo"]}, cfbackup.ExportArchive)
					filename := fmt.Sprintf("%s.backup", component)
					exists, _ := osutils.Exists(path.Join(target, filename))
					Ω(exists).Should(BeTrue())
				})

				It("Should have a nil error and not panic", func() {
					var err error
					Ω(func() {
						err = er.RunDbAction([]cfbackup.SystemDump{info.SystemDumps["ConsoledbInfo"]}, cfbackup.ExportArchive)
					}).ShouldNot(Panic())
					Ω(err).Should(BeNil())
				})
			})

			Context("Restore", func() {
				It("should return error if local file does not exist", func() {
					err := er.RunDbAction([]cfbackup.SystemDump{info.SystemDumps["ConsoledbInfo"]}, cfbackup.ImportArchive)
					Ω(err).ShouldNot(BeNil())
					Ω(err).Should(BeAssignableToTypeOf(ErrERInvalidPath))
				})

				Context("local file exists", func() {
					var filename = fmt.Sprintf("%s.backup", component)

					BeforeEach(func() {
						file, _ := os.Create(path.Join(target, filename))
						file.Close()
					})

					AfterEach(func() {
						os.Remove(path.Join(target, filename))
					})

					It("should upload file to remote w/o error", func() {
						err := er.RunDbAction([]cfbackup.SystemDump{info.SystemDumps["ConsoledbInfo"]}, cfbackup.ImportArchive)
						Ω(err).Should(BeNil())
					})

					Context("write failure", func() {
						var origInfo map[string]cfbackup.SystemDump

						BeforeEach(func() {
							origInfo = info.SystemDumps
							info = cfbackup.SystemsInfo{
								SystemDumps: map[string]cfbackup.SystemDump{
									"ConsoledbInfo": &PgInfoMock{
										failImport: true,
										SystemInfo: cfbackup.SystemInfo{
											Product:   product,
											Component: component,
											Identity:  username,
										},
									},
								},
							}
						})

						AfterEach(func() {
							info.SystemDumps = origInfo
						})
						It("should return error", func() {
							err := er.RunDbAction([]cfbackup.SystemDump{info.SystemDumps["ConsoledbInfo"]}, cfbackup.ImportArchive)
							Ω(err).ShouldNot(BeNil())
							Ω(err).ShouldNot(Equal(ErrorImport))
						})
					})
				})
			})
		})

		Context("with a valid product and component for consoledb", func() {
			var (
				product   = "cf"
				component = "consoledb"
				username  = "******"
				target    string
				er        ElasticRuntime
				info      = cfbackup.SystemsInfo{
					SystemDumps: map[string]cfbackup.SystemDump{
						"ConsoledbInfo": &PgInfoMock{
							SystemInfo: cfbackup.SystemInfo{
								Product:   product,
								Component: component,
								Identity:  username,
							},
						},
					},
				}
			)

			BeforeEach(func() {
				target, _ = ioutil.TempDir("/tmp", "spec")
				er = ElasticRuntime{
					JSONFile:      installationSettingsFilePath,
					HTTPGateway:   &fakes.MockHTTPGateway{},
					BackupContext: cfbackup.NewBackupContext(target, cfenv.CurrentEnv()),
					SystemsInfo:   info,
				}
				er.ReadAllUserCredentials()
			})

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

			Context("Backup", func() {

				It("Should write the dumped output to a file in the databaseDir", func() {
					er.RunDbAction([]cfbackup.SystemDump{info.SystemDumps["ConsoledbInfo"]}, cfbackup.ExportArchive)
					filename := fmt.Sprintf("%s.backup", component)
					exists, _ := osutils.Exists(path.Join(target, filename))
					Ω(exists).Should(BeTrue())
				})

				It("Should have a nil error and not panic", func() {
					var err error
					Ω(func() {
						err = er.RunDbAction([]cfbackup.SystemDump{info.SystemDumps["ConsoledbInfo"]}, cfbackup.ExportArchive)
					}).ShouldNot(Panic())
					Ω(err).Should(BeNil())
				})
			})
		})

		Context("with a valid product and component for uaadb", func() {
			var (
				product   = "cf"
				component = "uaadb"
				username  = "******"
				target    string
				er        ElasticRuntime
				info      = cfbackup.SystemsInfo{
					SystemDumps: map[string]cfbackup.SystemDump{
						"UaadbInfo": &PgInfoMock{
							SystemInfo: cfbackup.SystemInfo{
								Product:   product,
								Component: component,
								Identity:  username,
							},
						},
					},
				}
			)

			BeforeEach(func() {
				target, _ = ioutil.TempDir("/tmp", "spec")
				er = ElasticRuntime{
					JSONFile:      installationSettingsFilePath,
					HTTPGateway:   &fakes.MockHTTPGateway{},
					BackupContext: cfbackup.NewBackupContext(target, cfenv.CurrentEnv()),
					SystemsInfo:   info,
				}
				er.ReadAllUserCredentials()
			})

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

			Context("Backup", func() {

				It("Should write the dumped output to a file in the databaseDir", func() {
					er.RunDbAction([]cfbackup.SystemDump{info.SystemDumps["UaadbInfo"]}, cfbackup.ExportArchive)
					filename := fmt.Sprintf("%s.backup", component)
					exists, _ := osutils.Exists(path.Join(target, filename))
					Ω(exists).Should(BeTrue())
				})

				It("Should have a nil error and not panic", func() {
					var err error
					Ω(func() {
						err = er.RunDbAction([]cfbackup.SystemDump{info.SystemDumps["UaadbInfo"]}, cfbackup.ExportArchive)
					}).ShouldNot(Panic())
					Ω(err).Should(BeNil())
				})
			})

			Context("Restore", func() {

			})
		})

		Context("with a invalid product, username and component", func() {
			var (
				product   = "aaaaaaaa"
				component = "aaaaaaaa"
				username  = "******"
				target    string
				er        ElasticRuntime
				info      = cfbackup.SystemsInfo{
					SystemDumps: map[string]cfbackup.SystemDump{
						"ConsoledbInfo": &cfbackup.SystemInfo{
							Product:   product,
							Component: component,
							Identity:  username,
						},
					},
				}
			)

			BeforeEach(func() {
				target, _ = ioutil.TempDir("/tmp", "spec")
				er = ElasticRuntime{
					JSONFile:      installationSettingsFilePath,
					HTTPGateway:   &fakes.MockHTTPGateway{},
					BackupContext: cfbackup.NewBackupContext(target, cfenv.CurrentEnv()),
					SystemsInfo:   info,
				}
				er.ReadAllUserCredentials()
			})

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

			Context("Backup", func() {

				It("Should not write the dumped output to a file in the databaseDir", func() {
					er.RunDbAction([]cfbackup.SystemDump{info.SystemDumps["ConsoledbInfo"]}, cfbackup.ExportArchive)
					filename := fmt.Sprintf("%s.sql", component)
					exists, _ := osutils.Exists(path.Join(target, filename))
					Ω(exists).ShouldNot(BeTrue())
				})

				It("Should have a non nil error and not panic", func() {
					var err error
					Ω(func() {
						err = er.RunDbAction([]cfbackup.SystemDump{info.SystemDumps["ConsoledbInfo"]}, cfbackup.ExportArchive)
					}).ShouldNot(Panic())
					Ω(err).ShouldNot(BeNil())
				})
			})

			Context("Restore", func() {
				It("Should have a non nil error and not panic", func() {
					var err error
					Ω(func() {
						err = er.RunDbAction([]cfbackup.SystemDump{info.SystemDumps["ConsoledbInfo"]}, cfbackup.ImportArchive)
					}).ShouldNot(Panic())
					Ω(err).ShouldNot(BeNil())
				})
			})
		})
	})
}