features.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 General Public License as published by
  10. # the Free Software Foundation; either version 2 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 General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, see <http://www.gnu.org/licenses/>.
  20. import qubesadmin.features
  21. import qubesadmin.tests
  22. class TC_00_Features(qubesadmin.tests.QubesTestCase):
  23. def setUp(self):
  24. super(TC_00_Features, self).setUp()
  25. self.app.expected_calls[('dom0', 'admin.vm.List', None, None)] = \
  26. b'0\0test-vm class=AppVM state=Running\n' \
  27. b'test-vm2 class=AppVM state=Running\n' \
  28. b'test-vm3 class=AppVM state=Running\n'
  29. self.vm = self.app.domains['test-vm']
  30. self.features = qubesadmin.features.Features(self.vm)
  31. def test_000_list(self):
  32. self.app.expected_calls[
  33. ('test-vm', 'admin.vm.feature.List', None, None)] = \
  34. b'0\0feature1\nfeature2\n'
  35. self.assertEqual(sorted(self.vm.features.keys()),
  36. ['feature1', 'feature2'])
  37. self.assertAllCalled()
  38. def test_010_get(self):
  39. self.app.expected_calls[
  40. ('test-vm', 'admin.vm.feature.Get', 'feature1', None)] = \
  41. b'0\0value1'
  42. self.assertEqual(self.vm.features['feature1'], 'value1')
  43. self.assertAllCalled()
  44. def test_011_get_none(self):
  45. self.app.expected_calls[
  46. ('test-vm', 'admin.vm.feature.Get', 'feature1', None)] = \
  47. b'2\x00QubesFeatureNotFoundError\x00\x00feature1\x00'
  48. with self.assertRaises(KeyError):
  49. value = self.vm.features['feature1']
  50. self.assertAllCalled()
  51. def test_012_get_none(self):
  52. self.app.expected_calls[
  53. ('test-vm', 'admin.vm.feature.Get', 'feature1', None)] = \
  54. b'2\x00QubesFeatureNotFoundError\x00\x00feature1\x00'
  55. with self.assertRaises(KeyError):
  56. self.vm.features.get('feature1')
  57. self.assertAllCalled()
  58. def test_013_get_default(self):
  59. self.app.expected_calls[
  60. ('test-vm', 'admin.vm.feature.Get', 'feature1', None)] = \
  61. b'2\x00QubesFeatureNotFoundError\x00\x00feature1\x00'
  62. self.assertEqual(self.vm.features.get('feature1', 'other'), 'other')
  63. self.assertAllCalled()
  64. def test_020_set(self):
  65. self.app.expected_calls[
  66. ('test-vm', 'admin.vm.feature.Set', 'feature1', b'value')] = \
  67. b'0\0'
  68. self.vm.features['feature1'] = 'value'
  69. self.assertAllCalled()
  70. def test_021_set_bool(self):
  71. self.app.expected_calls[
  72. ('test-vm', 'admin.vm.feature.Set', 'feature1', b'1')] = \
  73. b'0\0'
  74. self.vm.features['feature1'] = True
  75. self.assertAllCalled()
  76. def test_022_set_bool_false(self):
  77. self.app.expected_calls[
  78. ('test-vm', 'admin.vm.feature.Set', 'feature1', b'')] = \
  79. b'0\0'
  80. self.vm.features['feature1'] = False
  81. self.assertAllCalled()