extra.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #
  2. # The Qubes OS Project, https://www.qubes-os.org/
  3. #
  4. # Copyright (C) 2016
  5. # Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
  6. #
  7. # This library is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU Lesser General Public
  9. # License as published by the Free Software Foundation; either
  10. # version 2.1 of the License, or (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. # Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public
  18. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  19. #
  20. import sys
  21. import pkg_resources
  22. import qubes.tests
  23. import qubes.vm.appvm
  24. class ExtraTestCase(qubes.tests.SystemTestCase):
  25. template = None
  26. def setUp(self):
  27. super(ExtraTestCase, self).setUp()
  28. self.init_default_template(self.template)
  29. def create_vms(self, names):
  30. """
  31. Create AppVMs for the duration of the test. Will be automatically
  32. removed after completing the test.
  33. :param names: list of VM names to create (each of them will be
  34. prefixed with some test specific string)
  35. :return: list of created VM objects
  36. """
  37. if self.template:
  38. template = self.app.domains[self.template]
  39. else:
  40. template = self.app.default_template
  41. for vmname in names:
  42. vm = self.app.add_new_vm(qubes.vm.appvm.AppVM,
  43. name=self.make_vm_name(vmname),
  44. template=template,
  45. label='red')
  46. vm.create_on_disk()
  47. self.app.save()
  48. # get objects after reload
  49. vms = []
  50. for vmname in names:
  51. vms.append(self.app.domains[self.make_vm_name(vmname)])
  52. return vms
  53. def enable_network(self):
  54. """
  55. Enable access to the network. Must be called before creating VMs.
  56. """
  57. self.init_networking()
  58. def load_tests(loader, tests, pattern):
  59. for entry in pkg_resources.iter_entry_points('qubes.tests.extra'):
  60. try:
  61. for test_case in entry.load()():
  62. tests.addTests(loader.loadTestsFromTestCase(test_case))
  63. except Exception as err: # pylint: disable=broad-except
  64. def runTest(self):
  65. raise err
  66. ExtraLoadFailure = type('ExtraLoadFailure',
  67. (qubes.tests.QubesTestCase,),
  68. {entry.name: runTest})
  69. tests.addTest(ExtraLoadFailure(entry.name))
  70. for entry in pkg_resources.iter_entry_points(
  71. 'qubes.tests.extra.for_template'):
  72. try:
  73. for test_case in entry.load()():
  74. for template in qubes.tests.list_templates():
  75. tests.addTests(loader.loadTestsFromTestCase(
  76. type(
  77. '{}_{}_{}'.format(
  78. entry.name, test_case.__name__, template),
  79. (test_case,),
  80. {'template': template}
  81. )
  82. ))
  83. except Exception as err: # pylint: disable=broad-except
  84. def runTest(self):
  85. raise err
  86. ExtraForTemplateLoadFailure = type('ExtraForTemplateLoadFailure',
  87. (qubes.tests.QubesTestCase,),
  88. {entry.name: runTest})
  89. tests.addTest(ExtraLoadFailure(entry.name))
  90. return tests