qvm_backup_restore.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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(mock_backup.return_value.options.exclude, ['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. mock_args.verify_only = False
  105. self.app.log = mock.Mock()
  106. qubesadmin.tools.qvm_backup_restore.handle_broken(
  107. self.app, mock_args, mock_restore_info)
  108. self.assertEqual(self.app.log.mock_calls, [
  109. mock.call.info(
  110. 'The above VMs will be copied and added to your system.'),
  111. mock.call.info(
  112. 'Exisiting VMs will NOT be removed.'),
  113. ])
  114. def assertAppropriateLogging(self, missing_name, action):
  115. '''
  116. :param missing_name: NetVM or TemplateVM
  117. :param action: 'skip_broken', 'ignore_missing'
  118. :return:
  119. '''
  120. expected_calls = [
  121. mock.call.info(
  122. 'The above VMs will be copied and added to your system.'),
  123. mock.call.info(
  124. 'Exisiting VMs will NOT be removed.'),
  125. mock.call.warning(
  126. '*** One or more {}s are missing on the host! ***'.format(
  127. missing_name)),
  128. ]
  129. if action == 'skip_broken':
  130. expected_calls.append(
  131. mock.call.warning(
  132. 'Skipping broken entries: VMs that depend on missing {}s '
  133. 'will NOT be restored.'.format(missing_name))
  134. )
  135. elif action == 'ignore_missing':
  136. expected_calls.append(
  137. mock.call.warning(
  138. 'Ignoring missing entries: VMs that depend on missing '
  139. '{}s will have default value assigned.'.format(
  140. missing_name))
  141. )
  142. self.assertEqual(self.app.log.mock_calls, expected_calls)
  143. def test_011_handle_broken_missing_template(self):
  144. vm1 = BackupVM()
  145. vm1.name = 'test-vm'
  146. vm1.backup_path = 'path/in/backup'
  147. vm1.template = 'not-existing-template'
  148. vm1.klass = 'AppVM'
  149. vm1.label = 'red'
  150. mock_restore_info = {
  151. 1: BackupRestore.VMToRestore(vm1),
  152. }
  153. mock_restore_info[1].problems.add(
  154. BackupRestore.VMToRestore.MISSING_TEMPLATE)
  155. with self.subTest('skip_broken'):
  156. mock_args = mock.Mock()
  157. mock_args.skip_broken = True
  158. mock_args.verify_only = False
  159. self.app.log = mock.Mock()
  160. qubesadmin.tools.qvm_backup_restore.handle_broken(
  161. self.app, mock_args, mock_restore_info)
  162. self.assertAppropriateLogging('TemplateVM', 'skip_broken')
  163. with self.subTest('ignore_missing'):
  164. mock_args = mock.Mock()
  165. mock_args.skip_broken = False
  166. mock_args.ignore_missing = True
  167. mock_args.verify_only = False
  168. self.app.log = mock.Mock()
  169. qubesadmin.tools.qvm_backup_restore.handle_broken(
  170. self.app, mock_args, mock_restore_info)
  171. self.assertAppropriateLogging('TemplateVM', 'ignore_missing')
  172. with self.subTest('error'):
  173. mock_args = mock.Mock()
  174. mock_args.skip_broken = False
  175. mock_args.ignore_missing = False
  176. mock_args.verify_only = False
  177. self.app.log = mock.Mock()
  178. with self.assertRaises(qubesadmin.exc.QubesException):
  179. qubesadmin.tools.qvm_backup_restore.handle_broken(
  180. self.app, mock_args, mock_restore_info)
  181. self.assertAppropriateLogging('TemplateVM', 'error')
  182. def test_012_handle_broken_missing_netvm(self):
  183. vm1 = BackupVM()
  184. vm1.name = 'test-vm'
  185. vm1.backup_path = 'path/in/backup'
  186. vm1.netvm = 'not-existing-netvm'
  187. vm1.klass = 'StandaloneVM'
  188. vm1.label = 'red'
  189. mock_restore_info = {
  190. 1: BackupRestore.VMToRestore(vm1),
  191. }
  192. mock_restore_info[1].problems.add(
  193. BackupRestore.VMToRestore.MISSING_NETVM)
  194. with self.subTest('skip_broken'):
  195. mock_args = mock.Mock()
  196. mock_args.skip_broken = True
  197. mock_args.verify_only = False
  198. self.app.log = mock.Mock()
  199. qubesadmin.tools.qvm_backup_restore.handle_broken(
  200. self.app, mock_args, mock_restore_info)
  201. self.assertAppropriateLogging('NetVM', 'skip_broken')
  202. with self.subTest('ignore_missing'):
  203. mock_args = mock.Mock()
  204. mock_args.skip_broken = False
  205. mock_args.ignore_missing = True
  206. mock_args.verify_only = False
  207. self.app.log = mock.Mock()
  208. qubesadmin.tools.qvm_backup_restore.handle_broken(
  209. self.app, mock_args, mock_restore_info)
  210. self.assertAppropriateLogging('NetVM', 'ignore_missing')
  211. with self.subTest('error'):
  212. mock_args = mock.Mock()
  213. mock_args.skip_broken = False
  214. mock_args.ignore_missing = False
  215. mock_args.verify_only = False
  216. self.app.log = mock.Mock()
  217. with self.assertRaises(qubesadmin.exc.QubesException):
  218. qubesadmin.tools.qvm_backup_restore.handle_broken(
  219. self.app, mock_args, mock_restore_info)
  220. self.assertAppropriateLogging('NetVM', 'error')