qvm_prefs.py 7.3 KB

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