devices.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. # -*- encoding: utf8 -*-
  2. #
  3. # The Qubes OS Project, http://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, see <http://www.gnu.org/licenses/>.
  20. import qubesadmin.tests
  21. import qubesadmin.devices
  22. class TC_00_DeviceCollection(qubesadmin.tests.QubesTestCase):
  23. def setUp(self):
  24. super(TC_00_DeviceCollection, self).setUp()
  25. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  26. b'0\0test-vm class=AppVM state=Running\n' \
  27. b'test-vm2 class=AppVM state=Running\n' \
  28. b'test-vm3 class=AppVM state=Running\n'
  29. self.vm = self.app.domains['test-vm']
  30. def test_000_available(self):
  31. self.app.expected_calls[
  32. ('test-vm', 'admin.vm.device.test.Available', None, None)] = \
  33. b'0\0dev1\n'
  34. devices = list(self.vm.devices['test'].available())
  35. self.assertEqual(len(devices), 1)
  36. dev = devices[0]
  37. self.assertIsInstance(dev, qubesadmin.devices.DeviceInfo)
  38. self.assertEqual(dev.backend_domain, self.vm)
  39. self.assertEqual(dev.ident, 'dev1')
  40. self.assertEqual(dev.description, '')
  41. self.assertEqual(dev.data, {})
  42. self.assertEqual(str(dev), 'test-vm:dev1')
  43. self.assertAllCalled()
  44. def test_001_available_desc(self):
  45. self.app.expected_calls[
  46. ('test-vm', 'admin.vm.device.test.Available', None, None)] = \
  47. b'0\0dev1 description=This is description\n'
  48. devices = list(self.vm.devices['test'].available())
  49. self.assertEqual(len(devices), 1)
  50. dev = devices[0]
  51. self.assertIsInstance(dev, qubesadmin.devices.DeviceInfo)
  52. self.assertEqual(dev.backend_domain, self.vm)
  53. self.assertEqual(dev.ident, 'dev1')
  54. self.assertEqual(dev.description, 'This is description')
  55. self.assertEqual(dev.data, {})
  56. self.assertEqual(str(dev), 'test-vm:dev1')
  57. def test_002_available_options(self):
  58. self.app.expected_calls[
  59. ('test-vm', 'admin.vm.device.test.Available', None, None)] = \
  60. b'0\0dev1 ro=True other=123 description=This is description\n'
  61. devices = list(self.vm.devices['test'].available())
  62. self.assertEqual(len(devices), 1)
  63. dev = devices[0]
  64. self.assertIsInstance(dev, qubesadmin.devices.DeviceInfo)
  65. self.assertEqual(dev.backend_domain, self.vm)
  66. self.assertEqual(dev.ident, 'dev1')
  67. self.assertEqual(dev.description, 'This is description')
  68. self.assertEqual(dev.data, {'ro': 'True', 'other': '123'})
  69. self.assertEqual(str(dev), 'test-vm:dev1')
  70. self.assertAllCalled()
  71. def test_010_getitem(self):
  72. self.app.expected_calls[
  73. ('test-vm', 'admin.vm.device.test.Available', None, None)] = \
  74. b'0\0dev1 description=This is description\n'
  75. dev = self.vm.devices['test']['dev1']
  76. self.assertIsInstance(dev, qubesadmin.devices.DeviceInfo)
  77. self.assertEqual(dev.backend_domain, self.vm)
  78. self.assertEqual(dev.ident, 'dev1')
  79. self.assertEqual(dev.description, 'This is description')
  80. self.assertEqual(dev.data, {})
  81. self.assertEqual(str(dev), 'test-vm:dev1')
  82. self.assertAllCalled()
  83. def test_011_getitem_missing(self):
  84. self.app.expected_calls[
  85. ('test-vm', 'admin.vm.device.test.Available', None, None)] = \
  86. b'0\0dev1 description=This is description\n'
  87. dev = self.vm.devices['test']['dev2']
  88. self.assertIsInstance(dev, qubesadmin.devices.UnknownDevice)
  89. self.assertEqual(dev.backend_domain, self.vm)
  90. self.assertEqual(dev.ident, 'dev2')
  91. self.assertEqual(dev.description, 'Unknown device')
  92. self.assertEqual(dev.data, {})
  93. self.assertEqual(str(dev), 'test-vm:dev2')
  94. self.assertAllCalled()
  95. def test_020_attach(self):
  96. self.app.expected_calls[
  97. ('test-vm', 'admin.vm.device.test.Attach', 'test-vm2+dev1', b'')] = \
  98. b'0\0'
  99. assign = qubesadmin.devices.DeviceAssignment(
  100. self.app.domains['test-vm2'], 'dev1')
  101. self.vm.devices['test'].attach(assign)
  102. self.assertAllCalled()
  103. def test_021_attach_options(self):
  104. self.app.expected_calls[
  105. ('test-vm', 'admin.vm.device.test.Attach', 'test-vm2+dev1',
  106. b'ro=True something=value')] = b'0\0'
  107. assign = qubesadmin.devices.DeviceAssignment(
  108. self.app.domains['test-vm2'], 'dev1')
  109. assign.options['ro'] = True
  110. assign.options['something'] = 'value'
  111. self.vm.devices['test'].attach(assign)
  112. self.assertAllCalled()
  113. def test_022_attach_persistent(self):
  114. self.app.expected_calls[
  115. ('test-vm', 'admin.vm.device.test.Attach', 'test-vm2+dev1',
  116. b'persistent=True')] = b'0\0'
  117. assign = qubesadmin.devices.DeviceAssignment(
  118. self.app.domains['test-vm2'], 'dev1')
  119. assign.persistent = True
  120. self.vm.devices['test'].attach(assign)
  121. self.assertAllCalled()
  122. def test_023_attach_persistent_options(self):
  123. self.app.expected_calls[
  124. ('test-vm', 'admin.vm.device.test.Attach', 'test-vm2+dev1',
  125. b'persistent=True ro=True')] = b'0\0'
  126. assign = qubesadmin.devices.DeviceAssignment(
  127. self.app.domains['test-vm2'], 'dev1')
  128. assign.persistent = True
  129. assign.options['ro'] = True
  130. self.vm.devices['test'].attach(assign)
  131. self.assertAllCalled()
  132. def test_030_detach(self):
  133. self.app.expected_calls[
  134. ('test-vm', 'admin.vm.device.test.Detach', 'test-vm2+dev1',
  135. None)] = b'0\0'
  136. assign = qubesadmin.devices.DeviceAssignment(
  137. self.app.domains['test-vm2'], 'dev1')
  138. self.vm.devices['test'].detach(assign)
  139. self.assertAllCalled()
  140. def test_040_assignments(self):
  141. self.app.expected_calls[
  142. ('test-vm', 'admin.vm.device.test.List', None, None)] = \
  143. b'0\0test-vm2+dev1\n' \
  144. b'test-vm3+dev2\n'
  145. self.app.expected_calls[
  146. ('test-vm2', 'admin.vm.device.test.Available', None, None)] = \
  147. b'0\0dev1 description=desc\n'
  148. self.app.expected_calls[
  149. ('test-vm3', 'admin.vm.device.test.Available', None, None)] = \
  150. b'0\0dev2 description=desc\n'
  151. assigns = list(self.vm.devices['test'].assignments())
  152. self.assertEqual(len(assigns), 2)
  153. self.assertIsInstance(assigns[0], qubesadmin.devices.DeviceAssignment)
  154. self.assertEqual(assigns[0].backend_domain,
  155. self.app.domains['test-vm2'])
  156. self.assertEqual(assigns[0].ident, 'dev1')
  157. self.assertEqual(assigns[0].frontend_domain,
  158. self.app.domains['test-vm'])
  159. self.assertEqual(assigns[0].options, {})
  160. self.assertEqual(assigns[0].devclass, 'test')
  161. self.assertEqual(assigns[0].device,
  162. self.app.domains['test-vm2'].devices['test']['dev1'])
  163. self.assertIsInstance(assigns[1], qubesadmin.devices.DeviceAssignment)
  164. self.assertEqual(assigns[1].backend_domain,
  165. self.app.domains['test-vm3'])
  166. self.assertEqual(assigns[1].ident, 'dev2')
  167. self.assertEqual(assigns[1].frontend_domain,
  168. self.app.domains['test-vm'])
  169. self.assertEqual(assigns[1].options, {})
  170. self.assertEqual(assigns[1].devclass, 'test')
  171. self.assertEqual(assigns[1].device,
  172. self.app.domains['test-vm3'].devices['test']['dev2'])
  173. self.assertAllCalled()
  174. def test_041_assignments_options(self):
  175. self.app.expected_calls[
  176. ('test-vm', 'admin.vm.device.test.List', None, None)] = \
  177. b'0\0test-vm2+dev1 ro=True\n' \
  178. b'test-vm3+dev2 ro=False persistent=True\n'
  179. assigns = list(self.vm.devices['test'].assignments())
  180. self.assertEqual(len(assigns), 2)
  181. self.assertIsInstance(assigns[0], qubesadmin.devices.DeviceAssignment)
  182. self.assertEqual(assigns[0].backend_domain,
  183. self.app.domains['test-vm2'])
  184. self.assertEqual(assigns[0].ident, 'dev1')
  185. self.assertEqual(assigns[0].frontend_domain,
  186. self.app.domains['test-vm'])
  187. self.assertEqual(assigns[0].options, {'ro': 'True'})
  188. self.assertEqual(assigns[0].persistent, False)
  189. self.assertEqual(assigns[0].devclass, 'test')
  190. self.assertIsInstance(assigns[1], qubesadmin.devices.DeviceAssignment)
  191. self.assertEqual(assigns[1].backend_domain,
  192. self.app.domains['test-vm3'])
  193. self.assertEqual(assigns[1].ident, 'dev2')
  194. self.assertEqual(assigns[1].frontend_domain,
  195. self.app.domains['test-vm'])
  196. self.assertEqual(assigns[1].options, {'ro': 'False'})
  197. self.assertEqual(assigns[1].persistent, True)
  198. self.assertEqual(assigns[1].devclass, 'test')
  199. self.assertAllCalled()
  200. def test_041_assignments_persistent(self):
  201. self.app.expected_calls[
  202. ('test-vm', 'admin.vm.device.test.List', None, None)] = \
  203. b'0\0test-vm2+dev1\n' \
  204. b'test-vm3+dev2 persistent=True\n'
  205. assigns = list(self.vm.devices['test'].assignments(True))
  206. self.assertEqual(len(assigns), 1)
  207. self.assertIsInstance(assigns[0], qubesadmin.devices.DeviceAssignment)
  208. self.assertEqual(assigns[0].backend_domain,
  209. self.app.domains['test-vm3'])
  210. self.assertEqual(assigns[0].ident, 'dev2')
  211. self.assertEqual(assigns[0].frontend_domain,
  212. self.app.domains['test-vm'])
  213. self.assertEqual(assigns[0].options, {})
  214. self.assertEqual(assigns[0].persistent, True)
  215. self.assertEqual(assigns[0].devclass, 'test')
  216. self.assertAllCalled()
  217. def test_042_assignments_non_persistent(self):
  218. self.app.expected_calls[
  219. ('test-vm', 'admin.vm.device.test.List', None, None)] = \
  220. b'0\0test-vm2+dev1\n' \
  221. b'test-vm3+dev2 persistent=True\n'
  222. assigns = list(self.vm.devices['test'].assignments(False))
  223. self.assertEqual(len(assigns), 1)
  224. self.assertIsInstance(assigns[0], qubesadmin.devices.DeviceAssignment)
  225. self.assertEqual(assigns[0].backend_domain,
  226. self.app.domains['test-vm2'])
  227. self.assertEqual(assigns[0].ident, 'dev1')
  228. self.assertEqual(assigns[0].frontend_domain,
  229. self.app.domains['test-vm'])
  230. self.assertEqual(assigns[0].options, {})
  231. self.assertEqual(assigns[0].persistent, False)
  232. self.assertAllCalled()
  233. def test_050_persistent(self):
  234. self.app.expected_calls[
  235. ('test-vm', 'admin.vm.device.test.List', None, None)] = \
  236. b'0\0test-vm2+dev1\n' \
  237. b'test-vm3+dev2 persistent=True\n'
  238. self.app.expected_calls[
  239. ('test-vm3', 'admin.vm.device.test.Available', None, None)] = \
  240. b'0\0dev2\n'
  241. devs = list(self.vm.devices['test'].persistent())
  242. self.assertEqual(len(devs), 1)
  243. self.assertIsInstance(devs[0], qubesadmin.devices.DeviceInfo)
  244. self.assertEqual(devs[0].backend_domain, self.app.domains['test-vm3'])
  245. self.assertEqual(devs[0].ident, 'dev2')
  246. self.assertAllCalled()
  247. def test_060_attached(self):
  248. self.app.expected_calls[
  249. ('test-vm', 'admin.vm.device.test.List', None, None)] = \
  250. b'0\0test-vm2+dev1\n' \
  251. b'test-vm3+dev2 persistent=True\n'
  252. self.app.expected_calls[
  253. ('test-vm2', 'admin.vm.device.test.Available', None, None)] = \
  254. b'0\0dev1\n'
  255. self.app.expected_calls[
  256. ('test-vm3', 'admin.vm.device.test.Available', None, None)] = \
  257. b'0\0dev2\n'
  258. devs = list(self.vm.devices['test'].attached())
  259. self.assertEqual(len(devs), 2)
  260. self.assertIsInstance(devs[0], qubesadmin.devices.DeviceInfo)
  261. self.assertEqual(devs[0].backend_domain, self.app.domains['test-vm2'])
  262. self.assertEqual(devs[0].ident, 'dev1')
  263. self.assertIsInstance(devs[1], qubesadmin.devices.DeviceInfo)
  264. self.assertEqual(devs[1].backend_domain, self.app.domains['test-vm3'])
  265. self.assertEqual(devs[1].ident, 'dev2')
  266. self.assertAllCalled()
  267. def test_070_update_persistent(self):
  268. self.app.expected_calls[
  269. ('test-vm', 'admin.vm.device.test.Set.persistent', 'test-vm2+dev1',
  270. b'True')] = b'0\0'
  271. dev = qubesadmin.devices.DeviceInfo(
  272. self.app.domains['test-vm2'], 'test', 'dev1')
  273. self.vm.devices['test'].update_persistent(dev, True)
  274. self.assertAllCalled()
  275. def test_071_update_persistent_false(self):
  276. self.app.expected_calls[
  277. ('test-vm', 'admin.vm.device.test.Set.persistent', 'test-vm2+dev1',
  278. b'False')] = b'0\0'
  279. dev = qubesadmin.devices.DeviceInfo(
  280. self.app.domains['test-vm2'], 'test', 'dev1')
  281. self.vm.devices['test'].update_persistent(dev, False)
  282. self.assertAllCalled()