properties.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 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.vm
  21. class TC_00_Properties(qubesadmin.tests.vm.VMTestCase):
  22. def test_000_list(self):
  23. self.app.expected_calls[
  24. ('test-vm', 'admin.vm.property.List', None, None)] = \
  25. b'0\x00prop1\nprop2\n'
  26. self.assertEqual(
  27. self.vm.property_list(),
  28. ['prop1', 'prop2'])
  29. self.assertAllCalled()
  30. def test_001_get_str(self):
  31. self.app.expected_calls[
  32. ('test-vm', 'admin.vm.property.Get', 'prop1', None)] = \
  33. b'0\x00default=False type=str value'
  34. self.assertEqual(self.vm.prop1, 'value')
  35. self.assertAllCalled()
  36. def test_002_get_int(self):
  37. self.app.expected_calls[
  38. ('test-vm', 'admin.vm.property.Get', 'prop1', None)] = \
  39. b'0\x00default=False type=int 123'
  40. self.assertEqual(self.vm.prop1, 123)
  41. self.assertAllCalled()
  42. def test_003_get_bool(self):
  43. self.app.expected_calls[
  44. ('test-vm', 'admin.vm.property.Get', 'prop1', None)] = \
  45. b'0\x00default=False type=bool True'
  46. self.assertEqual(self.vm.prop1, True)
  47. self.assertAllCalled()
  48. def test_004_get_vm(self):
  49. self.app.expected_calls[
  50. ('test-vm', 'admin.vm.property.Get', 'prop1', None)] = \
  51. b'0\x00default=False type=vm test-vm'
  52. self.assertIsInstance(self.vm.prop1, qubesadmin.vm.QubesVM)
  53. self.assertEqual(self.vm.prop1.name, 'test-vm')
  54. self.assertAllCalled()
  55. def test_005_get_none_vm(self):
  56. self.app.expected_calls[
  57. ('test-vm', 'admin.vm.property.Get', 'prop1', None)] = \
  58. b'0\x00default=False type=vm '
  59. self.assertEqual(self.vm.prop1, None)
  60. self.assertAllCalled()
  61. def test_006_get_none_bool(self):
  62. self.app.expected_calls[
  63. ('test-vm', 'admin.vm.property.Get', 'prop1', None)] = \
  64. b'0\x00default=False type=bool '
  65. with self.assertRaises(AttributeError):
  66. self.vm.prop1
  67. self.assertAllCalled()
  68. def test_007_get_none_int(self):
  69. self.app.expected_calls[
  70. ('test-vm', 'admin.vm.property.Get', 'prop1', None)] = \
  71. b'0\x00default=False type=int '
  72. with self.assertRaises(AttributeError):
  73. self.vm.prop1
  74. self.assertAllCalled()
  75. def test_008_get_none_str(self):
  76. self.app.expected_calls[
  77. ('test-vm', 'admin.vm.property.Get', 'prop1', None)] = \
  78. b'0\x00default=False type=str '
  79. self.assertEqual(self.vm.prop1, '')
  80. self.assertAllCalled()
  81. def test_010_get_default(self):
  82. self.app.expected_calls[
  83. ('test-vm', 'admin.vm.property.Get', 'prop1', None)] = \
  84. b'0\x00default=False type=str value'
  85. self.assertEqual(self.vm.property_is_default('prop1'), False)
  86. self.assertAllCalled()
  87. def test_011_get_default(self):
  88. self.app.expected_calls[
  89. ('test-vm', 'admin.vm.property.Get', 'prop1', None)] = \
  90. b'0\x00default=True type=str value'
  91. self.assertEqual(self.vm.property_is_default('prop1'), True)
  92. self.assertAllCalled()
  93. def test_012_get_invalid(self):
  94. self.app.expected_calls[
  95. ('test-vm', 'admin.vm.property.Get', 'invalid', None)] = \
  96. b'2\x00QubesNoSuchPropertyError\x00\x00Invalid property ' \
  97. b'\'invalid\' on test-vm\x00'
  98. with self.assertRaises(qubesadmin.exc.QubesNoSuchPropertyError):
  99. self.vm.invalid
  100. self.assertFalse(hasattr(self.vm, 'invalid'))
  101. self.assertAllCalled()
  102. def test_020_set_str(self):
  103. self.app.expected_calls[
  104. ('test-vm', 'admin.vm.property.Set', 'prop1', b'value')] = \
  105. b'0\x00'
  106. self.vm.prop1 = 'value'
  107. self.assertAllCalled()
  108. def test_021_set_int(self):
  109. self.app.expected_calls[
  110. ('test-vm', 'admin.vm.property.Set', 'prop1', b'123')] = \
  111. b'0\x00'
  112. self.vm.prop1 = 123
  113. self.assertAllCalled()
  114. def test_022_set_bool(self):
  115. self.app.expected_calls[
  116. ('test-vm', 'admin.vm.property.Set', 'prop1', b'True')] = \
  117. b'0\x00'
  118. self.vm.prop1 = True
  119. self.assertAllCalled()
  120. def test_023_set_vm(self):
  121. self.app.expected_calls[
  122. ('test-vm', 'admin.vm.property.Set', 'prop1', b'test-vm')] = \
  123. b'0\x00'
  124. self.vm.prop1 = self.vm
  125. self.assertAllCalled()
  126. def test_024_set_none(self):
  127. self.app.expected_calls[
  128. ('test-vm', 'admin.vm.property.Set', 'prop1', b'')] = \
  129. b'0\x00'
  130. self.vm.prop1 = None
  131. self.assertAllCalled()
  132. def test_030_reset(self):
  133. self.app.expected_calls[
  134. ('test-vm', 'admin.vm.property.Reset', 'prop1', None)] = \
  135. b'0\x00'
  136. self.vm.prop1 = qubesadmin.DEFAULT
  137. self.assertAllCalled()
  138. def test_031_reset(self):
  139. self.app.expected_calls[
  140. ('test-vm', 'admin.vm.property.Reset', 'prop1', None)] = \
  141. b'0\x00'
  142. del self.vm.prop1
  143. self.assertAllCalled()
  144. def test_040_get_default(self):
  145. self.app.expected_calls[
  146. ('test-vm', 'admin.vm.property.GetDefault', 'prop1', None)] = \
  147. b'0\x00type=str some value'
  148. default_value = self.vm.property_get_default('prop1')
  149. self.assertEqual(default_value, 'some value')
  150. self.assertAllCalled()
  151. def test_041_get_default_int(self):
  152. self.app.expected_calls[
  153. ('test-vm', 'admin.vm.property.GetDefault', 'prop1', None)] = \
  154. b'0\x00type=int 42'
  155. default_value = self.vm.property_get_default('prop1')
  156. self.assertEqual(default_value, 42)
  157. self.assertAllCalled()
  158. def test_042_get_default_none(self):
  159. self.app.expected_calls[
  160. ('test-vm', 'admin.vm.property.GetDefault', 'prop1', None)] = \
  161. b'0\x00'
  162. with self.assertRaises(AttributeError):
  163. self.vm.property_get_default('prop1')
  164. self.assertAllCalled()
  165. class TC_01_SpecialCases(qubesadmin.tests.vm.VMTestCase):
  166. def test_000_get_name(self):
  167. # should not make any mgmt call
  168. self.assertEqual(self.vm.name, 'test-vm')
  169. self.assertAllCalled()
  170. def test_001_set_name(self):
  171. # but this one should still do a call
  172. self.app.expected_calls[
  173. ('test-vm', 'admin.vm.property.Set', 'name', b'test-vm2')] = \
  174. b'0\x00'
  175. self.vm.name = 'test-vm2'
  176. # here should be no separate mgmt.vm.property.Get+name call
  177. self.assertEqual(self.vm.name, 'test-vm2')
  178. self.assertAllCalled()
  179. # check if VM list cache was cleared
  180. self.app.actual_calls = []
  181. del self.app.expected_calls[
  182. ('test-vm', 'admin.vm.property.Set', 'name', b'test-vm2')]
  183. vm = self.app.domains['test-vm']
  184. self.assertAllCalled()
  185. def test_010_power_state_running(self):
  186. self.app.expected_calls[('test-vm', 'admin.vm.List', None, None)] = \
  187. b'0\x00test-vm class=AppVM state=Running\n'
  188. self.assertEqual(self.vm.get_power_state(), 'Running')
  189. self.assertTrue(self.vm.is_running())
  190. self.assertFalse(self.vm.is_halted())
  191. self.assertFalse(self.vm.is_paused())
  192. def test_011_power_state_paused(self):
  193. self.app.expected_calls[('test-vm', 'admin.vm.List', None, None)] = \
  194. b'0\x00test-vm class=AppVM state=Paused\n'
  195. self.assertEqual(self.vm.get_power_state(), 'Paused')
  196. self.assertTrue(self.vm.is_running())
  197. self.assertFalse(self.vm.is_halted())
  198. self.assertTrue(self.vm.is_paused())
  199. def test_012_power_state_halted(self):
  200. self.app.expected_calls[('test-vm', 'admin.vm.List', None, None)] = \
  201. b'0\x00test-vm class=AppVM state=Halted\n'
  202. self.assertEqual(self.vm.get_power_state(), 'Halted')
  203. self.assertFalse(self.vm.is_running())
  204. self.assertTrue(self.vm.is_halted())
  205. self.assertFalse(self.vm.is_paused())
  206. def test_012_power_state_transient(self):
  207. self.app.expected_calls[('test-vm', 'admin.vm.List', None, None)] = \
  208. b'0\x00test-vm class=AppVM state=Transient\n'
  209. self.assertEqual(self.vm.get_power_state(), 'Transient')
  210. self.assertTrue(self.vm.is_running())
  211. self.assertFalse(self.vm.is_halted())
  212. self.assertFalse(self.vm.is_paused())