qvm_device.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/usr/bin/python2 -O
  2. # vim: fileencoding=utf-8
  3. # pylint: disable=protected-access,pointless-statement
  4. #
  5. # The Qubes OS Project, https://www.qubes-os.org/
  6. #
  7. # Copyright (C) 2015 Marek Marczykowski-Górecki
  8. # <marmarek@invisiblethingslab.com>
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; either version 2 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License along
  21. # with this program; if not, write to the Free Software Foundation, Inc.,
  22. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. #
  24. import qubes
  25. import qubes.devices
  26. import qubes.tools.qvm_device
  27. import qubes.tests
  28. import qubes.tests.devices
  29. import qubes.tests.tools
  30. class TestNamespace(object):
  31. def __init__(self, app, domains=None, device=None):
  32. super(TestNamespace, self).__init__()
  33. self.app = app
  34. self.devclass = 'testclass'
  35. if domains:
  36. self.domains = domains
  37. if device:
  38. self.device = device
  39. class TC_00_Actions(qubes.tests.QubesTestCase):
  40. def setUp(self):
  41. super(TC_00_Actions, self).setUp()
  42. self.app = qubes.tests.devices.TestApp()
  43. self.vm1 = qubes.tests.devices.TestVM(self.app, 'vm1')
  44. self.vm2 = qubes.tests.devices.TestVM(self.app, 'vm2')
  45. self.device = self.vm2.device
  46. def test_000_list_all(self):
  47. args = TestNamespace(self.app)
  48. with qubes.tests.tools.StdoutBuffer() as buf:
  49. qubes.tools.qvm_device.list_devices(args)
  50. self.assertEventFired(self.vm1,
  51. 'device-list:testclass')
  52. self.assertEventFired(self.vm2,
  53. 'device-list:testclass')
  54. self.assertEventNotFired(self.vm1,
  55. 'device-list-attached:testclass')
  56. self.assertEventNotFired(self.vm2,
  57. 'device-list-attached:testclass')
  58. self.assertEqual(
  59. [x.rstrip() for x in buf.getvalue().splitlines()],
  60. ['vm1:testdev Description',
  61. 'vm2:testdev Description']
  62. )
  63. def test_001_list_one(self):
  64. args = TestNamespace(self.app, [self.vm1])
  65. # simulate attach
  66. self.vm2.device.frontend_domain = self.vm1
  67. self.vm1.devices['testclass']._set.add(self.device)
  68. with qubes.tests.tools.StdoutBuffer() as buf:
  69. qubes.tools.qvm_device.list_devices(args)
  70. self.assertEventFired(self.vm1,
  71. 'device-list-attached:testclass')
  72. self.assertEventNotFired(self.vm1,
  73. 'device-list:testclass')
  74. self.assertEventNotFired(self.vm2,
  75. 'device-list:testclass')
  76. self.assertEventNotFired(self.vm2,
  77. 'device-list-attached:testclass')
  78. self.assertEqual(
  79. buf.getvalue(),
  80. 'vm2:testdev Description vm1\n'
  81. )
  82. def test_002_list_one_non_persistent(self):
  83. args = TestNamespace(self.app, [self.vm1])
  84. # simulate attach
  85. self.vm2.device.frontend_domain = self.vm1
  86. with qubes.tests.tools.StdoutBuffer() as buf:
  87. qubes.tools.qvm_device.list_devices(args)
  88. self.assertEventFired(self.vm1,
  89. 'device-list-attached:testclass')
  90. self.assertEventNotFired(self.vm1,
  91. 'device-list:testclass')
  92. self.assertEventNotFired(self.vm2,
  93. 'device-list:testclass')
  94. self.assertEventNotFired(self.vm2,
  95. 'device-list-attached:testclass')
  96. self.assertEqual(
  97. buf.getvalue(),
  98. 'vm2:testdev Description vm1\n'
  99. )
  100. def test_010_attach(self):
  101. args = TestNamespace(
  102. self.app,
  103. [self.vm1],
  104. self.device
  105. )
  106. qubes.tools.qvm_device.attach_device(args)
  107. self.assertEventFired(self.vm1,
  108. 'device-attach:testclass', [self.device])
  109. self.assertEventNotFired(self.vm2,
  110. 'device-attach:testclass', [self.device])
  111. def test_011_double_attach(self):
  112. args = TestNamespace(
  113. self.app,
  114. [self.vm1],
  115. self.device
  116. )
  117. qubes.tools.qvm_device.attach_device(args)
  118. with self.assertRaises(qubes.exc.QubesException):
  119. qubes.tools.qvm_device.attach_device(args)
  120. def test_020_detach(self):
  121. args = TestNamespace(
  122. self.app,
  123. [self.vm1],
  124. self.device
  125. )
  126. # simulate attach
  127. self.vm2.device.frontend_domain = self.vm1
  128. self.vm1.devices['testclass']._set.add(self.device)
  129. qubes.tools.qvm_device.detach_device(args)
  130. def test_021_detach_not_attached(self):
  131. args = TestNamespace(
  132. self.app,
  133. [self.vm1],
  134. self.device
  135. )
  136. with self.assertRaises(qubes.exc.QubesException):
  137. qubes.tools.qvm_device.detach_device(args)