__init__.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 unittest
  21. import qubesmgmt
  22. import qubesmgmt.app
  23. class TestVM(object):
  24. def __init__(self, name, **kwargs):
  25. self.name = name
  26. for key, value in kwargs.items():
  27. setattr(self, key, value)
  28. def get_power_state(self):
  29. return getattr(self, 'power_state', 'Running')
  30. def __str__(self):
  31. return self.name
  32. def __lt__(self, other):
  33. if isinstance(other, TestVM):
  34. return self.name < other.name
  35. return NotImplemented
  36. class TestVMCollection(dict):
  37. def __iter__(self):
  38. return iter(self.values())
  39. class QubesTest(qubesmgmt.app.QubesBase):
  40. expected_calls = None
  41. actual_calls = None
  42. def __init__(self):
  43. super(QubesTest, self).__init__()
  44. #: expected calls and saved replies for them
  45. self.expected_calls = {}
  46. #: actual calls made
  47. self.actual_calls = []
  48. def qubesd_call(self, dest, method, arg=None, payload=None):
  49. call_key = (dest, method, arg, payload)
  50. self.actual_calls.append(call_key)
  51. if call_key not in self.expected_calls:
  52. raise AssertionError('Unexpected call {!r}'.format(call_key))
  53. return_data = self.expected_calls[call_key]
  54. return self._parse_qubesd_response(return_data)
  55. class QubesTestCase(unittest.TestCase):
  56. def setUp(self):
  57. super(QubesTestCase, self).setUp()
  58. self.app = QubesTest()
  59. def assertAllCalled(self):
  60. self.assertEqual(
  61. set(self.app.expected_calls.keys()),
  62. set(self.app.actual_calls))