app.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/usr/bin/python2 -O
  2. # vim: fileencoding=utf-8
  3. # pylint: disable=protected-access,pointless-statement
  4. #
  5. # The Qubes OS Project, https://www.qubes-os.org/
  6. #
  7. # Copyright (C) 2014-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  8. # Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; either version 2 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License along
  21. # with this program; if not, write to the Free Software Foundation, Inc.,
  22. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. #
  24. import os
  25. import uuid
  26. import lxml.etree
  27. import qubes
  28. import qubes.events
  29. import qubes.tests
  30. # FIXME: blatant duplication with qubes.tests.init
  31. class TestVM(qubes.vm.BaseVM):
  32. qid = qubes.property('qid', type=int)
  33. name = qubes.property('name')
  34. netid = qid
  35. uuid = uuid.uuid5(uuid.NAMESPACE_DNS, 'testvm')
  36. class MockLibvirt(object):
  37. def undefine(self):
  38. pass
  39. libvirt_domain = MockLibvirt()
  40. def is_halted(self):
  41. return True
  42. def get_power_state(self):
  43. return "Halted"
  44. class TestApp(qubes.tests.TestEmitter):
  45. pass
  46. class TC_30_VMCollection(qubes.tests.QubesTestCase):
  47. def setUp(self):
  48. self.app = TestApp()
  49. self.vms = qubes.app.VMCollection(self.app)
  50. self.testvm1 = TestVM(None, None, qid=1, name='testvm1')
  51. self.testvm2 = TestVM(None, None, qid=2, name='testvm2')
  52. def test_000_contains(self):
  53. self.vms._dict = {1: self.testvm1}
  54. self.assertIn(1, self.vms)
  55. self.assertIn('testvm1', self.vms)
  56. self.assertIn(self.testvm1, self.vms)
  57. self.assertNotIn(2, self.vms)
  58. self.assertNotIn('testvm2', self.vms)
  59. self.assertNotIn(self.testvm2, self.vms)
  60. def test_001_getitem(self):
  61. self.vms._dict = {1: self.testvm1}
  62. self.assertIs(self.vms[1], self.testvm1)
  63. self.assertIs(self.vms['testvm1'], self.testvm1)
  64. self.assertIs(self.vms[self.testvm1], self.testvm1)
  65. def test_002_add(self):
  66. self.vms.add(self.testvm1)
  67. self.assertIn(1, self.vms)
  68. self.assertEventFired(self.app, 'domain-add', args=[self.testvm1])
  69. with self.assertRaises(TypeError):
  70. self.vms.add(object())
  71. testvm_qid_collision = TestVM(None, None, name='testvm2', qid=1)
  72. testvm_name_collision = TestVM(None, None, name='testvm1', qid=2)
  73. with self.assertRaises(ValueError):
  74. self.vms.add(testvm_qid_collision)
  75. with self.assertRaises(ValueError):
  76. self.vms.add(testvm_name_collision)
  77. def test_003_qids(self):
  78. self.vms.add(self.testvm1)
  79. self.vms.add(self.testvm2)
  80. self.assertItemsEqual(self.vms.qids(), [1, 2])
  81. self.assertItemsEqual(self.vms.keys(), [1, 2])
  82. def test_004_names(self):
  83. self.vms.add(self.testvm1)
  84. self.vms.add(self.testvm2)
  85. self.assertItemsEqual(self.vms.names(), ['testvm1', 'testvm2'])
  86. def test_005_vms(self):
  87. self.vms.add(self.testvm1)
  88. self.vms.add(self.testvm2)
  89. self.assertItemsEqual(self.vms.vms(), [self.testvm1, self.testvm2])
  90. self.assertItemsEqual(self.vms.values(), [self.testvm1, self.testvm2])
  91. def test_006_items(self):
  92. self.vms.add(self.testvm1)
  93. self.vms.add(self.testvm2)
  94. self.assertItemsEqual(self.vms.items(),
  95. [(1, self.testvm1), (2, self.testvm2)])
  96. def test_007_len(self):
  97. self.vms.add(self.testvm1)
  98. self.vms.add(self.testvm2)
  99. self.assertEqual(len(self.vms), 2)
  100. def test_008_delitem(self):
  101. self.vms.add(self.testvm1)
  102. self.vms.add(self.testvm2)
  103. del self.vms['testvm2']
  104. self.assertItemsEqual(self.vms.vms(), [self.testvm1])
  105. self.assertEventFired(self.app, 'domain-delete', args=[self.testvm2])
  106. def test_100_get_new_unused_qid(self):
  107. self.vms.add(self.testvm1)
  108. self.vms.add(self.testvm2)
  109. self.vms.get_new_unused_qid()
  110. def test_101_get_new_unused_netid(self):
  111. self.vms.add(self.testvm1)
  112. self.vms.add(self.testvm2)
  113. self.vms.get_new_unused_netid()
  114. # def test_200_get_vms_based_on(self):
  115. # pass
  116. # def test_201_get_vms_connected_to(self):
  117. # pass
  118. class TC_90_Qubes(qubes.tests.QubesTestCase):
  119. @qubes.tests.skipUnlessDom0
  120. def test_000_init_empty(self):
  121. # pylint: disable=no-self-use,unused-variable,bare-except
  122. try:
  123. os.unlink('/tmp/qubestest.xml')
  124. except:
  125. pass
  126. qubes.Qubes.create_empty_store('/tmp/qubestest.xml')
  127. @qubes.tests.skipUnlessGit
  128. def test_900_example_xml_in_doc(self):
  129. self.assertXMLIsValid(
  130. lxml.etree.parse(open(
  131. os.path.join(qubes.tests.in_git, 'doc/example.xml'), 'rb')),
  132. 'qubes.rng')