__init__.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # pylint: disable=protected-access,pointless-statement
  2. #
  3. # The Qubes OS Project, https://www.qubes-os.org/
  4. #
  5. # Copyright (C) 2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  6. # Copyright (C) 2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  7. #
  8. # This library is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU Lesser General Public
  10. # License as published by the Free Software Foundation; either
  11. # version 2.1 of the License, or (at your option) any later version.
  12. #
  13. # This library 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 GNU
  16. # Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public
  19. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  20. #
  21. import jinja2
  22. import unittest.mock
  23. import qubes.tests
  24. class TestVMM(object):
  25. # pylint: disable=too-few-public-methods
  26. def __init__(self, offline_mode=False):
  27. self.offline_mode = offline_mode
  28. self.xs = unittest.mock.Mock()
  29. @property
  30. def libvirt_conn(self):
  31. if self.offline_mode:
  32. import libvirt
  33. raise libvirt.libvirtError('phony error')
  34. else:
  35. libvirt_mock = unittest.mock.Mock()
  36. vm_mock = libvirt_mock.lookupByUUID.return_value
  37. vm_mock.isActive.return_value = False
  38. return libvirt_mock
  39. class TestHost(object):
  40. # pylint: disable=too-few-public-methods
  41. def __init__(self):
  42. self.memory_total = 1000 * 1024
  43. self.no_cpus = 4
  44. self.cpu_family_model = (6, 6)
  45. class TestVMsCollection(dict):
  46. def get_vms_connected_to(self, vm):
  47. return set()
  48. def close(self):
  49. self.clear()
  50. def __iter__(self):
  51. return iter(set(self.values()))
  52. class TestVolume(object):
  53. def __init__(self, pool):
  54. self.pool = pool
  55. self.size = 0
  56. self.source = None
  57. class TestPool(object):
  58. def init_volume(self, *args, **kwargs):
  59. return TestVolume(self)
  60. class TestApp(qubes.tests.TestEmitter):
  61. labels = {1: qubes.Label(1, '0xcc0000', 'red'),
  62. 2: qubes.Label(2, '0x00cc00', 'green'),
  63. 3: qubes.Label(3, '0x0000cc', 'blue'),
  64. 4: qubes.Label(4, '0xcccccc', 'black')}
  65. check_updates_vm = False
  66. def get_label(self, label):
  67. # pylint: disable=unused-argument
  68. if label in self.labels:
  69. return self.labels[label]
  70. for l in self.labels.values():
  71. if l.name == label:
  72. return l
  73. raise qubes.exc.QubesLabelNotFoundError(label)
  74. def get_pool(self, pool):
  75. return self.pools[pool]
  76. def __init__(self):
  77. super(TestApp, self).__init__()
  78. self.vmm = TestVMM()
  79. self.host = TestHost()
  80. default_pool = TestPool()
  81. self.pools = {
  82. 'default': default_pool,
  83. default_pool: default_pool,
  84. 'linux-kernel': TestPool(),
  85. }
  86. self.default_pool_volatile = 'default'
  87. self.default_pool_root = 'default'
  88. self.default_pool_private = 'default'
  89. self.default_pool_kernel = 'linux-kernel'
  90. self.default_qrexec_timeout = 60
  91. self.default_netvm = None
  92. self.default_guivm = None
  93. self.domains = TestVMsCollection()
  94. #: jinja2 environment for libvirt XML templates
  95. self.env = jinja2.Environment(
  96. loader=jinja2.FileSystemLoader([
  97. 'templates',
  98. '/etc/qubes/templates',
  99. '/usr/share/qubes/templates',
  100. ]),
  101. undefined=jinja2.StrictUndefined,
  102. autoescape=True)