extra.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 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. vm.create_on_disk(verbose=False)
  46. self.save_and_reload_db()
  47. # get objects after reload
  48. vms = []
  49. for vmname in names:
  50. vms.append(self.app.domains[self.make_vm_name])
  51. return vms
  52. def enable_network(self):
  53. """
  54. Enable access to the network. Must be called before creating VMs.
  55. """
  56. self.init_networking()
  57. def load_tests(loader, tests, pattern):
  58. for entry in pkg_resources.iter_entry_points('qubes.tests.extra'):
  59. for test_case in entry():
  60. tests.addTests(loader.loadTestsFromTestCase(test_case))
  61. try:
  62. app = qubes.Qubes()
  63. templates = [vm.name for vm in app.domains if
  64. isinstance(vm, qubes.vm.templatevm.TemplateVM)]
  65. except OSError:
  66. templates = []
  67. for entry in pkg_resources.iter_entry_points(
  68. 'qubes.tests.extra.for_template'):
  69. for test_case in entry.load()():
  70. for template in templates:
  71. tests.addTests(loader.loadTestsFromTestCase(
  72. type(
  73. '{}_{}_{}'.format(
  74. entry.name, test_case.__name__, template),
  75. (test_case,),
  76. {'template': template}
  77. )
  78. ))
  79. return tests