qvm_shutdown.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 asyncio
  21. import unittest.mock
  22. import qubesadmin.tests
  23. import qubesadmin.tests.tools
  24. import qubesadmin.tools.qvm_shutdown
  25. class TC_00_qvm_shutdown(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.Shutdown', None, None)] = b'0\x00'
  32. qubesadmin.tools.qvm_shutdown.main(['some-vm'], app=self.app)
  33. self.assertAllCalled()
  34. def test_001_missing_vm(self):
  35. with self.assertRaises(SystemExit):
  36. with qubesadmin.tests.tools.StderrBuffer() as stderr:
  37. qubesadmin.tools.qvm_shutdown.main([], app=self.app)
  38. self.assertIn('one of the arguments --all VMNAME is required',
  39. stderr.getvalue())
  40. self.assertAllCalled()
  41. def test_002_invalid_vm(self):
  42. self.app.expected_calls[
  43. ('dom0', 'admin.vm.List', None, None)] = \
  44. b'0\x00some-vm class=AppVM state=Running\n'
  45. with self.assertRaises(SystemExit):
  46. with qubesadmin.tests.tools.StderrBuffer() as stderr:
  47. qubesadmin.tools.qvm_shutdown.main(['no-such-vm'], app=self.app)
  48. self.assertIn('no such domain', stderr.getvalue())
  49. self.assertAllCalled()
  50. def test_003_not_running(self):
  51. # TODO: some option to ignore this error?
  52. self.app.expected_calls[
  53. ('some-vm', 'admin.vm.Shutdown', None, None)] = \
  54. b'2\x00QubesVMNotStartedError\x00\x00Domain is powered off: ' \
  55. b'some-vm\x00'
  56. self.app.expected_calls[
  57. ('dom0', 'admin.vm.List', None, None)] = \
  58. b'0\x00some-vm class=AppVM state=Halted\n'
  59. qubesadmin.tools.qvm_shutdown.main(['some-vm'], app=self.app)
  60. self.assertAllCalled()
  61. def test_004_multiple_vms(self):
  62. self.app.expected_calls[
  63. ('some-vm', 'admin.vm.Shutdown', None, None)] = \
  64. b'0\x00'
  65. self.app.expected_calls[
  66. ('other-vm', 'admin.vm.Shutdown', None, None)] = \
  67. b'0\x00'
  68. self.app.expected_calls[
  69. ('dom0', 'admin.vm.List', None, None)] = \
  70. b'0\x00some-vm class=AppVM state=Running\n' \
  71. b'other-vm class=AppVM state=Running\n'
  72. qubesadmin.tools.qvm_shutdown.main(['some-vm', 'other-vm'], app=self.app),
  73. self.assertAllCalled()
  74. @unittest.skipUnless(qubesadmin.tools.qvm_shutdown.have_events,
  75. 'Events not present')
  76. def test_010_wait(self):
  77. '''test --wait option'''
  78. loop = asyncio.new_event_loop()
  79. asyncio.set_event_loop(loop)
  80. patch = unittest.mock.patch(
  81. 'qubesadmin.events.EventsDispatcher._get_events_reader')
  82. mock_events = patch.start()
  83. self.addCleanup(patch.stop)
  84. mock_events.side_effect = qubesadmin.tests.tools.MockEventsReader([
  85. b'1\0\0connection-established\0\0',
  86. b'1\0some-vm\0domain-shutdown\0\0',
  87. ])
  88. self.app.expected_calls[
  89. ('some-vm', 'admin.vm.Shutdown', None, None)] = \
  90. b'0\x00'
  91. self.app.expected_calls[
  92. ('dom0', 'admin.vm.List', None, None)] = \
  93. b'0\x00some-vm class=AppVM state=Running\n'
  94. self.app.expected_calls[
  95. ('some-vm', 'admin.vm.List', None, None)] = \
  96. [b'0\x00some-vm class=AppVM state=Running\n'] + \
  97. [b'0\x00some-vm class=AppVM state=Halted\n']
  98. qubesadmin.tools.qvm_shutdown.main(['--wait', 'some-vm'], app=self.app)
  99. self.assertAllCalled()
  100. @unittest.skipUnless(qubesadmin.tools.qvm_shutdown.have_events,
  101. 'Events not present')
  102. def test_012_wait_all(self):
  103. '''test --wait option, with multiple VMs'''
  104. loop = asyncio.new_event_loop()
  105. asyncio.set_event_loop(loop)
  106. patch = unittest.mock.patch(
  107. 'qubesadmin.events.EventsDispatcher._get_events_reader')
  108. mock_events = patch.start()
  109. self.addCleanup(patch.stop)
  110. mock_events.side_effect = qubesadmin.tests.tools.MockEventsReader([
  111. b'1\0\0connection-established\0\0',
  112. b'1\0sys-net\0domain-shutdown\0\0',
  113. b'1\0some-vm\0domain-shutdown\0\0',
  114. b'1\0other-vm\0domain-shutdown\0\0',
  115. ])
  116. self.app.expected_calls[
  117. ('some-vm', 'admin.vm.Shutdown', None, None)] = \
  118. b'0\x00'
  119. self.app.expected_calls[
  120. ('other-vm', 'admin.vm.Shutdown', None, None)] = \
  121. b'0\x00'
  122. self.app.expected_calls[
  123. ('sys-net', 'admin.vm.Shutdown', None, None)] = \
  124. b'0\x00'
  125. self.app.expected_calls[
  126. ('dom0', 'admin.vm.List', None, None)] = \
  127. b'0\x00' \
  128. b'sys-net class=AppVM state=Running\n' \
  129. b'some-vm class=AppVM state=Running\n' \
  130. b'other-vm class=AppVM state=Running\n'
  131. self.app.expected_calls[
  132. ('some-vm', 'admin.vm.List', None, None)] = \
  133. b'0\x00some-vm class=AppVM state=Halted\n'
  134. self.app.expected_calls[
  135. ('other-vm', 'admin.vm.List', None, None)] = \
  136. b'0\x00other-vm class=AppVM state=Halted\n'
  137. self.app.expected_calls[
  138. ('sys-net', 'admin.vm.List', None, None)] = \
  139. b'0\x00sys-net class=AppVM state=Halted\n'
  140. qubesadmin.tools.qvm_shutdown.main(['--wait', '--all'], app=self.app)
  141. self.assertAllCalled()
  142. @unittest.skipUnless(qubesadmin.tools.qvm_shutdown.have_events,
  143. 'Events not present')
  144. def test_013_wait_all_order(self):
  145. '''test --wait option, with some VMs requiring specific shutdown
  146. order'''
  147. loop = asyncio.new_event_loop()
  148. asyncio.set_event_loop(loop)
  149. patch = unittest.mock.patch(
  150. 'qubesadmin.events.EventsDispatcher._get_events_reader')
  151. mock_events = patch.start()
  152. self.addCleanup(patch.stop)
  153. mock_events.side_effect = qubesadmin.tests.tools.MockEventsReader([
  154. b'1\0\0connection-established\0\0',
  155. b'1\0sys-net\0domain-shutdown\0\0',
  156. b'1\0some-vm\0domain-shutdown\0\0',
  157. b'1\0other-vm\0domain-shutdown\0\0',
  158. ])
  159. self.app.expected_calls[
  160. ('some-vm', 'admin.vm.Shutdown', None, None)] = \
  161. b'0\x00'
  162. self.app.expected_calls[
  163. ('other-vm', 'admin.vm.Shutdown', None, None)] = \
  164. b'0\x00'
  165. self.app.expected_calls[
  166. ('sys-net', 'admin.vm.Shutdown', None, None)] = [
  167. b'2\x00QubesVMError\x00\x00Other Vms connected\x00',
  168. b'0\x00',
  169. ]
  170. self.app.expected_calls[
  171. ('dom0', 'admin.vm.List', None, None)] = \
  172. b'0\x00' \
  173. b'sys-net class=AppVM state=Running\n' \
  174. b'some-vm class=AppVM state=Running\n' \
  175. b'other-vm class=AppVM state=Running\n'
  176. self.app.expected_calls[
  177. ('some-vm', 'admin.vm.List', None, None)] = \
  178. b'0\x00some-vm class=AppVM state=Halted\n'
  179. self.app.expected_calls[
  180. ('other-vm', 'admin.vm.List', None, None)] = \
  181. b'0\x00other-vm class=AppVM state=Halted\n'
  182. self.app.expected_calls[
  183. ('sys-net', 'admin.vm.List', None, None)] = \
  184. b'0\x00sys-net class=AppVM state=Halted\n'
  185. qubesadmin.tools.qvm_shutdown.main(['--wait', '--all'], app=self.app)
  186. self.assertAllCalled()
  187. def test_014_wait_all_order_no_events(self):
  188. '''test --wait option, with some VMs requiring specific shutdown
  189. order'''
  190. self.app.expected_calls[
  191. ('some-vm', 'admin.vm.Shutdown', None, None)] = \
  192. b'0\x00'
  193. self.app.expected_calls[
  194. ('other-vm', 'admin.vm.Shutdown', None, None)] = \
  195. b'0\x00'
  196. self.app.expected_calls[
  197. ('sys-net', 'admin.vm.Shutdown', None, None)] = [
  198. b'2\x00QubesVMError\x00\x00Other Vms connected\x00',
  199. b'0\x00',
  200. ]
  201. self.app.expected_calls[
  202. ('dom0', 'admin.vm.List', None, None)] = \
  203. b'0\x00' \
  204. b'sys-net class=AppVM state=Running\n' \
  205. b'some-vm class=AppVM state=Running\n' \
  206. b'other-vm class=AppVM state=Running\n'
  207. self.app.expected_calls[
  208. ('some-vm', 'admin.vm.List', None, None)] = \
  209. [b'0\x00some-vm class=AppVM state=Running\n',
  210. b'0\x00some-vm class=AppVM state=Halted\n',
  211. b'0\x00some-vm class=AppVM state=Halted\n']
  212. self.app.expected_calls[
  213. ('other-vm', 'admin.vm.List', None, None)] = \
  214. [b'0\x00other-vm class=AppVM state=Running\n',
  215. b'0\x00other-vm class=AppVM state=Halted\n',
  216. b'0\x00other-vm class=AppVM state=Halted\n']
  217. self.app.expected_calls[
  218. ('sys-net', 'admin.vm.List', None, None)] = \
  219. [b'0\x00sys-net class=AppVM state=Running\n',
  220. b'0\x00sys-net class=AppVM state=Halted\n',
  221. b'0\x00sys-net class=AppVM state=Halted\n']
  222. with unittest.mock.patch('qubesadmin.tools.qvm_shutdown.have_events',
  223. False):
  224. qubesadmin.tools.qvm_shutdown.main(['--wait', '--all'], app=self.app)
  225. self.assertAllCalled()