extra.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. import qubes.vm.templatevm
  26. class ExtraTestCase(qubes.tests.SystemTestCase):
  27. template = None
  28. def setUp(self):
  29. super(ExtraTestCase, self).setUp()
  30. self.init_default_template(self.template)
  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. if self.template:
  40. template = self.app.domains[self.template]
  41. else:
  42. template = self.app.default_template
  43. for vmname in names:
  44. vm = self.app.add_new_vm(qubes.vm.appvm.AppVM,
  45. name=self.make_vm_name(vmname),
  46. template=template,
  47. label='red')
  48. vm.create_on_disk()
  49. self.app.save()
  50. # get objects after reload
  51. vms = []
  52. for vmname in names:
  53. vms.append(self.app.domains[self.make_vm_name(vmname)])
  54. return vms
  55. def enable_network(self):
  56. """
  57. Enable access to the network. Must be called before creating VMs.
  58. """
  59. self.init_networking()
  60. def load_tests(loader, tests, pattern):
  61. for entry in pkg_resources.iter_entry_points('qubes.tests.extra'):
  62. try:
  63. for test_case in entry.load()():
  64. tests.addTests(loader.loadTestsFromTestCase(test_case))
  65. except Exception as err: # pylint: disable=broad-except
  66. def runTest(self):
  67. raise err
  68. ExtraLoadFailure = type('ExtraLoadFailure',
  69. (qubes.tests.QubesTestCase,),
  70. {entry.name: runTest})
  71. tests.addTest(ExtraLoadFailure(entry.name))
  72. try:
  73. app = qubes.Qubes()
  74. templates = [vm.name for vm in app.domains if
  75. isinstance(vm, qubes.vm.templatevm.TemplateVM)]
  76. except OSError:
  77. templates = []
  78. for entry in pkg_resources.iter_entry_points(
  79. 'qubes.tests.extra.for_template'):
  80. try:
  81. for test_case in entry.load()():
  82. for template in templates:
  83. tests.addTests(loader.loadTestsFromTestCase(
  84. type(
  85. '{}_{}_{}'.format(
  86. entry.name, test_case.__name__, template),
  87. (test_case,),
  88. {'template': template}
  89. )
  90. ))
  91. except Exception as err: # pylint: disable=broad-except
  92. def runTest(self):
  93. raise err
  94. ExtraForTemplateLoadFailure = type('ExtraForTemplateLoadFailure',
  95. (qubes.tests.QubesTestCase,),
  96. {entry.name: runTest})
  97. tests.addTest(ExtraLoadFailure(entry.name))
  98. return tests