qvm_ls.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 STATUS LABEL TEMPLATE NETVM\n'
  72. 'dom0 -r------ black - -\n'
  73. 'test-vm -r------ green template sys-net\n')
  74. class TC_90_List_with_qubesd_calls(qubesadmin.tests.QubesTestCase):
  75. def test_100_list_with_status(self):
  76. self.app.expected_calls[
  77. ('dom0', 'mgmt.vm.List', None, None)] = \
  78. b'0\x00vm1 class=AppVM state=Running\n' \
  79. b'template1 class=TemplateVM state=Halted\n' \
  80. b'sys-net class=AppVM state=Running\n'
  81. self.app.expected_calls[
  82. ('dom0', 'mgmt.label.List', None, None)] = \
  83. b'0\x00red\nblack\ngreen\nblue\n'
  84. self.app.expected_calls[
  85. ('vm1', 'mgmt.vm.List', None, None)] = \
  86. b'0\x00vm1 class=AppVM state=Running\n'
  87. self.app.expected_calls[
  88. ('sys-net', 'mgmt.vm.List', None, None)] = \
  89. b'0\x00sys-net class=AppVM state=Running\n'
  90. self.app.expected_calls[
  91. ('template1', 'mgmt.vm.List', None, None)] = \
  92. b'0\x00template1 class=TemplateVM state=Halted\n'
  93. props = {
  94. 'label': b'type=label green',
  95. 'template': b'type=vm template1',
  96. 'netvm': b'type=vm sys-net',
  97. 'updateable': b'type=bool False',
  98. 'provides_network': b'type=bool False',
  99. 'hvm': b'type=bool False',
  100. 'installed_by_rpm': b'type=bool False',
  101. 'internal': b'type=bool False',
  102. 'debug': b'type=bool False',
  103. 'autostart': b'type=bool False',
  104. }
  105. for key, value in props.items():
  106. self.app.expected_calls[
  107. ('vm1', 'mgmt.vm.property.Get', key, None)] = \
  108. b'0\x00default=True ' + value
  109. # setup template1
  110. props['label'] = b'type=label black'
  111. props['updateable'] = b'type=bool True'
  112. for key, value in props.items():
  113. self.app.expected_calls[
  114. ('template1', 'mgmt.vm.property.Get', key, None)] = \
  115. b'0\x00default=True ' + value
  116. self.app.expected_calls[
  117. ('template1', 'mgmt.vm.property.Get', 'template', None)] = \
  118. b'' # request refused - no such property
  119. # setup sys-net
  120. props['label'] = b'type=label red'
  121. props['provides_network'] = b'type=bool True'
  122. props['updateable'] = b'type=bool False'
  123. for key, value in props.items():
  124. self.app.expected_calls[
  125. ('sys-net', 'mgmt.vm.property.Get', key, None)] = \
  126. b'0\x00default=True ' + value
  127. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  128. qubesadmin.tools.qvm_ls.main([], app=self.app)
  129. self.assertEqual(stdout.getvalue(),
  130. 'NAME STATUS LABEL TEMPLATE NETVM\n'
  131. 'sys-net ar-N---- red template1 sys-net\n'
  132. 'template1 t-U----- black - sys-net\n'
  133. 'vm1 ar------ green template1 sys-net\n')
  134. self.assertAllCalled()