__init__.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. class TestVolume(object):
  51. def __init__(self, pool):
  52. self.pool = pool
  53. self.size = 0
  54. self.source = None
  55. class TestPool(object):
  56. def init_volume(self, *args, **kwargs):
  57. return TestVolume(self)
  58. class TestApp(qubes.tests.TestEmitter):
  59. labels = {1: qubes.Label(1, '0xcc0000', 'red'),
  60. 2: qubes.Label(2, '0x00cc00', 'green'),
  61. 3: qubes.Label(3, '0x0000cc', 'blue'),
  62. 4: qubes.Label(4, '0xcccccc', 'black')}
  63. check_updates_vm = False
  64. def get_label(self, label):
  65. # pylint: disable=unused-argument
  66. if label in self.labels:
  67. return self.labels[label]
  68. for l in self.labels.values():
  69. if l.name == label:
  70. return l
  71. raise KeyError(label)
  72. def get_pool(self, pool):
  73. return self.pools[pool]
  74. def __init__(self):
  75. super(TestApp, self).__init__()
  76. self.vmm = TestVMM()
  77. self.host = TestHost()
  78. default_pool = TestPool()
  79. self.pools = {
  80. 'default': default_pool,
  81. default_pool: default_pool,
  82. 'linux-kernel': TestPool(),
  83. }
  84. self.default_pool_volatile = 'default'
  85. self.default_pool_root = 'default'
  86. self.default_pool_private = 'default'
  87. self.default_pool_kernel = 'linux-kernel'
  88. self.default_qrexec_timeout = 60
  89. self.default_netvm = None
  90. self.domains = TestVMsCollection()
  91. #: jinja2 environment for libvirt XML templates
  92. self.env = jinja2.Environment(
  93. loader=jinja2.FileSystemLoader([
  94. 'templates',
  95. '/etc/qubes/templates',
  96. '/usr/share/qubes/templates',
  97. ]),
  98. undefined=jinja2.StrictUndefined)