qvm_prefs.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. self.assertAllCalled()
  45. def test_001_no_vm(self):
  46. with self.assertRaises(SystemExit):
  47. with qubesadmin.tests.tools.StderrBuffer() as stderr:
  48. qubesadmin.tools.qvm_prefs.main([], app=self.app)
  49. if sys.version_info[0] == 2:
  50. self.assertIn('too few arguments', stderr.getvalue())
  51. else:
  52. self.assertIn('error: the following arguments are required: VMNAME',
  53. stderr.getvalue())
  54. self.assertAllCalled()
  55. def test_002_set_property(self):
  56. self.app.expected_calls[
  57. ('dom0', 'admin.vm.List', None, None)] = \
  58. b'0\x00dom0 class=AdminVM state=Running\n'
  59. self.app.expected_calls[
  60. ('dom0', 'admin.vm.property.Set', 'default_user', b'testuser')] = \
  61. b'0\x00'
  62. self.assertEqual(0, qubesadmin.tools.qvm_prefs.main([
  63. 'dom0', 'default_user', 'testuser'], app=self.app))
  64. self.assertAllCalled()
  65. def test_003_invalid_property(self):
  66. self.app.expected_calls[
  67. ('dom0', 'admin.vm.List', None, None)] = \
  68. b'0\x00dom0 class=AdminVM state=Running\n'
  69. self.app.expected_calls[
  70. ('dom0', 'admin.vm.property.Get', 'no_such_property', None)] = \
  71. b'2\x00AttributeError\x00\x00no_such_property\x00'
  72. with self.assertRaises(SystemExit):
  73. with qubesadmin.tests.tools.StderrBuffer() as stderr:
  74. qubesadmin.tools.qvm_prefs.main([
  75. 'dom0', 'no_such_property'], app=self.app)
  76. self.assertIn('no such property: \'no_such_property\'',
  77. stderr.getvalue())
  78. self.assertAllCalled()
  79. def test_004_set_invalid_property(self):
  80. self.app.expected_calls[
  81. ('dom0', 'admin.vm.List', None, None)] = \
  82. b'0\x00dom0 class=AdminVM state=Running\n'
  83. self.app.expected_calls[
  84. ('dom0', 'admin.vm.property.Set', 'no_such_property', b'value')] = \
  85. b'2\x00AttributeError\x00\x00no_such_property\x00'
  86. with self.assertRaises(SystemExit):
  87. with qubesadmin.tests.tools.StderrBuffer() as stderr:
  88. qubesadmin.tools.qvm_prefs.main([
  89. 'dom0', 'no_such_property', 'value'], app=self.app)
  90. self.assertIn('no such property: \'no_such_property\'',
  91. stderr.getvalue())
  92. self.assertAllCalled()