qvm_run.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. # -*- encoding: utf-8 -*-
  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 io
  21. import os
  22. import unittest.mock
  23. import subprocess
  24. import sys
  25. import qubesadmin.tests
  26. import qubesadmin.tools.qvm_run
  27. class TC_00_qvm_run(qubesadmin.tests.QubesTestCase):
  28. def setUp(self):
  29. if sys.stdout is not sys.__stdout__ or \
  30. sys.stderr is not sys.__stderr__:
  31. self.skipTest('qvm-run change behavior on redirected stdout/stderr')
  32. super(TC_00_qvm_run, self).setUp()
  33. def default_filter_esc(self):
  34. return os.isatty(sys.stdout.fileno())
  35. def test_000_run_single(self):
  36. self.app.expected_calls[
  37. ('dom0', 'admin.vm.List', None, None)] = \
  38. b'0\x00test-vm class=AppVM state=Running\n'
  39. self.app.expected_calls[
  40. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
  41. b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
  42. self.app.expected_calls[
  43. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  44. b'0\x00power_state=Running'
  45. ret = qubesadmin.tools.qvm_run.main(
  46. ['--no-gui', 'test-vm', 'command'],
  47. app=self.app)
  48. self.assertEqual(ret, 0)
  49. self.assertEqual(self.app.service_calls, [
  50. ('test-vm', 'qubes.VMShell', {
  51. 'stdout': subprocess.DEVNULL,
  52. 'stderr': subprocess.DEVNULL,
  53. 'user': None,
  54. }),
  55. ('test-vm', 'qubes.VMShell', b'command; exit\n')
  56. ])
  57. self.assertAllCalled()
  58. def test_001_run_multiple(self):
  59. self.app.expected_calls[
  60. ('dom0', 'admin.vm.List', None, None)] = \
  61. b'0\x00test-vm class=AppVM state=Running\n' \
  62. b'test-vm2 class=AppVM state=Running\n' \
  63. b'test-vm3 class=AppVM state=Halted\n'
  64. self.app.expected_calls[
  65. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  66. b'0\x00power_state=Running'
  67. self.app.expected_calls[
  68. ('test-vm2', 'admin.vm.CurrentState', None, None)] = \
  69. b'0\x00power_state=Running'
  70. self.app.expected_calls[
  71. ('test-vm3', 'admin.vm.CurrentState', None, None)] = \
  72. b'0\x00power_state=Halted'
  73. self.app.expected_calls[
  74. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
  75. b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
  76. self.app.expected_calls[
  77. ('test-vm2', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
  78. b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
  79. ret = qubesadmin.tools.qvm_run.main(
  80. ['--no-gui', '--all', 'command'],
  81. app=self.app)
  82. self.assertEqual(ret, 0)
  83. self.assertEqual(self.app.service_calls, [
  84. ('test-vm', 'qubes.VMShell', {
  85. 'stdout': subprocess.DEVNULL,
  86. 'stderr': subprocess.DEVNULL,
  87. 'user': None,
  88. }),
  89. ('test-vm', 'qubes.VMShell', b'command; exit\n'),
  90. ('test-vm2', 'qubes.VMShell', {
  91. 'stdout': subprocess.DEVNULL,
  92. 'stderr': subprocess.DEVNULL,
  93. 'user': None,
  94. }),
  95. ('test-vm2', 'qubes.VMShell', b'command; exit\n')
  96. ])
  97. self.assertAllCalled()
  98. def test_002_passio(self):
  99. self.app.expected_calls[
  100. ('dom0', 'admin.vm.List', None, None)] = \
  101. b'0\x00test-vm class=AppVM state=Running\n'
  102. self.app.expected_calls[
  103. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
  104. b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
  105. self.app.expected_calls[
  106. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  107. b'0\x00power_state=Running'
  108. # self.app.expected_calls[
  109. # ('test-vm', 'admin.vm.List', None, None)] = \
  110. # b'0\x00test-vm class=AppVM state=Running\n'
  111. echo = subprocess.Popen(['echo', 'some-data'], stdout=subprocess.PIPE)
  112. with unittest.mock.patch('sys.stdin', echo.stdout):
  113. ret = qubesadmin.tools.qvm_run.main(
  114. ['--no-gui', '--pass-io', '--filter-escape-chars',
  115. 'test-vm', 'command'],
  116. app=self.app)
  117. echo.stdout.close()
  118. echo.wait()
  119. self.assertEqual(ret, 0)
  120. self.assertEqual(self.app.service_calls, [
  121. ('test-vm', 'qubes.VMShell', {
  122. 'filter_esc': True,
  123. 'stdout': None,
  124. 'stderr': None,
  125. 'user': None,
  126. }),
  127. # TODO: find a way to compare b'some-data\n' sent from another
  128. # proces
  129. ('test-vm', 'qubes.VMShell', b'command; exit\n')
  130. ])
  131. self.assertAllCalled()
  132. def test_002_passio_service(self):
  133. self.app.expected_calls[
  134. ('dom0', 'admin.vm.List', None, None)] = \
  135. b'0\x00test-vm class=AppVM state=Running\n'
  136. self.app.expected_calls[
  137. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  138. b'0\x00power_state=Running'
  139. echo = subprocess.Popen(['echo', 'some-data'], stdout=subprocess.PIPE)
  140. with unittest.mock.patch('sys.stdin', echo.stdout):
  141. ret = qubesadmin.tools.qvm_run.main(
  142. ['--no-gui', '--service', '--pass-io', '--filter-escape-chars',
  143. 'test-vm', 'test.service'],
  144. app=self.app)
  145. echo.stdout.close()
  146. echo.wait()
  147. self.assertEqual(ret, 0)
  148. self.assertEqual(self.app.service_calls, [
  149. ('test-vm', 'test.service', {
  150. 'filter_esc': True,
  151. 'stdout': None,
  152. 'stderr': None,
  153. 'user': None,
  154. }),
  155. # TODO: find a way to compare b'some-data\n' sent from another
  156. # proces
  157. ('test-vm', 'test.service', b'')
  158. ])
  159. self.assertAllCalled()
  160. @unittest.expectedFailure
  161. def test_002_color_output(self):
  162. self.app.expected_calls[
  163. ('dom0', 'admin.vm.List', None, None)] = \
  164. b'0\x00test-vm class=AppVM state=Running\n'
  165. self.app.expected_calls[
  166. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
  167. b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
  168. # self.app.expected_calls[
  169. # ('test-vm', 'admin.vm.List', None, None)] = \
  170. # b'0\x00test-vm class=AppVM state=Running\n'
  171. stdout = io.StringIO()
  172. echo = subprocess.Popen(['echo', 'some-data'], stdout=subprocess.PIPE)
  173. with unittest.mock.patch('sys.stdin', echo.stdout):
  174. with unittest.mock.patch('sys.stdout', stdout):
  175. ret = qubesadmin.tools.qvm_run.main(
  176. ['--no-gui', '--filter-esc', '--pass-io', 'test-vm',
  177. 'command'],
  178. app=self.app)
  179. echo.stdout.close()
  180. echo.wait()
  181. self.assertEqual(ret, 0)
  182. self.assertEqual(self.app.service_calls, [
  183. ('test-vm', 'qubes.VMShell', {
  184. 'filter_esc': True,
  185. 'stdout': None,
  186. 'stderr': None,
  187. 'user': None,
  188. }),
  189. ('test-vm', 'qubes.VMShell', b'command; exit\nsome-data\n')
  190. ])
  191. self.assertEqual(stdout.getvalue(), '\033[0;31m\033[0m')
  192. stdout.close()
  193. self.assertAllCalled()
  194. @unittest.expectedFailure
  195. def test_003_no_color_output(self):
  196. self.app.expected_calls[
  197. ('dom0', 'admin.vm.List', None, None)] = \
  198. b'0\x00test-vm class=AppVM state=Running\n'
  199. # self.app.expected_calls[
  200. # ('test-vm', 'admin.vm.List', None, None)] = \
  201. # b'0\x00test-vm class=AppVM state=Running\n'
  202. stdout = io.StringIO()
  203. echo = subprocess.Popen(['echo', 'some-data'], stdout=subprocess.PIPE)
  204. with unittest.mock.patch('sys.stdin', echo.stdout):
  205. with unittest.mock.patch('sys.stdout', stdout):
  206. ret = qubesadmin.tools.qvm_run.main(
  207. ['--no-gui', '--pass-io', '--no-color-output',
  208. 'test-vm', 'command'],
  209. app=self.app)
  210. echo.stdout.close()
  211. echo.wait()
  212. self.assertEqual(ret, 0)
  213. self.assertEqual(self.app.service_calls, [
  214. ('test-vm', 'qubes.VMShell', {
  215. 'filter_esc': self.default_filter_esc(),
  216. 'stdout': None,
  217. 'stderr': None,
  218. 'user': None,
  219. }),
  220. ('test-vm', 'qubes.VMShell', b'command; exit\nsome-data\n')
  221. ])
  222. self.assertEqual(stdout.getvalue(), '')
  223. stdout.close()
  224. self.assertAllCalled()
  225. @unittest.expectedFailure
  226. def test_004_no_filter_esc(self):
  227. self.app.expected_calls[
  228. ('dom0', 'admin.vm.List', None, None)] = \
  229. b'0\x00test-vm class=AppVM state=Running\n'
  230. self.app.expected_calls[
  231. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
  232. b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
  233. # self.app.expected_calls[
  234. # ('test-vm', 'admin.vm.List', None, None)] = \
  235. # b'0\x00test-vm class=AppVM state=Running\n'
  236. stdout = io.StringIO()
  237. echo = subprocess.Popen(['echo', 'some-data'], stdout=subprocess.PIPE)
  238. with unittest.mock.patch('sys.stdin', echo.stdout):
  239. with unittest.mock.patch('sys.stdout', stdout):
  240. ret = qubesadmin.tools.qvm_run.main(
  241. ['--no-gui', '--pass-io', '--no-filter-esc',
  242. 'test-vm', 'command'],
  243. app=self.app)
  244. echo.stdout.close()
  245. echo.wait()
  246. self.assertEqual(ret, 0)
  247. self.assertEqual(self.app.service_calls, [
  248. ('test-vm', 'qubes.VMShell', {
  249. 'filter_esc': False,
  250. 'stdout': None,
  251. 'stderr': None,
  252. 'user': None,
  253. }),
  254. ('test-vm', 'qubes.VMShell', b'command; exit\nsome-data\n')
  255. ])
  256. self.assertEqual(stdout.getvalue(), '')
  257. stdout.close()
  258. self.assertAllCalled()
  259. @unittest.mock.patch('subprocess.Popen')
  260. def test_005_localcmd(self, mock_popen):
  261. self.app.expected_calls[
  262. ('dom0', 'admin.vm.List', None, None)] = \
  263. b'0\x00test-vm class=AppVM state=Running\n'
  264. self.app.expected_calls[
  265. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
  266. b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
  267. self.app.expected_calls[
  268. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  269. b'0\x00power_state=Running'
  270. mock_popen.return_value.wait.return_value = 0
  271. ret = qubesadmin.tools.qvm_run.main(
  272. ['--no-gui', '--pass-io', '--localcmd', 'local-command',
  273. 'test-vm', 'command'],
  274. app=self.app)
  275. self.assertEqual(ret, 0)
  276. self.assertEqual(self.app.service_calls, [
  277. ('test-vm', 'qubes.VMShell', {
  278. 'stdout': subprocess.PIPE,
  279. 'stdin': subprocess.PIPE,
  280. 'stderr': None,
  281. 'user': None,
  282. }),
  283. ('test-vm', 'qubes.VMShell', b'command; exit\n')
  284. ])
  285. mock_popen.assert_called_once_with('local-command',
  286. # TODO: check if the right stdin/stdout objects are used
  287. stdout=unittest.mock.ANY, stdin=unittest.mock.ANY, shell=True)
  288. self.assertAllCalled()
  289. def test_006_run_single_with_gui(self):
  290. self.app.expected_calls[
  291. ('dom0', 'admin.vm.List', None, None)] = \
  292. b'0\x00test-vm class=AppVM state=Running\n'
  293. self.app.expected_calls[
  294. ('test-vm', 'admin.vm.property.Get', 'default_user', None)] = \
  295. b'0\x00default=yes type=str user'
  296. self.app.expected_calls[
  297. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
  298. b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
  299. self.app.expected_calls[
  300. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  301. b'0\x00power_state=Running'
  302. ret = qubesadmin.tools.qvm_run.main(
  303. ['test-vm', 'command'],
  304. app=self.app)
  305. self.assertEqual(ret, 0)
  306. # make sure we have the same instance below
  307. self.assertEqual(self.app.service_calls, [
  308. ('test-vm', 'qubes.WaitForSession', {
  309. 'stdout': subprocess.DEVNULL,
  310. 'stderr': subprocess.DEVNULL,
  311. }),
  312. ('test-vm', 'qubes.WaitForSession', b'user'),
  313. ('test-vm', 'qubes.VMShell', {
  314. 'stdout': subprocess.DEVNULL,
  315. 'stderr': subprocess.DEVNULL,
  316. 'user': None,
  317. }),
  318. ('test-vm', 'qubes.VMShell', b'command; exit\n')
  319. ])
  320. self.assertAllCalled()
  321. def test_007_run_service_with_gui(self):
  322. self.app.expected_calls[
  323. ('dom0', 'admin.vm.List', None, None)] = \
  324. b'0\x00test-vm class=AppVM state=Running\n'
  325. self.app.expected_calls[
  326. ('test-vm', 'admin.vm.property.Get', 'default_user', None)] = \
  327. b'0\x00default=yes type=str user'
  328. self.app.expected_calls[
  329. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  330. b'0\x00power_state=Running'
  331. ret = qubesadmin.tools.qvm_run.main(
  332. ['--service', 'test-vm', 'service.name'],
  333. app=self.app)
  334. self.assertEqual(ret, 0)
  335. # make sure we have the same instance below
  336. self.assertEqual(self.app.service_calls, [
  337. ('test-vm', 'qubes.WaitForSession', {
  338. 'stdout': subprocess.DEVNULL,
  339. 'stderr': subprocess.DEVNULL,
  340. }),
  341. ('test-vm', 'qubes.WaitForSession', b'user'),
  342. ('test-vm', 'service.name', {
  343. 'stdout': subprocess.DEVNULL,
  344. 'stderr': subprocess.DEVNULL,
  345. 'user': None,
  346. }),
  347. ('test-vm', 'service.name', b''),
  348. ])
  349. self.assertAllCalled()
  350. def test_008_dispvm_remote(self):
  351. self.app.expected_calls[
  352. ('$dispvm', 'admin.vm.CurrentState', None, None)] = \
  353. b'0\x00power_state=Running'
  354. ret = qubesadmin.tools.qvm_run.main(
  355. ['--dispvm', '--service', 'test.service'], app=self.app)
  356. self.assertEqual(ret, 0)
  357. self.assertEqual(self.app.service_calls, [
  358. ('$dispvm', 'test.service', {
  359. 'stdout': subprocess.DEVNULL,
  360. 'stderr': subprocess.DEVNULL,
  361. 'user': None,
  362. }),
  363. ('$dispvm', 'test.service', b''),
  364. ])
  365. self.assertAllCalled()
  366. def test_009_dispvm_remote_specific(self):
  367. self.app.expected_calls[
  368. ('$dispvm:test-vm', 'admin.vm.CurrentState', None, None)] = \
  369. b'0\x00power_state=Running'
  370. ret = qubesadmin.tools.qvm_run.main(
  371. ['--dispvm=test-vm', '--service', 'test.service'], app=self.app)
  372. self.assertEqual(ret, 0)
  373. self.assertEqual(self.app.service_calls, [
  374. ('$dispvm:test-vm', 'test.service', {
  375. 'stdout': subprocess.DEVNULL,
  376. 'stderr': subprocess.DEVNULL,
  377. 'user': None,
  378. }),
  379. ('$dispvm:test-vm', 'test.service', b''),
  380. ])
  381. self.assertAllCalled()
  382. def test_010_dispvm_local(self):
  383. self.app.qubesd_connection_type = 'socket'
  384. self.app.expected_calls[
  385. ('dom0', 'admin.vm.CreateDisposable', None, None)] = \
  386. b'0\0disp123'
  387. self.app.expected_calls[('disp123', 'admin.vm.Kill', None, None)] = \
  388. b'0\0'
  389. self.app.expected_calls[
  390. ('disp123', 'admin.vm.property.Get', 'qrexec_timeout', None)] = \
  391. b'0\0default=yes type=int 30'
  392. self.app.expected_calls[
  393. ('$dispvm', 'admin.vm.CurrentState', None, None)] = \
  394. b'0\x00power_state=Running'
  395. ret = qubesadmin.tools.qvm_run.main(
  396. ['--dispvm', '--service', 'test.service'], app=self.app)
  397. self.assertEqual(ret, 0)
  398. self.assertEqual(self.app.service_calls, [
  399. ('disp123', 'test.service', {
  400. 'stdout': subprocess.DEVNULL,
  401. 'stderr': subprocess.DEVNULL,
  402. 'user': None,
  403. 'connect_timeout': 30,
  404. }),
  405. ('disp123', 'test.service', b''),
  406. ])
  407. self.assertAllCalled()
  408. def test_011_dispvm_local_specific(self):
  409. self.app.qubesd_connection_type = 'socket'
  410. self.app.expected_calls[
  411. ('test-vm', 'admin.vm.CreateDisposable', None, None)] = \
  412. b'0\0disp123'
  413. self.app.expected_calls[('disp123', 'admin.vm.Kill', None, None)] = \
  414. b'0\0'
  415. self.app.expected_calls[
  416. ('disp123', 'admin.vm.property.Get', 'qrexec_timeout', None)] = \
  417. b'0\0default=yes type=int 30'
  418. self.app.expected_calls[
  419. ('$dispvm:test-vm', 'admin.vm.CurrentState', None, None)] = \
  420. b'0\x00power_state=Running'
  421. ret = qubesadmin.tools.qvm_run.main(
  422. ['--dispvm=test-vm', '--service', 'test.service'], app=self.app)
  423. self.assertEqual(ret, 0)
  424. self.assertEqual(self.app.service_calls, [
  425. ('disp123', 'test.service', {
  426. 'stdout': subprocess.DEVNULL,
  427. 'stderr': subprocess.DEVNULL,
  428. 'user': None,
  429. 'connect_timeout': 30,
  430. }),
  431. ('disp123', 'test.service', b''),
  432. ])
  433. self.assertAllCalled()
  434. def test_012_exclude(self):
  435. self.app.expected_calls[
  436. ('dom0', 'admin.vm.List', None, None)] = \
  437. b'0\x00test-vm class=AppVM state=Running\n' \
  438. b'test-vm2 class=AppVM state=Running\n' \
  439. b'test-vm3 class=AppVM state=Halted\n'
  440. self.app.expected_calls[
  441. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  442. b'0\x00power_state=Running'
  443. self.app.expected_calls[
  444. ('test-vm3', 'admin.vm.CurrentState', None, None)] = \
  445. b'0\x00power_state=Halted'
  446. self.app.expected_calls[
  447. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
  448. b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
  449. ret = qubesadmin.tools.qvm_run.main(
  450. ['--no-gui', '--all', '--exclude', 'test-vm2', 'command'],
  451. app=self.app)
  452. self.assertEqual(ret, 0)
  453. self.assertEqual(self.app.service_calls, [
  454. ('test-vm', 'qubes.VMShell', {
  455. 'stdout': subprocess.DEVNULL,
  456. 'stderr': subprocess.DEVNULL,
  457. 'user': None,
  458. }),
  459. ('test-vm', 'qubes.VMShell', b'command; exit\n'),
  460. ])
  461. self.assertAllCalled()
  462. def test_013_no_autostart(self):
  463. self.app.expected_calls[
  464. ('dom0', 'admin.vm.List', None, None)] = \
  465. b'0\x00test-vm class=AppVM state=Running\n' \
  466. b'test-vm2 class=AppVM state=Running\n' \
  467. b'test-vm3 class=AppVM state=Halted\n'
  468. self.app.expected_calls[
  469. ('test-vm3', 'admin.vm.CurrentState', None, None)] = \
  470. b'0\x00power_state=Halted'
  471. ret = qubesadmin.tools.qvm_run.main(
  472. ['--no-gui', '--no-autostart', 'test-vm3', 'command'],
  473. app=self.app)
  474. self.assertEqual(ret, 1)
  475. self.assertEqual(self.app.service_calls, [])
  476. self.assertAllCalled()
  477. def test_014_dispvm_local_gui(self):
  478. self.app.qubesd_connection_type = 'socket'
  479. self.app.expected_calls[
  480. ('dom0', 'admin.vm.CreateDisposable', None, None)] = \
  481. b'0\0disp123'
  482. self.app.expected_calls[('disp123', 'admin.vm.Kill', None, None)] = \
  483. b'0\0'
  484. self.app.expected_calls[
  485. ('disp123', 'admin.vm.property.Get', 'qrexec_timeout', None)] = \
  486. b'0\0default=yes type=int 30'
  487. self.app.expected_calls[
  488. ('disp123', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
  489. b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
  490. self.app.expected_calls[
  491. ('$dispvm', 'admin.vm.CurrentState', None, None)] = \
  492. b'0\x00power_state=Running'
  493. ret = qubesadmin.tools.qvm_run.main(
  494. ['--dispvm', '--', 'test.command'], app=self.app)
  495. self.assertEqual(ret, 0)
  496. self.assertEqual(self.app.service_calls, [
  497. ('disp123', 'qubes.VMShell+WaitForSession', {
  498. 'stdout': subprocess.DEVNULL,
  499. 'stderr': subprocess.DEVNULL,
  500. 'user': None,
  501. 'connect_timeout': 30,
  502. }),
  503. ('disp123', 'qubes.VMShell+WaitForSession',
  504. b'test.command; exit\n'),
  505. ])
  506. self.assertAllCalled()
  507. def test_015_dispvm_local_no_gui(self):
  508. self.app.qubesd_connection_type = 'socket'
  509. self.app.expected_calls[
  510. ('dom0', 'admin.vm.CreateDisposable', None, None)] = \
  511. b'0\0disp123'
  512. self.app.expected_calls[('disp123', 'admin.vm.Kill', None, None)] = \
  513. b'0\0'
  514. self.app.expected_calls[
  515. ('disp123', 'admin.vm.property.Get', 'qrexec_timeout', None)] = \
  516. b'0\0default=yes type=int 30'
  517. self.app.expected_calls[
  518. ('disp123', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
  519. b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
  520. self.app.expected_calls[
  521. ('$dispvm', 'admin.vm.CurrentState', None, None)] = \
  522. b'0\x00power_state=Running'
  523. ret = qubesadmin.tools.qvm_run.main(
  524. ['--dispvm', '--no-gui', 'test.command'], app=self.app)
  525. self.assertEqual(ret, 0)
  526. self.assertEqual(self.app.service_calls, [
  527. ('disp123', 'qubes.VMShell', {
  528. 'stdout': subprocess.DEVNULL,
  529. 'stderr': subprocess.DEVNULL,
  530. 'user': None,
  531. 'connect_timeout': 30,
  532. }),
  533. ('disp123', 'qubes.VMShell', b'test.command; exit\n'),
  534. ])
  535. self.assertAllCalled()
  536. def test_016_run_single_windows(self):
  537. self.app.expected_calls[
  538. ('dom0', 'admin.vm.List', None, None)] = \
  539. b'0\x00test-vm class=AppVM state=Running\n'
  540. self.app.expected_calls[
  541. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
  542. b'0\x00Windows'
  543. self.app.expected_calls[
  544. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  545. b'0\x00power_state=Running'
  546. ret = qubesadmin.tools.qvm_run.main(
  547. ['--no-gui', 'test-vm', 'command'],
  548. app=self.app)
  549. self.assertEqual(ret, 0)
  550. self.assertEqual(self.app.service_calls, [
  551. ('test-vm', 'qubes.VMShell', {
  552. 'stdout': subprocess.DEVNULL,
  553. 'stderr': subprocess.DEVNULL,
  554. 'user': None,
  555. }),
  556. ('test-vm', 'qubes.VMShell', b'command& exit\n')
  557. ])
  558. self.assertAllCalled()
  559. def test_020_run_exec_with_vmexec_not_supported(self):
  560. self.app.expected_calls[
  561. ('dom0', 'admin.vm.List', None, None)] = \
  562. b'0\x00test-vm class=AppVM state=Running\n'
  563. self.app.expected_calls[
  564. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
  565. b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
  566. self.app.expected_calls[
  567. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'vmexec', None)] = \
  568. b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'vmexec\' not set\x00'
  569. self.app.expected_calls[
  570. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  571. b'0\x00power_state=Running'
  572. ret = qubesadmin.tools.qvm_run.main(
  573. ['--no-gui', 'test-vm', 'command', 'arg'],
  574. app=self.app)
  575. self.assertEqual(ret, 0)
  576. self.assertEqual(self.app.service_calls, [
  577. ('test-vm', 'qubes.VMShell', {
  578. 'stdout': subprocess.DEVNULL,
  579. 'stderr': subprocess.DEVNULL,
  580. 'user': None,
  581. }),
  582. ('test-vm', 'qubes.VMShell', b'command arg; exit\n')
  583. ])
  584. self.assertAllCalled()
  585. def test_020_run_exec_with_vmexec_supported(self):
  586. self.app.expected_calls[
  587. ('dom0', 'admin.vm.List', None, None)] = \
  588. b'0\x00test-vm class=AppVM state=Running\n'
  589. self.app.expected_calls[
  590. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  591. 'vmexec', None)] = \
  592. b'0\x001'
  593. self.app.expected_calls[
  594. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  595. b'0\x00power_state=Running'
  596. ret = qubesadmin.tools.qvm_run.main(
  597. ['--no-gui', 'test-vm', 'command', 'arg'],
  598. app=self.app)
  599. self.assertEqual(ret, 0)
  600. self.assertEqual(self.app.service_calls, [
  601. ('test-vm', 'qubes.VMExec+command+arg', {
  602. 'stdout': subprocess.DEVNULL,
  603. 'stderr': subprocess.DEVNULL,
  604. 'user': None,
  605. }),
  606. ('test-vm', 'qubes.VMExec+command+arg', b'')
  607. ])
  608. self.assertAllCalled()
  609. def test_021_paused_vm(self):
  610. self.app.expected_calls[
  611. ('dom0', 'admin.vm.List', None, None)] = \
  612. b'0\x00test-vm class=AppVM state=Paused\n'
  613. self.app.expected_calls[
  614. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
  615. b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
  616. self.app.expected_calls[
  617. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  618. b'0\x00power_state=Paused'
  619. self.app.expected_calls[
  620. ('test-vm', 'admin.vm.Unpause', None, None)] = \
  621. b'0\x00'
  622. ret = qubesadmin.tools.qvm_run.main(
  623. ['--no-gui', 'test-vm', 'command'],
  624. app=self.app)
  625. self.assertEqual(ret, 0)
  626. self.assertEqual(self.app.service_calls, [
  627. ('test-vm', 'qubes.VMShell', {
  628. 'stdout': subprocess.DEVNULL,
  629. 'stderr': subprocess.DEVNULL,
  630. 'user': None,
  631. }),
  632. ('test-vm', 'qubes.VMShell', b'command; exit\n')
  633. ])
  634. self.assertAllCalled()