__init__.py 3.3 KB

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