extra.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program 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
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. import sys
  22. import pkg_resources
  23. import qubes.tests
  24. import qubes.vm.appvm
  25. class ExtraTestCase(qubes.tests.SystemTestCase):
  26. template = None
  27. def setUp(self):
  28. super(ExtraTestCase, self).setUp()
  29. self.init_default_template(self.template)
  30. def create_vms(self, names):
  31. """
  32. Create AppVMs for the duration of the test. Will be automatically
  33. removed after completing the test.
  34. :param names: list of VM names to create (each of them will be
  35. prefixed with some test specific string)
  36. :return: list of created VM objects
  37. """
  38. if self.template:
  39. template = self.app.domains[self.template]
  40. else:
  41. template = self.app.default_template
  42. for vmname in names:
  43. vm = self.app.add_new_vm(qubes.vm.appvm.AppVM,
  44. name=self.make_vm_name(vmname),
  45. template=template,
  46. label='red')
  47. vm.create_on_disk()
  48. self.app.save()
  49. # get objects after reload
  50. vms = []
  51. for vmname in names:
  52. vms.append(self.app.domains[self.make_vm_name(vmname)])
  53. return vms
  54. def enable_network(self):
  55. """
  56. Enable access to the network. Must be called before creating VMs.
  57. """
  58. self.init_networking()
  59. def load_tests(loader, tests, pattern):
  60. for entry in pkg_resources.iter_entry_points('qubes.tests.extra'):
  61. try:
  62. for test_case in entry.load()():
  63. tests.addTests(loader.loadTestsFromTestCase(test_case))
  64. except Exception as err: # pylint: disable=broad-except
  65. def runTest(self):
  66. raise err
  67. ExtraLoadFailure = type('ExtraLoadFailure',
  68. (qubes.tests.QubesTestCase,),
  69. {entry.name: runTest})
  70. tests.addTest(ExtraLoadFailure(entry.name))
  71. for entry in pkg_resources.iter_entry_points(
  72. 'qubes.tests.extra.for_template'):
  73. try:
  74. for test_case in entry.load()():
  75. for template in qubes.tests.list_templates():
  76. tests.addTests(loader.loadTestsFromTestCase(
  77. type(
  78. '{}_{}_{}'.format(
  79. entry.name, test_case.__name__, template),
  80. (test_case,),
  81. {'template': template}
  82. )
  83. ))
  84. except Exception as err: # pylint: disable=broad-except
  85. def runTest(self):
  86. raise err
  87. ExtraForTemplateLoadFailure = type('ExtraForTemplateLoadFailure',
  88. (qubes.tests.QubesTestCase,),
  89. {entry.name: runTest})
  90. tests.addTest(ExtraLoadFailure(entry.name))
  91. return tests