properties.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. # -*- encoding: utf-8 -*-
  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. def test_050_get_all(self):
  166. self.app.expected_calls[
  167. ('test-vm', 'admin.vm.property.GetAll', None, None)] = [
  168. b'0\x00name default=False type=str test-vm\n'
  169. b'debug default=True type=bool False\n'
  170. b'backup_timestamp default=True type=int \n'
  171. b'kernel default=True type=str 1.0\n'
  172. b'qid default=True type=int 3\n'
  173. b'kernelopts default=False type=str opt1\\nopt2\\nopt3\\\\opt4\n'
  174. b'klass default=True type=str AppVM\n', ]
  175. self.app.cache_enabled = True
  176. self.assertEqual(self.vm.name, 'test-vm')
  177. with self.assertRaises(AttributeError):
  178. self.vm.backup_timestamp
  179. self.assertEqual(self.vm.debug, False)
  180. self.assertEqual(self.vm.qid, 3)
  181. self.assertEqual(self.vm.kernelopts, 'opt1\nopt2\nopt3\\opt4')
  182. self.assertTrue(self.vm.property_is_default('kernel'))
  183. def test_051_get_all_fallback(self):
  184. self.app.expected_calls[
  185. ('test-vm', 'admin.vm.property.GetAll', None, None)] = [b'', ]
  186. self.app.expected_calls[
  187. ('test-vm', 'admin.vm.property.Get', 'qid', None)] = \
  188. b'0\x00default=True type=int 3'
  189. self.app.cache_enabled = True
  190. self.assertEqual(self.vm.qid, 3)
  191. # check if cached
  192. self.assertEqual(self.vm.qid, 3)
  193. self.assertAllCalled()
  194. class TC_01_SpecialCases(qubesadmin.tests.vm.VMTestCase):
  195. def test_000_get_name(self):
  196. # should not make any mgmt call
  197. self.assertEqual(self.vm.name, 'test-vm')
  198. self.assertAllCalled()
  199. def test_001_set_name(self):
  200. # but this one should still do a call
  201. self.app.expected_calls[
  202. ('test-vm', 'admin.vm.property.Set', 'name', b'test-vm2')] = \
  203. b'0\x00'
  204. self.vm.name = 'test-vm2'
  205. # here should be no separate mgmt.vm.property.Get+name call
  206. self.assertEqual(self.vm.name, 'test-vm2')
  207. self.assertAllCalled()
  208. # check if VM list cache was cleared
  209. self.app.actual_calls = []
  210. del self.app.expected_calls[
  211. ('test-vm', 'admin.vm.property.Set', 'name', b'test-vm2')]
  212. vm = self.app.domains['test-vm']
  213. self.assertAllCalled()
  214. def test_010_power_state_running(self):
  215. self.app.expected_calls[('test-vm', 'admin.vm.CurrentState', None, None)] = \
  216. b'0\x00power_state=Running'
  217. self.assertEqual(self.vm.get_power_state(), 'Running')
  218. self.assertTrue(self.vm.is_running())
  219. self.assertFalse(self.vm.is_halted())
  220. self.assertFalse(self.vm.is_paused())
  221. def test_011_power_state_paused(self):
  222. self.app.expected_calls[('test-vm', 'admin.vm.CurrentState', None, None)] = \
  223. b'0\x00power_state=Paused'
  224. self.assertEqual(self.vm.get_power_state(), 'Paused')
  225. self.assertTrue(self.vm.is_running())
  226. self.assertFalse(self.vm.is_halted())
  227. self.assertTrue(self.vm.is_paused())
  228. def test_012_power_state_halted(self):
  229. self.app.expected_calls[('test-vm', 'admin.vm.CurrentState', None, None)] = \
  230. b'0\x00power_state=Halted'
  231. self.assertEqual(self.vm.get_power_state(), 'Halted')
  232. self.assertFalse(self.vm.is_running())
  233. self.assertTrue(self.vm.is_halted())
  234. self.assertFalse(self.vm.is_paused())
  235. def test_012_power_state_transient(self):
  236. self.app.expected_calls[('test-vm', 'admin.vm.CurrentState', None, None)] = \
  237. b'0\x00power_state=Transient'
  238. self.assertEqual(self.vm.get_power_state(), 'Transient')
  239. self.assertTrue(self.vm.is_running())
  240. self.assertFalse(self.vm.is_halted())
  241. self.assertFalse(self.vm.is_paused())
  242. def test_015_mem(self):
  243. self.app.expected_calls[('test-vm', 'admin.vm.CurrentState', None, None)] = \
  244. b'0\x00mem=1234'
  245. self.assertEqual(self.vm.get_mem(), 1234)