app.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. # pylint: disable=protected-access,pointless-statement
  2. #
  3. # The Qubes OS Project, https://www.qubes-os.org/
  4. #
  5. # Copyright (C) 2014-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  6. # Copyright (C) 2014-2015 Wojtek Porczyk <woju@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, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  22. import os
  23. import unittest.mock as mock
  24. import lxml.etree
  25. import qubes
  26. import qubes.events
  27. import qubes.tests
  28. import qubes.tests.init
  29. class TestApp(qubes.tests.TestEmitter):
  30. pass
  31. class TC_20_QubesHost(qubes.tests.QubesTestCase):
  32. sample_xc_domain_getinfo = [
  33. {'paused': 0, 'cpu_time': 243951379111104, 'ssidref': 0,
  34. 'hvm': 0, 'shutdown_reason': 255, 'dying': 0,
  35. 'mem_kb': 3733212, 'domid': 0, 'max_vcpu_id': 7,
  36. 'crashed': 0, 'running': 1, 'maxmem_kb': 3734236,
  37. 'shutdown': 0, 'online_vcpus': 8,
  38. 'handle': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  39. 'cpupool': 0, 'blocked': 0},
  40. {'paused': 0, 'cpu_time': 2849496569205, 'ssidref': 0,
  41. 'hvm': 0, 'shutdown_reason': 255, 'dying': 0,
  42. 'mem_kb': 303916, 'domid': 1, 'max_vcpu_id': 0,
  43. 'crashed': 0, 'running': 0, 'maxmem_kb': 308224,
  44. 'shutdown': 0, 'online_vcpus': 1,
  45. 'handle': [116, 174, 229, 207, 17, 1, 79, 39, 191, 37, 41,
  46. 186, 205, 158, 219, 8],
  47. 'cpupool': 0, 'blocked': 1},
  48. {'paused': 0, 'cpu_time': 249658663079978, 'ssidref': 0,
  49. 'hvm': 0, 'shutdown_reason': 255, 'dying': 0,
  50. 'mem_kb': 3782668, 'domid': 11, 'max_vcpu_id': 7,
  51. 'crashed': 0, 'running': 0, 'maxmem_kb': 3783692,
  52. 'shutdown': 0, 'online_vcpus': 8,
  53. 'handle': [169, 95, 55, 127, 140, 94, 79, 220, 186, 210,
  54. 117, 5, 148, 11, 185, 206],
  55. 'cpupool': 0, 'blocked': 1}]
  56. def setUp(self):
  57. super(TC_20_QubesHost, self).setUp()
  58. self.app = TestApp()
  59. self.app.vmm = mock.Mock()
  60. self.qubes_host = qubes.app.QubesHost(self.app)
  61. def test_000_get_vm_stats_single(self):
  62. self.app.vmm.configure_mock(**{
  63. 'xc.domain_getinfo.return_value': self.sample_xc_domain_getinfo
  64. })
  65. info_time, info = self.qubes_host.get_vm_stats()
  66. self.assertEqual(self.app.vmm.mock_calls, [
  67. ('xc.domain_getinfo', (0, 1024), {}),
  68. ])
  69. self.assertIsNotNone(info_time)
  70. expected_info = {
  71. 0: {
  72. 'cpu_time': 243951379111104//8,
  73. 'cpu_usage': 0,
  74. 'memory_kb': 3733212,
  75. },
  76. 1: {
  77. 'cpu_time': 2849496569205,
  78. 'cpu_usage': 0,
  79. 'memory_kb': 303916,
  80. },
  81. 11: {
  82. 'cpu_time': 249658663079978//8,
  83. 'cpu_usage': 0,
  84. 'memory_kb': 3782668,
  85. },
  86. }
  87. self.assertEqual(info, expected_info)
  88. def test_001_get_vm_stats_twice(self):
  89. self.app.vmm.configure_mock(**{
  90. 'xc.domain_getinfo.return_value': self.sample_xc_domain_getinfo
  91. })
  92. prev_time, prev_info = self.qubes_host.get_vm_stats()
  93. prev_time -= 1
  94. prev_info[0]['cpu_time'] -= 10**8
  95. prev_info[1]['cpu_time'] -= 10**9
  96. prev_info[11]['cpu_time'] -= 125 * 10**6
  97. info_time, info = self.qubes_host.get_vm_stats(prev_time, prev_info)
  98. self.assertIsNotNone(info_time)
  99. expected_info = {
  100. 0: {
  101. 'cpu_time': 243951379111104//8,
  102. 'cpu_usage': 9,
  103. 'memory_kb': 3733212,
  104. },
  105. 1: {
  106. 'cpu_time': 2849496569205,
  107. 'cpu_usage': 99,
  108. 'memory_kb': 303916,
  109. },
  110. 11: {
  111. 'cpu_time': 249658663079978//8,
  112. 'cpu_usage': 12,
  113. 'memory_kb': 3782668,
  114. },
  115. }
  116. self.assertEqual(info, expected_info)
  117. self.assertEqual(self.app.vmm.mock_calls, [
  118. ('xc.domain_getinfo', (0, 1024), {}),
  119. ('xc.domain_getinfo', (0, 1024), {}),
  120. ])
  121. def test_002_get_vm_stats_one_vm(self):
  122. self.app.vmm.configure_mock(**{
  123. 'xc.domain_getinfo.return_value': [self.sample_xc_domain_getinfo[1]]
  124. })
  125. vm = mock.Mock
  126. vm.xid = 1
  127. vm.name = 'somevm'
  128. info_time, info = self.qubes_host.get_vm_stats(only_vm=vm)
  129. self.assertIsNotNone(info_time)
  130. self.assertEqual(self.app.vmm.mock_calls, [
  131. ('xc.domain_getinfo', (1, 1), {}),
  132. ])
  133. class TC_30_VMCollection(qubes.tests.QubesTestCase):
  134. def setUp(self):
  135. super().setUp()
  136. self.app = TestApp()
  137. self.vms = qubes.app.VMCollection(self.app)
  138. self.testvm1 = qubes.tests.init.TestVM(
  139. None, None, qid=1, name='testvm1')
  140. self.testvm2 = qubes.tests.init.TestVM(
  141. None, None, qid=2, name='testvm2')
  142. def test_000_contains(self):
  143. self.vms._dict = {1: self.testvm1}
  144. self.assertIn(1, self.vms)
  145. self.assertIn('testvm1', self.vms)
  146. self.assertIn(self.testvm1, self.vms)
  147. self.assertNotIn(2, self.vms)
  148. self.assertNotIn('testvm2', self.vms)
  149. self.assertNotIn(self.testvm2, self.vms)
  150. def test_001_getitem(self):
  151. self.vms._dict = {1: self.testvm1}
  152. self.assertIs(self.vms[1], self.testvm1)
  153. self.assertIs(self.vms['testvm1'], self.testvm1)
  154. self.assertIs(self.vms[self.testvm1], self.testvm1)
  155. def test_002_add(self):
  156. self.vms.add(self.testvm1)
  157. self.assertIn(1, self.vms)
  158. self.assertEventFired(self.app, 'domain-add',
  159. kwargs={'vm': self.testvm1})
  160. with self.assertRaises(TypeError):
  161. self.vms.add(object())
  162. testvm_qid_collision = qubes.tests.init.TestVM(
  163. None, None, name='testvm2', qid=1)
  164. testvm_name_collision = qubes.tests.init.TestVM(
  165. None, None, name='testvm1', qid=2)
  166. with self.assertRaises(ValueError):
  167. self.vms.add(testvm_qid_collision)
  168. with self.assertRaises(ValueError):
  169. self.vms.add(testvm_name_collision)
  170. def test_003_qids(self):
  171. self.vms.add(self.testvm1)
  172. self.vms.add(self.testvm2)
  173. self.assertCountEqual(self.vms.qids(), [1, 2])
  174. self.assertCountEqual(self.vms.keys(), [1, 2])
  175. def test_004_names(self):
  176. self.vms.add(self.testvm1)
  177. self.vms.add(self.testvm2)
  178. self.assertCountEqual(self.vms.names(), ['testvm1', 'testvm2'])
  179. def test_005_vms(self):
  180. self.vms.add(self.testvm1)
  181. self.vms.add(self.testvm2)
  182. self.assertCountEqual(self.vms.vms(), [self.testvm1, self.testvm2])
  183. self.assertCountEqual(self.vms.values(), [self.testvm1, self.testvm2])
  184. def test_006_items(self):
  185. self.vms.add(self.testvm1)
  186. self.vms.add(self.testvm2)
  187. self.assertCountEqual(self.vms.items(),
  188. [(1, self.testvm1), (2, self.testvm2)])
  189. def test_007_len(self):
  190. self.vms.add(self.testvm1)
  191. self.vms.add(self.testvm2)
  192. self.assertEqual(len(self.vms), 2)
  193. def test_008_delitem(self):
  194. self.vms.add(self.testvm1)
  195. self.vms.add(self.testvm2)
  196. del self.vms['testvm2']
  197. self.assertCountEqual(self.vms.vms(), [self.testvm1])
  198. self.assertEventFired(self.app, 'domain-delete',
  199. kwargs={'vm': self.testvm2})
  200. def test_100_get_new_unused_qid(self):
  201. self.vms.add(self.testvm1)
  202. self.vms.add(self.testvm2)
  203. self.vms.get_new_unused_qid()
  204. def test_101_get_new_unused_netid(self):
  205. self.vms.add(self.testvm1)
  206. self.vms.add(self.testvm2)
  207. self.vms.get_new_unused_netid()
  208. # def test_200_get_vms_based_on(self):
  209. # pass
  210. # def test_201_get_vms_connected_to(self):
  211. # pass
  212. class TC_90_Qubes(qubes.tests.QubesTestCase):
  213. def tearDown(self):
  214. try:
  215. os.unlink('/tmp/qubestest.xml')
  216. except:
  217. pass
  218. super(TC_90_Qubes, self).tearDown()
  219. @qubes.tests.skipUnlessDom0
  220. def test_000_init_empty(self):
  221. # pylint: disable=no-self-use,unused-variable,bare-except
  222. try:
  223. os.unlink('/tmp/qubestest.xml')
  224. except FileNotFoundError:
  225. pass
  226. qubes.Qubes.create_empty_store('/tmp/qubestest.xml')
  227. def test_100_clockvm(self):
  228. app = qubes.Qubes('/tmp/qubestest.xml', load=False, offline_mode=True)
  229. app.load_initial_values()
  230. template = app.add_new_vm('TemplateVM', name='test-template',
  231. label='green')
  232. appvm = app.add_new_vm('AppVM', name='test-vm', template=template,
  233. label='red')
  234. self.assertIsNone(app.clockvm)
  235. self.assertNotIn('service.clocksync', appvm.features)
  236. self.assertNotIn('service.clocksync', template.features)
  237. app.clockvm = appvm
  238. self.assertIn('service.clocksync', appvm.features)
  239. self.assertTrue(appvm.features['service.clocksync'])
  240. app.clockvm = template
  241. self.assertNotIn('service.clocksync', appvm.features)
  242. self.assertIn('service.clocksync', template.features)
  243. self.assertTrue(template.features['service.clocksync'])
  244. @qubes.tests.skipUnlessGit
  245. def test_900_example_xml_in_doc(self):
  246. self.assertXMLIsValid(
  247. lxml.etree.parse(open(
  248. os.path.join(qubes.tests.in_git, 'doc/example.xml'), 'rb')),
  249. 'qubes.rng')