qvm_check.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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.tools.qvm_check
  22. class TC_00_qvm_check(qubesadmin.tests.QubesTestCase):
  23. def test_000_exists(self):
  24. self.app.expected_calls[
  25. ('dom0', 'admin.vm.List', None, None)] = \
  26. b'0\x00some-vm class=AppVM state=Running\n'
  27. self.assertEqual(
  28. qubesadmin.tools.qvm_check.main(['some-vm'], app=self.app),
  29. 0)
  30. self.assertAllCalled()
  31. def test_001_exists_multi(self):
  32. self.app.expected_calls[
  33. ('dom0', 'admin.vm.List', None, None)] = \
  34. b'0\x00some-vm class=AppVM state=Running\n' \
  35. b'other-vm class=AppVM state=Running\n'
  36. self.assertEqual(
  37. qubesadmin.tools.qvm_check.main(['some-vm', 'other-vm'],
  38. app=self.app),
  39. 0)
  40. self.assertAllCalled()
  41. def test_002_exists_verbose(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 qubesadmin.tests.tools.StdoutBuffer() as stdout:
  46. self.assertEqual(
  47. qubesadmin.tools.qvm_check.main(['some-vm'], app=self.app),
  48. 0)
  49. self.assertEqual(stdout.getvalue(),
  50. 'VM some-vm exists\n')
  51. self.assertAllCalled()
  52. def test_003_exists_multi_verbose(self):
  53. self.app.expected_calls[
  54. ('dom0', 'admin.vm.List', None, None)] = \
  55. b'0\x00some-vm class=AppVM state=Running\n' \
  56. b'other-vm class=AppVM state=Running\n'
  57. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  58. self.assertEqual(
  59. qubesadmin.tools.qvm_check.main(['some-vm', 'other-vm'],
  60. app=self.app),
  61. 0)
  62. self.assertEqual(stdout.getvalue(),
  63. 'VMs other-vm, some-vm exist\n')
  64. self.assertAllCalled()
  65. def test_004_running_verbose(self):
  66. self.app.expected_calls[
  67. ('dom0', 'admin.vm.List', None, None)] = \
  68. b'0\x00some-vm class=AppVM state=Running\n' \
  69. b'some-vm2 class=AppVM state=Running\n' \
  70. b'some-vm3 class=AppVM state=Halted\n'
  71. self.app.expected_calls[
  72. ('some-vm', 'admin.vm.List', None, None)] = \
  73. b'0\x00some-vm class=AppVM state=Running\n'
  74. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  75. self.assertEqual(
  76. qubesadmin.tools.qvm_check.main(['--running',
  77. 'some-vm'], app=self.app),
  78. 0)
  79. self.assertEqual(stdout.getvalue(),
  80. 'VM some-vm is running\n')
  81. self.assertAllCalled()
  82. def test_005_running_multi_verbose(self):
  83. self.app.expected_calls[
  84. ('dom0', 'admin.vm.List', None, None)] = \
  85. b'0\x00some-vm class=AppVM state=Running\n' \
  86. b'some-vm2 class=AppVM state=Running\n' \
  87. b'some-vm3 class=AppVM state=Halted\n'
  88. self.app.expected_calls[
  89. ('some-vm', 'admin.vm.List', None, None)] = \
  90. b'0\x00some-vm class=AppVM state=Running\n'
  91. self.app.expected_calls[
  92. ('some-vm2', 'admin.vm.List', None, None)] = \
  93. b'0\x00some-vm2 class=AppVM state=Running\n'
  94. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  95. self.assertEqual(
  96. qubesadmin.tools.qvm_check.main(['--running',
  97. 'some-vm', 'some-vm2'],
  98. app=self.app),
  99. 0)
  100. self.assertEqual(stdout.getvalue(),
  101. 'VMs some-vm, some-vm2 are running\n')
  102. self.assertAllCalled()
  103. def test_006_running_multi_verbose2(self):
  104. self.app.expected_calls[
  105. ('dom0', 'admin.vm.List', None, None)] = \
  106. b'0\x00some-vm class=AppVM state=Running\n' \
  107. b'some-vm2 class=AppVM state=Running\n' \
  108. b'some-vm3 class=AppVM state=Halted\n'
  109. self.app.expected_calls[
  110. ('some-vm', 'admin.vm.List', None, None)] = \
  111. b'0\x00some-vm class=AppVM state=Running\n'
  112. self.app.expected_calls[
  113. ('some-vm2', 'admin.vm.List', None, None)] = \
  114. b'0\x00some-vm2 class=AppVM state=Running\n'
  115. self.app.expected_calls[
  116. ('some-vm3', 'admin.vm.List', None, None)] = \
  117. b'0\x00some-vm3 class=AppVM state=Halted\n'
  118. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  119. self.assertEqual(
  120. qubesadmin.tools.qvm_check.main(['--running',
  121. '--all'],
  122. app=self.app),
  123. 0)
  124. self.assertEqual(stdout.getvalue(),
  125. 'VMs some-vm, some-vm2 are running\n')
  126. self.assertAllCalled()
  127. def test_007_not_running_verbose(self):
  128. self.app.expected_calls[
  129. ('dom0', 'admin.vm.List', None, None)] = \
  130. b'0\x00some-vm class=AppVM state=Running\n' \
  131. b'some-vm2 class=AppVM state=Running\n' \
  132. b'some-vm3 class=AppVM state=Halted\n'
  133. self.app.expected_calls[
  134. ('some-vm3', 'admin.vm.List', None, None)] = \
  135. b'0\x00some-vm3 class=AppVM state=Halted\n'
  136. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  137. self.assertEqual(
  138. qubesadmin.tools.qvm_check.main(['--running',
  139. 'some-vm3'],
  140. app=self.app),
  141. 1)
  142. self.assertEqual(stdout.getvalue(),
  143. 'None of given VM is running\n')
  144. self.assertAllCalled()
  145. def test_008_paused(self):
  146. self.app.expected_calls[
  147. ('dom0', 'admin.vm.List', None, None)] = \
  148. b'0\x00some-vm class=AppVM state=Running\n' \
  149. b'some-vm2 class=AppVM state=Paused\n' \
  150. b'some-vm3 class=AppVM state=Halted\n'
  151. self.app.expected_calls[
  152. ('some-vm2', 'admin.vm.List', None, None)] = \
  153. b'0\x00some-vm2 class=AppVM state=Paused\n'
  154. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  155. self.assertEqual(
  156. qubesadmin.tools.qvm_check.main(['--paused',
  157. 'some-vm2'],
  158. app=self.app),
  159. 0)
  160. self.assertEqual(stdout.getvalue(),
  161. 'VM some-vm2 is paused\n')
  162. self.assertAllCalled()
  163. def test_009_paused_multi(self):
  164. self.app.expected_calls[
  165. ('dom0', 'admin.vm.List', None, None)] = \
  166. b'0\x00some-vm class=AppVM state=Running\n' \
  167. b'some-vm2 class=AppVM state=Paused\n' \
  168. b'some-vm3 class=AppVM state=Halted\n'
  169. self.app.expected_calls[
  170. ('some-vm2', 'admin.vm.List', None, None)] = \
  171. b'0\x00some-vm2 class=AppVM state=Paused\n'
  172. self.app.expected_calls[
  173. ('some-vm', 'admin.vm.List', None, None)] = \
  174. b'0\x00some-vm class=AppVM state=Running\n'
  175. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  176. self.assertEqual(
  177. qubesadmin.tools.qvm_check.main(['--paused',
  178. 'some-vm2', 'some-vm'],
  179. app=self.app),
  180. 0)
  181. self.assertEqual(stdout.getvalue(),
  182. 'VM some-vm2 is paused\n')
  183. self.assertAllCalled()
  184. def test_010_template(self):
  185. self.app.expected_calls[
  186. ('dom0', 'admin.vm.List', None, None)] = \
  187. b'0\x00some-vm class=AppVM state=Running\n' \
  188. b'some-vm2 class=AppVM state=Paused\n' \
  189. b'some-vm3 class=TemplateVM state=Halted\n'
  190. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  191. self.assertEqual(
  192. qubesadmin.tools.qvm_check.main(['--template',
  193. 'some-vm3'],
  194. app=self.app),
  195. 0)
  196. self.assertEqual(stdout.getvalue(),
  197. 'VM some-vm3 is a template\n')
  198. self.assertAllCalled()
  199. def test_011_template_multi(self):
  200. self.app.expected_calls[
  201. ('dom0', 'admin.vm.List', None, None)] = \
  202. b'0\x00some-vm class=AppVM state=Running\n' \
  203. b'some-vm2 class=AppVM state=Paused\n' \
  204. b'some-vm3 class=TemplateVM state=Halted\n'
  205. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  206. self.assertEqual(
  207. qubesadmin.tools.qvm_check.main(['--template',
  208. 'some-vm2', 'some-vm3'],
  209. app=self.app),
  210. 0)
  211. self.assertEqual(stdout.getvalue(),
  212. 'VM some-vm3 is a template\n')
  213. self.assertAllCalled()