qvm_device.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. # pylint: disable=protected-access
  2. #
  3. # The Qubes OS Project, https://www.qubes-os.org/
  4. #
  5. # Copyright (C) 2017 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser 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. ''' Tests for the `qvm-device` tool. '''
  23. import unittest.mock as mock
  24. import qubesadmin.tests
  25. import qubesadmin.tests.tools
  26. import qubesadmin.devices
  27. import qubesadmin.tools.qvm_device
  28. class TC_00_qvm_device(qubesadmin.tests.QubesTestCase):
  29. ''' Tests the output logic of the qvm-device tool '''
  30. def setUp(self):
  31. super(TC_00_qvm_device, self).setUp()
  32. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  33. b'0\0test-vm1 class=AppVM state=Running\n' \
  34. b'test-vm2 class=AppVM state=Running\n' \
  35. b'test-vm3 class=AppVM state=Running\n'
  36. self.app.expected_calls[('test-vm1', 'admin.vm.device.test.Available',
  37. None, None)] = \
  38. b'0\0dev1 description=Description here\n'
  39. self.vm1 = self.app.domains['test-vm1']
  40. self.vm2 = self.app.domains['test-vm2']
  41. self.vm1_device = self.app.domains['test-vm1'].devices['test']['dev1']
  42. def test_000_list_all(self):
  43. ''' List all exposed vm devices. No devices are attached to other
  44. domains.
  45. '''
  46. self.app.expected_calls[('test-vm2', 'admin.vm.device.test.Available',
  47. None, None)] = \
  48. b'0\0dev2 description=Description here2\n'
  49. self.app.expected_calls[('test-vm3', 'admin.vm.device.test.Available',
  50. None, None)] = \
  51. b'0\0'
  52. self.app.expected_calls[('test-vm1', 'admin.vm.device.test.List',
  53. None, None)] = b'0\0'
  54. self.app.expected_calls[('test-vm2', 'admin.vm.device.test.List',
  55. None, None)] = b'0\0'
  56. self.app.expected_calls[('test-vm3', 'admin.vm.device.test.List',
  57. None, None)] = b'0\0'
  58. with qubesadmin.tests.tools.StdoutBuffer() as buf:
  59. qubesadmin.tools.qvm_device.main(
  60. ['test', 'list'], app=self.app)
  61. self.assertEqual(
  62. [x.rstrip() for x in buf.getvalue().splitlines()],
  63. ['test-vm1:dev1 Description here',
  64. 'test-vm2:dev2 Description here2']
  65. )
  66. def test_001_list_persistent_attach(self):
  67. ''' Attach the device exposed by the `vm1` to the `vm3` persistently.
  68. '''
  69. self.app.expected_calls[('test-vm2', 'admin.vm.device.test.Available',
  70. None, None)] = \
  71. b'0\0dev2 description=Description here2\n'
  72. self.app.expected_calls[('test-vm3', 'admin.vm.device.test.Available',
  73. None, None)] = \
  74. b'0\0'
  75. self.app.expected_calls[('test-vm1', 'admin.vm.device.test.List',
  76. None, None)] = b'0\0'
  77. self.app.expected_calls[('test-vm2', 'admin.vm.device.test.List',
  78. None, None)] = b'0\0'
  79. self.app.expected_calls[('test-vm3', 'admin.vm.device.test.List',
  80. None, None)] = \
  81. b'0\0test-vm1+dev1 persistent=True\n'
  82. with qubesadmin.tests.tools.StdoutBuffer() as buf:
  83. qubesadmin.tools.qvm_device.main(
  84. ['test', 'list', 'test-vm3'], app=self.app)
  85. self.assertEqual(
  86. buf.getvalue(),
  87. 'test-vm1:dev1 Description here test-vm3\n'
  88. )
  89. def test_002_list_list_temp_attach(self):
  90. ''' Attach the device exposed by the `vm1` to the `vm3`
  91. non-persistently.
  92. '''
  93. self.app.expected_calls[('test-vm2', 'admin.vm.device.test.Available',
  94. None, None)] = \
  95. b'0\0dev2 description=Description here2\n'
  96. self.app.expected_calls[('test-vm3', 'admin.vm.device.test.Available',
  97. None, None)] = \
  98. b'0\0'
  99. self.app.expected_calls[('test-vm1', 'admin.vm.device.test.List',
  100. None, None)] = b'0\0'
  101. self.app.expected_calls[('test-vm2', 'admin.vm.device.test.List',
  102. None, None)] = b'0\0'
  103. self.app.expected_calls[('test-vm3', 'admin.vm.device.test.List',
  104. None, None)] = \
  105. b'0\0test-vm1+dev1\n'
  106. with qubesadmin.tests.tools.StdoutBuffer() as buf:
  107. qubesadmin.tools.qvm_device.main(
  108. ['test', 'list', 'test-vm3'], app=self.app)
  109. self.assertEqual(
  110. buf.getvalue(),
  111. 'test-vm1:dev1 Description here test-vm3\n'
  112. )
  113. def test_010_attach(self):
  114. ''' Test attach action '''
  115. self.app.expected_calls[('test-vm2', 'admin.vm.device.test.Attach',
  116. 'test-vm1+dev1', b'')] = b'0\0'
  117. qubesadmin.tools.qvm_device.main(
  118. ['test', 'attach', 'test-vm2', 'test-vm1:dev1'], app=self.app)
  119. self.assertAllCalled()
  120. def test_011_attach_options(self):
  121. ''' Test attach action '''
  122. self.app.expected_calls[('test-vm2', 'admin.vm.device.test.Attach',
  123. 'test-vm1+dev1', b'ro=True')] = b'0\0'
  124. qubesadmin.tools.qvm_device.main(
  125. ['test', 'attach', '-o', 'ro=True', 'test-vm2', 'test-vm1:dev1'],
  126. app=self.app)
  127. self.assertAllCalled()
  128. def test_011_attach_persistent(self):
  129. ''' Test attach action '''
  130. self.app.expected_calls[('test-vm2', 'admin.vm.device.test.Attach',
  131. 'test-vm1+dev1', b'persistent=True')] = b'0\0'
  132. qubesadmin.tools.qvm_device.main(
  133. ['test', 'attach', '-p', 'test-vm2', 'test-vm1:dev1'],
  134. app=self.app)
  135. self.assertAllCalled()
  136. def test_012_attach_invalid(self):
  137. ''' Test attach action '''
  138. with qubesadmin.tests.tools.StderrBuffer() as stderr:
  139. with self.assertRaises(SystemExit):
  140. qubesadmin.tools.qvm_device.main(
  141. ['test', 'attach', '-p', 'test-vm2', 'dev1'],
  142. app=self.app)
  143. self.assertIn('expected a backend vm & device id',
  144. stderr.getvalue())
  145. self.assertAllCalled()
  146. def test_013_attach_invalid2(self):
  147. ''' Test attach action '''
  148. with qubesadmin.tests.tools.StderrBuffer() as stderr:
  149. with self.assertRaises(SystemExit):
  150. qubesadmin.tools.qvm_device.main(
  151. ['test', 'attach', '-p', 'test-vm2', 'test-vm1:invalid'],
  152. app=self.app)
  153. self.assertIn('doesn\'t expose device',
  154. stderr.getvalue())
  155. self.assertAllCalled()
  156. def test_014_attach_invalid3(self):
  157. ''' Test attach action '''
  158. with qubesadmin.tests.tools.StderrBuffer() as stderr:
  159. with self.assertRaises(SystemExit):
  160. qubesadmin.tools.qvm_device.main(
  161. ['test', 'attach', '-p', 'test-vm2', 'no-such-vm:dev3'],
  162. app=self.app)
  163. self.assertIn('no backend vm',
  164. stderr.getvalue())
  165. self.assertAllCalled()
  166. def test_020_detach(self):
  167. ''' Test detach action '''
  168. self.app.expected_calls[('test-vm2', 'admin.vm.device.test.Detach',
  169. 'test-vm1+dev1', None)] = b'0\0'
  170. qubesadmin.tools.qvm_device.main(
  171. ['test', 'detach', 'test-vm2', 'test-vm1:dev1'], app=self.app)
  172. self.assertAllCalled()
  173. def test_021_detach_unknown(self):
  174. ''' Test detach action '''
  175. self.app.expected_calls[('test-vm2', 'admin.vm.device.test.Detach',
  176. 'test-vm1+dev7', None)] = b'0\0'
  177. qubesadmin.tools.qvm_device.main(
  178. ['test', 'detach', 'test-vm2', 'test-vm1:dev7'], app=self.app)
  179. self.assertAllCalled()
  180. def test_022_detach_all(self):
  181. ''' Test detach action '''
  182. self.app.expected_calls[('test-vm2', 'admin.vm.device.test.List',
  183. None, None)] = \
  184. b'0\0test-vm1+dev1\ntest-vm1+dev2\n'
  185. self.app.expected_calls[('test-vm2', 'admin.vm.device.test.Detach',
  186. 'test-vm1+dev1', None)] = b'0\0'
  187. self.app.expected_calls[('test-vm2', 'admin.vm.device.test.Detach',
  188. 'test-vm1+dev2', None)] = b'0\0'
  189. qubesadmin.tools.qvm_device.main(
  190. ['test', 'detach', 'test-vm2'], app=self.app)
  191. self.assertAllCalled()