qvm_backup.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 io
  21. import os
  22. import unittest.mock as mock
  23. import asyncio
  24. import qubesadmin.tests
  25. import qubesadmin.tests.tools
  26. import qubesadmin.tools.qvm_backup as qvm_backup
  27. class TC_00_qvm_backup(qubesadmin.tests.QubesTestCase):
  28. def test_000_write_backup_profile(self):
  29. args = qvm_backup.parser.parse_args(['/var/tmp'], app=self.app)
  30. profile = io.StringIO()
  31. qvm_backup.write_backup_profile(profile, args)
  32. expected_profile = (
  33. 'compression: true\n'
  34. 'destination_path: /var/tmp\n'
  35. 'destination_vm: dom0\n'
  36. 'include: null\n'
  37. )
  38. self.assertEqual(profile.getvalue(), expected_profile)
  39. def test_001_write_backup_profile_include(self):
  40. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  41. b'0\0dom0 class=AdminVM state=Running\n' \
  42. b'vm1 class=AppVM state=Halted\n' \
  43. b'vm2 class=AppVM state=Halted\n' \
  44. b'vm3 class=AppVM state=Halted\n'
  45. args = qvm_backup.parser.parse_args(['/var/tmp', 'vm1', 'vm2'],
  46. app=self.app)
  47. profile = io.StringIO()
  48. qvm_backup.write_backup_profile(profile, args)
  49. expected_profile = (
  50. 'compression: true\n'
  51. 'destination_path: /var/tmp\n'
  52. 'destination_vm: dom0\n'
  53. 'include:\n'
  54. '- vm1\n'
  55. '- vm2\n'
  56. )
  57. self.assertEqual(profile.getvalue(), expected_profile)
  58. self.assertAllCalled()
  59. def test_002_write_backup_profile_exclude(self):
  60. args = qvm_backup.parser.parse_args([
  61. '-x', 'vm1', '-x', 'vm2', '/var/tmp'], app=self.app)
  62. profile = io.StringIO()
  63. qvm_backup.write_backup_profile(profile, args)
  64. expected_profile = (
  65. 'compression: true\n'
  66. 'destination_path: /var/tmp\n'
  67. 'destination_vm: dom0\n'
  68. 'exclude:\n'
  69. '- vm1\n'
  70. '- vm2\n'
  71. 'include: null\n'
  72. )
  73. self.assertEqual(profile.getvalue(), expected_profile)
  74. def test_003_write_backup_with_passphrase(self):
  75. args = qvm_backup.parser.parse_args(['/var/tmp'], app=self.app)
  76. profile = io.StringIO()
  77. qvm_backup.write_backup_profile(profile, args, passphrase='test123')
  78. expected_profile = (
  79. 'compression: true\n'
  80. 'destination_path: /var/tmp\n'
  81. 'destination_vm: dom0\n'
  82. 'include: null\n'
  83. 'passphrase_text: test123\n'
  84. )
  85. self.assertEqual(profile.getvalue(), expected_profile)
  86. def test_004_write_backup_profile_no_compress(self):
  87. args = qvm_backup.parser.parse_args(['--no-compress', '/var/tmp'],
  88. app=self.app)
  89. profile = io.StringIO()
  90. qvm_backup.write_backup_profile(profile, args)
  91. expected_profile = (
  92. 'compression: false\n'
  93. 'destination_path: /var/tmp\n'
  94. 'destination_vm: dom0\n'
  95. 'include: null\n'
  96. )
  97. self.assertEqual(profile.getvalue(), expected_profile)
  98. @mock.patch('qubesadmin.tools.qvm_backup.backup_profile_dir', '/tmp')
  99. @mock.patch('qubesadmin.tools.qvm_backup.input', create=True)
  100. @mock.patch('getpass.getpass')
  101. def test_010_main_save_profile_cancel(self, mock_getpass, mock_input):
  102. asyncio.set_event_loop(asyncio.new_event_loop())
  103. mock_input.return_value = 'n'
  104. mock_getpass.return_value = 'some password'
  105. self.app.qubesd_connection_type = 'socket'
  106. self.app.expected_calls[('dom0', 'admin.backup.Info', 'test-profile',
  107. None)] = \
  108. b'0\0backup summary'
  109. profile_path = '/tmp/test-profile.conf'
  110. try:
  111. qvm_backup.main(['--save-profile', 'test-profile', '/var/tmp'],
  112. app=self.app)
  113. expected_profile = (
  114. 'compression: true\n'
  115. 'destination_path: /var/tmp\n'
  116. 'destination_vm: dom0\n'
  117. 'include: null\n'
  118. )
  119. with open(profile_path) as f:
  120. self.assertEqual(expected_profile, f.read())
  121. finally:
  122. os.unlink(profile_path)
  123. @mock.patch('qubesadmin.tools.qvm_backup.backup_profile_dir', '/tmp')
  124. @mock.patch('qubesadmin.tools.qvm_backup.input', create=True)
  125. @mock.patch('getpass.getpass')
  126. def test_011_main_save_profile_confirm(self, mock_getpass, mock_input):
  127. asyncio.set_event_loop(asyncio.new_event_loop())
  128. mock_input.return_value = 'y'
  129. mock_getpass.return_value = 'some password'
  130. self.app.qubesd_connection_type = 'socket'
  131. self.app.expected_calls[('dom0', 'admin.backup.Info', 'test-profile',
  132. None)] = \
  133. b'0\0backup summary'
  134. self.app.expected_calls[('dom0', 'admin.backup.Execute', 'test-profile',
  135. None)] = \
  136. b'0\0'
  137. profile_path = '/tmp/test-profile.conf'
  138. try:
  139. qvm_backup.main(['--save-profile', 'test-profile', '/var/tmp'],
  140. app=self.app)
  141. expected_profile = (
  142. 'compression: true\n'
  143. 'destination_path: /var/tmp\n'
  144. 'destination_vm: dom0\n'
  145. 'include: null\n'
  146. 'passphrase_text: some password\n'
  147. )
  148. with open(profile_path) as f:
  149. self.assertEqual(expected_profile, f.read())
  150. finally:
  151. os.unlink(profile_path)
  152. @mock.patch('qubesadmin.tools.qvm_backup.backup_profile_dir', '/tmp')
  153. @mock.patch('qubesadmin.tools.qvm_backup.input', create=True)
  154. @mock.patch('getpass.getpass')
  155. def test_012_main_existing_profile(self, mock_getpass, mock_input):
  156. asyncio.set_event_loop(asyncio.new_event_loop())
  157. mock_input.return_value = 'y'
  158. mock_getpass.return_value = 'some password'
  159. self.app.qubesd_connection_type = 'socket'
  160. self.app.expected_calls[('dom0', 'admin.backup.Info', 'test-profile',
  161. None)] = \
  162. b'0\0backup summary'
  163. self.app.expected_calls[('dom0', 'admin.backup.Execute', 'test-profile',
  164. None)] = \
  165. b'0\0'
  166. self.app.expected_calls[('dom0', 'admin.Events', None,
  167. None)] = \
  168. b'0\0'
  169. try:
  170. patch = mock.patch(
  171. 'qubesadmin.events.EventsDispatcher._get_events_reader')
  172. mock_events = patch.start()
  173. self.addCleanup(patch.stop)
  174. mock_events.side_effect = qubesadmin.tests.tools.MockEventsReader([
  175. b'1\0\0connection-established\0\0',
  176. b'1\0\0backup-progress\0backup_profile\0test-profile\0progress\x000'
  177. b'.25\0\0',
  178. ])
  179. except ImportError:
  180. pass
  181. qvm_backup.main(['--profile', 'test-profile'],
  182. app=self.app)
  183. self.assertFalse(os.path.exists('/tmp/test-profile.conf'))
  184. self.assertFalse(mock_getpass.called)
  185. @mock.patch('qubesadmin.tools.qvm_backup.backup_profile_dir', '/tmp')
  186. @mock.patch('qubesadmin.tools.qvm_backup.input', create=True)
  187. @mock.patch('getpass.getpass')
  188. def test_013_main_new_profile_vm(self, mock_getpass, mock_input):
  189. asyncio.set_event_loop(asyncio.new_event_loop())
  190. mock_input.return_value = 'y'
  191. mock_getpass.return_value = 'some password'
  192. self.app.qubesd_connection_type = 'qrexec'
  193. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  194. qvm_backup.main(['-x', 'vm1', '/var/tmp'],
  195. app=self.app)
  196. expected_output = (
  197. 'To perform the backup according to selected options, create '
  198. 'backup profile (/tmp/profile_name.conf) in dom0 with following '
  199. 'content:\n'
  200. 'compression: true\n'
  201. 'destination_path: /var/tmp\n'
  202. 'destination_vm: dom0\n'
  203. 'exclude:\n'
  204. '- vm1\n'
  205. 'include: null\n'
  206. '# specify backup passphrase below\n'
  207. 'passphrase_text: ...\n'
  208. )
  209. self.assertEqual(stdout.getvalue(), expected_output)
  210. @mock.patch('qubesadmin.tools.qvm_backup.backup_profile_dir', '/tmp')
  211. @mock.patch('qubesadmin.tools.qvm_backup.input', create=True)
  212. @mock.patch('getpass.getpass')
  213. def test_014_main_passphrase_file(self, mock_getpass, mock_input):
  214. asyncio.set_event_loop(asyncio.new_event_loop())
  215. mock_input.return_value = 'y'
  216. mock_getpass.return_value = 'some password'
  217. self.app.qubesd_connection_type = 'socket'
  218. self.app.expected_calls[('dom0', 'admin.backup.Info', 'test-profile',
  219. None)] = \
  220. b'0\0backup summary'
  221. self.app.expected_calls[('dom0', 'admin.backup.Execute', 'test-profile',
  222. None)] = \
  223. b'0\0'
  224. profile_path = '/tmp/test-profile.conf'
  225. try:
  226. stdin = io.StringIO()
  227. stdin.write('other passphrase\n')
  228. stdin.seek(0)
  229. with mock.patch('sys.stdin', stdin):
  230. qvm_backup.main(['--passphrase-file', '-', '--save-profile',
  231. 'test-profile', '/var/tmp'],
  232. app=self.app)
  233. expected_profile = (
  234. 'compression: true\n'
  235. 'destination_path: /var/tmp\n'
  236. 'destination_vm: dom0\n'
  237. 'include: null\n'
  238. 'passphrase_text: other passphrase\n'
  239. )
  240. with open(profile_path) as f:
  241. self.assertEqual(expected_profile, f.read())
  242. finally:
  243. os.unlink(profile_path)