devices_pci.py 6.8 KB

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