func (context *ElasticRuntime) getReadWriter(fpath string, action int) (rw io.ReadWriter, err error) { switch action { case IMPORT_ARCHIVE: var exists bool if exists, err = osutils.Exists(fpath); exists && err == nil { rw, err = os.Open(fpath) } else { var pathError os.PathError pathError = *ER_ERROR_INVALID_PATH pathError.Path = fpath err = &pathError } case EXPORT_ARCHIVE: rw, err = osutils.SafeCreate(fpath) } return }
func testERWithVersionSpecificFile(installationSettingsFilePath string) { Describe("Backup / Restore", func() { Context("with valid properties (DirectorInfo)", func() { var ( product = BoshName() component = "director" username = "******" target string er ElasticRuntime info = SystemsInfo{ SystemDumps: map[string]SystemDump{ "DirectorInfo": &SystemInfo{ Product: product, Component: component, Identity: username, }, "ConsoledbInfo": &PgInfoMock{ SystemInfo: SystemInfo{ Product: product, Component: component, Identity: username, }, }, }, } ps = []SystemDump{info.SystemDumps["ConsoledbInfo"]} ) BeforeEach(func() { target, _ = ioutil.TempDir("/tmp", "spec") er = ElasticRuntime{ JSONFile: installationSettingsFilePath, HTTPGateway: &MockHttpGateway{}, BackupContext: 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 []SystemDump BeforeEach(func() { psOrig = ps er.PersistentSystems = []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 []SystemDump BeforeEach(func() { psOrig = ps er.PersistentSystems = []SystemDump{ &PgInfoMock{ SystemInfo: SystemInfo{ Product: product, Component: component, Identity: username, }, }, &PgInfoMock{ SystemInfo: 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 = SystemsInfo{ SystemDumps: map[string]SystemDump{ "ConsoledbInfo": &SystemInfo{ Product: product, Component: component, Identity: username, }, }, } ) BeforeEach(func() { target, _ = ioutil.TempDir("/tmp", "spec") er = ElasticRuntime{ JSONFile: installationSettingsFilePath, HTTPGateway: &MockHttpGateway{true, 500, `{"state":"notdone"}`}, BackupContext: 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 = SystemsInfo{ SystemDumps: map[string]SystemDump{ "ConsoledbInfo": &PgInfoMock{ SystemInfo: SystemInfo{ Product: product, Component: component, Identity: username, }, }, }, } ) BeforeEach(func() { target, _ = ioutil.TempDir("/tmp", "spec") er = ElasticRuntime{ JSONFile: installationSettingsFilePath, HTTPGateway: &MockHttpGateway{}, BackupContext: 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([]SystemDump{info.SystemDumps["ConsoledbInfo"]}, 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([]SystemDump{info.SystemDumps["ConsoledbInfo"]}, ExportArchive) }).ShouldNot(Panic()) Ω(err).Should(BeNil()) }) }) Context("Restore", func() { It("should return error if local file does not exist", func() { err := er.RunDbAction([]SystemDump{info.SystemDumps["ConsoledbInfo"]}, 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([]SystemDump{info.SystemDumps["ConsoledbInfo"]}, ImportArchive) Ω(err).Should(BeNil()) }) Context("write failure", func() { var origInfo map[string]SystemDump BeforeEach(func() { origInfo = info.SystemDumps info = SystemsInfo{ SystemDumps: map[string]SystemDump{ "ConsoledbInfo": &PgInfoMock{ failImport: true, SystemInfo: SystemInfo{ Product: product, Component: component, Identity: username, }, }, }, } }) AfterEach(func() { info.SystemDumps = origInfo }) It("should return error", func() { err := er.RunDbAction([]SystemDump{info.SystemDumps["ConsoledbInfo"]}, 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 = SystemsInfo{ SystemDumps: map[string]SystemDump{ "ConsoledbInfo": &PgInfoMock{ SystemInfo: SystemInfo{ Product: product, Component: component, Identity: username, }, }, }, } ) BeforeEach(func() { target, _ = ioutil.TempDir("/tmp", "spec") er = ElasticRuntime{ JSONFile: installationSettingsFilePath, HTTPGateway: &MockHttpGateway{}, BackupContext: 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([]SystemDump{info.SystemDumps["ConsoledbInfo"]}, 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([]SystemDump{info.SystemDumps["ConsoledbInfo"]}, 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 = SystemsInfo{ SystemDumps: map[string]SystemDump{ "UaadbInfo": &PgInfoMock{ SystemInfo: SystemInfo{ Product: product, Component: component, Identity: username, }, }, }, } ) BeforeEach(func() { target, _ = ioutil.TempDir("/tmp", "spec") er = ElasticRuntime{ JSONFile: installationSettingsFilePath, HTTPGateway: &MockHttpGateway{}, BackupContext: 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([]SystemDump{info.SystemDumps["UaadbInfo"]}, 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([]SystemDump{info.SystemDumps["UaadbInfo"]}, 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 = SystemsInfo{ SystemDumps: map[string]SystemDump{ "ConsoledbInfo": &SystemInfo{ Product: product, Component: component, Identity: username, }, }, } ) BeforeEach(func() { target, _ = ioutil.TempDir("/tmp", "spec") er = ElasticRuntime{ JSONFile: installationSettingsFilePath, HTTPGateway: &MockHttpGateway{}, BackupContext: 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([]SystemDump{info.SystemDumps["ConsoledbInfo"]}, 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([]SystemDump{info.SystemDumps["ConsoledbInfo"]}, 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([]SystemDump{info.SystemDumps["ConsoledbInfo"]}, ImportArchive) }).ShouldNot(Panic()) Ω(err).ShouldNot(BeNil()) }) }) }) }) }
Username: "******", Password: "******", BackupContext: fakes.NewFakeBackupContext(path.Join(tmpDir, "backup"), cfenv.CurrentEnv(), new(cfbackup.DiskProvider)), Executer: &fakes.FailExecuter{}, LocalExecuter: fakes.NewLocalMockExecuter(), DeploymentDir: "fixtures/encryptionkey", OpsmanagerBackupDir: "opsmanager", } }) It("should return non nil error and not write installation.json", func() { err := opsManager.Backup() filepath := path.Join(backupDir, "installation.json") Ω(err).ShouldNot(BeNil()) Ω(osutils.Exists(filepath)).Should(BeFalse()) }) It("should return non nil error and not write deployments.tar.gz", func() { err := opsManager.Backup() filepath := path.Join(backupDir, "deployments.tar.gz") Ω(err).ShouldNot(BeNil()) Ω(osutils.Exists(filepath)).Should(BeTrue()) }) }) Context("called yielding a successful rest call", func() { BeforeEach(func() { tmpDir, _ = ioutil.TempDir("/tmp", "test") backupDir = path.Join(tmpDir, "backup", "opsmanager")
func testERWithVersionSpecificFile(installationSettingsFilePath string) { Describe("Backup / Restore", func() { Context("with valid properties (DirectorInfo)", func() { var ( product string = "microbosh" component string = "director" username string = "director" target string er ElasticRuntime info map[string]SystemDump = map[string]SystemDump{ "DirectorInfo": &SystemInfo{ Product: product, Component: component, Identity: username, }, "ConsoledbInfo": &PgInfoMock{ SystemInfo: SystemInfo{ Product: product, Component: component, Identity: username, }, }, } ps []SystemDump = []SystemDump{info["ConsoledbInfo"]} ) BeforeEach(func() { target, _ = ioutil.TempDir("/tmp", "spec") er = ElasticRuntime{ JsonFile: installationSettingsFilePath, HttpGateway: &MockHttpGateway{}, BackupContext: BackupContext{ TargetDir: target, }, SystemsInfo: info, PersistentSystems: ps, Logger: Logger(), } }) 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 string = 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 []SystemDump BeforeEach(func() { psOrig = ps er.PersistentSystems = []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(ER_ERROR_EMPTY_DB_LIST)) }) }) Context("Restore", func() { It("Should return error on empty list of persistence stores", func() { err := er.Restore() Ω(err).ShouldNot(BeNil()) Ω(err).Should(Equal(ER_ERROR_EMPTY_DB_LIST)) }) }) }) Context("When db backup fails", func() { var psOrig []SystemDump BeforeEach(func() { psOrig = ps er.PersistentSystems = []SystemDump{ &PgInfoMock{ SystemInfo: SystemInfo{ Product: product, Component: component, Identity: username, }, }, &PgInfoMock{ SystemInfo: 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(ER_DB_BACKUP)) }) }) Context("Restore", func() { It("should return error if db backup fails", func() { err := er.Backup() Ω(err).ShouldNot(BeNil()) Ω(err).Should(Equal(ER_DB_BACKUP)) }) }) }) }) Context("with invalid properties", func() { var ( product string = "cf" component string = "consoledb" username string = "root" target string er ElasticRuntime info map[string]SystemDump = map[string]SystemDump{ "ConsoledbInfo": &SystemInfo{ Product: product, Component: component, Identity: username, }, } ) BeforeEach(func() { target, _ = ioutil.TempDir("/tmp", "spec") er = ElasticRuntime{ JsonFile: installationSettingsFilePath, HttpGateway: &MockHttpGateway{true, 500, `{"state":"notdone"}`}, BackupContext: BackupContext{ TargetDir: target, }, SystemsInfo: info, Logger: Logger(), } }) AfterEach(func() { os.Remove(target) }) Context("Backup", func() { It("Should not return nil error", func() { err := er.Backup() Ω(err).ShouldNot(BeNil()) Ω(err).Should(Equal(ER_ERROR_DIRECTOR_CREDS)) }) 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(ER_ERROR_DIRECTOR_CREDS)) }) 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 string = "cf" component string = "consoledb" username string = "root" target string er ElasticRuntime info map[string]SystemDump = map[string]SystemDump{ "ConsoledbInfo": &PgInfoMock{ SystemInfo: SystemInfo{ Product: product, Component: component, Identity: username, }, }, } ) BeforeEach(func() { target, _ = ioutil.TempDir("/tmp", "spec") er = ElasticRuntime{ JsonFile: installationSettingsFilePath, HttpGateway: &MockHttpGateway{}, BackupContext: BackupContext{ TargetDir: target, }, SystemsInfo: info, Logger: Logger(), } 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([]SystemDump{info["ConsoledbInfo"]}, EXPORT_ARCHIVE) 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([]SystemDump{info["ConsoledbInfo"]}, EXPORT_ARCHIVE) }).ShouldNot(Panic()) Ω(err).Should(BeNil()) }) }) Context("Restore", func() { It("should return error if local file does not exist", func() { err := er.RunDbAction([]SystemDump{info["ConsoledbInfo"]}, IMPORT_ARCHIVE) Ω(err).ShouldNot(BeNil()) Ω(err).Should(BeAssignableToTypeOf(ER_ERROR_INVALID_PATH)) }) Context("local file exists", func() { var filename string = 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([]SystemDump{info["ConsoledbInfo"]}, IMPORT_ARCHIVE) Ω(err).Should(BeNil()) }) Context("write failure", func() { var origInfo map[string]SystemDump BeforeEach(func() { origInfo = info info = map[string]SystemDump{ "ConsoledbInfo": &PgInfoMock{ failImport: true, SystemInfo: SystemInfo{ Product: product, Component: component, Identity: username, }, }, } }) AfterEach(func() { info = origInfo }) It("should return error", func() { err := er.RunDbAction([]SystemDump{info["ConsoledbInfo"]}, IMPORT_ARCHIVE) Ω(err).ShouldNot(BeNil()) Ω(err).ShouldNot(Equal(ERROR_IMPORT)) }) }) }) }) }) Context("with a valid product and component for consoledb", func() { var ( product string = "cf" component string = "consoledb" username string = "root" target string er ElasticRuntime info map[string]SystemDump = map[string]SystemDump{ "ConsoledbInfo": &PgInfoMock{ SystemInfo: SystemInfo{ Product: product, Component: component, Identity: username, }, }, } ) BeforeEach(func() { target, _ = ioutil.TempDir("/tmp", "spec") er = ElasticRuntime{ JsonFile: installationSettingsFilePath, HttpGateway: &MockHttpGateway{}, BackupContext: BackupContext{ TargetDir: target, }, SystemsInfo: info, Logger: Logger(), } 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([]SystemDump{info["ConsoledbInfo"]}, EXPORT_ARCHIVE) 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([]SystemDump{info["ConsoledbInfo"]}, EXPORT_ARCHIVE) }).ShouldNot(Panic()) Ω(err).Should(BeNil()) }) }) }) Context("with a valid product and component for uaadb", func() { var ( product string = "cf" component string = "uaadb" username string = "root" target string er ElasticRuntime info map[string]SystemDump = map[string]SystemDump{ "UaadbInfo": &PgInfoMock{ SystemInfo: SystemInfo{ Product: product, Component: component, Identity: username, }, }, } ) BeforeEach(func() { target, _ = ioutil.TempDir("/tmp", "spec") er = ElasticRuntime{ JsonFile: installationSettingsFilePath, HttpGateway: &MockHttpGateway{}, BackupContext: BackupContext{ TargetDir: target, }, SystemsInfo: info, Logger: Logger(), } 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([]SystemDump{info["UaadbInfo"]}, EXPORT_ARCHIVE) 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([]SystemDump{info["UaadbInfo"]}, EXPORT_ARCHIVE) }).ShouldNot(Panic()) Ω(err).Should(BeNil()) }) }) Context("Restore", func() { }) }) Context("with a invalid product, username and component", func() { var ( product string = "aaaaaaaa" component string = "aaaaaaaa" username string = "aaaaaaaa" target string er ElasticRuntime info map[string]SystemDump = map[string]SystemDump{ "ConsoledbInfo": &SystemInfo{ Product: product, Component: component, Identity: username, }, } ) BeforeEach(func() { target, _ = ioutil.TempDir("/tmp", "spec") er = ElasticRuntime{ JsonFile: installationSettingsFilePath, HttpGateway: &MockHttpGateway{}, BackupContext: BackupContext{ TargetDir: target, }, SystemsInfo: info, Logger: Logger(), } 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([]SystemDump{info["ConsoledbInfo"]}, EXPORT_ARCHIVE) 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([]SystemDump{info["ConsoledbInfo"]}, EXPORT_ARCHIVE) }).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([]SystemDump{info["ConsoledbInfo"]}, IMPORT_ARCHIVE) }).ShouldNot(Panic()) Ω(err).ShouldNot(BeNil()) }) }) }) }) }