properties.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. class TC_01_SpecialCases(qubesadmin.tests.vm.VMTestCase):
  145. def test_000_get_name(self):
  146. # should not make any mgmt call
  147. self.assertEqual(self.vm.name, 'test-vm')
  148. self.assertAllCalled()
  149. def test_001_set_name(self):
  150. # but this one should still do a call
  151. self.app.expected_calls[
  152. ('test-vm', 'admin.vm.property.Set', 'name', b'test-vm2')] = \
  153. b'0\x00'
  154. self.vm.name = 'test-vm2'
  155. # here should be no separate mgmt.vm.property.Get+name call
  156. self.assertEqual(self.vm.name, 'test-vm2')
  157. self.assertAllCalled()
  158. # check if VM list cache was cleared
  159. self.app.actual_calls = []
  160. del self.app.expected_calls[
  161. ('test-vm', 'admin.vm.property.Set', 'name', b'test-vm2')]
  162. vm = self.app.domains['test-vm']
  163. self.assertAllCalled()
  164. def test_010_power_state_running(self):
  165. self.app.expected_calls[('test-vm', 'admin.vm.List', None, None)] = \
  166. b'0\x00test-vm class=AppVM state=Running\n'
  167. self.assertEqual(self.vm.get_power_state(), 'Running')
  168. self.assertTrue(self.vm.is_running())
  169. self.assertFalse(self.vm.is_halted())
  170. self.assertFalse(self.vm.is_paused())
  171. def test_011_power_state_paused(self):
  172. self.app.expected_calls[('test-vm', 'admin.vm.List', None, None)] = \
  173. b'0\x00test-vm class=AppVM state=Paused\n'
  174. self.assertEqual(self.vm.get_power_state(), 'Paused')
  175. self.assertTrue(self.vm.is_running())
  176. self.assertFalse(self.vm.is_halted())
  177. self.assertTrue(self.vm.is_paused())
  178. def test_012_power_state_halted(self):
  179. self.app.expected_calls[('test-vm', 'admin.vm.List', None, None)] = \
  180. b'0\x00test-vm class=AppVM state=Halted\n'
  181. self.assertEqual(self.vm.get_power_state(), 'Halted')
  182. self.assertFalse(self.vm.is_running())
  183. self.assertTrue(self.vm.is_halted())
  184. self.assertFalse(self.vm.is_paused())
  185. def test_012_power_state_transient(self):
  186. self.app.expected_calls[('test-vm', 'admin.vm.List', None, None)] = \
  187. b'0\x00test-vm class=AppVM state=Transient\n'
  188. self.assertEqual(self.vm.get_power_state(), 'Transient')
  189. self.assertTrue(self.vm.is_running())
  190. self.assertFalse(self.vm.is_halted())
  191. self.assertFalse(self.vm.is_paused())