errors.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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.exc
  21. import qubesadmin.tests
  22. import unittest
  23. class TC_00_Errors(qubesadmin.tests.QubesTestCase):
  24. def test_000_exception(self):
  25. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  26. b'2\x00QubesException\x00\x00An error occurred\x00'
  27. with self.assertRaises(qubesadmin.exc.QubesException) as context:
  28. vms = list(self.app.domains)
  29. self.assertEqual(str(context.exception), 'An error occurred')
  30. def test_001_exception_with_fields(self):
  31. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  32. b'2\x00QubesException\x00\x00' \
  33. b'An error occurred: %s, %s\x00string\x00other\x00'
  34. with self.assertRaises(qubesadmin.exc.QubesException) as context:
  35. vms = list(self.app.domains)
  36. self.assertEqual(str(context.exception),
  37. 'An error occurred: string, other')
  38. def test_002_exception_with_numbers(self):
  39. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  40. b'2\x00QubesException\x00\x00' \
  41. b'An error occurred: %d, %d\x001\x002\x00'
  42. try:
  43. with self.assertRaises(qubesadmin.exc.QubesException) as context:
  44. vms = list(self.app.domains)
  45. except TypeError as e:
  46. self.fail('TypeError: {!s}'.format(e))
  47. self.assertEqual(str(context.exception), 'An error occurred: 1, 2')
  48. def test_010_empty(self):
  49. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = b''
  50. with self.assertRaises(qubesadmin.exc.QubesDaemonNoResponseError) \
  51. as context:
  52. vms = list(self.app.domains)
  53. self.assertEqual(str(context.exception),
  54. 'Got empty response from qubesd. '
  55. 'See journalctl in dom0 for details.')