extra.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 sys
  24. import pkg_resources
  25. import qubes.tests
  26. import qubes.vm.appvm
  27. import qubes.vm.templatevm
  28. class ExtraTestCase(qubes.tests.SystemTestsMixin, qubes.tests.QubesTestCase):
  29. template = None
  30. def setUp(self):
  31. super(ExtraTestCase, self).setUp()
  32. self.init_default_template(self.template)
  33. def create_vms(self, names):
  34. """
  35. Create AppVMs for the duration of the test. Will be automatically
  36. removed after completing the test.
  37. :param names: list of VM names to create (each of them will be
  38. prefixed with some test specific string)
  39. :return: list of created VM objects
  40. """
  41. if self.template:
  42. template = self.app.domains[self.template]
  43. else:
  44. template = self.app.default_template
  45. for vmname in names:
  46. vm = self.app.add_new_vm(qubes.vm.appvm.AppVM,
  47. name=self.make_vm_name(vmname),
  48. template=template,
  49. label='red')
  50. vm.create_on_disk()
  51. self.save_and_reload_db()
  52. # get objects after reload
  53. vms = []
  54. for vmname in names:
  55. vms.append(self.app.domains[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. self.init_networking()
  62. def load_tests(loader, tests, pattern):
  63. for entry in pkg_resources.iter_entry_points('qubes.tests.extra'):
  64. try:
  65. for test_case in entry.load()():
  66. tests.addTests(loader.loadTestsFromTestCase(test_case))
  67. except Exception as err: # pylint: disable=broad-except
  68. tb = sys.exc_info()[2]
  69. def runTest(self):
  70. raise err, None, tb
  71. ExtraLoadFailure = type('ExtraLoadFailure',
  72. (qubes.tests.QubesTestCase,),
  73. {entry.name: runTest})
  74. tests.addTest(ExtraLoadFailure(entry.name))
  75. try:
  76. app = qubes.Qubes()
  77. templates = [vm.name for vm in app.domains if
  78. isinstance(vm, qubes.vm.templatevm.TemplateVM)]
  79. except OSError:
  80. templates = []
  81. for entry in pkg_resources.iter_entry_points(
  82. 'qubes.tests.extra.for_template'):
  83. try:
  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,),
  91. {'template': template}
  92. )
  93. ))
  94. except Exception as err: # pylint: disable=broad-except
  95. tb = sys.exc_info()[2]
  96. def runTest(self):
  97. raise err, None, tb
  98. ExtraForTemplateLoadFailure = type('ExtraForTemplateLoadFailure',
  99. (qubes.tests.QubesTestCase,),
  100. {entry.name: runTest})
  101. tests.addTest(ExtraLoadFailure(entry.name))
  102. return tests