app.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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 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 os
  21. import shutil
  22. import socket
  23. import subprocess
  24. import unittest
  25. import multiprocessing
  26. try:
  27. import unittest.mock as mock
  28. except ImportError:
  29. import mock
  30. import tempfile
  31. import qubesadmin.exc
  32. import qubesadmin.tests
  33. class TC_00_VMCollection(qubesadmin.tests.QubesTestCase):
  34. def test_000_list(self):
  35. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  36. b'0\x00test-vm class=AppVM state=Running\n'
  37. self.assertEqual(
  38. list(self.app.domains.keys()),
  39. ['test-vm'])
  40. self.assertAllCalled()
  41. def test_001_getitem(self):
  42. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  43. b'0\x00test-vm class=AppVM state=Running\n'
  44. try:
  45. vm = self.app.domains['test-vm']
  46. self.assertEqual(vm.name, 'test-vm')
  47. except KeyError:
  48. self.fail('VM not found in collection')
  49. self.assertAllCalled()
  50. with self.assertRaises(KeyError):
  51. vm = self.app.domains['test-non-existent']
  52. self.assertAllCalled()
  53. def test_002_in(self):
  54. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  55. b'0\x00test-vm class=AppVM state=Running\n'
  56. self.assertIn('test-vm', self.app.domains)
  57. self.assertAllCalled()
  58. self.assertNotIn('test-non-existent', self.app.domains)
  59. self.assertAllCalled()
  60. def test_003_iter(self):
  61. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  62. b'0\x00test-vm class=AppVM state=Running\n'
  63. self.assertEqual([vm.name for vm in self.app.domains], ['test-vm'])
  64. self.assertAllCalled()
  65. def test_004_delitem(self):
  66. self.app.expected_calls[('test-vm', 'admin.vm.Remove', None, None)] = \
  67. b'0\x00'
  68. del self.app.domains['test-vm']
  69. self.assertAllCalled()
  70. def test_005_keys(self):
  71. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  72. b'0\x00test-vm class=AppVM state=Running\n' \
  73. b'test-vm2 class=AppVM state=Running\n'
  74. self.assertEqual(set(self.app.domains.keys()),
  75. set(['test-vm', 'test-vm2']))
  76. self.assertAllCalled()
  77. def test_006_values(self):
  78. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  79. b'0\x00test-vm class=AppVM state=Running\n' \
  80. b'test-vm2 class=AppVM state=Running\n'
  81. values = self.app.domains.values()
  82. for obj in values:
  83. self.assertIsInstance(obj, qubesadmin.vm.QubesVM)
  84. self.assertEqual(set([vm.name for vm in values]),
  85. set(['test-vm', 'test-vm2']))
  86. self.assertAllCalled()
  87. def test_007_getitem_blind_mode(self):
  88. self.app.blind_mode = True
  89. try:
  90. vm = self.app.domains['test-vm']
  91. self.assertEqual(vm.name, 'test-vm')
  92. except KeyError:
  93. self.fail('VM not found in collection')
  94. self.assertAllCalled()
  95. with self.assertNotRaises(KeyError):
  96. vm = self.app.domains['test-non-existent']
  97. self.assertAllCalled()
  98. def test_008_in_blind_mode(self):
  99. self.app.blind_mode = True
  100. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  101. b'0\x00test-vm class=AppVM state=Running\n'
  102. self.assertIn('test-vm', self.app.domains)
  103. self.assertAllCalled()
  104. self.assertNotIn('test-non-existent', self.app.domains)
  105. self.assertAllCalled()
  106. def test_009_getitem_cache_class(self):
  107. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  108. b'0\x00test-vm class=AppVM state=Running\n'
  109. try:
  110. vm = self.app.domains['test-vm']
  111. self.assertEqual(vm.name, 'test-vm')
  112. self.assertEqual(vm.klass, 'AppVM')
  113. except KeyError:
  114. self.fail('VM not found in collection')
  115. self.assertAllCalled()
  116. class TC_10_QubesBase(qubesadmin.tests.QubesTestCase):
  117. def test_010_new_simple(self):
  118. self.app.expected_calls[('dom0', 'admin.vm.Create.AppVM', None,
  119. b'name=new-vm label=red')] = b'0\x00'
  120. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  121. b'0\x00new-vm class=AppVM state=Running\n'
  122. vm = self.app.add_new_vm('AppVM', 'new-vm', 'red')
  123. self.assertEqual(vm.name, 'new-vm')
  124. self.assertEqual(vm.klass, 'AppVM')
  125. self.assertAllCalled()
  126. def test_011_new_template(self):
  127. self.app.expected_calls[('dom0', 'admin.vm.Create.TemplateVM', None,
  128. b'name=new-template label=red')] = b'0\x00'
  129. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  130. b'0\x00new-template class=TemplateVM state=Running\n'
  131. vm = self.app.add_new_vm('TemplateVM', 'new-template', 'red')
  132. self.assertEqual(vm.name, 'new-template')
  133. self.assertEqual(vm.klass, 'TemplateVM')
  134. self.assertAllCalled()
  135. def test_012_new_template_based(self):
  136. self.app.expected_calls[('dom0', 'admin.vm.Create.AppVM',
  137. 'some-template', b'name=new-vm label=red')] = b'0\x00'
  138. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  139. b'0\x00new-vm class=AppVM state=Running\n'
  140. vm = self.app.add_new_vm('AppVM', 'new-vm', 'red', 'some-template')
  141. self.assertEqual(vm.name, 'new-vm')
  142. self.assertEqual(vm.klass, 'AppVM')
  143. self.assertAllCalled()
  144. def test_013_new_objects_params(self):
  145. self.app.expected_calls[('dom0', 'admin.vm.Create.AppVM',
  146. 'some-template', b'name=new-vm label=red')] = b'0\x00'
  147. self.app.expected_calls[('dom0', 'admin.label.List', None, None)] = \
  148. b'0\x00red\nblue\n'
  149. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  150. b'0\x00new-vm class=AppVM state=Running\n' \
  151. b'some-template class=TemplateVM state=Running\n'
  152. vm = self.app.add_new_vm(self.app.get_vm_class('AppVM'), 'new-vm',
  153. self.app.get_label('red'), self.app.domains['some-template'])
  154. self.assertEqual(vm.name, 'new-vm')
  155. self.assertEqual(vm.klass, 'AppVM')
  156. self.assertAllCalled()
  157. def test_014_new_pool(self):
  158. self.app.expected_calls[('dom0', 'admin.vm.CreateInPool.AppVM', None,
  159. b'name=new-vm label=red pool=some-pool')] = b'0\x00'
  160. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  161. b'0\x00new-vm class=AppVM state=Running\n'
  162. vm = self.app.add_new_vm('AppVM', 'new-vm', 'red', pool='some-pool')
  163. self.assertEqual(vm.name, 'new-vm')
  164. self.assertEqual(vm.klass, 'AppVM')
  165. self.assertAllCalled()
  166. def test_015_new_pools(self):
  167. self.app.expected_calls[('dom0', 'admin.vm.CreateInPool.AppVM', None,
  168. b'name=new-vm label=red pool:private=some-pool '
  169. b'pool:volatile=other-pool')] = b'0\x00'
  170. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  171. b'0\x00new-vm class=AppVM state=Running\n'
  172. vm = self.app.add_new_vm('AppVM', 'new-vm', 'red',
  173. pools={'private': 'some-pool', 'volatile': 'other-pool'})
  174. self.assertEqual(vm.name, 'new-vm')
  175. self.assertEqual(vm.klass, 'AppVM')
  176. self.assertAllCalled()
  177. def test_016_new_template_based_default(self):
  178. self.app.expected_calls[('dom0', 'admin.vm.Create.AppVM',
  179. None, b'name=new-vm label=red')] = b'0\x00'
  180. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  181. b'0\x00new-vm class=AppVM state=Running\n'
  182. vm = self.app.add_new_vm('AppVM', 'new-vm', 'red',
  183. template=qubesadmin.DEFAULT)
  184. self.assertEqual(vm.name, 'new-vm')
  185. self.assertEqual(vm.klass, 'AppVM')
  186. self.assertAllCalled()
  187. def test_020_get_label(self):
  188. self.app.expected_calls[('dom0', 'admin.label.List', None, None)] = \
  189. b'0\x00red\nblue\n'
  190. label = self.app.get_label('red')
  191. self.assertEqual(label.name, 'red')
  192. self.assertAllCalled()
  193. def clone_setup_common_calls(self, src, dst):
  194. # labels
  195. self.app.expected_calls[('dom0', 'admin.label.List', None, None)] = \
  196. b'0\x00red\ngreen\nblue\n'
  197. # have each property type with default=no, each special-cased,
  198. # and some with default=yes
  199. properties = {
  200. 'label': 'default=False type=label red',
  201. 'template': 'default=False type=vm test-template',
  202. 'memory': 'default=False type=int 400',
  203. 'kernel': 'default=False type=str 4.9.31',
  204. 'netvm': 'default=False type=vm test-net',
  205. 'virt_mode': 'default=False type=str hvm',
  206. 'default_user': 'default=True type=str user',
  207. }
  208. self.app.expected_calls[
  209. (src, 'admin.vm.property.List', None, None)] = \
  210. b'0\0qid\nname\n' + \
  211. b'\n'.join(prop.encode() for prop in properties.keys()) + \
  212. b'\n'
  213. for prop, value in properties.items():
  214. self.app.expected_calls[
  215. (src, 'admin.vm.property.Get', prop, None)] = \
  216. b'0\0' + value.encode()
  217. # special cases handled by admin.vm.Create call
  218. if prop in ('label', 'template'):
  219. continue
  220. # default properties should not be set
  221. if 'default=True' in value:
  222. continue
  223. self.app.expected_calls[
  224. (dst, 'admin.vm.property.Set', prop,
  225. value.split()[-1].encode())] = b'0\0'
  226. # tags
  227. self.app.expected_calls[
  228. (src, 'admin.vm.tag.List', None, None)] = \
  229. b'0\0tag1\ntag2\n'
  230. self.app.expected_calls[
  231. (dst, 'admin.vm.tag.Set', 'tag1', None)] = b'0\0'
  232. self.app.expected_calls[
  233. (dst, 'admin.vm.tag.Set', 'tag2', None)] = b'0\0'
  234. # features
  235. self.app.expected_calls[
  236. (src, 'admin.vm.feature.List', None, None)] = \
  237. b'0\0feat1\nfeat2\n'
  238. self.app.expected_calls[
  239. (src, 'admin.vm.feature.Get', 'feat1', None)] = \
  240. b'0\0feat1-value with spaces'
  241. self.app.expected_calls[
  242. (src, 'admin.vm.feature.Get', 'feat2', None)] = \
  243. b'0\x001'
  244. self.app.expected_calls[
  245. (dst, 'admin.vm.feature.Set', 'feat1',
  246. b'feat1-value with spaces')] = b'0\0'
  247. self.app.expected_calls[
  248. (dst, 'admin.vm.feature.Set', 'feat2', b'1')] = b'0\0'
  249. # firewall
  250. rules = (
  251. b'action=drop dst4=192.168.0.0/24\n'
  252. b'action=accept\n'
  253. )
  254. self.app.expected_calls[
  255. (src, 'admin.vm.firewall.Get', None, None)] = \
  256. b'0\x00' + rules
  257. self.app.expected_calls[
  258. (dst, 'admin.vm.firewall.Set', None, rules)] = \
  259. b'0\x00'
  260. # storage
  261. self.app.expected_calls[
  262. (dst, 'admin.vm.volume.List', None, None)] = \
  263. b'0\x00root\nprivate\nvolatile\nkernel\n'
  264. self.app.expected_calls[
  265. (dst, 'admin.vm.volume.Info', 'root', None)] = \
  266. b'0\x00pool=lvm\n' \
  267. b'vid=vm-test-vm/root\n' \
  268. b'size=10737418240\n' \
  269. b'usage=2147483648\n' \
  270. b'rw=False\n' \
  271. b'internal=True\n' \
  272. b'source=vm-test-template/root\n' \
  273. b'save_on_stop=False\n' \
  274. b'snap_on_start=True\n'
  275. self.app.expected_calls[
  276. (dst, 'admin.vm.volume.Info', 'private', None)] = \
  277. b'0\x00pool=lvm\n' \
  278. b'vid=vm-test-vm/private\n' \
  279. b'size=2147483648\n' \
  280. b'usage=214748364\n' \
  281. b'rw=True\n' \
  282. b'internal=True\n' \
  283. b'save_on_stop=True\n' \
  284. b'snap_on_start=False\n'
  285. self.app.expected_calls[
  286. (dst, 'admin.vm.volume.Info', 'volatile', None)] = \
  287. b'0\x00pool=lvm\n' \
  288. b'vid=vm-test-vm/volatile\n' \
  289. b'size=10737418240\n' \
  290. b'usage=0\n' \
  291. b'rw=True\n' \
  292. b'internal=True\n' \
  293. b'source=None\n' \
  294. b'save_on_stop=False\n' \
  295. b'snap_on_start=False\n'
  296. self.app.expected_calls[
  297. (dst, 'admin.vm.volume.Info', 'kernel', None)] = \
  298. b'0\x00pool=linux-kernel\n' \
  299. b'vid=\n' \
  300. b'size=0\n' \
  301. b'usage=0\n' \
  302. b'rw=False\n' \
  303. b'internal=True\n' \
  304. b'source=None\n' \
  305. b'save_on_stop=False\n' \
  306. b'snap_on_start=False\n'
  307. self.app.expected_calls[
  308. (src, 'admin.vm.volume.List', None, None)] = \
  309. b'0\x00root\nprivate\nvolatile\nkernel\n'
  310. self.app.expected_calls[
  311. (src, 'admin.vm.volume.CloneFrom', 'private', None)] = \
  312. b'0\x00token-private'
  313. self.app.expected_calls[
  314. (dst, 'admin.vm.volume.CloneTo', 'private', b'token-private')] = \
  315. b'0\x00'
  316. def test_030_clone(self):
  317. self.clone_setup_common_calls('test-vm', 'new-name')
  318. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  319. b'0\x00new-name class=AppVM state=Halted\n' \
  320. b'test-vm class=AppVM state=Halted\n' \
  321. b'test-template class=TemplateVM state=Halted\n' \
  322. b'test-net class=AppVM state=Halted\n'
  323. self.app.expected_calls[('dom0', 'admin.vm.Create.AppVM',
  324. 'test-template', b'name=new-name label=red')] = b'0\x00'
  325. new_vm = self.app.clone_vm('test-vm', 'new-name')
  326. self.assertEqual(new_vm.name, 'new-name')
  327. self.assertAllCalled()
  328. def test_031_clone_object(self):
  329. self.clone_setup_common_calls('test-vm', 'new-name')
  330. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  331. b'0\x00new-name class=AppVM state=Halted\n' \
  332. b'test-vm class=AppVM state=Halted\n' \
  333. b'test-template class=TemplateVM state=Halted\n' \
  334. b'test-net class=AppVM state=Halted\n'
  335. self.app.expected_calls[('dom0', 'admin.vm.Create.AppVM',
  336. 'test-template', b'name=new-name label=red')] = b'0\x00'
  337. new_vm = self.app.clone_vm(self.app.domains['test-vm'], 'new-name')
  338. self.assertEqual(new_vm.name, 'new-name')
  339. self.assertAllCalled()
  340. def test_032_clone_pool(self):
  341. self.clone_setup_common_calls('test-vm', 'new-name')
  342. self.app.expected_calls[('dom0', 'admin.vm.CreateInPool.AppVM',
  343. 'test-template',
  344. b'name=new-name label=red pool=some-pool')] = b'0\x00'
  345. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  346. b'0\x00new-name class=AppVM state=Halted\n' \
  347. b'test-vm class=AppVM state=Halted\n' \
  348. b'test-template class=TemplateVM state=Halted\n' \
  349. b'test-net class=AppVM state=Halted\n'
  350. new_vm = self.app.clone_vm('test-vm', 'new-name', pool='some-pool')
  351. self.assertEqual(new_vm.name, 'new-name')
  352. self.assertAllCalled()
  353. def test_033_clone_pools(self):
  354. self.clone_setup_common_calls('test-vm', 'new-name')
  355. self.app.expected_calls[('dom0', 'admin.vm.CreateInPool.AppVM',
  356. 'test-template',
  357. b'name=new-name label=red pool:private=some-pool '
  358. b'pool:volatile=other-pool')] = b'0\x00'
  359. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  360. b'0\x00new-name class=AppVM state=Halted\n' \
  361. b'test-vm class=AppVM state=Halted\n' \
  362. b'test-template class=TemplateVM state=Halted\n' \
  363. b'test-net class=AppVM state=Halted\n'
  364. new_vm = self.app.clone_vm('test-vm', 'new-name',
  365. pools={'private': 'some-pool', 'volatile': 'other-pool'})
  366. self.assertEqual(new_vm.name, 'new-name')
  367. self.assertAllCalled()
  368. def test_034_clone_class_change(self):
  369. self.clone_setup_common_calls('test-vm', 'new-name')
  370. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  371. b'0\x00new-name class=StandaloneVM state=Halted\n' \
  372. b'test-vm class=AppVM state=Halted\n' \
  373. b'test-template class=TemplateVM state=Halted\n' \
  374. b'test-net class=AppVM state=Halted\n'
  375. self.app.expected_calls[('dom0', 'admin.vm.Create.StandaloneVM',
  376. 'test-template', b'name=new-name label=red')] = b'0\x00'
  377. self.app.expected_calls[
  378. ('new-name', 'admin.vm.volume.Info', 'root', None)] = \
  379. b'0\x00pool=lvm\n' \
  380. b'vid=vm-new-name/root\n' \
  381. b'size=10737418240\n' \
  382. b'usage=2147483648\n' \
  383. b'rw=True\n' \
  384. b'internal=True\n' \
  385. b'source=None\n' \
  386. b'save_on_stop=True\n' \
  387. b'snap_on_start=False\n'
  388. self.app.expected_calls[
  389. ('test-vm', 'admin.vm.volume.CloneFrom', 'root', None)] = \
  390. b'0\x00token-root'
  391. self.app.expected_calls[
  392. ('new-name', 'admin.vm.volume.CloneTo', 'root', b'token-root')] = \
  393. b'0\x00'
  394. new_vm = self.app.clone_vm('test-vm', 'new-name',
  395. new_cls='StandaloneVM')
  396. self.assertEqual(new_vm.name, 'new-name')
  397. self.assertEqual(new_vm.klass, 'StandaloneVM')
  398. self.assertAllCalled()
  399. def test_035_clone_fail(self):
  400. self.app.expected_calls[('dom0', 'admin.label.List', None, None)] = \
  401. b'0\x00red\ngreen\nblue\n'
  402. self.app.expected_calls[
  403. ('test-vm', 'admin.vm.property.List', None, None)] = \
  404. b'0\0qid\nname\ntemplate\nlabel\nmemory\n'
  405. self.app.expected_calls[
  406. ('test-vm', 'admin.vm.property.Get', 'label', None)] = \
  407. b'0\0default=False type=label red'
  408. self.app.expected_calls[
  409. ('test-vm', 'admin.vm.property.Get', 'template', None)] = \
  410. b'0\0default=False type=vm test-template'
  411. self.app.expected_calls[
  412. ('test-vm', 'admin.vm.property.Get', 'memory', None)] = \
  413. b'0\0default=False type=int 400'
  414. self.app.expected_calls[
  415. ('new-name', 'admin.vm.property.Set', 'memory', b'400')] = \
  416. b'2\0QubesException\0\0something happened\0'
  417. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  418. b'0\x00new-name class=AppVM state=Halted\n' \
  419. b'test-vm class=AppVM state=Halted\n' \
  420. b'test-template class=TemplateVM state=Halted\n' \
  421. b'test-net class=AppVM state=Halted\n'
  422. self.app.expected_calls[('dom0', 'admin.vm.Create.AppVM',
  423. 'test-template', b'name=new-name label=red')] = b'0\x00'
  424. self.app.expected_calls[('new-name', 'admin.vm.Remove', None, None)] = \
  425. b'0\x00'
  426. with self.assertRaises(qubesadmin.exc.QubesException):
  427. self.app.clone_vm('test-vm', 'new-name')
  428. self.assertAllCalled()
  429. def test_036_clone_ignore_errors_prop(self):
  430. self.clone_setup_common_calls('test-vm', 'new-name')
  431. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  432. b'0\x00new-name class=AppVM state=Halted\n' \
  433. b'test-vm class=AppVM state=Halted\n' \
  434. b'test-template class=TemplateVM state=Halted\n' \
  435. b'test-net class=AppVM state=Halted\n'
  436. self.app.expected_calls[('dom0', 'admin.vm.Create.AppVM',
  437. 'test-template', b'name=new-name label=red')] = b'0\x00'
  438. self.app.expected_calls[
  439. ('new-name', 'admin.vm.property.Set', 'memory', b'400')] = \
  440. b'2\0QubesException\0\0something happened\0'
  441. new_vm = self.app.clone_vm('test-vm', 'new-name', ignore_errors=True)
  442. self.assertEqual(new_vm.name, 'new-name')
  443. self.assertAllCalled()
  444. def test_037_clone_ignore_errors_feature(self):
  445. self.clone_setup_common_calls('test-vm', 'new-name')
  446. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  447. b'0\x00new-name class=AppVM state=Halted\n' \
  448. b'test-vm class=AppVM state=Halted\n' \
  449. b'test-template class=TemplateVM state=Halted\n' \
  450. b'test-net class=AppVM state=Halted\n'
  451. self.app.expected_calls[('dom0', 'admin.vm.Create.AppVM',
  452. 'test-template', b'name=new-name label=red')] = b'0\x00'
  453. self.app.expected_calls[
  454. ('new-name', 'admin.vm.feature.Set', 'feat2', b'1')] = \
  455. b'2\0QubesException\0\0something happened\0'
  456. new_vm = self.app.clone_vm('test-vm', 'new-name', ignore_errors=True)
  457. self.assertEqual(new_vm.name, 'new-name')
  458. self.assertAllCalled()
  459. def test_038_clone_ignore_errors_tag(self):
  460. self.clone_setup_common_calls('test-vm', 'new-name')
  461. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  462. b'0\x00new-name class=AppVM state=Halted\n' \
  463. b'test-vm class=AppVM state=Halted\n' \
  464. b'test-template class=TemplateVM state=Halted\n' \
  465. b'test-net class=AppVM state=Halted\n'
  466. self.app.expected_calls[('dom0', 'admin.vm.Create.AppVM',
  467. 'test-template', b'name=new-name label=red')] = b'0\x00'
  468. self.app.expected_calls[
  469. ('new-name', 'admin.vm.tag.Set', 'tag1', None)] = \
  470. b'2\0QubesException\0\0something happened\0'
  471. new_vm = self.app.clone_vm('test-vm', 'new-name', ignore_errors=True)
  472. self.assertEqual(new_vm.name, 'new-name')
  473. self.assertAllCalled()
  474. def test_039_clone_ignore_errors_firewall(self):
  475. self.clone_setup_common_calls('test-vm', 'new-name')
  476. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  477. b'0\x00new-name class=AppVM state=Halted\n' \
  478. b'test-vm class=AppVM state=Halted\n' \
  479. b'test-template class=TemplateVM state=Halted\n' \
  480. b'test-net class=AppVM state=Halted\n'
  481. self.app.expected_calls[('dom0', 'admin.vm.Create.AppVM',
  482. 'test-template', b'name=new-name label=red')] = b'0\x00'
  483. self.app.expected_calls[
  484. ('new-name', 'admin.vm.firewall.Set', None,
  485. b'action=drop dst4=192.168.0.0/24\naction=accept\n')] = \
  486. b'2\0QubesException\0\0something happened\0'
  487. new_vm = self.app.clone_vm('test-vm', 'new-name', ignore_errors=True)
  488. self.assertEqual(new_vm.name, 'new-name')
  489. self.assertAllCalled()
  490. def test_040_clone_ignore_errors_storage(self):
  491. self.clone_setup_common_calls('test-vm', 'new-name')
  492. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  493. b'0\x00new-name class=AppVM state=Halted\n' \
  494. b'test-vm class=AppVM state=Halted\n' \
  495. b'test-template class=TemplateVM state=Halted\n' \
  496. b'test-net class=AppVM state=Halted\n'
  497. self.app.expected_calls[('dom0', 'admin.vm.Create.AppVM',
  498. 'test-template', b'name=new-name label=red')] = b'0\x00'
  499. self.app.expected_calls[
  500. ('new-name', 'admin.vm.volume.CloneTo', 'private',
  501. b'token-private')] = \
  502. b'2\0QubesException\0\0something happened\0'
  503. self.app.expected_calls[('new-name', 'admin.vm.Remove', None, None)] = \
  504. b'0\x00'
  505. del self.app.expected_calls[
  506. ('new-name', 'admin.vm.volume.Info', 'root', None)]
  507. del self.app.expected_calls[
  508. ('new-name', 'admin.vm.volume.Info', 'volatile', None)]
  509. with self.assertRaises(qubesadmin.exc.QubesException):
  510. self.app.clone_vm('test-vm', 'new-name', ignore_errors=True)
  511. self.assertAllCalled()
  512. def test_041_clone_fail_storage(self):
  513. self.clone_setup_common_calls('test-vm', 'new-name')
  514. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  515. b'0\x00new-name class=AppVM state=Halted\n' \
  516. b'test-vm class=AppVM state=Halted\n' \
  517. b'test-template class=TemplateVM state=Halted\n' \
  518. b'test-net class=AppVM state=Halted\n'
  519. self.app.expected_calls[('dom0', 'admin.vm.Create.AppVM',
  520. 'test-template', b'name=new-name label=red')] = b'0\x00'
  521. self.app.expected_calls[
  522. ('new-name', 'admin.vm.volume.CloneTo', 'private',
  523. b'token-private')] = \
  524. b'2\0QubesException\0\0something happened\0'
  525. self.app.expected_calls[('new-name', 'admin.vm.Remove', None, None)] = \
  526. b'0\x00'
  527. del self.app.expected_calls[
  528. ('new-name', 'admin.vm.volume.Info', 'root', None)]
  529. del self.app.expected_calls[
  530. ('new-name', 'admin.vm.volume.Info', 'volatile', None)]
  531. with self.assertRaises(qubesadmin.exc.QubesException):
  532. self.app.clone_vm('test-vm', 'new-name')
  533. self.assertAllCalled()
  534. class TC_20_QubesLocal(unittest.TestCase):
  535. def setUp(self):
  536. super(TC_20_QubesLocal, self).setUp()
  537. self.socket_dir = tempfile.mkdtemp()
  538. self.orig_sock = qubesadmin.config.QUBESD_SOCKET
  539. qubesadmin.config.QUBESD_SOCKET = os.path.join(self.socket_dir, 'sock')
  540. self.proc = None
  541. self.app = qubesadmin.app.QubesLocal()
  542. def listen_and_send(self, send_data):
  543. '''Listen on socket and send data in response.
  544. :param bytes send_data: data to send
  545. '''
  546. self.socket_pipe, child_pipe = multiprocessing.Pipe()
  547. self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  548. self.socket.bind(os.path.join(self.socket_dir, 'sock'))
  549. self.socket.listen(1)
  550. def worker(sock, pipe, send_data_):
  551. conn, addr = sock.accept()
  552. pipe.send(conn.makefile('rb').read())
  553. conn.sendall(send_data_)
  554. conn.close()
  555. self.proc = multiprocessing.Process(target=worker,
  556. args=(self.socket, child_pipe, send_data))
  557. self.proc.start()
  558. self.socket.close()
  559. def get_request(self):
  560. '''Get request sent to "qubesd" mock'''
  561. return self.socket_pipe.recv()
  562. def tearDown(self):
  563. qubesadmin.config.QUBESD_SOCKET = self.orig_sock
  564. if self.proc is not None:
  565. try:
  566. self.proc.terminate()
  567. except OSError:
  568. pass
  569. shutil.rmtree(self.socket_dir)
  570. super(TC_20_QubesLocal, self).tearDown()
  571. def test_000_qubesd_call(self):
  572. self.listen_and_send(b'0\0')
  573. self.app.qubesd_call('test-vm', 'some.method', 'arg1', b'payload')
  574. self.assertEqual(self.get_request(),
  575. b'dom0\0some.method\0test-vm\0arg1\0payload')
  576. def test_001_qubesd_call_none_arg(self):
  577. self.listen_and_send(b'0\0')
  578. self.app.qubesd_call('test-vm', 'some.method', None, b'payload')
  579. self.assertEqual(self.get_request(),
  580. b'dom0\0some.method\0test-vm\0\0payload')
  581. def test_002_qubesd_call_none_payload(self):
  582. self.listen_and_send(b'0\0')
  583. self.app.qubesd_call('test-vm', 'some.method', None, None)
  584. self.assertEqual(self.get_request(),
  585. b'dom0\0some.method\0test-vm\0\0')
  586. def test_003_qubesd_call_payload_stream(self):
  587. # this should really be in setUp()...
  588. tmpdir = tempfile.mkdtemp()
  589. self.addCleanup(shutil.rmtree, tmpdir)
  590. service_path = os.path.join(tmpdir, 'test.service')
  591. payload_input = os.path.join(tmpdir, 'payload-input')
  592. with open(service_path, 'w') as f:
  593. f.write('#!/bin/bash\n'
  594. 'env > {dir}/env\n'
  595. 'echo "$@" > {dir}/args\n'
  596. 'cat > {dir}/payload\n'
  597. 'echo -en \'0\\0return-value\'\n'.format(dir=tmpdir))
  598. os.chmod(service_path, 0o755)
  599. with open(payload_input, 'w+') as payload_file:
  600. payload_file.write('some payload\n')
  601. payload_file.seek(0)
  602. with mock.patch('qubesadmin.config.QREXEC_SERVICES_DIR',
  603. tmpdir):
  604. value = self.app.qubesd_call('test-vm', 'test.service',
  605. 'some-arg', payload_stream=payload_file)
  606. self.assertEqual(value, b'return-value')
  607. self.assertTrue(os.path.exists(tmpdir + '/env'))
  608. with open(tmpdir + '/env') as env:
  609. self.assertIn('QREXEC_REMOTE_DOMAIN=dom0\n', env)
  610. self.assertIn('QREXEC_REQUESTED_TARGET=test-vm\n', env)
  611. self.assertTrue(os.path.exists(tmpdir + '/args'))
  612. with open(tmpdir + '/args') as args:
  613. self.assertEqual(args.read(), 'some-arg\n')
  614. self.assertTrue(os.path.exists(tmpdir + '/payload'))
  615. with open(tmpdir + '/payload') as payload:
  616. self.assertEqual(payload.read(), 'some payload\n')
  617. def test_004_qubesd_call_payload_stream_proc(self):
  618. # this should really be in setUp()...
  619. tmpdir = tempfile.mkdtemp()
  620. self.addCleanup(shutil.rmtree, tmpdir)
  621. service_path = os.path.join(tmpdir, 'test.service')
  622. echo = subprocess.Popen(['echo', 'some payload'],
  623. stdout=subprocess.PIPE)
  624. with open(service_path, 'w') as f:
  625. f.write('#!/bin/bash\n'
  626. 'env > {dir}/env\n'
  627. 'echo "$@" > {dir}/args\n'
  628. 'cat > {dir}/payload\n'
  629. 'echo -en \'0\\0return-value\'\n'.format(dir=tmpdir))
  630. os.chmod(service_path, 0o755)
  631. with mock.patch('qubesadmin.config.QREXEC_SERVICES_DIR',
  632. tmpdir):
  633. value = self.app.qubesd_call('test-vm', 'test.service',
  634. 'some-arg', payload_stream=echo.stdout)
  635. echo.stdout.close()
  636. self.assertEqual(value, b'return-value')
  637. self.assertTrue(os.path.exists(tmpdir + '/env'))
  638. with open(tmpdir + '/env') as env:
  639. self.assertIn('QREXEC_REMOTE_DOMAIN=dom0\n', env)
  640. self.assertIn('QREXEC_REQUESTED_TARGET=test-vm\n', env)
  641. self.assertTrue(os.path.exists(tmpdir + '/args'))
  642. with open(tmpdir + '/args') as args:
  643. self.assertEqual(args.read(), 'some-arg\n')
  644. self.assertTrue(os.path.exists(tmpdir + '/payload'))
  645. with open(tmpdir + '/payload') as payload:
  646. self.assertEqual(payload.read(), 'some payload\n')
  647. def test_010_run_service(self):
  648. self.listen_and_send(b'0\0')
  649. with mock.patch('subprocess.Popen') as mock_proc:
  650. p = self.app.run_service('some-vm', 'service.name')
  651. mock_proc.assert_called_once_with([
  652. qubesadmin.config.QREXEC_CLIENT,
  653. '-d', 'some-vm', 'DEFAULT:QUBESRPC service.name dom0'],
  654. stdin=subprocess.PIPE, stdout=subprocess.PIPE,
  655. stderr=subprocess.PIPE)
  656. self.assertEqual(self.get_request(),
  657. b'dom0\0admin.vm.Start\0some-vm\0\0')
  658. def test_011_run_service_filter_esc(self):
  659. self.listen_and_send(b'0\0')
  660. with mock.patch('subprocess.Popen') as mock_proc:
  661. p = self.app.run_service('some-vm', 'service.name', filter_esc=True)
  662. mock_proc.assert_called_once_with([
  663. qubesadmin.config.QREXEC_CLIENT,
  664. '-d', 'some-vm', '-t', '-T',
  665. 'DEFAULT:QUBESRPC service.name dom0'],
  666. stdin=subprocess.PIPE, stdout=subprocess.PIPE,
  667. stderr=subprocess.PIPE)
  668. self.assertEqual(self.get_request(),
  669. b'dom0\0admin.vm.Start\0some-vm\0\0')
  670. def test_012_run_service_user(self):
  671. self.listen_and_send(b'0\0')
  672. with mock.patch('subprocess.Popen') as mock_proc:
  673. p = self.app.run_service('some-vm', 'service.name', user='user')
  674. mock_proc.assert_called_once_with([
  675. qubesadmin.config.QREXEC_CLIENT,
  676. '-d', 'some-vm',
  677. 'user:QUBESRPC service.name dom0'],
  678. stdin=subprocess.PIPE, stdout=subprocess.PIPE,
  679. stderr=subprocess.PIPE)
  680. self.assertEqual(self.get_request(),
  681. b'dom0\0admin.vm.Start\0some-vm\0\0')
  682. def test_013_run_service_default_target(self):
  683. with self.assertRaises(ValueError):
  684. self.app.run_service('', 'service.name')
  685. class TC_30_QubesRemote(unittest.TestCase):
  686. def setUp(self):
  687. super(TC_30_QubesRemote, self).setUp()
  688. self.proc_mock = mock.Mock()
  689. self.proc_mock.configure_mock(**{
  690. 'return_value.returncode': 0
  691. })
  692. self.proc_patch = mock.patch('subprocess.Popen', self.proc_mock)
  693. self.proc_patch.start()
  694. self.app = qubesadmin.app.QubesRemote()
  695. def set_proc_stdout(self, send_data):
  696. self.proc_mock.configure_mock(**{
  697. 'return_value.communicate.return_value': (send_data, None)
  698. })
  699. def tearDown(self):
  700. self.proc_patch.stop()
  701. super(TC_30_QubesRemote, self).tearDown()
  702. def test_000_qubesd_call(self):
  703. self.set_proc_stdout(b'0\0')
  704. self.app.qubesd_call('test-vm', 'some.method', 'arg1', b'payload')
  705. self.assertEqual(self.proc_mock.mock_calls, [
  706. mock.call([qubesadmin.config.QREXEC_CLIENT_VM, 'test-vm',
  707. 'some.method+arg1'],
  708. stdin=subprocess.PIPE, stdout=subprocess.PIPE,
  709. stderr=subprocess.PIPE),
  710. mock.call().communicate(b'payload')
  711. ])
  712. def test_001_qubesd_call_none_arg(self):
  713. self.set_proc_stdout(b'0\0')
  714. self.app.qubesd_call('test-vm', 'some.method', None, b'payload')
  715. self.assertEqual(self.proc_mock.mock_calls, [
  716. mock.call([qubesadmin.config.QREXEC_CLIENT_VM, 'test-vm',
  717. 'some.method'],
  718. stdin=subprocess.PIPE, stdout=subprocess.PIPE,
  719. stderr=subprocess.PIPE),
  720. mock.call().communicate(b'payload')
  721. ])
  722. def test_002_qubesd_call_none_payload(self):
  723. self.set_proc_stdout(b'0\0')
  724. self.app.qubesd_call('test-vm', 'some.method', None, None)
  725. self.assertEqual(self.proc_mock.mock_calls, [
  726. mock.call([qubesadmin.config.QREXEC_CLIENT_VM, 'test-vm',
  727. 'some.method'],
  728. stdin=subprocess.PIPE, stdout=subprocess.PIPE,
  729. stderr=subprocess.PIPE),
  730. mock.call().communicate(None)
  731. ])
  732. def test_003_qubesd_call_payload_stream(self):
  733. self.set_proc_stdout(b'0\0return-value')
  734. tmpdir = tempfile.mkdtemp()
  735. self.addCleanup(shutil.rmtree, tmpdir)
  736. payload_input = os.path.join(tmpdir, 'payload-input')
  737. with open(payload_input, 'w+') as payload_file:
  738. payload_file.write('some payload\n')
  739. payload_file.seek(0)
  740. value = self.app.qubesd_call('test-vm', 'some.method',
  741. 'some-arg', payload_stream=payload_file)
  742. self.assertEqual(self.proc_mock.mock_calls, [
  743. mock.call([qubesadmin.config.QREXEC_CLIENT_VM, 'test-vm',
  744. 'some.method+some-arg'],
  745. stdin=payload_file, stdout=subprocess.PIPE,
  746. stderr=subprocess.PIPE),
  747. mock.call().communicate(None)
  748. ])
  749. self.assertEqual(value, b'return-value')
  750. def test_010_run_service(self):
  751. self.app.run_service('some-vm', 'service.name')
  752. self.proc_mock.assert_called_once_with([
  753. qubesadmin.config.QREXEC_CLIENT_VM,
  754. 'some-vm', 'service.name'],
  755. stdin=subprocess.PIPE, stdout=subprocess.PIPE,
  756. stderr=subprocess.PIPE)
  757. def test_011_run_service_filter_esc(self):
  758. with self.assertRaises(NotImplementedError):
  759. p = self.app.run_service('some-vm', 'service.name', filter_esc=True)
  760. def test_012_run_service_user(self):
  761. with self.assertRaises(ValueError):
  762. p = self.app.run_service('some-vm', 'service.name', user='user')
  763. def test_013_run_service_default_target(self):
  764. self.app.run_service('', 'service.name')
  765. self.proc_mock.assert_called_once_with([
  766. qubesadmin.config.QREXEC_CLIENT_VM,
  767. '', 'service.name'],
  768. stdin=subprocess.PIPE, stdout=subprocess.PIPE,
  769. stderr=subprocess.PIPE)