qvm_features.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.tools.qvm_features
  22. class TC_00_qvm_features(qubesadmin.tests.QubesTestCase):
  23. def test_000_list(self):
  24. self.app.expected_calls[
  25. ('dom0', 'admin.vm.List', None, None)] = \
  26. b'0\x00some-vm class=AppVM state=Running\n'
  27. self.app.expected_calls[
  28. ('some-vm', 'admin.vm.feature.List', None, None)] = \
  29. b'0\x00feature1\nfeature2\n'
  30. self.app.expected_calls[
  31. ('some-vm', 'admin.vm.feature.Get', 'feature1', None)] = \
  32. b'0\x00value1'
  33. self.app.expected_calls[
  34. ('some-vm', 'admin.vm.feature.Get', 'feature2', None)] = \
  35. b'0\x00value2 with spaces'
  36. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  37. self.assertEqual(
  38. qubesadmin.tools.qvm_features.main(['some-vm'], app=self.app),
  39. 0)
  40. self.assertEqual(stdout.getvalue(),
  41. 'feature1 value1\n'
  42. 'feature2 value2 with spaces\n')
  43. self.assertAllCalled()
  44. def test_001_set(self):
  45. self.app.expected_calls[
  46. ('dom0', 'admin.vm.List', None, None)] = \
  47. b'0\x00some-vm class=AppVM state=Running\n'
  48. self.app.expected_calls[
  49. ('some-vm', 'admin.vm.feature.Set',
  50. 'feature3', b'value of feature')] = b'0\x00'
  51. self.assertEqual(
  52. qubesadmin.tools.qvm_features.main(['some-vm', 'feature3',
  53. 'value of feature'],
  54. app=self.app),
  55. 0)
  56. self.assertAllCalled()
  57. def test_002_get(self):
  58. self.app.expected_calls[
  59. ('dom0', 'admin.vm.List', None, None)] = \
  60. b'0\x00some-vm class=AppVM state=Running\n'
  61. self.app.expected_calls[
  62. ('some-vm', 'admin.vm.feature.Get', 'feature3', None)] = \
  63. b'0\x00value2 with spaces'
  64. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  65. self.assertEqual(
  66. qubesadmin.tools.qvm_features.main(['some-vm', 'feature3'],
  67. app=self.app),
  68. 0)
  69. self.assertEqual(stdout.getvalue(),
  70. 'value2 with spaces\n')
  71. self.assertAllCalled()
  72. def test_003_del(self):
  73. self.app.expected_calls[
  74. ('dom0', 'admin.vm.List', None, None)] = \
  75. b'0\x00some-vm class=AppVM state=Running\n'
  76. self.app.expected_calls[
  77. ('some-vm', 'admin.vm.feature.Remove', 'feature4', None)] = \
  78. b'0\x00'
  79. with qubesadmin.tests.tools.StdoutBuffer() as stdout:
  80. self.assertEqual(
  81. qubesadmin.tools.qvm_features.main(['--unset', 'some-vm',
  82. 'feature4'],
  83. app=self.app),
  84. 0)
  85. self.assertEqual(stdout.getvalue(),
  86. '')
  87. self.assertAllCalled()