app.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. self.addCleanup(self.cleanup_vmcollection)
  143. def cleanup_vmcollection(self):
  144. self.testvm1.close()
  145. self.testvm2.close()
  146. self.vms.close()
  147. del self.testvm1
  148. del self.testvm2
  149. del self.vms
  150. del self.app
  151. def test_000_contains(self):
  152. self.vms._dict = {1: self.testvm1}
  153. self.assertIn(1, self.vms)
  154. self.assertIn('testvm1', self.vms)
  155. self.assertIn(self.testvm1, self.vms)
  156. self.assertNotIn(2, self.vms)
  157. self.assertNotIn('testvm2', self.vms)
  158. self.assertNotIn(self.testvm2, self.vms)
  159. def test_001_getitem(self):
  160. self.vms._dict = {1: self.testvm1}
  161. self.assertIs(self.vms[1], self.testvm1)
  162. self.assertIs(self.vms['testvm1'], self.testvm1)
  163. self.assertIs(self.vms[self.testvm1], self.testvm1)
  164. def test_002_add(self):
  165. self.vms.add(self.testvm1)
  166. self.assertIn(1, self.vms)
  167. self.assertEventFired(self.app, 'domain-add',
  168. kwargs={'vm': self.testvm1})
  169. with self.assertRaises(TypeError):
  170. self.vms.add(object())
  171. testvm_qid_collision = qubes.tests.init.TestVM(
  172. None, None, name='testvm2', qid=1)
  173. testvm_name_collision = qubes.tests.init.TestVM(
  174. None, None, name='testvm1', qid=2)
  175. with self.assertRaises(ValueError):
  176. self.vms.add(testvm_qid_collision)
  177. with self.assertRaises(ValueError):
  178. self.vms.add(testvm_name_collision)
  179. def test_003_qids(self):
  180. self.vms.add(self.testvm1)
  181. self.vms.add(self.testvm2)
  182. self.assertCountEqual(self.vms.qids(), [1, 2])
  183. self.assertCountEqual(self.vms.keys(), [1, 2])
  184. def test_004_names(self):
  185. self.vms.add(self.testvm1)
  186. self.vms.add(self.testvm2)
  187. self.assertCountEqual(self.vms.names(), ['testvm1', 'testvm2'])
  188. def test_005_vms(self):
  189. self.vms.add(self.testvm1)
  190. self.vms.add(self.testvm2)
  191. self.assertCountEqual(self.vms.vms(), [self.testvm1, self.testvm2])
  192. self.assertCountEqual(self.vms.values(), [self.testvm1, self.testvm2])
  193. def test_006_items(self):
  194. self.vms.add(self.testvm1)
  195. self.vms.add(self.testvm2)
  196. self.assertCountEqual(self.vms.items(),
  197. [(1, self.testvm1), (2, self.testvm2)])
  198. def test_007_len(self):
  199. self.vms.add(self.testvm1)
  200. self.vms.add(self.testvm2)
  201. self.assertEqual(len(self.vms), 2)
  202. def test_008_delitem(self):
  203. self.vms.add(self.testvm1)
  204. self.vms.add(self.testvm2)
  205. del self.vms['testvm2']
  206. self.assertCountEqual(self.vms.vms(), [self.testvm1])
  207. self.assertEventFired(self.app, 'domain-delete',
  208. kwargs={'vm': self.testvm2})
  209. def test_100_get_new_unused_qid(self):
  210. self.vms.add(self.testvm1)
  211. self.vms.add(self.testvm2)
  212. self.vms.get_new_unused_qid()
  213. def test_101_get_new_unused_netid(self):
  214. self.vms.add(self.testvm1)
  215. self.vms.add(self.testvm2)
  216. self.vms.get_new_unused_netid()
  217. # def test_200_get_vms_based_on(self):
  218. # pass
  219. # def test_201_get_vms_connected_to(self):
  220. # pass
  221. class TC_90_Qubes(qubes.tests.QubesTestCase):
  222. def tearDown(self):
  223. try:
  224. os.unlink('/tmp/qubestest.xml')
  225. except:
  226. pass
  227. super(TC_90_Qubes, self).tearDown()
  228. @qubes.tests.skipUnlessDom0
  229. def test_000_init_empty(self):
  230. # pylint: disable=no-self-use,unused-variable,bare-except
  231. try:
  232. os.unlink('/tmp/qubestest.xml')
  233. except FileNotFoundError:
  234. pass
  235. qubes.Qubes.create_empty_store('/tmp/qubestest.xml').close()
  236. def test_100_clockvm(self):
  237. app = qubes.Qubes('/tmp/qubestest.xml', load=False, offline_mode=True)
  238. app.load_initial_values()
  239. template = app.add_new_vm('TemplateVM', name='test-template',
  240. label='green')
  241. appvm = app.add_new_vm('AppVM', name='test-vm', template=template,
  242. label='red')
  243. self.assertIsNone(app.clockvm)
  244. self.assertNotIn('service.clocksync', appvm.features)
  245. self.assertNotIn('service.clocksync', template.features)
  246. app.clockvm = appvm
  247. self.assertIn('service.clocksync', appvm.features)
  248. self.assertTrue(appvm.features['service.clocksync'])
  249. app.clockvm = template
  250. self.assertNotIn('service.clocksync', appvm.features)
  251. self.assertIn('service.clocksync', template.features)
  252. self.assertTrue(template.features['service.clocksync'])
  253. app.close()
  254. @qubes.tests.skipUnlessGit
  255. def test_900_example_xml_in_doc(self):
  256. self.assertXMLIsValid(
  257. lxml.etree.parse(open(
  258. os.path.join(qubes.tests.in_git, 'doc/example.xml'), 'rb')),
  259. 'qubes.rng')