qvm_ls.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. # pylint: disable=protected-access,pointless-statement
  2. #
  3. # The Qubes OS Project, https://www.qubesmgmt-os.org/
  4. #
  5. # Copyright (C) 2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  6. # Copyright (C) 2015 Wojtek Porczyk <woju@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
  21. import qubesadmin
  22. import qubesadmin.vm
  23. import qubesadmin.tools.qvm_ls
  24. import qubesadmin.tests
  25. import qubesadmin.tests.tools
  26. from qubesadmin.tests import TestVM, TestVMCollection
  27. class TestApp(object):
  28. def __init__(self):
  29. self.domains = TestVMCollection(
  30. [
  31. ('dom0', TestVM('dom0')),
  32. ('test-vm', TestVM('test-vm')),
  33. ]
  34. )
  35. class TC_00_Column(qubesadmin.tests.QubesTestCase):
  36. def test_100_init(self):
  37. try:
  38. testcolumn = qubesadmin.tools.qvm_ls.Column('TESTCOLUMN')
  39. self.assertEqual(testcolumn.ls_head, 'TESTCOLUMN')
  40. finally:
  41. try:
  42. qubesadmin.tools.qvm_ls.Column.columns['TESTCOLUMN']
  43. except KeyError:
  44. pass
  45. class TC_10_globals(qubesadmin.tests.QubesTestCase):
  46. def test_100_simple_flag(self):
  47. flag = qubesadmin.tools.qvm_ls.simple_flag(1, 'T', 'internal')
  48. # TODO after serious testing of QubesVM and Qubes app, this should be
  49. # using normal components
  50. vm = TestVM('test-vm', internal=False)
  51. self.assertFalse(flag(None, vm))
  52. vm.internal = True
  53. self.assertTrue(flag(None, vm))
  54. @unittest.skip('column list generated dynamically')
  55. def test_900_formats_columns(self):
  56. for fmt in qubesadmin.tools.qvm_ls.formats:
  57. for col in qubesadmin.tools.qvm_ls.formats[fmt]:
  58. self.assertIn(col.upper(), qubesadmin.tools.qvm_ls.Column.columns)
  59. class TC_50_List(qubesadmin.tests.QubesTestCase):
  60. def test_100_list_with_status(self):
  61. app = TestApp()
  62. app.domains['test-vm'].internal = False
  63. app.domains['test-vm'].updateable = False
  64. app.domains['test-vm'].template = TestVM('template')
  65. app.domains['test-vm'].netvm = TestVM('sys-net')
  66. app.domains['test-vm'].label = 'green'
  67. app.domains['dom0'].label = 'black'
  68. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  69. qubesadmin.tools.qvm_ls.main([], app=app)
  70. self.assertEqual(stdout.getvalue(),
  71. 'NAME STATE CLASS LABEL TEMPLATE NETVM\n'
  72. 'dom0 Running TestVM black - -\n'
  73. 'test-vm Running TestVM green template sys-net\n')
  74. def test_101_list_with_underscore(self):
  75. app = TestApp()
  76. app.domains['test-vm'].virt_mode = 'pv'
  77. app.domains['test-vm'].label = 'green'
  78. app.domains['dom0'].label = 'black'
  79. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  80. qubesadmin.tools.qvm_ls.main(['-O', 'name,virt_mode,class'], app=app)
  81. self.assertEqual(stdout.getvalue(),
  82. 'NAME VIRT-MODE CLASS\n'
  83. 'dom0 - TestVM\n'
  84. 'test-vm pv TestVM\n')
  85. def test_102_list_selected(self):
  86. app = TestApp()
  87. app.domains['test-vm'].internal = False
  88. app.domains['test-vm'].updateable = False
  89. app.domains['test-vm'].template = TestVM('template')
  90. app.domains['test-vm'].netvm = TestVM('sys-net')
  91. app.domains['test-vm'].label = 'green'
  92. app.domains['dom0'].label = 'black'
  93. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  94. qubesadmin.tools.qvm_ls.main(['test-vm'], app=app)
  95. self.assertEqual(stdout.getvalue(),
  96. 'NAME STATE CLASS LABEL TEMPLATE NETVM\n'
  97. 'test-vm Running TestVM green template sys-net\n')
  98. def test_102_raw_list(self):
  99. app = TestApp()
  100. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  101. qubesadmin.tools.qvm_ls.main(['--raw-list'], app=app)
  102. self.assertEqual(stdout.getvalue(),
  103. 'dom0\n'
  104. 'test-vm\n')
  105. def test_103_list_all(self):
  106. app = TestApp()
  107. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  108. qubesadmin.tools.qvm_ls.main(['--all'], app=app)
  109. self.assertEqual(stdout.getvalue(),
  110. 'NAME STATE CLASS LABEL TEMPLATE NETVM\n'
  111. 'dom0 Running TestVM - - -\n'
  112. 'test-vm Running TestVM - - -\n')
  113. def test_110_network_tree(self):
  114. app = TestApp()
  115. app.domains = TestVMCollection(
  116. [
  117. ('dom0', TestVM('dom0')),
  118. ('test-vm-temp', TestVM('test-vm-temp')),
  119. ('test-vm-proxy', TestVM('test-vm-proxy')),
  120. ('test-vm-1', TestVM('test-vm-1')),
  121. ('test-vm-2', TestVM('test-vm-2')),
  122. ('test-vm-3', TestVM('test-vm-3')),
  123. ('test-vm-4', TestVM('test-vm-4')),
  124. ('test-vm-net-1', TestVM('test-vm-net-1')),
  125. ('test-vm-net-2', TestVM('test-vm-net-2')),
  126. ]
  127. )
  128. ad = app.domains # For the sake of a 80 character line
  129. ad['dom0'].label = 'black'
  130. ad['test-vm-temp'].template = TestVM('template')
  131. ad['test-vm-net-1'].netvm = None
  132. ad['test-vm-net-1'].provides_network = True
  133. ad['test-vm-net-2'].netvm = None
  134. ad['test-vm-net-2'].provides_network = True
  135. ad['test-vm-proxy'].netvm = TestVM('test-vm-net-2')
  136. ad['test-vm-proxy'].provides_network = True
  137. ad['test-vm-1'].netvm = TestVM('test-vm-net-1')
  138. ad['test-vm-1'].provides_network = False
  139. ad['test-vm-2'].netvm = TestVM('test-vm-proxy')
  140. ad['test-vm-2'].provides_network = False
  141. ad['test-vm-3'].netvm = TestVM('test-vm-proxy')
  142. ad['test-vm-3'].provides_network = False
  143. ad['test-vm-4'].netvm = TestVM('test-vm-net-2')
  144. ad['test-vm-4'].provides_network = False
  145. ad['test-vm-net-1'].connected_vms = [ad['test-vm-1']]
  146. ad['test-vm-proxy'].connected_vms = [ad['test-vm-2'],
  147. ad['test-vm-3']]
  148. ad['test-vm-net-2'].connected_vms = [ad['test-vm-proxy'],
  149. ad['test-vm-4']]
  150. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  151. qubesadmin.tools.qvm_ls.main(['--tree'], app=app)
  152. self.assertEqual(stdout.getvalue(),
  153. 'NAME STATE CLASS LABEL TEMPLATE NETVM\n'
  154. 'dom0 Running TestVM black - -\n'
  155. 'test-vm-temp Running TestVM - template -\n'
  156. 'test-vm-net-1 Running TestVM - - -\n'
  157. '└─test-vm-1 Running TestVM - - test-vm-net-1\n'
  158. 'test-vm-net-2 Running TestVM - - -\n'
  159. '└─test-vm-proxy Running TestVM - - test-vm-net-2\n'
  160. ' └─test-vm-2 Running TestVM - - test-vm-proxy\n'
  161. ' └─test-vm-3 Running TestVM - - test-vm-proxy\n'
  162. '└─test-vm-4 Running TestVM - - test-vm-net-2\n')
  163. class TC_70_Tags(qubesadmin.tests.QubesTestCase):
  164. def setUp(self):
  165. self.app = TestApp()
  166. self.app.domains = TestVMCollection(
  167. [
  168. (
  169. 'dom0',
  170. TestVM(
  171. 'dom0',
  172. tags=['my'],
  173. label='black'
  174. )
  175. ),
  176. (
  177. 'test-vm',
  178. TestVM(
  179. 'test-vm',
  180. tags=['not-my', 'other'],
  181. label='red',
  182. netvm=TestVM('sys-firewall'),
  183. template=TestVM('template')
  184. )
  185. ),
  186. ]
  187. )
  188. def test_100_tag(self):
  189. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  190. qubesadmin.tools.qvm_ls.main(['--tags', 'my'], app=self.app)
  191. self.assertEqual(stdout.getvalue(),
  192. 'NAME STATE CLASS LABEL TEMPLATE NETVM\n'
  193. 'dom0 Running TestVM black - -\n')
  194. def test_100_tag_nomatch(self):
  195. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  196. qubesadmin.tools.qvm_ls.main(['--tags', 'nx'], app=self.app)
  197. self.assertEqual(stdout.getvalue(),
  198. 'NAME STATE CLASS LABEL TEMPLATE NETVM\n')
  199. def test_100_tags(self):
  200. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  201. qubesadmin.tools.qvm_ls.main(['--tags', 'my', 'other'], app=self.app)
  202. self.assertEqual(stdout.getvalue(),
  203. 'NAME STATE CLASS LABEL TEMPLATE NETVM\n'
  204. 'dom0 Running TestVM black - -\n'
  205. 'test-vm Running TestVM red template sys-firewall\n')
  206. def test_100_tags_nomatch(self):
  207. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  208. qubesadmin.tools.qvm_ls.main(['--tags', 'nx1', 'nx2'], app=self.app)
  209. self.assertEqual(stdout.getvalue(),
  210. 'NAME STATE CLASS LABEL TEMPLATE NETVM\n')
  211. class TC_80_Power_state_filters(qubesadmin.tests.QubesTestCase):
  212. def setUp(self):
  213. self.app = TestApp()
  214. self.app.domains = TestVMCollection(
  215. [
  216. ('a', TestVM('a', power_state='Halted')),
  217. ('b', TestVM('b', power_state='Transient')),
  218. ('c', TestVM('c', power_state='Running'))
  219. ]
  220. )
  221. def test_100_nofilter(self):
  222. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  223. qubesadmin.tools.qvm_ls.main([], app=self.app)
  224. self.assertEqual(stdout.getvalue(),
  225. 'NAME STATE CLASS LABEL TEMPLATE NETVM\n'
  226. 'a Halted TestVM - - -\n'
  227. 'b Transient TestVM - - -\n'
  228. 'c Running TestVM - - -\n')
  229. def test_100_running(self):
  230. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  231. qubesadmin.tools.qvm_ls.main(['--running'], app=self.app)
  232. self.assertEqual(stdout.getvalue(),
  233. 'NAME STATE CLASS LABEL TEMPLATE NETVM\n'
  234. 'c Running TestVM - - -\n')
  235. def test_100_running_or_halted(self):
  236. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  237. qubesadmin.tools.qvm_ls.main(['--running', '--halted'], app=self.app)
  238. self.assertEqual(stdout.getvalue(),
  239. 'NAME STATE CLASS LABEL TEMPLATE NETVM\n'
  240. 'a Halted TestVM - - -\n'
  241. 'c Running TestVM - - -\n')
  242. class TC_90_List_with_qubesd_calls(qubesadmin.tests.QubesTestCase):
  243. def test_100_list_with_status(self):
  244. self.app.expected_calls[
  245. ('dom0', 'admin.vm.List', None, None)] = \
  246. b'0\x00vm1 class=AppVM state=Running\n' \
  247. b'template1 class=TemplateVM state=Halted\n' \
  248. b'sys-net class=AppVM state=Running\n'
  249. props = {
  250. 'label': 'type=label green',
  251. 'template': 'type=vm template1',
  252. 'netvm': 'type=vm sys-net',
  253. # 'virt_mode': b'type=str pv',
  254. }
  255. self.app.expected_calls[
  256. ('vm1', 'admin.vm.property.GetAll', None, None)] = \
  257. b'0\x00' + ''.join(
  258. '{} default=True {}\n'.format(key, value)
  259. for key, value in props.items()).encode()
  260. # setup sys-net
  261. props['label'] = 'type=label red'
  262. self.app.expected_calls[
  263. ('sys-net', 'admin.vm.property.GetAll', None, None)] = \
  264. b'0\x00' + ''.join(
  265. '{} default=True {}\n'.format(key, value)
  266. for key, value in props.items()).encode()
  267. # setup template1
  268. props['label'] = 'type=label black'
  269. del props['template']
  270. self.app.expected_calls[
  271. ('template1', 'admin.vm.property.GetAll', None, None)] = \
  272. b'0\x00' + ''.join(
  273. '{} default=True {}\n'.format(key, value)
  274. for key, value in props.items()).encode()
  275. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  276. qubesadmin.tools.qvm_ls.main([], app=self.app)
  277. self.assertEqual(stdout.getvalue(),
  278. 'NAME STATE CLASS LABEL TEMPLATE NETVM\n'
  279. 'sys-net Running AppVM red template1 sys-net\n'
  280. 'template1 Halted TemplateVM black - sys-net\n'
  281. 'vm1 Running AppVM green template1 sys-net\n')
  282. self.assertAllCalled()
  283. def test_101_list_selected(self):
  284. self.app.expected_calls[
  285. ('dom0', 'admin.vm.List', None, None)] = \
  286. b'0\x00vm1 class=AppVM state=Running\n' \
  287. b'template1 class=TemplateVM state=Halted\n' \
  288. b'sys-net class=AppVM state=Running\n'
  289. self.app.expected_calls[
  290. ('vm1', 'admin.vm.CurrentState', None, None)] = \
  291. b'0\x00power_state=Running'
  292. self.app.expected_calls[
  293. ('sys-net', 'admin.vm.CurrentState', None, None)] = \
  294. b'0\x00power_state=Running'
  295. props = {
  296. 'label': 'type=label green',
  297. 'template': 'type=vm template1',
  298. 'netvm': 'type=vm sys-net',
  299. # 'virt_mode': b'type=str pv',
  300. }
  301. self.app.expected_calls[
  302. ('vm1', 'admin.vm.property.GetAll', None, None)] = \
  303. b'0\x00' + ''.join(
  304. '{} default=True {}\n'.format(key, value)
  305. for key, value in props.items()).encode()
  306. # setup sys-net
  307. props['label'] = 'type=label red'
  308. self.app.expected_calls[
  309. ('sys-net', 'admin.vm.property.GetAll', None, None)] = \
  310. b'0\x00' + ''.join(
  311. '{} default=True {}\n'.format(key, value)
  312. for key, value in props.items()).encode()
  313. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  314. qubesadmin.tools.qvm_ls.main(['vm1', 'sys-net'], app=self.app)
  315. self.assertEqual(stdout.getvalue(),
  316. 'NAME STATE CLASS LABEL TEMPLATE NETVM\n'
  317. 'sys-net Running AppVM red template1 sys-net\n'
  318. 'vm1 Running AppVM green template1 sys-net\n')
  319. self.assertAllCalled()