devices.py 14 KB

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