def process_result(self, result): if result['returncode'] == 0: repo, created = RepoModel.get_or_create(url=result['cmd'][-1]) if not result['data']: result['data'] = { } # TODO: Workaround for tests. Can't read mock results 2x. remote_archives = result['data'].get('archives', []) # Delete archives that don't exist on the remote side for archive in ArchiveModel.select().where( ArchiveModel.repo == repo.id): if not list( filter(lambda s: s['id'] == archive.snapshot_id, remote_archives)): archive.delete_instance() # Add remote archives we don't have locally. for archive in result['data'].get('archives', []): new_archive, _ = ArchiveModel.get_or_create( snapshot_id=archive['id'], repo=repo.id, defaults={ 'name': archive['name'], 'time': dt.fromisoformat(archive['time']) }) new_archive.save()
def delete_result(self, result): if result['returncode'] == 0: self._set_status(self.tr('Archive deleted.')) deleted_row = self.archiveTable.findItems( self.archive_name, QtCore.Qt.MatchExactly)[0].row() self.archiveTable.removeRow(deleted_row) ArchiveModel.get(name=self.archive_name).delete_instance() del self.archive_name else: self._toggle_all_buttons(True)
def list_diff_result(self, result): self._set_status('') if result['returncode'] == 0: archive_newer = ArchiveModel.get( name=result['params']['archive_name_newer']) archive_older = ArchiveModel.get( name=result['params']['archive_name_older']) window = DiffResult(result['data'], archive_newer, archive_older, result['params']['json_lines']) self._toggle_all_buttons(True) window.setParent(self, QtCore.Qt.Sheet) self._resultwindow = window # for testing window.show()
def test_archive_rename(qapp, qtbot, mocker, borg_json_output): main = qapp.main_window tab = main.archiveTab main.tabWidget.setCurrentIndex(3) tab.populate_from_profile() qtbot.waitUntil(lambda: tab.archiveTable.rowCount() == 2) tab.archiveTable.selectRow(0) new_archive_name = 'idf89d8f9d8fd98' stdout, stderr = borg_json_output('rename') popen_result = mocker.MagicMock(stdout=stdout, stderr=stderr, returncode=0) mocker.patch.object(vorta.borg.borg_job, 'Popen', return_value=popen_result) mocker.patch.object(vorta.views.archive_tab.QInputDialog, 'getText', return_value=(new_archive_name, True)) tab.rename_action() # Successful rename case qtbot.waitUntil(lambda: tab.mountErrors.text() == 'Archive renamed.', **pytest._wait_defaults) assert ArchiveModel.select().filter(name=new_archive_name).count() == 1 # Duplicate name case exp_text = 'An archive with this name already exists.' mocker.patch.object(vorta.views.archive_tab.QInputDialog, 'getText', return_value=(new_archive_name, True)) tab.rename_action() qtbot.waitUntil(lambda: tab.mountErrors.text() == exp_text, **pytest._wait_defaults)
def process_result(self, result): if result['returncode'] == 0: remote_archives = result['data'].get('archives', []) # get info stored during BorgJob.prepare() # repo_id = self.params['repo_id'] repo_id = result['params']['repo_id'] # Update remote archives. for remote_archive in remote_archives: archive = ArchiveModel.get_or_none( snapshot_id=remote_archive['id'], repo=repo_id) archive.name = remote_archive['name'] # incase name changed # archive.time = parser.parse(remote_archive['time']) archive.duration = remote_archive['duration'] archive.size = remote_archive['stats']['deduplicated_size'] archive.save() if 'cache' in result['data']: stats = result['data']['cache']['stats'] repo = RepoModel.get(id=result['params']['repo_id']) repo.total_size = stats['total_size'] repo.unique_csize = stats['unique_csize'] repo.unique_size = stats['unique_size'] repo.total_unique_chunks = stats['total_unique_chunks'] repo.save()
def process_result(self, result): if result['returncode'] in [0, 1] and 'archive' in result['data']: new_archive, created = ArchiveModel.get_or_create( snapshot_id=result['data']['archive']['id'], defaults={ 'name': result['data']['archive']['name'], 'time': dt.fromisoformat(result['data']['archive']['start']), 'repo': result['params']['repo_id'], 'duration': result['data']['archive']['duration'], 'size': result['data']['archive']['stats']['deduplicated_size'] }) new_archive.save() if 'cache' in result['data'] and created: stats = result['data']['cache']['stats'] repo = RepoModel.get(id=result['params']['repo_id']) repo.total_size = stats['total_size'] repo.unique_csize = stats['unique_csize'] repo.unique_size = stats['unique_size'] repo.total_unique_chunks = stats['total_unique_chunks'] repo.save() if result['returncode'] == 1: self.app.backup_progress_event.emit( self. tr('Backup finished with warnings. See logs for details.')) else: self.app.backup_progress_event.emit( self.tr('Backup finished.'))
def list_archive_result(self, result): self._set_status('') if result['returncode'] == 0: def process_result(): def receive(): extraction_folder = dialog.selectedFiles() if extraction_folder: params = BorgExtractJob.prepare( self.profile(), archive.name, window.selected, extraction_folder[0]) if params['ok']: self._toggle_all_buttons(False) job = BorgExtractJob(params['cmd'], params, self.profile().repo.id) job.updated.connect(self.mountErrors.setText) job.result.connect(self.extract_archive_result) self.app.jobs_manager.add_job(job) else: self._set_status(params['message']) dialog = choose_file_dialog(self, self.tr("Choose Extraction Point"), want_folder=True) dialog.open(receive) archive = ArchiveModel.get(name=result['params']['archive_name']) window = ExtractDialog(result['data'], archive) self._toggle_all_buttons(True) window.setParent(self, QtCore.Qt.Sheet) self._window = window # for testing window.show() window.accepted.connect(process_result)
def process_result(self, result): if result['returncode'] == 0: repo_url, old_name = result['cmd'][-2].split('::') new_name = result['cmd'][-1] repo = RepoModel.get(url=repo_url) renamed_archive = ArchiveModel.get(name=old_name, repo=repo) renamed_archive.name = new_name renamed_archive.save()
def repo_unlink_action(self): profile = self.profile() self.init_repo_stats() msg = QMessageBox() msg.setStandardButtons(QMessageBox.Ok) msg.setParent(self, QtCore.Qt.Sheet) selected_repo_id = self.repoSelector.currentData() selected_repo_index = self.repoSelector.currentIndex() if selected_repo_index > 2: repo = RepoModel.get(id=selected_repo_id) ArchiveModel.delete().where( ArchiveModel.repo_id == repo.id).execute() profile.repo = None profile.save() repo.delete_instance(recursive=True) # This also deletes archives. self.repoSelector.setCurrentIndex(0) self.repoSelector.removeItem(selected_repo_index) msg.setWindowTitle(self.tr('Repository was Unlinked')) msg.setText(self.tr('You can always connect it again later.')) msg.show() self.repo_changed.emit() self.init_repo_stats()
def test_create(qapp, borg_json_output, mocker, qtbot): main = qapp.main_window stdout, stderr = borg_json_output('create') popen_result = mocker.MagicMock(stdout=stdout, stderr=stderr, returncode=0) mocker.patch.object(vorta.borg.borg_job, 'Popen', return_value=popen_result) qtbot.mouseClick(main.createStartBtn, QtCore.Qt.LeftButton) qtbot.waitUntil( lambda: main.progressText.text().startswith('Backup finished.'), **pytest._wait_defaults) qtbot.waitUntil(lambda: main.createStartBtn.isEnabled(), **pytest._wait_defaults) assert EventLogModel.select().count() == 1 assert ArchiveModel.select().count() == 3 assert RepoModel.get(id=1).unique_size == 15520474 assert main.createStartBtn.isEnabled() assert main.archiveTab.archiveTable.rowCount() == 3 assert main.scheduleTab.logTableWidget.rowCount() == 1
def test_archive_delete(qapp, qtbot, mocker, borg_json_output): main = qapp.main_window tab = main.archiveTab main.tabWidget.setCurrentIndex(3) tab.populate_from_profile() qtbot.waitUntil(lambda: tab.archiveTable.rowCount() == 2) tab.archiveTable.selectRow(0) stdout, stderr = borg_json_output('delete') popen_result = mocker.MagicMock(stdout=stdout, stderr=stderr, returncode=0) mocker.patch.object(vorta.borg.borg_job, 'Popen', return_value=popen_result) mocker.patch.object(vorta.views.archive_tab.ArchiveTab, 'confirm_dialog', lambda x, y, z: True) tab.delete_action() qtbot.waitUntil(lambda: main.progressText.text() == 'Archive deleted.', **pytest._wait_defaults) assert ArchiveModel.select().count() == 1 assert tab.archiveTable.rowCount() == 1
def rename_action(self): profile = self.profile() params = BorgRenameJob.prepare(profile) if not params['ok']: self._set_status(params['message']) return archive_name = self.selected_archive_name() if archive_name is not None: new_name, finished = QInputDialog.getText( self, self.tr("Change name"), self.tr("New archive name:"), text=archive_name) if not finished: return if not new_name: self._set_status(self.tr('Archive name cannot be blank.')) return new_name_exists = ArchiveModel.get_or_none(name=new_name, repo=profile.repo) if new_name_exists is not None: self._set_status( self.tr('An archive with this name already exists.')) return params['cmd'][-1] += f'::{archive_name}' params['cmd'].append(new_name) job = BorgRenameJob(params['cmd'], params, self.profile().repo.id) job.updated.connect(self._set_status) job.result.connect(self.rename_result) self._toggle_all_buttons(False) self.app.jobs_manager.add_job(job) else: self._set_status(self.tr("No archive selected"))
def init_db(qapp, qtbot, tmpdir_factory): tmp_db = tmpdir_factory.mktemp('Vorta').join('settings.sqlite') mock_db = SqliteDatabase(str(tmp_db), pragmas={'journal_mode': 'wal', }) vorta.store.connection.init_db(mock_db) default_profile = BackupProfileModel(name='Default') default_profile.save() new_repo = RepoModel(url='[email protected]:repo') new_repo.encryption = 'none' new_repo.save() default_profile.repo = new_repo.id default_profile.dont_run_on_metered_networks = False default_profile.validation_on = False default_profile.save() test_archive = ArchiveModel(snapshot_id='99999', name='test-archive', time=dt(2000, 1, 1, 0, 0), repo=1) test_archive.save() test_archive1 = ArchiveModel(snapshot_id='99998', name='test-archive1', time=dt(2000, 1, 1, 0, 0), repo=1) test_archive1.save() source_dir = SourceFileModel(dir='/tmp/another', repo=new_repo, dir_size=100, dir_files_count=18, path_isdir=True) source_dir.save() qapp.main_window.deleteLater() del qapp.main_window qapp.main_window = MainWindow(qapp) # Re-open main window to apply mock data in UI yield qapp.jobs_manager.cancel_all_jobs() qtbot.waitUntil(lambda: not qapp.jobs_manager.is_worker_running(), **pytest._wait_defaults) mock_db.close()
def test_repo_list(qapp, qtbot, mocker, borg_json_output): main = qapp.main_window tab = main.archiveTab stdout, stderr = borg_json_output('list') popen_result = mocker.MagicMock(stdout=stdout, stderr=stderr, returncode=0) mocker.patch.object(vorta.borg.borg_job, 'Popen', return_value=popen_result) main.tabWidget.setCurrentIndex(3) tab.list_action() qtbot.waitUntil(lambda: not tab.checkButton.isEnabled(), **pytest._wait_defaults) assert not tab.checkButton.isEnabled() qtbot.waitUntil( lambda: main.progressText.text() == 'Refreshing archives done.', **pytest._wait_defaults) assert ArchiveModel.select().count() == 6 assert main.progressText.text() == 'Refreshing archives done.' assert tab.checkButton.isEnabled()