qvm_pool.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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(['-l'], 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_list_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(['--help-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_030_remove(self):
  65. self.app.expected_calls[
  66. ('dom0', 'admin.pool.Remove', 'test-pool', None)] = b'0\x00'
  67. self.assertEqual(0,
  68. qubesadmin.tools.qvm_pool.main(['--remove', 'test-pool'],
  69. app=self.app))
  70. self.assertAllCalled()
  71. def test_040_info(self):
  72. self.app.expected_calls[('dom0', 'admin.pool.List', None, None)] = \
  73. b'0\x00pool-file\npool-lvm\n'
  74. self.app.expected_calls[
  75. ('dom0', 'admin.pool.Info', 'pool-lvm', None)] = \
  76. b'0\x00driver=lvm\nvolume_group=qubes_dom0\nthin_pool=pool00\n'
  77. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  78. self.assertEqual(0,
  79. qubesadmin.tools.qvm_pool.main(['-i', 'pool-lvm'],
  80. app=self.app))
  81. self.assertEqual(stdout.getvalue(),
  82. 'name pool-lvm\n'
  83. 'driver lvm\n'
  84. 'thin_pool pool00\n'
  85. 'volume_group qubes_dom0\n'
  86. )
  87. self.assertAllCalled()
  88. def test_050_set(self):
  89. self.app.expected_calls[('dom0', 'admin.pool.List', None, None)] = \
  90. b'0\x00pool-file\npool-lvm\n'
  91. self.app.expected_calls[
  92. ('dom0', 'admin.pool.Set.revisions_to_keep', 'pool-lvm', b'2')] = \
  93. b'0\x00'
  94. self.assertEqual(0,
  95. qubesadmin.tools.qvm_pool.main(['-s', 'pool-lvm', '-o',
  96. 'revisions_to_keep=2'],
  97. app=self.app))
  98. self.assertAllCalled()
  99. def test_051_set_invalid(self):
  100. self.app.expected_calls[('dom0', 'admin.pool.List', None, None)] = \
  101. b'0\x00pool-file\npool-lvm\n'
  102. with self.assertRaises(SystemExit) as e:
  103. qubesadmin.tools.qvm_pool.main(
  104. ['-s', 'pool-lvm', '-o', 'prop=1'],
  105. app=self.app)
  106. self.assertEqual(e.exception.code, 2)
  107. self.assertAllCalled()