extra.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.vm.appvm
  26. import qubes.vm.templatevm
  27. class ExtraTestCase(qubes.tests.SystemTestsMixin, qubes.tests.QubesTestCase):
  28. template = None
  29. def setUp(self):
  30. super(ExtraTestCase, self).setUp()
  31. self.init_default_template(self.template)
  32. def create_vms(self, names):
  33. """
  34. Create AppVMs for the duration of the test. Will be automatically
  35. removed after completing the test.
  36. :param names: list of VM names to create (each of them will be
  37. prefixed with some test specific string)
  38. :return: list of created VM objects
  39. """
  40. if self.template:
  41. template = self.app.domains[self.template]
  42. else:
  43. template = self.app.default_template
  44. for vmname in names:
  45. vm = self.app.add_new_vm(qubes.vm.appvm.AppVM,
  46. name=self.make_vm_name(vmname),
  47. template=template,
  48. label='red')
  49. vm.create_on_disk()
  50. self.save_and_reload_db()
  51. # get objects after reload
  52. vms = []
  53. for vmname in names:
  54. vms.append(self.app.domains[self.make_vm_name(vmname)])
  55. return vms
  56. def enable_network(self):
  57. """
  58. Enable access to the network. Must be called before creating VMs.
  59. """
  60. self.init_networking()
  61. def load_tests(loader, tests, pattern):
  62. for entry in pkg_resources.iter_entry_points('qubes.tests.extra'):
  63. for test_case in entry.load()():
  64. tests.addTests(loader.loadTestsFromTestCase(test_case))
  65. try:
  66. app = qubes.Qubes()
  67. templates = [vm.name for vm in app.domains if
  68. isinstance(vm, qubes.vm.templatevm.TemplateVM)]
  69. except OSError:
  70. templates = []
  71. for entry in pkg_resources.iter_entry_points(
  72. 'qubes.tests.extra.for_template'):
  73. for test_case in entry.load()():
  74. for template in 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. return tests