qvm_volume.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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 tempfile
  21. import unittest.mock
  22. import qubesadmin.tests
  23. import qubesadmin.tests.tools
  24. import qubesadmin.tools.qvm_volume
  25. class TC_00_qvm_volume(qubesadmin.tests.QubesTestCase):
  26. def setup_expected_calls_for_list(self, vms=('vm1', 'sys-net')):
  27. self.app.expected_calls[
  28. ('dom0', 'admin.vm.List', None, None)] = \
  29. b'0\x00vm1 class=AppVM state=Running\n' \
  30. b'sys-net class=AppVM state=Running\n'
  31. for vm in vms:
  32. for vol in ('root', 'private'):
  33. self.app.expected_calls[
  34. (vm, 'admin.vm.volume.Info', vol, None)] = \
  35. b'0\x00' + \
  36. (b'pool=pool-file\n' if vol == 'root' else
  37. b'pool=other-pool\n') + \
  38. b'vid=' + vm.encode() + b'-' + vol.encode() + b'\n' \
  39. b'size=10737418240\n'
  40. self.app.expected_calls[
  41. (vm, 'admin.vm.volume.ListSnapshots', vol, None)] = \
  42. b'0\x00snap1\n' if vol == 'private' else b'0\x00'
  43. self.app.expected_calls[
  44. (vm, 'admin.vm.volume.List', None, None)] = \
  45. b'0\x00root\nprivate\n'
  46. def test_000_list(self):
  47. self.setup_expected_calls_for_list()
  48. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  49. self.assertEqual(0,
  50. qubesadmin.tools.qvm_volume.main(['ls'], app=self.app))
  51. self.assertEqual(stdout.getvalue(),
  52. 'POOL:VOLUME VMNAME VOLUME_NAME '
  53. 'REVERT_POSSIBLE\n'
  54. 'other-pool:sys-net-private sys-net private Yes\n'
  55. 'other-pool:vm1-private vm1 private Yes\n'
  56. 'pool-file:sys-net-root sys-net root No\n'
  57. 'pool-file:vm1-root vm1 root No\n'
  58. )
  59. self.assertAllCalled()
  60. def test_001_list_domain(self):
  61. self.setup_expected_calls_for_list(vms=('vm1',))
  62. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  63. self.assertEqual(0,
  64. qubesadmin.tools.qvm_volume.main(['ls', 'vm1'],
  65. app=self.app))
  66. self.assertEqual(stdout.getvalue(),
  67. 'POOL:VOLUME VMNAME VOLUME_NAME REVERT_POSSIBLE\n'
  68. 'other-pool:vm1-private vm1 private Yes\n'
  69. 'pool-file:vm1-root vm1 root No\n'
  70. )
  71. self.assertAllCalled()
  72. def test_002_list_domain_pool(self):
  73. self.setup_expected_calls_for_list(vms=('vm1',))
  74. self.app.expected_calls[('dom0', 'admin.pool.List', None, None)] = \
  75. b'0\x00pool-file\nother-pool\n'
  76. del self.app.expected_calls[
  77. ('vm1', 'admin.vm.volume.ListSnapshots', 'private', None)]
  78. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  79. self.assertEqual(0,
  80. qubesadmin.tools.qvm_volume.main(
  81. ['ls', '-p', 'pool-file', 'vm1'],
  82. app=self.app))
  83. self.assertEqual(stdout.getvalue(),
  84. 'POOL:VOLUME VMNAME VOLUME_NAME REVERT_POSSIBLE\n'
  85. 'pool-file:vm1-root vm1 root No\n'
  86. )
  87. self.assertAllCalled()
  88. def test_003_list_pool(self):
  89. self.setup_expected_calls_for_list()
  90. self.app.expected_calls[('dom0', 'admin.pool.List', None, None)] = \
  91. b'0\x00pool-file\nother-pool\n'
  92. del self.app.expected_calls[
  93. ('vm1', 'admin.vm.volume.ListSnapshots', 'private', None)]
  94. del self.app.expected_calls[
  95. ('sys-net', 'admin.vm.volume.ListSnapshots', 'private', None)]
  96. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  97. self.assertEqual(0,
  98. qubesadmin.tools.qvm_volume.main(
  99. ['ls', '-p', 'pool-file'],
  100. app=self.app))
  101. self.assertEqual(stdout.getvalue(),
  102. 'POOL:VOLUME VMNAME VOLUME_NAME REVERT_POSSIBLE\n'
  103. 'pool-file:sys-net-root sys-net root No\n'
  104. 'pool-file:vm1-root vm1 root No\n'
  105. )
  106. self.assertAllCalled()
  107. def test_004_list_multiple_domains(self):
  108. self.setup_expected_calls_for_list(vms=('vm1', 'vm2'))
  109. self.app.expected_calls[
  110. ('dom0', 'admin.vm.List', None, None)] = \
  111. b'0\x00vm1 class=AppVM state=Running\n' \
  112. b'vm2 class=AppVM state=Running\n' \
  113. b'vm3 class=AppVM state=Running\n'
  114. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  115. self.assertEqual(0,
  116. qubesadmin.tools.qvm_volume.main(
  117. ['ls', 'vm1', 'vm2'], app=self.app))
  118. self.assertEqual(stdout.getvalue(),
  119. 'POOL:VOLUME VMNAME VOLUME_NAME REVERT_POSSIBLE\n'
  120. 'other-pool:vm1-private vm1 private Yes\n'
  121. 'other-pool:vm2-private vm2 private Yes\n'
  122. 'pool-file:vm1-root vm1 root No\n'
  123. 'pool-file:vm2-root vm2 root No\n'
  124. )
  125. self.assertAllCalled()
  126. def test_005_list_default_action(self):
  127. self.setup_expected_calls_for_list()
  128. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  129. self.assertEqual(0,
  130. qubesadmin.tools.qvm_volume.main([], app=self.app))
  131. self.assertEqual(stdout.getvalue(),
  132. 'POOL:VOLUME VMNAME VOLUME_NAME '
  133. 'REVERT_POSSIBLE\n'
  134. 'other-pool:sys-net-private sys-net private Yes\n'
  135. 'other-pool:vm1-private vm1 private Yes\n'
  136. 'pool-file:sys-net-root sys-net root No\n'
  137. 'pool-file:vm1-root vm1 root No\n'
  138. )
  139. self.assertAllCalled()
  140. def test_010_extend(self):
  141. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  142. b'0\x00testvm class=AppVM state=Running\n'
  143. self.app.expected_calls[
  144. ('testvm', 'admin.vm.volume.List', None, None)] = \
  145. b'0\x00root\nprivate\n'
  146. self.app.expected_calls[
  147. ('testvm', 'admin.vm.volume.Info', 'private', None)] = \
  148. b'0\x00pool=lvm\n' \
  149. b'vid=qubes_dom0/vm-testvm-private\n' \
  150. b'size=2147483648\n' \
  151. b'usage=10000000\n' \
  152. b'rw=True\n' \
  153. b'source=\n' \
  154. b'save_on_stop=True\n' \
  155. b'snap_on_start=False\n' \
  156. b'revisions_to_keep=3\n' \
  157. b'is_outdated=False\n'
  158. self.app.expected_calls[
  159. ('testvm', 'admin.vm.volume.Resize', 'private', b'10737418240')] = \
  160. b'0\x00'
  161. self.assertEqual(0,
  162. qubesadmin.tools.qvm_volume.main(
  163. ['extend', 'testvm:private', '10GiB'],
  164. app=self.app))
  165. self.assertAllCalled()
  166. def test_011_extend_error(self):
  167. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  168. b'0\x00testvm class=AppVM state=Running\n'
  169. self.app.expected_calls[
  170. ('testvm', 'admin.vm.volume.List', None, None)] = \
  171. b'0\x00root\nprivate\n'
  172. self.app.expected_calls[
  173. ('testvm', 'admin.vm.volume.Info', 'private', None)] = \
  174. b'0\x00pool=lvm\n' \
  175. b'vid=qubes_dom0/vm-testvm-private\n' \
  176. b'size=2147483648\n' \
  177. b'usage=10000000\n' \
  178. b'rw=True\n' \
  179. b'source=\n' \
  180. b'save_on_stop=True\n' \
  181. b'snap_on_start=False\n' \
  182. b'revisions_to_keep=3\n' \
  183. b'is_outdated=False\n'
  184. self.app.expected_calls[
  185. ('testvm', 'admin.vm.volume.Resize', 'private', b'10737418240')] = \
  186. b'2\x00StoragePoolException\x00\x00Failed to resize volume: ' \
  187. b'error: success\x00'
  188. with qubesadmin.tests.tools.StderrBuffer() as stderr:
  189. self.assertEqual(1,
  190. qubesadmin.tools.qvm_volume.main(
  191. ['extend', 'testvm:private', '10GiB'],
  192. app=self.app))
  193. self.assertIn('error: success', stderr.getvalue())
  194. self.assertAllCalled()
  195. def test_012_extend_deny_shrink(self):
  196. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  197. b'0\x00testvm class=AppVM state=Running\n'
  198. self.app.expected_calls[
  199. ('testvm', 'admin.vm.volume.List', None, None)] = \
  200. b'0\x00root\nprivate\n'
  201. self.app.expected_calls[
  202. ('testvm', 'admin.vm.volume.Info', 'private', None)] = \
  203. b'0\x00pool=lvm\n' \
  204. b'vid=qubes_dom0/vm-testvm-private\n' \
  205. b'size=2147483648\n' \
  206. b'usage=10000000\n' \
  207. b'rw=True\n' \
  208. b'source=\n' \
  209. b'save_on_stop=True\n' \
  210. b'snap_on_start=False\n' \
  211. b'revisions_to_keep=3\n' \
  212. b'is_outdated=False\n'
  213. with qubesadmin.tests.tools.StderrBuffer() as stderr:
  214. self.assertEqual(1,
  215. qubesadmin.tools.qvm_volume.main(
  216. ['resize', 'testvm:private', '1GiB'],
  217. app=self.app))
  218. self.assertIn('shrinking of private is disabled', stderr.getvalue())
  219. self.assertAllCalled()
  220. def test_013_resize_force_shrink(self):
  221. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  222. b'0\x00testvm class=AppVM state=Running\n'
  223. self.app.expected_calls[
  224. ('testvm', 'admin.vm.volume.List', None, None)] = \
  225. b'0\x00root\nprivate\n'
  226. self.app.expected_calls[
  227. ('testvm', 'admin.vm.volume.Resize', 'private', b'1073741824')] = \
  228. b'0\x00'
  229. self.assertEqual(0,
  230. qubesadmin.tools.qvm_volume.main(
  231. ['resize', '-f', 'testvm:private', '1GiB'],
  232. app=self.app))
  233. self.assertAllCalled()
  234. def test_020_revert(self):
  235. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  236. b'0\x00testvm class=AppVM state=Running\n'
  237. self.app.expected_calls[
  238. ('testvm', 'admin.vm.volume.List', None, None)] = \
  239. b'0\x00root\nprivate\n'
  240. self.app.expected_calls[
  241. ('testvm', 'admin.vm.volume.ListSnapshots', 'private', None)] = \
  242. b'0\x00200101010000\n200201010000\n200301010000\n'
  243. self.app.expected_calls[
  244. ('testvm', 'admin.vm.volume.Revert', 'private', b'200301010000')] = \
  245. b'0\x00'
  246. self.assertEqual(0,
  247. qubesadmin.tools.qvm_volume.main(
  248. ['revert', 'testvm:private'],
  249. app=self.app))
  250. self.assertAllCalled()
  251. def test_021_revert_error(self):
  252. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  253. b'0\x00testvm class=AppVM state=Running\n'
  254. self.app.expected_calls[
  255. ('testvm', 'admin.vm.volume.List', None, None)] = \
  256. b'0\x00root\nprivate\n'
  257. self.app.expected_calls[
  258. ('testvm', 'admin.vm.volume.ListSnapshots', 'private', None)] = \
  259. b'0\x00200101010000\n200201010000\n200301010000\n'
  260. self.app.expected_calls[
  261. ('testvm', 'admin.vm.volume.Revert', 'private', b'200301010000')] = \
  262. b'2\x00StoragePoolException\x00\x00Failed to revert volume: ' \
  263. b'some error\x00'
  264. with qubesadmin.tests.tools.StderrBuffer() as stderr:
  265. self.assertEqual(1,
  266. qubesadmin.tools.qvm_volume.main(
  267. ['revert', 'testvm:private'],
  268. app=self.app))
  269. self.assertIn('some error', stderr.getvalue())
  270. self.assertAllCalled()
  271. def test_022_revert_no_snapshots(self):
  272. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  273. b'0\x00testvm class=AppVM state=Running\n'
  274. self.app.expected_calls[
  275. ('testvm', 'admin.vm.volume.List', None, None)] = \
  276. b'0\x00root\nprivate\n'
  277. self.app.expected_calls[
  278. ('testvm', 'admin.vm.volume.ListSnapshots', 'private', None)] = \
  279. b'0\x00'
  280. with qubesadmin.tests.tools.StderrBuffer() as stderr:
  281. self.assertEqual(1,
  282. qubesadmin.tools.qvm_volume.main(
  283. ['revert', 'testvm:private'],
  284. app=self.app))
  285. self.assertIn('No snapshots', stderr.getvalue())
  286. self.assertAllCalled()
  287. def test_023_revert_specific(self):
  288. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  289. b'0\x00testvm class=AppVM state=Running\n'
  290. self.app.expected_calls[
  291. ('testvm', 'admin.vm.volume.List', None, None)] = \
  292. b'0\x00root\nprivate\n'
  293. self.app.expected_calls[
  294. ('testvm', 'admin.vm.volume.Revert', 'private', b'20050101')] = \
  295. b'0\x00'
  296. self.assertEqual(0,
  297. qubesadmin.tools.qvm_volume.main(
  298. ['revert', 'testvm:private', '20050101'],
  299. app=self.app))
  300. self.assertAllCalled()
  301. def test_030_set_revisions_to_keep(self):
  302. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  303. b'0\x00testvm class=AppVM state=Running\n'
  304. self.app.expected_calls[
  305. ('testvm', 'admin.vm.volume.List', None, None)] = \
  306. b'0\x00root\nprivate\n'
  307. self.app.expected_calls[
  308. ('testvm', 'admin.vm.volume.Set.revisions_to_keep', 'private',
  309. b'3')] = b'0\x00'
  310. self.assertEqual(0,
  311. qubesadmin.tools.qvm_volume.main(
  312. ['set', 'testvm:private', 'revisions_to_keep', '3'],
  313. app=self.app))
  314. self.assertAllCalled()
  315. def test_031_set_rw(self):
  316. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  317. b'0\x00testvm class=AppVM state=Running\n'
  318. self.app.expected_calls[
  319. ('testvm', 'admin.vm.volume.List', None, None)] = \
  320. b'0\x00root\nprivate\n'
  321. self.app.expected_calls[
  322. ('testvm', 'admin.vm.volume.Set.rw', 'private',
  323. b'True')] = b'0\x00'
  324. self.assertEqual(0,
  325. qubesadmin.tools.qvm_volume.main(
  326. ['set', 'testvm:private', 'rw', 'True'],
  327. app=self.app))
  328. self.assertAllCalled()
  329. def test_032_set_invalid(self):
  330. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  331. b'0\x00testvm class=AppVM state=Running\n'
  332. self.app.expected_calls[
  333. ('testvm', 'admin.vm.volume.List', None, None)] = \
  334. b'0\x00root\nprivate\n'
  335. self.assertNotEqual(0,
  336. qubesadmin.tools.qvm_volume.main(
  337. ['set', 'testvm:private', 'invalid', 'True'],
  338. app=self.app))
  339. self.assertAllCalled()
  340. def test_040_info(self):
  341. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  342. b'0\x00testvm class=AppVM state=Running\n'
  343. self.app.expected_calls[
  344. ('testvm', 'admin.vm.volume.List', None, None)] = \
  345. b'0\x00root\nprivate\n'
  346. self.app.expected_calls[
  347. ('testvm', 'admin.vm.volume.Info', 'private', None)] = \
  348. b'0\x00pool=lvm\n' \
  349. b'vid=qubes_dom0/vm-testvm-private\n' \
  350. b'size=2147483648\n' \
  351. b'usage=10000000\n' \
  352. b'rw=True\n' \
  353. b'source=\n' \
  354. b'save_on_stop=True\n' \
  355. b'snap_on_start=False\n' \
  356. b'revisions_to_keep=3\n' \
  357. b'is_outdated=False\n'
  358. self.app.expected_calls[
  359. ('testvm', 'admin.vm.volume.ListSnapshots', 'private', None)] = \
  360. b'0\x00200101010000\n200201010000\n200301010000\n'
  361. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  362. self.assertEqual(0,
  363. qubesadmin.tools.qvm_volume.main(['info', 'testvm:private'],
  364. app=self.app))
  365. output = stdout.getvalue()
  366. # travis...
  367. output = output.replace('\nsource\n', '\nsource \n')
  368. self.assertEqual(output,
  369. 'pool lvm\n'
  370. 'vid qubes_dom0/vm-testvm-private\n'
  371. 'rw True\n'
  372. 'source \n'
  373. 'save_on_stop True\n'
  374. 'snap_on_start False\n'
  375. 'size 2147483648\n'
  376. 'usage 10000000\n'
  377. 'revisions_to_keep 3\n'
  378. 'is_outdated False\n'
  379. 'Available revisions (for revert):\n'
  380. ' 200101010000\n'
  381. ' 200201010000\n'
  382. ' 200301010000\n')
  383. self.assertAllCalled()
  384. def test_041_info_no_revisions(self):
  385. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  386. b'0\x00testvm class=AppVM state=Running\n'
  387. self.app.expected_calls[
  388. ('testvm', 'admin.vm.volume.List', None, None)] = \
  389. b'0\x00root\nprivate\n'
  390. self.app.expected_calls[
  391. ('testvm', 'admin.vm.volume.Info', 'root', None)] = \
  392. b'0\x00pool=lvm\n' \
  393. b'vid=qubes_dom0/vm-testvm-root\n' \
  394. b'size=2147483648\n' \
  395. b'usage=10000000\n' \
  396. b'rw=True\n' \
  397. b'source=qubes_dom0/vm-fedora-26-root\n' \
  398. b'save_on_stop=False\n' \
  399. b'snap_on_start=True\n' \
  400. b'revisions_to_keep=0\n' \
  401. b'is_outdated=False\n'
  402. self.app.expected_calls[
  403. ('testvm', 'admin.vm.volume.ListSnapshots', 'root', None)] = \
  404. b'0\x00'
  405. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  406. self.assertEqual(0,
  407. qubesadmin.tools.qvm_volume.main(['info', 'testvm:root'],
  408. app=self.app))
  409. self.assertEqual(stdout.getvalue(),
  410. 'pool lvm\n'
  411. 'vid qubes_dom0/vm-testvm-root\n'
  412. 'rw True\n'
  413. 'source qubes_dom0/vm-fedora-26-root\n'
  414. 'save_on_stop False\n'
  415. 'snap_on_start True\n'
  416. 'size 2147483648\n'
  417. 'usage 10000000\n'
  418. 'revisions_to_keep 0\n'
  419. 'is_outdated False\n'
  420. 'Available revisions (for revert): none\n')
  421. self.assertAllCalled()
  422. def test_042_info_single_prop(self):
  423. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  424. b'0\x00testvm class=AppVM state=Running\n'
  425. self.app.expected_calls[
  426. ('testvm', 'admin.vm.volume.List', None, None)] = \
  427. b'0\x00root\nprivate\n'
  428. self.app.expected_calls[
  429. ('testvm', 'admin.vm.volume.Info', 'root', None)] = \
  430. b'0\x00pool=lvm\n' \
  431. b'vid=qubes_dom0/vm-testvm-root\n' \
  432. b'size=2147483648\n' \
  433. b'usage=10000000\n' \
  434. b'rw=True\n' \
  435. b'source=qubes_dom0/vm-fedora-26-root\n' \
  436. b'save_on_stop=False\n' \
  437. b'snap_on_start=True\n' \
  438. b'revisions_to_keep=0\n' \
  439. b'is_outdated=False\n'
  440. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  441. self.assertEqual(0,
  442. qubesadmin.tools.qvm_volume.main(
  443. ['info', 'testvm:root', 'usage'],
  444. app=self.app))
  445. self.assertEqual(stdout.getvalue(), '10000000\n')
  446. self.assertAllCalled()
  447. def test_043_info_revisions_only(self):
  448. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  449. b'0\x00testvm class=AppVM state=Running\n'
  450. self.app.expected_calls[
  451. ('testvm', 'admin.vm.volume.List', None, None)] = \
  452. b'0\x00root\nprivate\n'
  453. self.app.expected_calls[
  454. ('testvm', 'admin.vm.volume.ListSnapshots', 'private', None)] = \
  455. b'0\x00200101010000\n200201010000\n200301010000\n'
  456. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  457. self.assertEqual(0,
  458. qubesadmin.tools.qvm_volume.main(
  459. ['info', 'testvm:private', 'revisions'],
  460. app=self.app))
  461. self.assertEqual(stdout.getvalue(),
  462. '200101010000\n'
  463. '200201010000\n'
  464. '200301010000\n')
  465. self.assertAllCalled()
  466. def test_050_import_file(self):
  467. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  468. b'0\x00testvm class=AppVM state=Running\n'
  469. self.app.expected_calls[
  470. ('testvm', 'admin.vm.volume.List', None, None)] = \
  471. b'0\x00root\nprivate\n'
  472. self.app.expected_calls[
  473. ('testvm', 'admin.vm.volume.ImportWithSize', 'private', b'9\ntest-data')] = \
  474. b'0\x00'
  475. with tempfile.NamedTemporaryFile() as input_file:
  476. input_file.write(b'test-data')
  477. input_file.seek(0)
  478. input_file.flush()
  479. self.assertEqual(0,
  480. qubesadmin.tools.qvm_volume.main(
  481. ['import', 'testvm:private', input_file.name],
  482. app=self.app))
  483. self.assertAllCalled()
  484. def test_051_import_stdin(self):
  485. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  486. b'0\x00testvm class=AppVM state=Running\n'
  487. self.app.expected_calls[
  488. ('testvm', 'admin.vm.volume.List', None, None)] = \
  489. b'0\x00root\nprivate\n'
  490. self.app.expected_calls[
  491. ('testvm', 'admin.vm.volume.ImportWithSize', 'private', b'9\ntest-data')] = \
  492. b'0\x00'
  493. with tempfile.NamedTemporaryFile() as input_file:
  494. input_file.write(b'test-data')
  495. input_file.seek(0)
  496. with unittest.mock.patch('sys.stdin') as mock_stdin:
  497. mock_stdin.buffer = input_file
  498. self.assertEqual(0,
  499. qubesadmin.tools.qvm_volume.main(
  500. ['import', 'testvm:private', '-'],
  501. app=self.app))
  502. self.assertAllCalled()
  503. def test_052_import_file_size(self):
  504. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  505. b'0\x00testvm class=AppVM state=Running\n'
  506. self.app.expected_calls[
  507. ('testvm', 'admin.vm.volume.List', None, None)] = \
  508. b'0\x00root\nprivate\n'
  509. self.app.expected_calls[
  510. ('testvm', 'admin.vm.volume.ImportWithSize', 'private', b'512\ntest-data')] = \
  511. b'0\x00'
  512. with tempfile.NamedTemporaryFile() as input_file:
  513. input_file.write(b'test-data')
  514. input_file.seek(0)
  515. input_file.flush()
  516. self.assertEqual(0,
  517. qubesadmin.tools.qvm_volume.main(
  518. ['import', '--size=512', 'testvm:private', input_file.name],
  519. app=self.app))
  520. self.assertAllCalled()
  521. def test_053_import_file_noresize(self):
  522. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  523. b'0\x00testvm class=AppVM state=Running\n'
  524. self.app.expected_calls[
  525. ('testvm', 'admin.vm.volume.List', None, None)] = \
  526. b'0\x00root\nprivate\n'
  527. self.app.expected_calls[
  528. ('testvm', 'admin.vm.volume.Import', 'private', b'test-data')] = \
  529. b'0\x00'
  530. with tempfile.NamedTemporaryFile() as input_file:
  531. input_file.write(b'test-data')
  532. input_file.seek(0)
  533. input_file.flush()
  534. self.assertEqual(0,
  535. qubesadmin.tools.qvm_volume.main(
  536. ['import', '--no-resize', 'testvm:private', input_file.name],
  537. app=self.app))
  538. self.assertAllCalled()
  539. def test_053_import_file_matching_size(self):
  540. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  541. b'0\x00testvm class=AppVM state=Running\n'
  542. self.app.expected_calls[
  543. ('testvm', 'admin.vm.volume.List', None, None)] = \
  544. b'0\x00root\nprivate\n'
  545. self.app.expected_calls[
  546. ('testvm', 'admin.vm.volume.ImportWithSize', 'private', b'9\ntest-data')] = \
  547. b'0\x00'
  548. with tempfile.NamedTemporaryFile() as input_file:
  549. input_file.write(b'test-data')
  550. input_file.seek(0)
  551. input_file.flush()
  552. self.assertEqual(0,
  553. qubesadmin.tools.qvm_volume.main(
  554. ['import', 'testvm:private', input_file.name],
  555. app=self.app))
  556. self.assertAllCalled()