extra.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/python2 -O
  2. # vim: fileencoding=utf-8
  3. #
  4. # The Qubes OS Project, https://www.qubes-os.org/
  5. #
  6. # Copyright (C) 2016
  7. # Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. #
  23. import pkg_resources
  24. import qubes.tests
  25. import qubes.qubes
  26. class ExtraTestMixin(qubes.tests.SystemTestsMixin):
  27. template = None
  28. def setUp(self):
  29. super(ExtraTestMixin, self).setUp()
  30. self.qc.unlock_db()
  31. def create_vms(self, names):
  32. """
  33. Create AppVMs for the duration of the test. Will be automatically
  34. removed after completing the test.
  35. :param names: list of VM names to create (each of them will be
  36. prefixed with some test specific string)
  37. :return: list of created VM objects
  38. """
  39. self.qc.lock_db_for_writing()
  40. self.qc.load()
  41. if self.template:
  42. template = self.qc.get_vm_by_name(self.template)
  43. else:
  44. template = self.qc.get_default_template()
  45. for vmname in names:
  46. vm = self.qc.add_new_vm("QubesAppVm",
  47. name=self.make_vm_name(vmname),
  48. template=template)
  49. vm.create_on_disk(verbose=False)
  50. self.save_and_reload_db()
  51. self.qc.unlock_db()
  52. # get objects after reload
  53. vms = []
  54. for vmname in names:
  55. vms.append(self.qc.get_vm_by_name(self.make_vm_name(vmname)))
  56. return vms
  57. def enable_network(self):
  58. """
  59. Enable access to the network. Must be called before creating VMs.
  60. """
  61. # nothing to do in core2
  62. pass
  63. def load_tests(loader, tests, pattern):
  64. for entry in pkg_resources.iter_entry_points('qubes.tests.extra'):
  65. for test_case in entry():
  66. tests.addTests(loader.loadTestsFromTestCase(
  67. type(
  68. entry.name + '_' + test_case.__name__,
  69. (test_case, ExtraTestMixin, qubes.tests.QubesTestCase),
  70. {}
  71. )
  72. ))
  73. try:
  74. qc = qubes.qubes.QubesVmCollection()
  75. qc.lock_db_for_reading()
  76. qc.load()
  77. qc.unlock_db()
  78. templates = [vm.name for vm in qc.values() if
  79. isinstance(vm, qubes.qubes.QubesTemplateVm)]
  80. except OSError:
  81. templates = []
  82. for entry in pkg_resources.iter_entry_points(
  83. 'qubes.tests.extra.for_template'):
  84. for test_case in entry.load()():
  85. for template in templates:
  86. tests.addTests(loader.loadTestsFromTestCase(
  87. type(
  88. '{}_{}_{}'.format(
  89. entry.name, test_case.__name__, template),
  90. (test_case, ExtraTestMixin,
  91. qubes.tests.QubesTestCase),
  92. {'template': template}
  93. )
  94. ))
  95. return tests