qvm_check.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 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), 0)
  29. self.assertAllCalled()
  30. def test_001_exists_multi(self):
  31. self.app.expected_calls[
  32. ('dom0', 'admin.vm.List', None, None)] = \
  33. b'0\x00some-vm class=AppVM state=Running\n' \
  34. b'other-vm class=AppVM state=Running\n'
  35. self.assertEqual(
  36. qubesadmin.tools.qvm_check.main(['some-vm', 'other-vm'],
  37. app=self.app), 0)
  38. self.assertAllCalled()
  39. def test_002_exists_verbose(self):
  40. self.app.expected_calls[
  41. ('dom0', 'admin.vm.List', None, None)] = \
  42. b'0\x00some-vm class=AppVM state=Running\n'
  43. with self.assertLogs() as logger:
  44. self.assertEqual(
  45. qubesadmin.tools.qvm_check.main(['some-vm'], app=self.app), 0)
  46. self.assertEqual(logger.output, ['INFO:qvm-check:some-vm: exists'])
  47. self.assertAllCalled()
  48. def test_003_exists_multi_verbose(self):
  49. self.app.expected_calls[
  50. ('dom0', 'admin.vm.List', None, None)] = \
  51. b'0\x00some-vm class=AppVM state=Running\n' \
  52. b'other-vm class=AppVM state=Running\n'
  53. with self.assertLogs() as logger:
  54. self.assertEqual(
  55. qubesadmin.tools.qvm_check.main(['some-vm', 'other-vm'],
  56. app=self.app), 0)
  57. self.assertEqual(logger.output, ['INFO:qvm-check:other-vm: exists',
  58. 'INFO:qvm-check:some-vm: exists'])
  59. self.assertAllCalled()
  60. def test_004_running_verbose(self):
  61. self.app.expected_calls[
  62. ('dom0', 'admin.vm.List', None, None)] = \
  63. b'0\x00some-vm class=AppVM state=Running\n' \
  64. b'some-vm2 class=AppVM state=Running\n' \
  65. b'some-vm3 class=AppVM state=Halted\n'
  66. self.app.expected_calls[
  67. ('some-vm', 'admin.vm.CurrentState', None, None)] = \
  68. b'0\x00power_state=Running'
  69. with self.assertLogs() as logger:
  70. self.assertEqual(
  71. qubesadmin.tools.qvm_check.main(['--running', 'some-vm'],
  72. app=self.app), 0)
  73. self.assertEqual(logger.output, ['INFO:qvm-check:some-vm: running'])
  74. self.assertAllCalled()
  75. def test_005_running_multi_verbose(self):
  76. self.app.expected_calls[
  77. ('dom0', 'admin.vm.List', None, None)] = \
  78. b'0\x00some-vm class=AppVM state=Running\n' \
  79. b'some-vm2 class=AppVM state=Running\n' \
  80. b'some-vm3 class=AppVM state=Halted\n'
  81. self.app.expected_calls[
  82. ('some-vm', 'admin.vm.CurrentState', None, None)] = \
  83. b'0\x00power_state=Running'
  84. self.app.expected_calls[
  85. ('some-vm2', 'admin.vm.CurrentState', None, None)] = \
  86. b'0\x00power_state=Running'
  87. with self.assertLogs() as logger:
  88. self.assertEqual(qubesadmin.tools.qvm_check.main(
  89. ['--running', 'some-vm', 'some-vm2'], app=self.app), 0)
  90. self.assertEqual(logger.output, ['INFO:qvm-check:some-vm: running',
  91. 'INFO:qvm-check:some-vm2: running']
  92. )
  93. self.assertAllCalled()
  94. def test_006_running_multi_verbose2(self):
  95. self.app.expected_calls[
  96. ('dom0', 'admin.vm.List', None, None)] = \
  97. b'0\x00some-vm class=AppVM state=Running\n' \
  98. b'some-vm2 class=AppVM state=Running\n' \
  99. b'some-vm3 class=AppVM state=Halted\n'
  100. self.app.expected_calls[
  101. ('some-vm', 'admin.vm.CurrentState', None, None)] = \
  102. b'0\x00power_state=Running'
  103. self.app.expected_calls[
  104. ('some-vm2', 'admin.vm.CurrentState', None, None)] = \
  105. b'0\x00power_state=Running'
  106. self.app.expected_calls[
  107. ('some-vm3', 'admin.vm.CurrentState', None, None)] = \
  108. b'0\x00power_state=Halted'
  109. with self.assertLogs() as logger:
  110. self.assertEqual(
  111. qubesadmin.tools.qvm_check.main(['--running', '--all'],
  112. app=self.app), 3)
  113. self.assertEqual(logger.output, ['INFO:qvm-check:some-vm: running',
  114. 'INFO:qvm-check:some-vm2: running']
  115. )
  116. self.assertAllCalled()
  117. def test_007_not_running_verbose(self):
  118. self.app.expected_calls[
  119. ('dom0', 'admin.vm.List', None, None)] = \
  120. b'0\x00some-vm class=AppVM state=Running\n' \
  121. b'some-vm2 class=AppVM state=Running\n' \
  122. b'some-vm3 class=AppVM state=Halted\n'
  123. self.app.expected_calls[
  124. ('some-vm3', 'admin.vm.CurrentState', None, None)] = \
  125. b'0\x00power_state=Halted'
  126. with self.assertLogs() as logger:
  127. self.assertEqual(
  128. qubesadmin.tools.qvm_check.main(['--running', 'some-vm3'],
  129. app=self.app), 1)
  130. self.assertEqual(logger.output,
  131. ['INFO:qvm-check:None of qubes: running'])
  132. self.assertAllCalled()
  133. def test_008_paused(self):
  134. self.app.expected_calls[
  135. ('dom0', 'admin.vm.List', None, None)] = \
  136. b'0\x00some-vm class=AppVM state=Running\n' \
  137. b'some-vm2 class=AppVM state=Paused\n' \
  138. b'some-vm3 class=AppVM state=Halted\n'
  139. self.app.expected_calls[
  140. ('some-vm2', 'admin.vm.CurrentState', None, None)] = \
  141. b'0\x00power_state=Paused'
  142. with self.assertLogs() as logger:
  143. self.assertEqual(
  144. qubesadmin.tools.qvm_check.main(['--paused', 'some-vm2'],
  145. app=self.app), 0)
  146. self.assertEqual(logger.output, ['INFO:qvm-check:some-vm2: paused'])
  147. self.assertAllCalled()
  148. def test_009_paused_multi(self):
  149. self.app.expected_calls[
  150. ('dom0', 'admin.vm.List', None, None)] = \
  151. b'0\x00some-vm class=AppVM state=Running\n' \
  152. b'some-vm2 class=AppVM state=Paused\n' \
  153. b'some-vm3 class=AppVM state=Halted\n'
  154. self.app.expected_calls[
  155. ('some-vm2', 'admin.vm.CurrentState', None, None)] = \
  156. b'0\x00power_state=Paused'
  157. self.app.expected_calls[
  158. ('some-vm', 'admin.vm.CurrentState', None, None)] = \
  159. b'0\x00power_state=Running'
  160. with self.assertLogs() as logger:
  161. self.assertEqual(qubesadmin.tools.qvm_check.main(
  162. ['--paused', 'some-vm2', 'some-vm'], app=self.app), 3)
  163. self.assertEqual(logger.output, ['INFO:qvm-check:some-vm2: paused'])
  164. self.assertAllCalled()
  165. def test_010_template(self):
  166. self.app.expected_calls[
  167. ('dom0', 'admin.vm.List', None, None)] = \
  168. b'0\x00some-vm class=AppVM state=Running\n' \
  169. b'some-vm2 class=AppVM state=Paused\n' \
  170. b'some-vm3 class=TemplateVM state=Halted\n'
  171. with self.assertLogs() as logger:
  172. self.assertEqual(
  173. qubesadmin.tools.qvm_check.main(['--template', 'some-vm3'],
  174. app=self.app), 0)
  175. self.assertEqual(logger.output,
  176. ['INFO:qvm-check:some-vm3: template'])
  177. self.assertAllCalled()
  178. def test_011_template_multi(self):
  179. self.app.expected_calls[
  180. ('dom0', 'admin.vm.List', None, None)] = \
  181. b'0\x00some-vm class=AppVM state=Running\n' \
  182. b'some-vm2 class=AppVM state=Paused\n' \
  183. b'some-vm3 class=TemplateVM state=Halted\n'
  184. with self.assertLogs() as logger:
  185. self.assertEqual(qubesadmin.tools.qvm_check.main(
  186. ['--template', 'some-vm2', 'some-vm3'], app=self.app), 3)
  187. self.assertEqual(logger.output,
  188. ['INFO:qvm-check:some-vm3: template'])
  189. self.assertAllCalled()
  190. def test_012_networked(self):
  191. self.app.expected_calls[
  192. ('dom0', 'admin.vm.List', None, None)] = \
  193. b'0\x00some-vm class=AppVM state=Running\n' \
  194. b'some-vm2 class=AppVM state=Running\n'
  195. self.app.expected_calls[
  196. ('some-vm2', 'admin.vm.property.Get', 'provides_network', None)] = \
  197. b'0\x00default=false type=bool false'
  198. self.app.expected_calls[
  199. ('some-vm2', 'admin.vm.property.Get', 'netvm', None)] = \
  200. b'0\x00default=false type=vm some-vm'
  201. with self.assertLogs() as logger:
  202. self.assertEqual(
  203. qubesadmin.tools.qvm_check.main(['--networked', 'some-vm2'],
  204. app=self.app), 0)
  205. self.assertEqual(logger.output,
  206. ['INFO:qvm-check:some-vm2: networked'])
  207. self.assertAllCalled()
  208. def test_013_networked_multi(self):
  209. self.app.expected_calls[
  210. ('dom0', 'admin.vm.List', None, None)] = \
  211. b'0\x00some-vm class=AppVM state=Running\n' \
  212. b'some-vm2 class=AppVM state=Running\n' \
  213. b'some-vm3 class=TemplateVM state=Halted\n'
  214. self.app.expected_calls[
  215. ('some-vm2', 'admin.vm.property.Get', 'provides_network', None)] = \
  216. b'0\x00default=false type=bool false'
  217. self.app.expected_calls[
  218. ('some-vm3', 'admin.vm.property.Get', 'provides_network', None)] = \
  219. b'0\x00default=false type=bool false'
  220. self.app.expected_calls[
  221. ('some-vm2', 'admin.vm.property.Get', 'netvm', None)] = \
  222. b'0\x00default=false type=vm some-vm'
  223. self.app.expected_calls[
  224. ('some-vm3', 'admin.vm.property.Get', 'netvm', None)] = \
  225. b"0\x00default=false type=vm "
  226. with self.assertLogs() as logger:
  227. self.assertEqual(qubesadmin.tools.qvm_check.main(
  228. ['--networked', 'some-vm2', 'some-vm3'], app=self.app), 3)
  229. self.assertEqual(logger.output,
  230. ['INFO:qvm-check:some-vm2: networked'])
  231. self.assertAllCalled()