qvm_backup_restore.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. # -*- encoding: utf8 -*-
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2017 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation; either version 2.1 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. import qubesadmin.tests
  21. import qubesadmin.tests.tools
  22. import qubesadmin.tools.qvm_backup_restore
  23. from unittest import mock
  24. from qubesadmin.backup import BackupVM
  25. from qubesadmin.backup.restore import BackupRestore
  26. class TC_00_qvm_backup_restore(qubesadmin.tests.QubesTestCase):
  27. def setUp(self):
  28. super(TC_00_qvm_backup_restore, self).setUp()
  29. def tearDown(self):
  30. super(TC_00_qvm_backup_restore, self).tearDown()
  31. @mock.patch('qubesadmin.tools.qvm_backup_restore.input', create=True)
  32. @mock.patch('getpass.getpass')
  33. @mock.patch('qubesadmin.tools.qvm_backup_restore.BackupRestore')
  34. def test_000_simple(self, mock_backup, mock_getpass, mock_input):
  35. mock_getpass.return_value = 'testpass'
  36. mock_input.return_value = 'Y'
  37. vm1 = BackupVM()
  38. vm1.name = 'test-vm'
  39. vm1.backup_path = 'path/in/backup'
  40. vm1.template = None
  41. vm1.klass = 'StandaloneVM'
  42. vm1.label = 'red'
  43. mock_restore_info = {
  44. 1: BackupRestore.VMToRestore(vm1),
  45. }
  46. mock_backup.configure_mock(**{
  47. 'return_value.get_restore_summary.return_value': '',
  48. 'return_value.get_restore_info.return_value': mock_restore_info,
  49. })
  50. with mock.patch('qubesadmin.tools.qvm_backup_restore.handle_broken') \
  51. as mock_handle_broken:
  52. qubesadmin.tools.qvm_backup_restore.main(['/some/path'],
  53. app=self.app)
  54. mock_handle_broken.assert_called_once_with(
  55. self.app, mock.ANY, mock_restore_info)
  56. mock_backup.assert_called_once_with(
  57. self.app, '/some/path', None, 'testpass')
  58. self.assertAllCalled()
  59. @mock.patch('qubesadmin.tools.qvm_backup_restore.input', create=True)
  60. @mock.patch('getpass.getpass')
  61. @mock.patch('qubesadmin.tools.qvm_backup_restore.BackupRestore')
  62. def test_001_selected_vms(self, mock_backup, mock_getpass, mock_input):
  63. mock_getpass.return_value = 'testpass'
  64. mock_input.return_value = 'Y'
  65. vm1 = BackupVM()
  66. vm1.name = 'test-vm'
  67. vm1.backup_path = 'path/in/backup'
  68. vm1.template = None
  69. vm1.klass = 'StandaloneVM'
  70. vm1.label = 'red'
  71. vm2 = BackupVM()
  72. vm2.name = 'test-vm2'
  73. vm2.backup_path = 'path/in/backup2'
  74. vm2.template = None
  75. vm2.klass = 'StandaloneVM'
  76. vm2.label = 'red'
  77. mock_restore_info = {
  78. 1: BackupRestore.VMToRestore(vm1),
  79. 2: BackupRestore.VMToRestore(vm2),
  80. }
  81. exclude_list = []
  82. mock_backup.configure_mock(**{
  83. 'return_value.get_restore_summary.return_value': '',
  84. 'return_value.options.exclude': exclude_list,
  85. 'return_value.get_restore_info.return_value': mock_restore_info,
  86. })
  87. qubesadmin.tools.qvm_backup_restore.main(['/some/path', 'test-vm'],
  88. app=self.app)
  89. mock_backup.assert_called_once_with(
  90. self.app, '/some/path', None, 'testpass')
  91. self.assertEqual(exclude_list, ['test-vm2'])
  92. self.assertAllCalled()
  93. def test_010_handle_broken_no_problems(self):
  94. vm1 = BackupVM()
  95. vm1.name = 'test-vm'
  96. vm1.backup_path = 'path/in/backup'
  97. vm1.template = None
  98. vm1.klass = 'StandaloneVM'
  99. vm1.label = 'red'
  100. mock_restore_info = {
  101. 1: BackupRestore.VMToRestore(vm1),
  102. }
  103. mock_args = mock.Mock()
  104. self.app.log = mock.Mock()
  105. qubesadmin.tools.qvm_backup_restore.handle_broken(
  106. self.app, mock_args, mock_restore_info)
  107. self.assertEqual(self.app.log.mock_calls, [
  108. mock.call.info(
  109. 'The above VMs will be copied and added to your system.'),
  110. mock.call.info(
  111. 'Exisiting VMs will NOT be removed.'),
  112. ])
  113. def assertAppropriateLogging(self, missing_name, action):
  114. '''
  115. :param missing_name: NetVM or TemplateVM
  116. :param action: 'skip_broken', 'ignore_missing'
  117. :return:
  118. '''
  119. expected_calls = [
  120. mock.call.info(
  121. 'The above VMs will be copied and added to your system.'),
  122. mock.call.info(
  123. 'Exisiting VMs will NOT be removed.'),
  124. mock.call.warning(
  125. '*** One or more {}s are missing on the host! ***'.format(
  126. missing_name)),
  127. ]
  128. if action == 'skip_broken':
  129. expected_calls.append(
  130. mock.call.warning(
  131. 'Skipping broken entries: VMs that depend on missing {}s '
  132. 'will NOT be restored.'.format(missing_name))
  133. )
  134. elif action == 'ignore_missing':
  135. expected_calls.append(
  136. mock.call.warning(
  137. 'Ignoring missing entries: VMs that depend on missing '
  138. '{}s will have default value assigned.'.format(
  139. missing_name))
  140. )
  141. self.assertEqual(self.app.log.mock_calls, expected_calls)
  142. def test_011_handle_broken_missing_template(self):
  143. vm1 = BackupVM()
  144. vm1.name = 'test-vm'
  145. vm1.backup_path = 'path/in/backup'
  146. vm1.template = 'not-existing-template'
  147. vm1.klass = 'AppVM'
  148. vm1.label = 'red'
  149. mock_restore_info = {
  150. 1: BackupRestore.VMToRestore(vm1),
  151. }
  152. mock_restore_info[1].problems.add(
  153. BackupRestore.VMToRestore.MISSING_TEMPLATE)
  154. with self.subTest('skip_broken'):
  155. mock_args = mock.Mock()
  156. mock_args.skip_broken = True
  157. self.app.log = mock.Mock()
  158. qubesadmin.tools.qvm_backup_restore.handle_broken(
  159. self.app, mock_args, mock_restore_info)
  160. self.assertAppropriateLogging('TemplateVM', 'skip_broken')
  161. with self.subTest('ignore_missing'):
  162. mock_args = mock.Mock()
  163. mock_args.skip_broken = False
  164. mock_args.ignore_missing = True
  165. self.app.log = mock.Mock()
  166. qubesadmin.tools.qvm_backup_restore.handle_broken(
  167. self.app, mock_args, mock_restore_info)
  168. self.assertAppropriateLogging('TemplateVM', 'ignore_missing')
  169. with self.subTest('error'):
  170. mock_args = mock.Mock()
  171. mock_args.skip_broken = False
  172. mock_args.ignore_missing = False
  173. self.app.log = mock.Mock()
  174. with self.assertRaises(qubesadmin.exc.QubesException):
  175. qubesadmin.tools.qvm_backup_restore.handle_broken(
  176. self.app, mock_args, mock_restore_info)
  177. self.assertAppropriateLogging('TemplateVM', 'error')
  178. def test_012_handle_broken_missing_netvm(self):
  179. vm1 = BackupVM()
  180. vm1.name = 'test-vm'
  181. vm1.backup_path = 'path/in/backup'
  182. vm1.netvm = 'not-existing-netvm'
  183. vm1.klass = 'StandaloneVM'
  184. vm1.label = 'red'
  185. mock_restore_info = {
  186. 1: BackupRestore.VMToRestore(vm1),
  187. }
  188. mock_restore_info[1].problems.add(
  189. BackupRestore.VMToRestore.MISSING_NETVM)
  190. with self.subTest('skip_broken'):
  191. mock_args = mock.Mock()
  192. mock_args.skip_broken = True
  193. self.app.log = mock.Mock()
  194. qubesadmin.tools.qvm_backup_restore.handle_broken(
  195. self.app, mock_args, mock_restore_info)
  196. self.assertAppropriateLogging('NetVM', 'skip_broken')
  197. with self.subTest('ignore_missing'):
  198. mock_args = mock.Mock()
  199. mock_args.skip_broken = False
  200. mock_args.ignore_missing = True
  201. self.app.log = mock.Mock()
  202. qubesadmin.tools.qvm_backup_restore.handle_broken(
  203. self.app, mock_args, mock_restore_info)
  204. self.assertAppropriateLogging('NetVM', 'ignore_missing')
  205. with self.subTest('error'):
  206. mock_args = mock.Mock()
  207. mock_args.skip_broken = False
  208. mock_args.ignore_missing = False
  209. self.app.log = mock.Mock()
  210. with self.assertRaises(qubesadmin.exc.QubesException):
  211. qubesadmin.tools.qvm_backup_restore.handle_broken(
  212. self.app, mock_args, mock_restore_info)
  213. self.assertAppropriateLogging('NetVM', 'error')