def test_001_selected_vms(self, mock_backup, mock_getpass, mock_input): mock_getpass.return_value = 'testpass' mock_input.return_value = 'Y' vm1 = BackupVM() vm1.name = 'test-vm' vm1.backup_path = 'path/in/backup' vm1.template = None vm1.klass = 'StandaloneVM' vm1.label = 'red' vm2 = BackupVM() vm2.name = 'test-vm2' vm2.backup_path = 'path/in/backup2' vm2.template = None vm2.klass = 'StandaloneVM' vm2.label = 'red' mock_restore_info = { 1: BackupRestore.VMToRestore(vm1), 2: BackupRestore.VMToRestore(vm2), } exclude_list = [] mock_backup.configure_mock( **{ 'return_value.get_restore_summary.return_value': '', 'return_value.options.exclude': exclude_list, 'return_value.get_restore_info.return_value': mock_restore_info, }) qubesadmin.tools.qvm_backup_restore.main(['/some/path', 'test-vm'], app=self.app) mock_backup.assert_called_once_with(self.app, '/some/path', None, 'testpass') self.assertEqual(exclude_list, ['test-vm2']) self.assertAllCalled()
def test_000_simple(self, mock_backup, mock_getpass, mock_input): mock_getpass.return_value = 'testpass' mock_input.return_value = 'Y' vm1 = BackupVM() vm1.name = 'test-vm' vm1.backup_path = 'path/in/backup' vm1.template = None vm1.klass = 'StandaloneVM' vm1.label = 'red' mock_restore_info = { 1: BackupRestore.VMToRestore(vm1), } mock_backup.configure_mock( **{ 'return_value.get_restore_summary.return_value': '', 'return_value.get_restore_info.return_value': mock_restore_info, }) with mock.patch('qubesadmin.tools.qvm_backup_restore.handle_broken') \ as mock_handle_broken: qubesadmin.tools.qvm_backup_restore.main(['/some/path'], app=self.app) mock_handle_broken.assert_called_once_with(self.app, mock.ANY, mock_restore_info) mock_backup.assert_called_once_with(self.app, '/some/path', None, 'testpass') self.assertAllCalled()
def test_010_handle_broken_no_problems(self): vm1 = BackupVM() vm1.name = 'test-vm' vm1.backup_path = 'path/in/backup' vm1.template = None vm1.klass = 'StandaloneVM' vm1.label = 'red' mock_restore_info = { 1: BackupRestore.VMToRestore(vm1), } mock_args = mock.Mock() self.app.log = mock.Mock() qubesadmin.tools.qvm_backup_restore.handle_broken( self.app, mock_args, mock_restore_info) self.assertEqual(self.app.log.mock_calls, [ mock.call.info( 'The above VMs will be copied and added to your system.'), mock.call.info('Exisiting VMs will NOT be removed.'), ])
def test_012_handle_broken_missing_netvm(self): vm1 = BackupVM() vm1.name = 'test-vm' vm1.backup_path = 'path/in/backup' vm1.netvm = 'not-existing-netvm' vm1.klass = 'StandaloneVM' vm1.label = 'red' mock_restore_info = { 1: BackupRestore.VMToRestore(vm1), } mock_restore_info[1].problems.add( BackupRestore.VMToRestore.MISSING_NETVM) with self.subTest('skip_broken'): mock_args = mock.Mock() mock_args.skip_broken = True mock_args.verify_only = False self.app.log = mock.Mock() qubesadmin.tools.qvm_backup_restore.handle_broken( self.app, mock_args, mock_restore_info) self.assertAppropriateLogging('NetVM', 'skip_broken') with self.subTest('ignore_missing'): mock_args = mock.Mock() mock_args.skip_broken = False mock_args.ignore_missing = True mock_args.verify_only = False self.app.log = mock.Mock() qubesadmin.tools.qvm_backup_restore.handle_broken( self.app, mock_args, mock_restore_info) self.assertAppropriateLogging('NetVM', 'ignore_missing') with self.subTest('error'): mock_args = mock.Mock() mock_args.skip_broken = False mock_args.ignore_missing = False mock_args.verify_only = False self.app.log = mock.Mock() with self.assertRaises(qubesadmin.exc.QubesException): qubesadmin.tools.qvm_backup_restore.handle_broken( self.app, mock_args, mock_restore_info) self.assertAppropriateLogging('NetVM', 'error')
def main(args=None, app=None): '''Main function of qvm-backup-restore''' # pylint: disable=too-many-return-statements args = parser.parse_args(args, app=app) appvm = None if args.appvm: try: appvm = args.app.domains[args.appvm] except KeyError: parser.error('no such domain: {!r}'.format(args.appvm)) if args.location_is_service and not args.appvm: parser.error('--location-is-service option requires -d') if args.paranoid_mode: args.dom0_home = False args.app.log.info("Starting restore process in a DisposableVM...") args.app.log.info("When operation completes, close its window " "manually.") restore_in_dispvm = RestoreInDisposableVM(args.app, args) try: backup_log = restore_in_dispvm.run() if args.auto_close: print_backup_log(backup_log) except qubesadmin.exc.BackupRestoreError as e: if e.backup_log is not None: print_backup_log(e.backup_log) parser.error_runtime(str(e)) return 1 except qubesadmin.exc.QubesException as e: parser.error_runtime(str(e)) return 1 return if args.pass_file is not None: pass_f = open(args.pass_file) if args.pass_file != "-" else sys.stdin passphrase = pass_f.readline().rstrip() if pass_f is not sys.stdin: pass_f.close() else: passphrase = getpass.getpass("Please enter the passphrase to verify " "and (if encrypted) decrypt the backup: ") args.app.log.info("Checking backup content...") try: backup = BackupRestore(args.app, args.backup_location, appvm, passphrase, location_is_service=args.location_is_service, force_compression_filter=args.compression) except qubesadmin.exc.QubesException as e: parser.error_runtime(str(e)) # unreachable - error_runtime will raise SystemExit return 1 backup.options.use_default_template = args.ignore_missing backup.options.use_default_netvm = args.ignore_missing backup.options.rename_conflicting = args.rename_conflicting backup.options.dom0_home = args.dom0_home backup.options.ignore_username_mismatch = args.ignore_username_mismatch backup.options.ignore_size_limit = args.ignore_size_limit backup.options.exclude = args.exclude backup.options.verify_only = args.verify_only restore_info = None try: restore_info = backup.get_restore_info() except qubesadmin.exc.QubesException as e: parser.error_runtime(str(e)) if args.vms: # use original name here, not renamed backup.options.exclude += [ vm_info.vm.name for vm_info in restore_info.values() if vm_info.vm.name not in args.vms ] restore_info = backup.restore_info_verify(restore_info) print(backup.get_restore_summary(restore_info)) try: handle_broken(args.app, args, restore_info) except qubesadmin.exc.QubesException as e: parser.error_runtime(str(e)) if args.pass_file is None: if input("Do you want to proceed? [y/N] ").upper() != "Y": sys.exit(0) try: backup.restore_do(restore_info) except qubesadmin.exc.QubesException as e: parser.error_runtime(str(e))
def main(args=None, app=None): '''Main function of qvm-backup-restore''' # pylint: disable=too-many-return-statements args = parser.parse_args(args, app=app) appvm = None if args.appvm: try: appvm = args.app.domains[args.appvm] except KeyError: parser.error('no such domain: {!r}'.format(args.appvm)) if args.pass_file is not None: pass_f = open(args.pass_file) if args.pass_file != "-" else sys.stdin passphrase = pass_f.readline().rstrip() if pass_f is not sys.stdin: pass_f.close() else: passphrase = getpass.getpass("Please enter the passphrase to verify " "and (if encrypted) decrypt the backup: ") args.app.log.info("Checking backup content...") try: backup = BackupRestore(args.app, args.backup_location, appvm, passphrase) except qubesadmin.exc.QubesException as e: parser.error_runtime(str(e)) # unreachable - error_runtime will raise SystemExit return 1 if args.ignore_missing: backup.options.use_default_template = True backup.options.use_default_netvm = True if args.replace_template: backup.options.replace_template = args.replace_template if args.rename_conflicting: backup.options.rename_conflicting = True if not args.dom0_home: backup.options.dom0_home = False if args.ignore_username_mismatch: backup.options.ignore_username_mismatch = True if args.ignore_size_limit: backup.options.ignore_size_limit = True if args.exclude: backup.options.exclude = args.exclude if args.verify_only: backup.options.verify_only = True restore_info = None try: restore_info = backup.get_restore_info() except qubesadmin.exc.QubesException as e: parser.error_runtime(str(e)) if args.vms: # use original name here, not renamed backup.options.exclude += [ vm_info.vm.name for vm_info in restore_info.values() if vm_info.vm.name not in args.vms ] restore_info = backup.restore_info_verify(restore_info) print(backup.get_restore_summary(restore_info)) try: handle_broken(args.app, args, restore_info) except qubesadmin.exc.QubesException as e: parser.error_runtime(str(e)) if args.pass_file is None: if input("Do you want to proceed? [y/N] ").upper() != "Y": exit(0) try: backup.restore_do(restore_info) except qubesadmin.exc.QubesException as e: parser.error_runtime(str(e))