qvm_start.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 unittest.mock
  21. import subprocess
  22. import qubesadmin.tests
  23. import qubesadmin.tests.tools
  24. import qubesadmin.tools.qvm_start
  25. class TC_00_qvm_start(qubesadmin.tests.QubesTestCase):
  26. def test_000_with_vm(self):
  27. self.app.expected_calls[
  28. ('dom0', 'admin.vm.List', None, None)] = \
  29. b'0\x00some-vm class=AppVM state=Running\n'
  30. self.app.expected_calls[
  31. ('some-vm', 'admin.vm.List', None, None)] = \
  32. b'0\x00some-vm class=AppVM state=Halted\n'
  33. self.app.expected_calls[
  34. ('some-vm', 'admin.vm.Start', None, None)] = b'0\x00'
  35. qubesadmin.tools.qvm_start.main(['some-vm'], app=self.app)
  36. self.assertAllCalled()
  37. def test_001_missing_vm(self):
  38. with self.assertRaises(SystemExit):
  39. with qubesadmin.tests.tools.StderrBuffer() as stderr:
  40. qubesadmin.tools.qvm_start.main([], app=self.app)
  41. self.assertIn('one of the arguments --all VMNAME is required',
  42. stderr.getvalue())
  43. self.assertAllCalled()
  44. def test_002_invalid_vm(self):
  45. self.app.expected_calls[
  46. ('dom0', 'admin.vm.List', None, None)] = \
  47. b'0\x00some-vm class=AppVM state=Running\n'
  48. with self.assertRaises(SystemExit):
  49. with qubesadmin.tests.tools.StderrBuffer() as stderr:
  50. qubesadmin.tools.qvm_start.main(['no-such-vm'], app=self.app)
  51. self.assertIn('no such domain', stderr.getvalue())
  52. self.assertAllCalled()
  53. def test_003_already_running(self):
  54. self.app.expected_calls[
  55. ('dom0', 'admin.vm.List', None, None)] = \
  56. b'0\x00some-vm class=AppVM state=Running\n'
  57. self.app.expected_calls[
  58. ('some-vm', 'admin.vm.List', None, None)] = \
  59. b'0\x00some-vm class=AppVM state=Runnin\n'
  60. self.assertEqual(
  61. qubesadmin.tools.qvm_start.main(['some-vm'], app=self.app),
  62. 1)
  63. self.assertAllCalled()
  64. def test_010_drive_cdrom(self):
  65. self.app.expected_calls[
  66. ('dom0', 'admin.vm.List', None, None)] = \
  67. b'0\x00dom0 class=AdminVM state=Running\n' \
  68. b'some-vm class=AppVM state=Running\n'
  69. self.app.expected_calls[
  70. ('some-vm', 'admin.vm.List', None, None)] = \
  71. b'0\x00some-vm class=AppVM state=Halted\n'
  72. self.app.expected_calls[
  73. ('dom0', 'admin.vm.device.block.Available', None, None)] = \
  74. b'0\x00sr0\n'
  75. self.app.expected_calls[
  76. ('some-vm', 'admin.vm.Start', None, None)] = b'0\x00'
  77. self.app.expected_calls[
  78. ('some-vm', 'admin.vm.device.block.Attach', 'dom0+sr0',
  79. b'devtype=cdrom persistent=True read-only=True')] = b'0\x00'
  80. self.app.expected_calls[
  81. ('some-vm', 'admin.vm.device.block.Set.persistent', 'dom0+sr0',
  82. b'False')] = b'0\x00'
  83. qubesadmin.tools.qvm_start.main(['--cdrom=dom0:sr0', 'some-vm'],
  84. app=self.app)
  85. self.assertAllCalled()
  86. def test_011_drive_disk(self):
  87. self.app.expected_calls[
  88. ('dom0', 'admin.vm.List', None, None)] = \
  89. b'0\x00dom0 class=AdminVM state=Running\n' \
  90. b'some-vm class=AppVM state=Running\n'
  91. self.app.expected_calls[
  92. ('some-vm', 'admin.vm.List', None, None)] = \
  93. b'0\x00some-vm class=AppVM state=Halted\n'
  94. self.app.expected_calls[
  95. ('dom0', 'admin.vm.device.block.Available', None, None)] = \
  96. b'0\x00sdb1\n'
  97. self.app.expected_calls[
  98. ('some-vm', 'admin.vm.Start', None, None)] = b'0\x00'
  99. self.app.expected_calls[
  100. ('some-vm', 'admin.vm.device.block.Attach', 'dom0+sdb1',
  101. b'devtype=disk persistent=True read-only=False')] = b'0\x00'
  102. self.app.expected_calls[
  103. ('some-vm', 'admin.vm.device.block.Set.persistent', 'dom0+sdb1',
  104. b'False')] = b'0\x00'
  105. qubesadmin.tools.qvm_start.main(['--hd=dom0:sdb1', 'some-vm'],
  106. app=self.app)
  107. self.assertAllCalled()
  108. def test_012_drive_disk(self):
  109. self.app.expected_calls[
  110. ('dom0', 'admin.vm.List', None, None)] = \
  111. b'0\x00dom0 class=AdminVM state=Running\n' \
  112. b'some-vm class=AppVM state=Running\n'
  113. self.app.expected_calls[
  114. ('some-vm', 'admin.vm.List', None, None)] = \
  115. b'0\x00some-vm class=AppVM state=Halted\n'
  116. self.app.expected_calls[
  117. ('dom0', 'admin.vm.device.block.Available', None, None)] = \
  118. b'0\x00sdb1\n'
  119. self.app.expected_calls[
  120. ('some-vm', 'admin.vm.Start', None, None)] = b'0\x00'
  121. self.app.expected_calls[
  122. ('some-vm', 'admin.vm.device.block.Attach', 'dom0+sdb1',
  123. b'devtype=disk persistent=True read-only=False')] = b'0\x00'
  124. self.app.expected_calls[
  125. ('some-vm', 'admin.vm.device.block.Set.persistent', 'dom0+sdb1',
  126. b'False')] = b'0\x00'
  127. qubesadmin.tools.qvm_start.main(['--drive=hd:dom0:sdb1', 'some-vm'],
  128. app=self.app)
  129. self.assertAllCalled()
  130. @unittest.mock.patch('subprocess.check_output')
  131. def test_013_drive_loop_local(self, mock_subprocess):
  132. self.app.expected_calls[
  133. ('dom0', 'admin.vm.List', None, None)] = \
  134. b'0\x00dom0 class=AdminVM state=Running\n' \
  135. b'some-vm class=AppVM state=Running\n'
  136. self.app.expected_calls[
  137. ('some-vm', 'admin.vm.List', None, None)] = \
  138. b'0\x00some-vm class=AppVM state=Halted\n'
  139. self.app.expected_calls[
  140. ('dom0', 'admin.vm.device.block.Available', None, None)] = \
  141. b'0\x00loop12\n'
  142. self.app.expected_calls[
  143. ('some-vm', 'admin.vm.Start', None, None)] = b'0\x00'
  144. self.app.expected_calls[
  145. ('some-vm', 'admin.vm.device.block.Attach', 'dom0+loop12',
  146. b'devtype=cdrom persistent=True read-only=True')] = b'0\x00'
  147. self.app.expected_calls[
  148. ('some-vm', 'admin.vm.device.block.Set.persistent', 'dom0+loop12',
  149. b'False')] = b'0\x00'
  150. mock_subprocess.return_value = b'/dev/loop12'
  151. qubesadmin.tools.qvm_start.main([
  152. '--cdrom=dom0:/home/some/image.iso',
  153. 'some-vm'],
  154. app=self.app)
  155. self.assertAllCalled()
  156. mock_subprocess.assert_called_once_with(
  157. ['sudo', 'losetup', '-f', '--show', '/home/some/image.iso'])
  158. def test_014_drive_loop_remote(self):
  159. self.app.expected_calls[
  160. ('dom0', 'admin.vm.List', None, None)] = \
  161. b'0\x00dom0 class=AdminVM state=Running\n' \
  162. b'other-vm class=AppVM state=Running\n' \
  163. b'some-vm class=AppVM state=Running\n'
  164. self.app.expected_calls[
  165. ('some-vm', 'admin.vm.List', None, None)] = \
  166. b'0\x00some-vm class=AppVM state=Halted\n'
  167. self.app.expected_calls[
  168. ('other-vm', 'admin.vm.device.block.Available', None, None)] = \
  169. b'0\x00loop7\n'
  170. self.app.expected_calls[
  171. ('some-vm', 'admin.vm.Start', None, None)] = b'0\x00'
  172. self.app.expected_calls[
  173. ('some-vm', 'admin.vm.device.block.Attach', 'other-vm+loop7',
  174. b'devtype=cdrom persistent=True read-only=True')] = b'0\x00'
  175. self.app.expected_calls[
  176. ('some-vm', 'admin.vm.device.block.Set.persistent',
  177. 'other-vm+loop7',
  178. b'False')] = b'0\x00'
  179. with unittest.mock.patch.object(self.app.domains['other-vm'], 'run') \
  180. as mock_run:
  181. mock_run.return_value = (b'/dev/loop7', b'')
  182. qubesadmin.tools.qvm_start.main([
  183. '--cdrom=other-vm:/home/some/image.iso',
  184. 'some-vm'],
  185. app=self.app)
  186. mock_run.assert_called_once_with(
  187. 'losetup -f --show /home/some/image.iso',
  188. user='root')
  189. self.assertAllCalled()
  190. def test_015_drive_failed_start(self):
  191. self.app.expected_calls[
  192. ('dom0', 'admin.vm.List', None, None)] = \
  193. b'0\x00dom0 class=AdminVM state=Running\n' \
  194. b'other-vm class=AppVM state=Running\n' \
  195. b'some-vm class=AppVM state=Running\n'
  196. self.app.expected_calls[
  197. ('some-vm', 'admin.vm.List', None, None)] = \
  198. b'0\x00some-vm class=AppVM state=Halted\n'
  199. self.app.expected_calls[
  200. ('some-vm', 'admin.vm.device.block.Attach', 'other-vm+loop7',
  201. b'devtype=cdrom persistent=True read-only=True')] = b'0\x00'
  202. self.app.expected_calls[
  203. ('some-vm', 'admin.vm.Start', None, None)] = \
  204. b'2\x00QubesException\x00\x00An error occurred\x00'
  205. self.app.expected_calls[
  206. ('some-vm', 'admin.vm.device.block.Detach',
  207. 'other-vm+loop7', None)] = b'0\x00'
  208. qubesadmin.tools.qvm_start.main([
  209. '--cdrom=other-vm:loop7',
  210. 'some-vm'],
  211. app=self.app)
  212. self.assertAllCalled()
  213. def test_016_drive_failed_attach(self):
  214. self.app.expected_calls[
  215. ('dom0', 'admin.vm.List', None, None)] = \
  216. b'0\x00dom0 class=AdminVM state=Running\n' \
  217. b'other-vm class=AppVM state=Running\n' \
  218. b'some-vm class=AppVM state=Running\n'
  219. self.app.expected_calls[
  220. ('some-vm', 'admin.vm.List', None, None)] = \
  221. b'0\x00some-vm class=AppVM state=Halted\n'
  222. self.app.expected_calls[
  223. ('some-vm', 'admin.vm.device.block.Attach', 'other-vm+loop7',
  224. b'devtype=cdrom persistent=True read-only=True')] = \
  225. b'2\x00QubesException\x00\x00An error occurred\x00'
  226. retcode = qubesadmin.tools.qvm_start.main([
  227. '--cdrom=other-vm:loop7',
  228. 'some-vm'],
  229. app=self.app)
  230. self.assertEqual(retcode, 1)
  231. self.assertAllCalled()