qvm_shutdown.py 8.8 KB

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