qvm_volume.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. # -*- encoding: utf8 -*-
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2017 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation; either version 2.1 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. import qubesadmin.tests
  21. import qubesadmin.tests.tools
  22. import qubesadmin.tools.qvm_volume
  23. class TC_00_qvm_volume(qubesadmin.tests.QubesTestCase):
  24. def setup_expected_calls_for_list(self, vms=('vm1', 'sys-net')):
  25. self.app.expected_calls[
  26. ('dom0', 'admin.vm.List', None, None)] = \
  27. b'0\x00vm1 class=AppVM state=Running\n' \
  28. b'sys-net class=AppVM state=Running\n'
  29. for vm in vms:
  30. for vol in ('root', 'private'):
  31. self.app.expected_calls[
  32. (vm, 'admin.vm.volume.Info', vol, None)] = \
  33. b'0\x00' + \
  34. (b'pool=pool-file\n' if vol == 'root' else
  35. b'pool=other-pool\n') + \
  36. b'vid=' + vm.encode() + b'-' + vol.encode() + b'\n' \
  37. b'size=10737418240\n'
  38. self.app.expected_calls[
  39. (vm, 'admin.vm.volume.ListSnapshots', vol, None)] = \
  40. b'0\x00snap1\n' if vol == 'private' else b'0\x00'
  41. self.app.expected_calls[
  42. (vm, 'admin.vm.volume.List', None, None)] = \
  43. b'0\x00root\nprivate\n'
  44. def test_000_list(self):
  45. self.setup_expected_calls_for_list()
  46. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  47. self.assertEqual(0,
  48. qubesadmin.tools.qvm_volume.main(['ls'], app=self.app))
  49. self.assertEqual(stdout.getvalue(),
  50. 'POOL:VOLUME VMNAME VOLUME_NAME '
  51. 'REVERT_POSSIBLE\n'
  52. 'other-pool:sys-net-private sys-net private Yes\n'
  53. 'other-pool:vm1-private vm1 private Yes\n'
  54. 'pool-file:sys-net-root sys-net root No\n'
  55. 'pool-file:vm1-root vm1 root No\n'
  56. )
  57. self.assertAllCalled()
  58. def test_001_list_domain(self):
  59. self.setup_expected_calls_for_list(vms=('vm1',))
  60. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  61. self.assertEqual(0,
  62. qubesadmin.tools.qvm_volume.main(['ls', 'vm1'],
  63. app=self.app))
  64. self.assertEqual(stdout.getvalue(),
  65. 'POOL:VOLUME VMNAME VOLUME_NAME REVERT_POSSIBLE\n'
  66. 'other-pool:vm1-private vm1 private Yes\n'
  67. 'pool-file:vm1-root vm1 root No\n'
  68. )
  69. self.assertAllCalled()
  70. def test_002_list_domain_pool(self):
  71. self.setup_expected_calls_for_list(vms=('vm1',))
  72. self.app.expected_calls[('dom0', 'admin.pool.List', None, None)] = \
  73. b'0\x00pool-file\nother-pool\n'
  74. del self.app.expected_calls[
  75. ('vm1', 'admin.vm.volume.ListSnapshots', 'private', None)]
  76. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  77. self.assertEqual(0,
  78. qubesadmin.tools.qvm_volume.main(
  79. ['ls', '-p', 'pool-file', 'vm1'],
  80. app=self.app))
  81. self.assertEqual(stdout.getvalue(),
  82. 'POOL:VOLUME VMNAME VOLUME_NAME REVERT_POSSIBLE\n'
  83. 'pool-file:vm1-root vm1 root No\n'
  84. )
  85. self.assertAllCalled()
  86. def test_003_list_pool(self):
  87. self.setup_expected_calls_for_list()
  88. self.app.expected_calls[('dom0', 'admin.pool.List', None, None)] = \
  89. b'0\x00pool-file\nother-pool\n'
  90. del self.app.expected_calls[
  91. ('vm1', 'admin.vm.volume.ListSnapshots', 'private', None)]
  92. del self.app.expected_calls[
  93. ('sys-net', 'admin.vm.volume.ListSnapshots', 'private', None)]
  94. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  95. self.assertEqual(0,
  96. qubesadmin.tools.qvm_volume.main(
  97. ['ls', '-p', 'pool-file'],
  98. app=self.app))
  99. self.assertEqual(stdout.getvalue(),
  100. 'POOL:VOLUME VMNAME VOLUME_NAME REVERT_POSSIBLE\n'
  101. 'pool-file:sys-net-root sys-net root No\n'
  102. 'pool-file:vm1-root vm1 root No\n'
  103. )
  104. self.assertAllCalled()
  105. def test_004_list_multiple_domains(self):
  106. self.setup_expected_calls_for_list(vms=('vm1', 'vm2'))
  107. self.app.expected_calls[
  108. ('dom0', 'admin.vm.List', None, None)] = \
  109. b'0\x00vm1 class=AppVM state=Running\n' \
  110. b'vm2 class=AppVM state=Running\n' \
  111. b'vm3 class=AppVM state=Running\n'
  112. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  113. self.assertEqual(0,
  114. qubesadmin.tools.qvm_volume.main(
  115. ['ls', 'vm1', 'vm2'], app=self.app))
  116. self.assertEqual(stdout.getvalue(),
  117. 'POOL:VOLUME VMNAME VOLUME_NAME REVERT_POSSIBLE\n'
  118. 'other-pool:vm1-private vm1 private Yes\n'
  119. 'other-pool:vm2-private vm2 private Yes\n'
  120. 'pool-file:vm1-root vm1 root No\n'
  121. 'pool-file:vm2-root vm2 root No\n'
  122. )
  123. self.assertAllCalled()
  124. def test_005_list_default_action(self):
  125. self.setup_expected_calls_for_list()
  126. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  127. self.assertEqual(0,
  128. qubesadmin.tools.qvm_volume.main([], app=self.app))
  129. self.assertEqual(stdout.getvalue(),
  130. 'POOL:VOLUME VMNAME VOLUME_NAME '
  131. 'REVERT_POSSIBLE\n'
  132. 'other-pool:sys-net-private sys-net private Yes\n'
  133. 'other-pool:vm1-private vm1 private Yes\n'
  134. 'pool-file:sys-net-root sys-net root No\n'
  135. 'pool-file:vm1-root vm1 root No\n'
  136. )
  137. self.assertAllCalled()
  138. def test_010_extend(self):
  139. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  140. b'0\x00testvm class=AppVM state=Running\n'
  141. self.app.expected_calls[
  142. ('testvm', 'admin.vm.volume.List', None, None)] = \
  143. b'0\x00root\nprivate\n'
  144. self.app.expected_calls[
  145. ('testvm', 'admin.vm.volume.Resize', 'private', b'10737418240')] = \
  146. b'0\x00'
  147. self.assertEqual(0,
  148. qubesadmin.tools.qvm_volume.main(
  149. ['extend', 'testvm:private', '10GiB'],
  150. app=self.app))
  151. self.assertAllCalled()
  152. def test_011_extend_error(self):
  153. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  154. b'0\x00testvm class=AppVM state=Running\n'
  155. self.app.expected_calls[
  156. ('testvm', 'admin.vm.volume.List', None, None)] = \
  157. b'0\x00root\nprivate\n'
  158. self.app.expected_calls[
  159. ('testvm', 'admin.vm.volume.Resize', 'private', b'1073741824')] = \
  160. b'2\x00StoragePoolException\x00\x00Failed to resize volume: ' \
  161. b'shrink not allowed\x00'
  162. with qubesadmin.tests.tools.StderrBuffer() as stderr:
  163. self.assertEqual(1,
  164. qubesadmin.tools.qvm_volume.main(
  165. ['extend', 'testvm:private', '1GiB'],
  166. app=self.app))
  167. self.assertIn('shrink not allowed', stderr.getvalue())
  168. self.assertAllCalled()
  169. def test_020_revert(self):
  170. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  171. b'0\x00testvm class=AppVM state=Running\n'
  172. self.app.expected_calls[
  173. ('testvm', 'admin.vm.volume.List', None, None)] = \
  174. b'0\x00root\nprivate\n'
  175. self.app.expected_calls[
  176. ('testvm', 'admin.vm.volume.ListSnapshots', 'private', None)] = \
  177. b'0\x00200101010000\n200201010000\n200301010000\n'
  178. self.app.expected_calls[
  179. ('testvm', 'admin.vm.volume.Revert', 'private', b'200301010000')] = \
  180. b'0\x00'
  181. self.assertEqual(0,
  182. qubesadmin.tools.qvm_volume.main(
  183. ['revert', 'testvm:private'],
  184. app=self.app))
  185. self.assertAllCalled()
  186. def test_021_revert_error(self):
  187. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  188. b'0\x00testvm class=AppVM state=Running\n'
  189. self.app.expected_calls[
  190. ('testvm', 'admin.vm.volume.List', None, None)] = \
  191. b'0\x00root\nprivate\n'
  192. self.app.expected_calls[
  193. ('testvm', 'admin.vm.volume.ListSnapshots', 'private', None)] = \
  194. b'0\x00200101010000\n200201010000\n200301010000\n'
  195. self.app.expected_calls[
  196. ('testvm', 'admin.vm.volume.Revert', 'private', b'200301010000')] = \
  197. b'2\x00StoragePoolException\x00\x00Failed to revert volume: ' \
  198. b'some error\x00'
  199. with qubesadmin.tests.tools.StderrBuffer() as stderr:
  200. self.assertEqual(1,
  201. qubesadmin.tools.qvm_volume.main(
  202. ['revert', 'testvm:private'],
  203. app=self.app))
  204. self.assertIn('some error', stderr.getvalue())
  205. self.assertAllCalled()
  206. def test_022_revert_no_snapshots(self):
  207. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  208. b'0\x00testvm class=AppVM state=Running\n'
  209. self.app.expected_calls[
  210. ('testvm', 'admin.vm.volume.List', None, None)] = \
  211. b'0\x00root\nprivate\n'
  212. self.app.expected_calls[
  213. ('testvm', 'admin.vm.volume.ListSnapshots', 'private', None)] = \
  214. b'0\x00'
  215. with qubesadmin.tests.tools.StderrBuffer() as stderr:
  216. self.assertEqual(1,
  217. qubesadmin.tools.qvm_volume.main(
  218. ['revert', 'testvm:private'],
  219. app=self.app))
  220. self.assertIn('No snapshots', stderr.getvalue())
  221. self.assertAllCalled()
  222. def test_023_revert_specific(self):
  223. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  224. b'0\x00testvm class=AppVM state=Running\n'
  225. self.app.expected_calls[
  226. ('testvm', 'admin.vm.volume.List', None, None)] = \
  227. b'0\x00root\nprivate\n'
  228. self.app.expected_calls[
  229. ('testvm', 'admin.vm.volume.Revert', 'private', b'20050101')] = \
  230. b'0\x00'
  231. self.assertEqual(0,
  232. qubesadmin.tools.qvm_volume.main(
  233. ['revert', 'testvm:private', '20050101'],
  234. app=self.app))
  235. self.assertAllCalled()