qvm_ls.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. class TC_90_List_with_qubesd_calls(qubesadmin.tests.QubesTestCase):
  106. def test_100_list_with_status(self):
  107. self.app.expected_calls[
  108. ('dom0', 'admin.vm.List', None, None)] = \
  109. b'0\x00vm1 class=AppVM state=Running\n' \
  110. b'template1 class=TemplateVM state=Halted\n' \
  111. b'sys-net class=AppVM state=Running\n'
  112. self.app.expected_calls[
  113. ('vm1', 'admin.vm.List', None, None)] = \
  114. b'0\x00vm1 class=AppVM state=Running\n'
  115. self.app.expected_calls[
  116. ('sys-net', 'admin.vm.List', None, None)] = \
  117. b'0\x00sys-net class=AppVM state=Running\n'
  118. self.app.expected_calls[
  119. ('template1', 'admin.vm.List', None, None)] = \
  120. b'0\x00template1 class=TemplateVM state=Halted\n'
  121. props = {
  122. 'label': b'type=label green',
  123. 'template': b'type=vm template1',
  124. 'netvm': b'type=vm sys-net',
  125. # 'virt_mode': b'type=str pv',
  126. }
  127. for key, value in props.items():
  128. self.app.expected_calls[
  129. ('vm1', 'admin.vm.property.Get', key, None)] = \
  130. b'0\x00default=True ' + value
  131. # setup template1
  132. props['label'] = b'type=label black'
  133. for key, value in props.items():
  134. self.app.expected_calls[
  135. ('template1', 'admin.vm.property.Get', key, None)] = \
  136. b'0\x00default=True ' + value
  137. self.app.expected_calls[
  138. ('template1', 'admin.vm.property.Get', 'template', None)] = \
  139. b'' # request refused - no such property
  140. # setup sys-net
  141. props['label'] = b'type=label red'
  142. for key, value in props.items():
  143. self.app.expected_calls[
  144. ('sys-net', 'admin.vm.property.Get', key, None)] = \
  145. b'0\x00default=True ' + value
  146. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  147. qubesadmin.tools.qvm_ls.main([], app=self.app)
  148. self.assertEqual(stdout.getvalue(),
  149. 'NAME STATE CLASS LABEL TEMPLATE NETVM\n'
  150. 'sys-net Running AppVM red template1 sys-net\n'
  151. 'template1 Halted TemplateVM black - sys-net\n'
  152. 'vm1 Running AppVM green template1 sys-net\n')
  153. self.assertAllCalled()
  154. def test_101_list_selected(self):
  155. self.app.expected_calls[
  156. ('dom0', 'admin.vm.List', None, None)] = \
  157. b'0\x00vm1 class=AppVM state=Running\n' \
  158. b'template1 class=TemplateVM state=Halted\n' \
  159. b'sys-net class=AppVM state=Running\n'
  160. self.app.expected_calls[
  161. ('vm1', 'admin.vm.List', None, None)] = \
  162. b'0\x00vm1 class=AppVM state=Running\n'
  163. self.app.expected_calls[
  164. ('sys-net', 'admin.vm.List', None, None)] = \
  165. b'0\x00sys-net class=AppVM state=Running\n'
  166. props = {
  167. 'label': b'type=label green',
  168. 'template': b'type=vm template1',
  169. 'netvm': b'type=vm sys-net',
  170. # 'virt_mode': b'type=str pv',
  171. }
  172. for key, value in props.items():
  173. self.app.expected_calls[
  174. ('vm1', 'admin.vm.property.Get', key, None)] = \
  175. b'0\x00default=True ' + value
  176. # setup sys-net
  177. props['label'] = b'type=label red'
  178. for key, value in props.items():
  179. self.app.expected_calls[
  180. ('sys-net', 'admin.vm.property.Get', key, None)] = \
  181. b'0\x00default=True ' + value
  182. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  183. qubesadmin.tools.qvm_ls.main(['vm1', 'sys-net'], app=self.app)
  184. self.assertEqual(stdout.getvalue(),
  185. 'NAME STATE CLASS LABEL TEMPLATE NETVM\n'
  186. 'sys-net Running AppVM red template1 sys-net\n'
  187. 'vm1 Running AppVM green template1 sys-net\n')
  188. self.assertAllCalled()