devices_pci.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program 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
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  22. import os
  23. import subprocess
  24. import time
  25. import unittest
  26. import qubes.devices
  27. import qubes.ext.pci
  28. import qubes.tests
  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.get('QUBES_TEST_PCIDEV', None)
  34. if pcidev is None:
  35. self.skipTest('Specify PCI device with QUBES_TEST_PCIDEV '
  36. 'environment variable')
  37. self.dev = self.app.domains[0].devices['pci'][pcidev]
  38. self.assignment = qubes.devices.DeviceAssignment(backend_domain=self.dev.backend_domain, ident=self.dev.ident, 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.create_on_disk()
  47. self.vm.features['pci-no-strict-reset/' + pcidev] = True
  48. self.app.save()
  49. @unittest.expectedFailure
  50. def test_000_list(self):
  51. p = subprocess.Popen(['lspci'], stdout=subprocess.PIPE)
  52. # get a dict: BDF -> description
  53. actual_devices = dict(
  54. l.split(' (')[0].split(' ', 1)
  55. for l in p.communicate()[0].decode().splitlines())
  56. for dev in self.app.domains[0].devices['pci']:
  57. self.assertIsInstance(dev, qubes.ext.pci.PCIDevice)
  58. self.assertEqual(dev.backend_domain, self.app.domains[0])
  59. self.assertIn(dev.ident, actual_devices)
  60. self.assertEqual(dev.description, actual_devices[dev.ident])
  61. actual_devices.pop(dev.ident)
  62. if actual_devices:
  63. self.fail('Not all devices listed, missing: {}'.format(
  64. actual_devices))
  65. def assertDeviceNotInCollection(self, dev, dev_col):
  66. self.assertNotIn(dev, dev_col.attached())
  67. self.assertNotIn(dev, dev_col.persistent())
  68. self.assertNotIn(dev, dev_col.assignments())
  69. self.assertNotIn(dev, dev_col.assignments(persistent=True))
  70. def test_010_attach_offline_persistent(self):
  71. dev_col = self.vm.devices['pci']
  72. self.assertDeviceNotInCollection(self.dev, dev_col)
  73. dev_col.attach(self.assignment)
  74. self.app.save()
  75. self.assertNotIn(self.dev, dev_col.attached())
  76. self.assertIn(self.dev, dev_col.persistent())
  77. self.assertIn(self.dev, dev_col.assignments())
  78. self.assertIn(self.dev, dev_col.assignments(persistent=True))
  79. self.assertNotIn(self.dev, dev_col.assignments(persistent=False))
  80. self.vm.start()
  81. self.assertIn(self.dev, dev_col.attached())
  82. p = self.vm.run('lspci', passio_popen=True)
  83. (stdout, _) = p.communicate()
  84. self.assertIn(self.dev.description, stdout.decode())
  85. def test_011_attach_offline_temp_fail(self):
  86. dev_col = self.vm.devices['pci']
  87. self.assertDeviceNotInCollection(self.dev, dev_col)
  88. self.assignment.persistent = False
  89. with self.assertRaises(qubes.exc.QubesVMNotRunningError):
  90. dev_col.attach(self.assignment)
  91. def test_020_attach_online_persistent(self):
  92. self.vm.start()
  93. dev_col = self.vm.devices['pci']
  94. self.assertDeviceNotInCollection(self.dev, dev_col)
  95. dev_col.attach(self.assignment)
  96. self.assertIn(self.dev, dev_col.attached())
  97. self.assertIn(self.dev, dev_col.persistent())
  98. self.assertIn(self.dev, dev_col.assignments())
  99. self.assertIn(self.dev, dev_col.assignments(persistent=True))
  100. self.assertNotIn(self.dev, dev_col.assignments(persistent=False))
  101. # give VM kernel some time to discover new device
  102. time.sleep(1)
  103. p = self.vm.run('lspci', passio_popen=True)
  104. (stdout, _) = p.communicate()
  105. self.assertIn(self.dev.description, stdout.decode())
  106. def test_021_persist_detach_online_fail(self):
  107. dev_col = self.vm.devices['pci']
  108. self.assertDeviceNotInCollection(self.dev, dev_col)
  109. dev_col.attach(self.assignment)
  110. self.app.save()
  111. self.vm.start()
  112. with self.assertRaises(qubes.exc.QubesVMNotHaltedError):
  113. self.vm.devices['pci'].detach(self.assignment)
  114. def test_030_persist_attach_detach_offline(self):
  115. dev_col = self.vm.devices['pci']
  116. self.assertDeviceNotInCollection(self.dev, dev_col)
  117. dev_col.attach(self.assignment)
  118. self.app.save()
  119. self.assertNotIn(self.dev, dev_col.attached())
  120. self.assertIn(self.dev, dev_col.persistent())
  121. self.assertIn(self.dev, dev_col.assignments())
  122. self.assertIn(self.dev, dev_col.assignments(persistent=True))
  123. self.assertNotIn(self.dev, dev_col.assignments(persistent=False))
  124. dev_col.detach(self.assignment)
  125. self.assertDeviceNotInCollection(self.dev, dev_col)
  126. def test_031_attach_detach_online_temp(self):
  127. dev_col = self.vm.devices['pci']
  128. self.vm.start()
  129. self.assignment.persistent = False
  130. self.assertDeviceNotInCollection(self.dev, dev_col)
  131. dev_col.attach(self.assignment)
  132. self.assertIn(self.dev, dev_col.attached())
  133. self.assertNotIn(self.dev, dev_col.persistent())
  134. self.assertIn(self.dev, dev_col.assignments())
  135. self.assertIn(self.dev, dev_col.assignments(persistent=False))
  136. self.assertNotIn(self.dev, dev_col.assignments(persistent=True))
  137. self.assertIn(self.dev, dev_col.assignments(persistent=False))
  138. # give VM kernel some time to discover new device
  139. time.sleep(1)
  140. p = self.vm.run('lspci', passio_popen=True)
  141. (stdout, _) = p.communicate()
  142. self.assertIn(self.dev.description, stdout.decode())
  143. dev_col.detach(self.assignment)
  144. self.assertDeviceNotInCollection(self.dev, dev_col)
  145. p = self.vm.run('lspci', passio_popen=True)
  146. (stdout, _) = p.communicate()
  147. self.assertNotIn(self.dev.description, stdout.decode())