qubes_prefs.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 qubesadmin.tests
  21. import qubesadmin.tests.tools
  22. import qubesadmin.tools.qubes_prefs
  23. class TC_00_qubes_prefs(qubesadmin.tests.QubesTestCase):
  24. def test_000_list(self):
  25. self.app.expected_calls[
  26. ('dom0', 'admin.property.List', None, None)] = \
  27. b'0\x00prop1\nprop2\n'
  28. self.app.expected_calls[
  29. ('dom0', 'admin.property.GetAll', None, None)] = \
  30. b'0\x00prop1 default=True type=str value1\n' \
  31. b'prop2 default=False type=str value2\n'
  32. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  33. self.assertEqual(0, qubesadmin.tools.qubes_prefs.main([], app=self.app))
  34. self.assertEqual(stdout.getvalue(),
  35. 'prop1 D value1\n'
  36. 'prop2 - value2\n')
  37. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  38. self.assertEqual(0, qubesadmin.tools.qubes_prefs.main([
  39. '--hide-default'], app=self.app))
  40. self.assertEqual(stdout.getvalue(),
  41. 'prop2 - value2\n')
  42. self.assertAllCalled()
  43. def test_002_set_property(self):
  44. self.app.expected_calls[
  45. ('dom0', 'admin.property.Set', 'default_user', b'testuser')]\
  46. = b'0\x00'
  47. self.assertEqual(0, qubesadmin.tools.qubes_prefs.main([
  48. 'default_user', 'testuser'], app=self.app))
  49. self.assertAllCalled()
  50. def test_003_invalid_property(self):
  51. self.app.expected_calls[
  52. ('dom0', 'admin.property.Get', 'no_such_property', None)] = \
  53. b'2\x00AttributeError\x00\x00no_such_property\x00'
  54. with self.assertRaises(SystemExit):
  55. with qubesadmin.tests.tools.StderrBuffer() as stderr:
  56. qubesadmin.tools.qubes_prefs.main([
  57. 'no_such_property'], app=self.app)
  58. self.assertIn('no such property: \'no_such_property\'',
  59. stderr.getvalue())
  60. self.assertAllCalled()
  61. def test_004_set_invalid_property(self):
  62. self.app.expected_calls[
  63. ('dom0', 'admin.property.Set', 'no_such_property', b'value')]\
  64. = b'2\x00AttributeError\x00\x00no_such_property\x00'
  65. with self.assertRaises(SystemExit):
  66. with qubesadmin.tests.tools.StderrBuffer() as stderr:
  67. qubesadmin.tools.qubes_prefs.main([
  68. 'no_such_property', 'value'], app=self.app)
  69. self.assertIn('no such property: \'no_such_property\'',
  70. stderr.getvalue())
  71. self.assertAllCalled()