qvm_prefs.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 sys
  21. import qubesadmin.tests
  22. import qubesadmin.tests.tools
  23. import qubesadmin.tools.qvm_prefs
  24. class TC_00_qvm_prefs(qubesadmin.tests.QubesTestCase):
  25. def test_000_list(self):
  26. self.app.expected_calls[
  27. ('dom0', 'admin.vm.List', None, None)] = \
  28. b'0\x00dom0 class=AdminVM state=Running\n'
  29. self.app.expected_calls[
  30. ('dom0', 'admin.vm.property.List', None, None)] = \
  31. b'0\x00prop1\nprop2\n'
  32. self.app.expected_calls[
  33. ('dom0', 'admin.vm.property.GetAll', None, None)] = \
  34. b'0\x00prop1 default=True type=str value1\n' \
  35. b'prop2 default=False type=str value2\n'
  36. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  37. self.assertEqual(0, qubesadmin.tools.qvm_prefs.main([
  38. 'dom0'], app=self.app))
  39. self.assertEqual(stdout.getvalue(),
  40. 'prop1 D value1\n'
  41. 'prop2 - value2\n')
  42. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  43. self.assertEqual(0, qubesadmin.tools.qvm_prefs.main([
  44. 'dom0', '--hide-default'], app=self.app))
  45. self.assertEqual(stdout.getvalue(),
  46. 'prop2 - value2\n')
  47. self.assertAllCalled()
  48. def test_001_no_vm(self):
  49. with self.assertRaises(SystemExit):
  50. with qubesadmin.tests.tools.StderrBuffer() as stderr:
  51. qubesadmin.tools.qvm_prefs.main([], app=self.app)
  52. if sys.version_info[0] == 2:
  53. self.assertIn('too few arguments', stderr.getvalue())
  54. else:
  55. self.assertIn('error: the following arguments are required: VMNAME',
  56. stderr.getvalue())
  57. self.assertAllCalled()
  58. def test_002_set_property(self):
  59. self.app.expected_calls[
  60. ('dom0', 'admin.vm.List', None, None)] = \
  61. b'0\x00dom0 class=AdminVM state=Running\n'
  62. self.app.expected_calls[
  63. ('dom0', 'admin.vm.property.Set', 'default_user', b'testuser')] = \
  64. b'0\x00'
  65. self.assertEqual(0, qubesadmin.tools.qvm_prefs.main([
  66. 'dom0', 'default_user', 'testuser'], app=self.app))
  67. self.assertAllCalled()
  68. def test_003_invalid_property(self):
  69. self.app.expected_calls[
  70. ('dom0', 'admin.vm.List', None, None)] = \
  71. b'0\x00dom0 class=AdminVM state=Running\n'
  72. self.app.expected_calls[
  73. ('dom0', 'admin.vm.property.Get', 'no_such_property', None)] = \
  74. b'2\x00AttributeError\x00\x00no_such_property\x00'
  75. with self.assertRaises(SystemExit):
  76. with qubesadmin.tests.tools.StderrBuffer() as stderr:
  77. qubesadmin.tools.qvm_prefs.main([
  78. 'dom0', 'no_such_property'], app=self.app)
  79. self.assertIn('no such property: \'no_such_property\'',
  80. stderr.getvalue())
  81. self.assertAllCalled()
  82. def test_004_set_invalid_property(self):
  83. self.app.expected_calls[
  84. ('dom0', 'admin.vm.List', None, None)] = \
  85. b'0\x00dom0 class=AdminVM state=Running\n'
  86. self.app.expected_calls[
  87. ('dom0', 'admin.vm.property.Set', 'no_such_property', b'value')] = \
  88. b'2\x00AttributeError\x00\x00no_such_property\x00'
  89. with self.assertRaises(SystemExit):
  90. with qubesadmin.tests.tools.StderrBuffer() as stderr:
  91. qubesadmin.tools.qvm_prefs.main([
  92. 'dom0', 'no_such_property', 'value'], app=self.app)
  93. self.assertIn('no such property: \'no_such_property\'',
  94. stderr.getvalue())
  95. self.assertAllCalled()
  96. def test_005_get_str(self):
  97. self.app.expected_calls[
  98. ('dom0', 'admin.vm.List', None, None)] = \
  99. b'0\x00dom0 class=AdminVM state=Running\n'
  100. self.app.expected_calls[
  101. ('dom0', 'admin.vm.property.Get', 'prop1', None)] = \
  102. b'0\x00default=True type=str value1'
  103. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  104. qubesadmin.tools.qvm_prefs.main(['dom0', 'prop1'], app=self.app)
  105. self.assertEqual('value1\n', stdout.getvalue())
  106. self.assertAllCalled()
  107. def test_006_get_vm(self):
  108. self.app.expected_calls[
  109. ('dom0', 'admin.vm.List', None, None)] = \
  110. b'0\x00dom0 class=AdminVM state=Running\n' \
  111. b'vm1 class=AppVM state=Stopped\n'
  112. self.app.expected_calls[
  113. ('dom0', 'admin.vm.property.Get', 'prop1', None)] = \
  114. b'0\x00default=True type=vm vm1'
  115. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  116. qubesadmin.tools.qvm_prefs.main(['dom0', 'prop1'], app=self.app)
  117. self.assertEqual('vm1\n', stdout.getvalue())
  118. self.assertAllCalled()
  119. def test_007_get_vm_none(self):
  120. self.app.expected_calls[
  121. ('dom0', 'admin.vm.List', None, None)] = \
  122. b'0\x00dom0 class=AdminVM state=Running\n' \
  123. b'vm1 class=AppVM state=Stopped\n'
  124. self.app.expected_calls[
  125. ('dom0', 'admin.vm.property.Get', 'prop1', None)] = \
  126. b'0\x00default=True type=vm '
  127. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  128. qubesadmin.tools.qvm_prefs.main(['dom0', 'prop1'], app=self.app)
  129. self.assertEqual('', stdout.getvalue())
  130. self.assertAllCalled()
  131. def test_008_set_vm_prop_none(self):
  132. self.app.expected_calls[
  133. ('dom0', 'admin.vm.List', None, None)] = \
  134. b'0\x00dom0 class=AdminVM state=Running\n'
  135. self.app.expected_calls[
  136. ('dom0', 'admin.vm.property.Set', 'netvm', b'')] = \
  137. b'0\x00'
  138. self.app.expected_calls[
  139. ('dom0', 'admin.vm.property.Set', 'default_dispvm', b'')] = \
  140. b'0\x00'
  141. self.app.expected_calls[
  142. ('dom0', 'admin.vm.property.Set', 'user', b'none')] = \
  143. b'0\x00'
  144. self.app.expected_calls[
  145. ('dom0', 'admin.vm.property.Set', 'prop1', b'None')] = \
  146. b'0\x00'
  147. self.assertEqual(0, qubesadmin.tools.qvm_prefs.main([
  148. 'dom0', 'netvm', 'None'], app=self.app))
  149. self.assertEqual(0, qubesadmin.tools.qvm_prefs.main([
  150. 'dom0', 'default_dispvm', 'none'], app=self.app))
  151. self.assertEqual(0, qubesadmin.tools.qvm_prefs.main([
  152. 'dom0', 'user', 'none'], app=self.app))
  153. self.assertEqual(0, qubesadmin.tools.qvm_prefs.main([
  154. 'dom0', 'prop1', 'None'], app=self.app))
  155. self.assertAllCalled()