qvm_pool.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_pool
  23. class TC_00_qvm_pool(qubesadmin.tests.QubesTestCase):
  24. def test_000_list(self):
  25. self.app.expected_calls[('dom0', 'admin.pool.List', None, None)] = \
  26. b'0\x00pool-file\npool-lvm\n'
  27. self.app.expected_calls[
  28. ('dom0', 'admin.pool.Info', 'pool-file', None)] = \
  29. b'0\x00driver=file\ndir_path=/var/lib/qubes\n'
  30. self.app.expected_calls[
  31. ('dom0', 'admin.pool.Info', 'pool-lvm', None)] = \
  32. b'0\x00driver=lvm\nvolume_group=qubes_dom0\nthin_pool=pool00\n'
  33. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  34. self.assertEqual(0,
  35. qubesadmin.tools.qvm_pool.main(['list'], app=self.app))
  36. self.assertEqual(stdout.getvalue(),
  37. 'NAME DRIVER\n'
  38. 'pool-file file\n'
  39. 'pool-lvm lvm\n')
  40. self.assertAllCalled()
  41. def test_010_drivers(self):
  42. self.app.expected_calls[
  43. ('dom0', 'admin.pool.ListDrivers', None, None)] = \
  44. b'0\x00file dir_path revisions_to_keep\n' \
  45. b'lvm volume_group thin_pool revisions_to_keep\n'
  46. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  47. self.assertEqual(0,
  48. qubesadmin.tools.qvm_pool.main(['drivers'], app=self.app))
  49. self.assertEqual(stdout.getvalue(),
  50. 'DRIVER OPTIONS\n'
  51. 'file dir_path, revisions_to_keep\n'
  52. 'lvm volume_group, thin_pool, revisions_to_keep\n'
  53. )
  54. self.assertAllCalled()
  55. def test_020_add(self):
  56. self.app.expected_calls[
  57. ('dom0', 'admin.pool.Add', 'file',
  58. b'name=test-pool\ndir_path=/some/path\n')] = b'0\x00'
  59. self.assertEqual(0,
  60. qubesadmin.tools.qvm_pool.main(
  61. ['add', 'test-pool', 'file', '-o', 'dir_path=/some/path'],
  62. app=self.app))
  63. self.assertAllCalled()
  64. def test_021_add_multiple(self):
  65. self.app.expected_calls[
  66. ('dom0', 'admin.pool.Add', 'file',
  67. b'name=test-pool\ndir_path=/some/path\nrw=True\n')] = b'0\x00'
  68. self.assertEqual(0,
  69. qubesadmin.tools.qvm_pool.main(
  70. ['add', 'test-pool', 'file', '-o', 'dir_path=/some/path',
  71. '-o', 'rw=True'],
  72. app=self.app))
  73. self.assertAllCalled()
  74. def test_030_remove(self):
  75. self.app.expected_calls[
  76. ('dom0', 'admin.pool.Remove', 'test-pool', None)] = b'0\x00'
  77. self.assertEqual(0,
  78. qubesadmin.tools.qvm_pool.main(['remove', 'test-pool'],
  79. app=self.app))
  80. self.assertAllCalled()
  81. def test_031_remove_multiple(self):
  82. self.app.expected_calls[
  83. ('dom0', 'admin.pool.Remove', 'test-pool-1', None)] = b'0\x00'
  84. self.app.expected_calls[
  85. ('dom0', 'admin.pool.Remove', 'test-pool-2', None)] = b'0\x00'
  86. self.assertEqual(0,
  87. qubesadmin.tools.qvm_pool.main(
  88. ['remove', 'test-pool-1', 'test-pool-2'],
  89. app=self.app))
  90. self.assertAllCalled()
  91. def test_040_info(self):
  92. self.app.expected_calls[('dom0', 'admin.pool.List', None, None)] = \
  93. b'0\x00pool-file\npool-lvm\n'
  94. self.app.expected_calls[
  95. ('dom0', 'admin.pool.Info', 'pool-lvm', None)] = \
  96. b'0\x00driver=lvm\nvolume_group=qubes_dom0\nthin_pool=pool00\n'
  97. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  98. self.assertEqual(0,
  99. qubesadmin.tools.qvm_pool.main(['info', 'pool-lvm'],
  100. app=self.app))
  101. self.assertEqual(stdout.getvalue(),
  102. 'name pool-lvm\n'
  103. 'driver lvm\n'
  104. 'thin_pool pool00\n'
  105. 'volume_group qubes_dom0\n'
  106. )
  107. self.assertAllCalled()
  108. def test_050_set(self):
  109. self.app.expected_calls[('dom0', 'admin.pool.List', None, None)] = \
  110. b'0\x00pool-file\npool-lvm\n'
  111. self.app.expected_calls[
  112. ('dom0', 'admin.pool.Set.revisions_to_keep', 'pool-lvm', b'2')] = \
  113. b'0\x00'
  114. self.assertEqual(0,
  115. qubesadmin.tools.qvm_pool.main(['set', 'pool-lvm', '-o',
  116. 'revisions_to_keep=2'],
  117. app=self.app))
  118. self.assertAllCalled()
  119. def test_051_set_invalid(self):
  120. self.app.expected_calls[('dom0', 'admin.pool.List', None, None)] = \
  121. b'0\x00pool-file\npool-lvm\n'
  122. with self.assertRaises(SystemExit) as e:
  123. qubesadmin.tools.qvm_pool.main(
  124. ['set', 'pool-lvm', '-o', 'prop=1'],
  125. app=self.app)
  126. self.assertEqual(e.exception.code, 1)
  127. self.assertAllCalled()