devices_pci.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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.loop.run_until_complete(
  47. self.vm.create_on_disk())
  48. self.vm.features['pci-no-strict-reset/' + pcidev] = True
  49. self.app.save()
  50. @unittest.expectedFailure
  51. def test_000_list(self):
  52. p = subprocess.Popen(['lspci'], stdout=subprocess.PIPE)
  53. # get a dict: BDF -> description
  54. actual_devices = dict(
  55. l.split(' (')[0].split(' ', 1)
  56. for l in p.communicate()[0].decode().splitlines())
  57. for dev in self.app.domains[0].devices['pci']:
  58. lspci_ident = dev.ident.replace('_', ':')
  59. self.assertIsInstance(dev, qubes.ext.pci.PCIDevice)
  60. self.assertEqual(dev.backend_domain, self.app.domains[0])
  61. self.assertIn(lspci_ident, actual_devices)
  62. self.assertEqual(dev.description, actual_devices[lspci_ident])
  63. actual_devices.pop(lspci_ident)
  64. if actual_devices:
  65. self.fail('Not all devices listed, missing: {}'.format(
  66. actual_devices))
  67. def assertDeviceNotInCollection(self, dev, dev_col):
  68. self.assertNotIn(dev, dev_col.attached())
  69. self.assertNotIn(dev, dev_col.persistent())
  70. self.assertNotIn(dev, dev_col.assignments())
  71. self.assertNotIn(dev, dev_col.assignments(persistent=True))
  72. def test_010_attach_offline_persistent(self):
  73. dev_col = self.vm.devices['pci']
  74. self.assertDeviceNotInCollection(self.dev, dev_col)
  75. self.loop.run_until_complete(
  76. dev_col.attach(self.assignment))
  77. self.app.save()
  78. self.assertNotIn(self.dev, dev_col.attached())
  79. self.assertIn(self.dev, dev_col.persistent())
  80. self.assertIn(self.dev, dev_col.assignments())
  81. self.assertIn(self.dev, dev_col.assignments(persistent=True))
  82. self.assertNotIn(self.dev, dev_col.assignments(persistent=False))
  83. self.loop.run_until_complete(self.vm.start())
  84. self.assertIn(self.dev, dev_col.attached())
  85. (stdout, _) = self.loop.run_until_complete(
  86. self.vm.run_for_stdio('lspci'))
  87. self.assertIn(self.dev.description, stdout.decode())
  88. def test_011_attach_offline_temp_fail(self):
  89. dev_col = self.vm.devices['pci']
  90. self.assertDeviceNotInCollection(self.dev, dev_col)
  91. self.assignment.persistent = False
  92. with self.assertRaises(qubes.exc.QubesVMNotRunningError):
  93. self.loop.run_until_complete(
  94. dev_col.attach(self.assignment))
  95. def test_020_attach_online_persistent(self):
  96. self.loop.run_until_complete(
  97. self.vm.start())
  98. dev_col = self.vm.devices['pci']
  99. self.assertDeviceNotInCollection(self.dev, dev_col)
  100. self.loop.run_until_complete(
  101. dev_col.attach(self.assignment))
  102. self.assertIn(self.dev, dev_col.attached())
  103. self.assertIn(self.dev, dev_col.persistent())
  104. self.assertIn(self.dev, dev_col.assignments())
  105. self.assertIn(self.dev, dev_col.assignments(persistent=True))
  106. self.assertNotIn(self.dev, dev_col.assignments(persistent=False))
  107. # give VM kernel some time to discover new device
  108. time.sleep(1)
  109. (stdout, _) = self.loop.run_until_complete(
  110. self.vm.run_for_stdio('lspci'))
  111. self.assertIn(self.dev.description, stdout.decode())
  112. def test_021_persist_detach_online_fail(self):
  113. dev_col = self.vm.devices['pci']
  114. self.assertDeviceNotInCollection(self.dev, dev_col)
  115. self.loop.run_until_complete(
  116. dev_col.attach(self.assignment))
  117. self.app.save()
  118. self.loop.run_until_complete(
  119. self.vm.start())
  120. with self.assertRaises(qubes.exc.QubesVMNotHaltedError):
  121. self.loop.run_until_complete(
  122. self.vm.devices['pci'].detach(self.assignment))
  123. def test_030_persist_attach_detach_offline(self):
  124. dev_col = self.vm.devices['pci']
  125. self.assertDeviceNotInCollection(self.dev, dev_col)
  126. self.loop.run_until_complete(
  127. dev_col.attach(self.assignment))
  128. self.app.save()
  129. self.assertNotIn(self.dev, dev_col.attached())
  130. self.assertIn(self.dev, dev_col.persistent())
  131. self.assertIn(self.dev, dev_col.assignments())
  132. self.assertIn(self.dev, dev_col.assignments(persistent=True))
  133. self.assertNotIn(self.dev, dev_col.assignments(persistent=False))
  134. self.loop.run_until_complete(
  135. dev_col.detach(self.assignment))
  136. self.assertDeviceNotInCollection(self.dev, dev_col)
  137. def test_031_attach_detach_online_temp(self):
  138. dev_col = self.vm.devices['pci']
  139. self.loop.run_until_complete(
  140. self.vm.start())
  141. self.assignment.persistent = False
  142. self.assertDeviceNotInCollection(self.dev, dev_col)
  143. self.loop.run_until_complete(
  144. dev_col.attach(self.assignment))
  145. self.assertIn(self.dev, dev_col.attached())
  146. self.assertNotIn(self.dev, dev_col.persistent())
  147. self.assertIn(self.dev, dev_col.assignments())
  148. self.assertIn(self.dev, dev_col.assignments(persistent=False))
  149. self.assertNotIn(self.dev, dev_col.assignments(persistent=True))
  150. self.assertIn(self.dev, dev_col.assignments(persistent=False))
  151. # give VM kernel some time to discover new device
  152. time.sleep(1)
  153. (stdout, _) = self.loop.run_until_complete(
  154. self.vm.run_for_stdio('lspci'))
  155. self.assertIn(self.dev.description, stdout.decode())
  156. self.loop.run_until_complete(
  157. dev_col.detach(self.assignment))
  158. self.assertDeviceNotInCollection(self.dev, dev_col)
  159. (stdout, _) = self.loop.run_until_complete(
  160. self.vm.run_for_stdio('lspci'))
  161. self.assertNotIn(self.dev.description, stdout.decode())