qubes_prefs.py 3.1 KB

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