qvm_run.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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. ret = qubesadmin.tools.qvm_run.main(
  352. ['--dispvm', '--service', 'test.service'], app=self.app)
  353. self.assertEqual(ret, 0)
  354. self.assertEqual(self.app.service_calls, [
  355. ('$dispvm', 'test.service', {
  356. 'stdout': subprocess.DEVNULL,
  357. 'stderr': subprocess.DEVNULL,
  358. 'user': None,
  359. }),
  360. ('$dispvm', 'test.service', b''),
  361. ])
  362. self.assertAllCalled()
  363. def test_009_dispvm_remote_specific(self):
  364. ret = qubesadmin.tools.qvm_run.main(
  365. ['--dispvm=test-vm', '--service', 'test.service'], app=self.app)
  366. self.assertEqual(ret, 0)
  367. self.assertEqual(self.app.service_calls, [
  368. ('$dispvm:test-vm', 'test.service', {
  369. 'stdout': subprocess.DEVNULL,
  370. 'stderr': subprocess.DEVNULL,
  371. 'user': None,
  372. }),
  373. ('$dispvm:test-vm', 'test.service', b''),
  374. ])
  375. self.assertAllCalled()
  376. def test_010_dispvm_local(self):
  377. self.app.qubesd_connection_type = 'socket'
  378. self.app.expected_calls[
  379. ('dom0', 'admin.vm.CreateDisposable', None, None)] = \
  380. b'0\0disp123'
  381. self.app.expected_calls[('disp123', 'admin.vm.Kill', None, None)] = \
  382. b'0\0'
  383. self.app.expected_calls[
  384. ('disp123', 'admin.vm.property.Get', 'qrexec_timeout', None)] = \
  385. b'0\0default=yes type=int 30'
  386. ret = qubesadmin.tools.qvm_run.main(
  387. ['--dispvm', '--service', 'test.service'], app=self.app)
  388. self.assertEqual(ret, 0)
  389. self.assertEqual(self.app.service_calls, [
  390. ('disp123', 'test.service', {
  391. 'stdout': subprocess.DEVNULL,
  392. 'stderr': subprocess.DEVNULL,
  393. 'user': None,
  394. 'connect_timeout': 30,
  395. }),
  396. ('disp123', 'test.service', b''),
  397. ])
  398. self.assertAllCalled()
  399. def test_011_dispvm_local_specific(self):
  400. self.app.qubesd_connection_type = 'socket'
  401. self.app.expected_calls[
  402. ('test-vm', 'admin.vm.CreateDisposable', None, None)] = \
  403. b'0\0disp123'
  404. self.app.expected_calls[('disp123', 'admin.vm.Kill', None, None)] = \
  405. b'0\0'
  406. self.app.expected_calls[
  407. ('disp123', 'admin.vm.property.Get', 'qrexec_timeout', None)] = \
  408. b'0\0default=yes type=int 30'
  409. ret = qubesadmin.tools.qvm_run.main(
  410. ['--dispvm=test-vm', '--service', 'test.service'], app=self.app)
  411. self.assertEqual(ret, 0)
  412. self.assertEqual(self.app.service_calls, [
  413. ('disp123', 'test.service', {
  414. 'stdout': subprocess.DEVNULL,
  415. 'stderr': subprocess.DEVNULL,
  416. 'user': None,
  417. 'connect_timeout': 30,
  418. }),
  419. ('disp123', 'test.service', b''),
  420. ])
  421. self.assertAllCalled()
  422. def test_012_exclude(self):
  423. self.app.expected_calls[
  424. ('dom0', 'admin.vm.List', None, None)] = \
  425. b'0\x00test-vm class=AppVM state=Running\n' \
  426. b'test-vm2 class=AppVM state=Running\n' \
  427. b'test-vm3 class=AppVM state=Halted\n'
  428. self.app.expected_calls[
  429. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  430. b'0\x00power_state=Running'
  431. self.app.expected_calls[
  432. ('test-vm3', 'admin.vm.CurrentState', None, None)] = \
  433. b'0\x00power_state=Halted'
  434. self.app.expected_calls[
  435. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
  436. b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
  437. ret = qubesadmin.tools.qvm_run.main(
  438. ['--no-gui', '--all', '--exclude', 'test-vm2', 'command'],
  439. app=self.app)
  440. self.assertEqual(ret, 0)
  441. self.assertEqual(self.app.service_calls, [
  442. ('test-vm', 'qubes.VMShell', {
  443. 'stdout': subprocess.DEVNULL,
  444. 'stderr': subprocess.DEVNULL,
  445. 'user': None,
  446. }),
  447. ('test-vm', 'qubes.VMShell', b'command; exit\n'),
  448. ])
  449. self.assertAllCalled()
  450. def test_013_no_autostart(self):
  451. self.app.expected_calls[
  452. ('dom0', 'admin.vm.List', None, None)] = \
  453. b'0\x00test-vm class=AppVM state=Running\n' \
  454. b'test-vm2 class=AppVM state=Running\n' \
  455. b'test-vm3 class=AppVM state=Halted\n'
  456. self.app.expected_calls[
  457. ('test-vm3', 'admin.vm.CurrentState', None, None)] = \
  458. b'0\x00power_state=Halted'
  459. ret = qubesadmin.tools.qvm_run.main(
  460. ['--no-gui', '--no-autostart', 'test-vm3', 'command'],
  461. app=self.app)
  462. self.assertEqual(ret, 1)
  463. self.assertEqual(self.app.service_calls, [])
  464. self.assertAllCalled()
  465. def test_014_dispvm_local_gui(self):
  466. self.app.qubesd_connection_type = 'socket'
  467. self.app.expected_calls[
  468. ('dom0', 'admin.vm.CreateDisposable', None, None)] = \
  469. b'0\0disp123'
  470. self.app.expected_calls[('disp123', 'admin.vm.Kill', None, None)] = \
  471. b'0\0'
  472. self.app.expected_calls[
  473. ('disp123', 'admin.vm.property.Get', 'qrexec_timeout', None)] = \
  474. b'0\0default=yes type=int 30'
  475. self.app.expected_calls[
  476. ('disp123', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
  477. b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
  478. ret = qubesadmin.tools.qvm_run.main(
  479. ['--dispvm', '--', 'test.command'], app=self.app)
  480. self.assertEqual(ret, 0)
  481. self.assertEqual(self.app.service_calls, [
  482. ('disp123', 'qubes.VMShell+WaitForSession', {
  483. 'stdout': subprocess.DEVNULL,
  484. 'stderr': subprocess.DEVNULL,
  485. 'user': None,
  486. 'connect_timeout': 30,
  487. }),
  488. ('disp123', 'qubes.VMShell+WaitForSession',
  489. b'test.command; exit\n'),
  490. ])
  491. self.assertAllCalled()
  492. def test_015_dispvm_local_no_gui(self):
  493. self.app.qubesd_connection_type = 'socket'
  494. self.app.expected_calls[
  495. ('dom0', 'admin.vm.CreateDisposable', None, None)] = \
  496. b'0\0disp123'
  497. self.app.expected_calls[('disp123', 'admin.vm.Kill', None, None)] = \
  498. b'0\0'
  499. self.app.expected_calls[
  500. ('disp123', 'admin.vm.property.Get', 'qrexec_timeout', None)] = \
  501. b'0\0default=yes type=int 30'
  502. self.app.expected_calls[
  503. ('disp123', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
  504. b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
  505. ret = qubesadmin.tools.qvm_run.main(
  506. ['--dispvm', '--no-gui', 'test.command'], app=self.app)
  507. self.assertEqual(ret, 0)
  508. self.assertEqual(self.app.service_calls, [
  509. ('disp123', 'qubes.VMShell', {
  510. 'stdout': subprocess.DEVNULL,
  511. 'stderr': subprocess.DEVNULL,
  512. 'user': None,
  513. 'connect_timeout': 30,
  514. }),
  515. ('disp123', 'qubes.VMShell', b'test.command; exit\n'),
  516. ])
  517. self.assertAllCalled()
  518. def test_016_run_single_windows(self):
  519. self.app.expected_calls[
  520. ('dom0', 'admin.vm.List', None, None)] = \
  521. b'0\x00test-vm class=AppVM state=Running\n'
  522. self.app.expected_calls[
  523. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
  524. b'0\x00Windows'
  525. self.app.expected_calls[
  526. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  527. b'0\x00power_state=Running'
  528. ret = qubesadmin.tools.qvm_run.main(
  529. ['--no-gui', 'test-vm', 'command'],
  530. app=self.app)
  531. self.assertEqual(ret, 0)
  532. self.assertEqual(self.app.service_calls, [
  533. ('test-vm', 'qubes.VMShell', {
  534. 'stdout': subprocess.DEVNULL,
  535. 'stderr': subprocess.DEVNULL,
  536. 'user': None,
  537. }),
  538. ('test-vm', 'qubes.VMShell', b'command& exit\n')
  539. ])
  540. self.assertAllCalled()
  541. def test_020_run_exec_with_vmexec_not_supported(self):
  542. self.app.expected_calls[
  543. ('dom0', 'admin.vm.List', None, None)] = \
  544. b'0\x00test-vm class=AppVM state=Running\n'
  545. self.app.expected_calls[
  546. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
  547. b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
  548. self.app.expected_calls[
  549. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'vmexec', None)] = \
  550. b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'vmexec\' not set\x00'
  551. self.app.expected_calls[
  552. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  553. b'0\x00power_state=Running'
  554. ret = qubesadmin.tools.qvm_run.main(
  555. ['--no-gui', 'test-vm', 'command', 'arg'],
  556. app=self.app)
  557. self.assertEqual(ret, 0)
  558. self.assertEqual(self.app.service_calls, [
  559. ('test-vm', 'qubes.VMShell', {
  560. 'stdout': subprocess.DEVNULL,
  561. 'stderr': subprocess.DEVNULL,
  562. 'user': None,
  563. }),
  564. ('test-vm', 'qubes.VMShell', b'command arg; exit\n')
  565. ])
  566. self.assertAllCalled()
  567. def test_020_run_exec_with_vmexec_supported(self):
  568. self.app.expected_calls[
  569. ('dom0', 'admin.vm.List', None, None)] = \
  570. b'0\x00test-vm class=AppVM state=Running\n'
  571. self.app.expected_calls[
  572. ('test-vm', 'admin.vm.feature.CheckWithTemplate',
  573. 'vmexec', None)] = \
  574. b'0\x001'
  575. self.app.expected_calls[
  576. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  577. b'0\x00power_state=Running'
  578. ret = qubesadmin.tools.qvm_run.main(
  579. ['--no-gui', 'test-vm', 'command', 'arg'],
  580. app=self.app)
  581. self.assertEqual(ret, 0)
  582. self.assertEqual(self.app.service_calls, [
  583. ('test-vm', 'qubes.VMExec+command+arg', {
  584. 'stdout': subprocess.DEVNULL,
  585. 'stderr': subprocess.DEVNULL,
  586. 'user': None,
  587. }),
  588. ('test-vm', 'qubes.VMExec+command+arg', b'')
  589. ])
  590. self.assertAllCalled()
  591. def test_021_paused_vm(self):
  592. self.app.expected_calls[
  593. ('dom0', 'admin.vm.List', None, None)] = \
  594. b'0\x00test-vm class=AppVM state=Paused\n'
  595. self.app.expected_calls[
  596. ('test-vm', 'admin.vm.feature.CheckWithTemplate', 'os', None)] = \
  597. b'2\x00QubesFeatureNotFoundError\x00\x00Feature \'os\' not set\x00'
  598. self.app.expected_calls[
  599. ('test-vm', 'admin.vm.CurrentState', None, None)] = \
  600. b'0\x00power_state=Paused'
  601. self.app.expected_calls[
  602. ('test-vm', 'admin.vm.Unpause', None, None)] = \
  603. b'0\x00'
  604. ret = qubesadmin.tools.qvm_run.main(
  605. ['--no-gui', 'test-vm', 'command'],
  606. app=self.app)
  607. self.assertEqual(ret, 0)
  608. self.assertEqual(self.app.service_calls, [
  609. ('test-vm', 'qubes.VMShell', {
  610. 'stdout': subprocess.DEVNULL,
  611. 'stderr': subprocess.DEVNULL,
  612. 'user': None,
  613. }),
  614. ('test-vm', 'qubes.VMShell', b'command; exit\n')
  615. ])
  616. self.assertAllCalled()