devices_pci.py 6.9 KB

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