devices_pci.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # pylint: disable=protected-access,pointless-statement
  2. #
  3. # The Qubes OS Project, https://www.qubes-os.org/
  4. #
  5. # Copyright (C) 2015-2016 Joanna Rutkowska <joanna@invisiblethingslab.com>
  6. # Copyright (C) 2015-2016 Wojtek Porczyk <woju@invisiblethingslab.com>
  7. #
  8. # This library is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU Lesser General Public
  10. # License as published by the Free Software Foundation; either
  11. # version 2.1 of the License, or (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. # Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public
  19. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  20. #
  21. import os
  22. import subprocess
  23. import time
  24. import unittest
  25. import qubes.devices
  26. import qubes.ext.pci
  27. import qubes.tests
  28. @qubes.tests.skipUnlessEnv('QUBES_TEST_PCIDEV')
  29. class TC_00_Devices_PCI(qubes.tests.SystemTestCase):
  30. def setUp(self):
  31. super(TC_00_Devices_PCI, self).setUp()
  32. if self._testMethodName not in ['test_000_list']:
  33. pcidev = os.environ['QUBES_TEST_PCIDEV']
  34. self.dev = self.app.domains[0].devices['pci'][pcidev]
  35. self.assignment = qubes.devices.DeviceAssignment(
  36. backend_domain=self.dev.backend_domain,
  37. ident=self.dev.ident,
  38. persistent=True)
  39. if isinstance(self.dev, qubes.devices.UnknownDevice):
  40. self.skipTest('Specified device {} does not exists'.format(pcidev))
  41. self.init_default_template()
  42. self.vm = self.app.add_new_vm(qubes.vm.appvm.AppVM,
  43. name=self.make_vm_name('vm'),
  44. label='red',
  45. )
  46. self.vm.virt_mode = 'hvm'
  47. self.loop.run_until_complete(
  48. self.vm.create_on_disk())
  49. self.vm.features['pci-no-strict-reset/' + pcidev] = True
  50. self.app.save()
  51. @unittest.expectedFailure
  52. def test_000_list(self):
  53. p = subprocess.Popen(['lspci'], stdout=subprocess.PIPE)
  54. # get a dict: BDF -> description
  55. actual_devices = dict(
  56. l.split(' (')[0].split(' ', 1)
  57. for l in p.communicate()[0].decode().splitlines())
  58. for dev in self.app.domains[0].devices['pci']:
  59. lspci_ident = dev.ident.replace('_', ':')
  60. self.assertIsInstance(dev, qubes.ext.pci.PCIDevice)
  61. self.assertEqual(dev.backend_domain, self.app.domains[0])
  62. self.assertIn(lspci_ident, actual_devices)
  63. self.assertEqual(dev.description, actual_devices[lspci_ident])
  64. actual_devices.pop(lspci_ident)
  65. if actual_devices:
  66. self.fail('Not all devices listed, missing: {}'.format(
  67. actual_devices))
  68. def assertDeviceNotInCollection(self, dev, dev_col):
  69. self.assertNotIn(dev, dev_col.attached())
  70. self.assertNotIn(dev, dev_col.persistent())
  71. self.assertNotIn(dev, dev_col.assignments())
  72. self.assertNotIn(dev, dev_col.assignments(persistent=True))
  73. def test_010_attach_offline_persistent(self):
  74. dev_col = self.vm.devices['pci']
  75. self.assertDeviceNotInCollection(self.dev, dev_col)
  76. self.loop.run_until_complete(
  77. dev_col.attach(self.assignment))
  78. self.app.save()
  79. self.assertNotIn(self.dev, dev_col.attached())
  80. self.assertIn(self.dev, dev_col.persistent())
  81. self.assertIn(self.dev, dev_col.assignments())
  82. self.assertIn(self.dev, dev_col.assignments(persistent=True))
  83. self.assertNotIn(self.dev, dev_col.assignments(persistent=False))
  84. self.loop.run_until_complete(self.vm.start())
  85. self.assertIn(self.dev, dev_col.attached())
  86. (stdout, _) = self.loop.run_until_complete(
  87. self.vm.run_for_stdio('lspci'))
  88. self.assertIn(self.dev.description, stdout.decode())
  89. def test_011_attach_offline_temp_fail(self):
  90. dev_col = self.vm.devices['pci']
  91. self.assertDeviceNotInCollection(self.dev, dev_col)
  92. self.assignment.persistent = False
  93. with self.assertRaises(qubes.exc.QubesVMNotRunningError):
  94. self.loop.run_until_complete(
  95. dev_col.attach(self.assignment))
  96. def test_020_attach_online_persistent(self):
  97. self.loop.run_until_complete(
  98. self.vm.start())
  99. dev_col = self.vm.devices['pci']
  100. self.assertDeviceNotInCollection(self.dev, dev_col)
  101. self.loop.run_until_complete(
  102. dev_col.attach(self.assignment))
  103. self.assertIn(self.dev, dev_col.attached())
  104. self.assertIn(self.dev, dev_col.persistent())
  105. self.assertIn(self.dev, dev_col.assignments())
  106. self.assertIn(self.dev, dev_col.assignments(persistent=True))
  107. self.assertNotIn(self.dev, dev_col.assignments(persistent=False))
  108. # give VM kernel some time to discover new device
  109. time.sleep(1)
  110. (stdout, _) = self.loop.run_until_complete(
  111. self.vm.run_for_stdio('lspci'))
  112. self.assertIn(self.dev.description, stdout.decode())
  113. def test_021_persist_detach_online_fail(self):
  114. dev_col = self.vm.devices['pci']
  115. self.assertDeviceNotInCollection(self.dev, dev_col)
  116. self.loop.run_until_complete(
  117. dev_col.attach(self.assignment))
  118. self.app.save()
  119. self.loop.run_until_complete(
  120. self.vm.start())
  121. with self.assertRaises(qubes.exc.QubesVMNotHaltedError):
  122. self.loop.run_until_complete(
  123. self.vm.devices['pci'].detach(self.assignment))
  124. def test_030_persist_attach_detach_offline(self):
  125. dev_col = self.vm.devices['pci']
  126. self.assertDeviceNotInCollection(self.dev, dev_col)
  127. self.loop.run_until_complete(
  128. dev_col.attach(self.assignment))
  129. self.app.save()
  130. self.assertNotIn(self.dev, dev_col.attached())
  131. self.assertIn(self.dev, dev_col.persistent())
  132. self.assertIn(self.dev, dev_col.assignments())
  133. self.assertIn(self.dev, dev_col.assignments(persistent=True))
  134. self.assertNotIn(self.dev, dev_col.assignments(persistent=False))
  135. self.loop.run_until_complete(
  136. dev_col.detach(self.assignment))
  137. self.assertDeviceNotInCollection(self.dev, dev_col)
  138. def test_031_attach_detach_online_temp(self):
  139. dev_col = self.vm.devices['pci']
  140. self.loop.run_until_complete(
  141. self.vm.start())
  142. self.assignment.persistent = False
  143. self.assertDeviceNotInCollection(self.dev, dev_col)
  144. self.loop.run_until_complete(
  145. dev_col.attach(self.assignment))
  146. self.assertIn(self.dev, dev_col.attached())
  147. self.assertNotIn(self.dev, dev_col.persistent())
  148. self.assertIn(self.dev, dev_col.assignments())
  149. self.assertIn(self.dev, dev_col.assignments(persistent=False))
  150. self.assertNotIn(self.dev, dev_col.assignments(persistent=True))
  151. self.assertIn(self.dev, dev_col.assignments(persistent=False))
  152. # give VM kernel some time to discover new device
  153. time.sleep(1)
  154. (stdout, _) = self.loop.run_until_complete(
  155. self.vm.run_for_stdio('lspci'))
  156. self.assertIn(self.dev.description, stdout.decode())
  157. self.loop.run_until_complete(
  158. dev_col.detach(self.assignment))
  159. self.assertDeviceNotInCollection(self.dev, dev_col)
  160. (stdout, _) = self.loop.run_until_complete(
  161. self.vm.run_for_stdio('lspci'))
  162. self.assertNotIn(self.dev.description, stdout.decode())