init2.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 unittest
  26. import lxml.etree
  27. import qubes
  28. import qubes.events
  29. import qubes.vm
  30. import qubes.tests
  31. class TestVM(qubes.vm.BaseVM):
  32. qid = qubes.property('qid', type=int)
  33. name = qubes.property('name')
  34. netid = qid
  35. class TestApp(qubes.tests.TestEmitter):
  36. pass
  37. class TC_30_VMCollection(qubes.tests.QubesTestCase):
  38. def setUp(self):
  39. self.app = TestApp()
  40. self.vms = qubes.VMCollection(self.app)
  41. self.testvm1 = TestVM(None, None, qid=1, name='testvm1')
  42. self.testvm2 = TestVM(None, None, qid=2, name='testvm2')
  43. def test_000_contains(self):
  44. self.vms._dict = {1: self.testvm1}
  45. self.assertIn(1, self.vms)
  46. self.assertIn('testvm1', self.vms)
  47. self.assertIn(self.testvm1, self.vms)
  48. self.assertNotIn(2, self.vms)
  49. self.assertNotIn('testvm2', self.vms)
  50. self.assertNotIn(self.testvm2, self.vms)
  51. def test_001_getitem(self):
  52. self.vms._dict = {1: self.testvm1}
  53. self.assertIs(self.vms[1], self.testvm1)
  54. self.assertIs(self.vms['testvm1'], self.testvm1)
  55. self.assertIs(self.vms[self.testvm1], self.testvm1)
  56. def test_002_add(self):
  57. self.vms.add(self.testvm1)
  58. self.assertIn(1, self.vms)
  59. self.assertEventFired(self.app, 'domain-add', args=[self.testvm1])
  60. with self.assertRaises(TypeError):
  61. self.vms.add(object())
  62. testvm_qid_collision = TestVM(None, None, name='testvm2', qid=1)
  63. testvm_name_collision = TestVM(None, None, name='testvm1', qid=2)
  64. with self.assertRaises(ValueError):
  65. self.vms.add(testvm_qid_collision)
  66. with self.assertRaises(ValueError):
  67. self.vms.add(testvm_name_collision)
  68. def test_003_qids(self):
  69. self.vms.add(self.testvm1)
  70. self.vms.add(self.testvm2)
  71. self.assertItemsEqual(self.vms.qids(), [1, 2])
  72. self.assertItemsEqual(self.vms.keys(), [1, 2])
  73. def test_004_names(self):
  74. self.vms.add(self.testvm1)
  75. self.vms.add(self.testvm2)
  76. self.assertItemsEqual(self.vms.names(), ['testvm1', 'testvm2'])
  77. def test_005_vms(self):
  78. self.vms.add(self.testvm1)
  79. self.vms.add(self.testvm2)
  80. self.assertItemsEqual(self.vms.vms(), [self.testvm1, self.testvm2])
  81. self.assertItemsEqual(self.vms.values(), [self.testvm1, self.testvm2])
  82. def test_006_items(self):
  83. self.vms.add(self.testvm1)
  84. self.vms.add(self.testvm2)
  85. self.assertItemsEqual(self.vms.items(),
  86. [(1, self.testvm1), (2, self.testvm2)])
  87. def test_007_len(self):
  88. self.vms.add(self.testvm1)
  89. self.vms.add(self.testvm2)
  90. self.assertEqual(len(self.vms), 2)
  91. def test_008_delitem(self):
  92. self.vms.add(self.testvm1)
  93. self.vms.add(self.testvm2)
  94. del self.vms['testvm2']
  95. self.assertItemsEqual(self.vms.vms(), [self.testvm1])
  96. self.assertEventFired(self.app, 'domain-delete', args=[self.testvm2])
  97. def test_100_get_new_unused_qid(self):
  98. self.vms.add(self.testvm1)
  99. self.vms.add(self.testvm2)
  100. self.vms.get_new_unused_qid()
  101. def test_101_get_new_unused_netid(self):
  102. self.vms.add(self.testvm1)
  103. self.vms.add(self.testvm2)
  104. self.vms.get_new_unused_netid()
  105. # def test_200_get_vms_based_on(self):
  106. # pass
  107. # def test_201_get_vms_connected_to(self):
  108. # pass
  109. class TC_90_Qubes(qubes.tests.QubesTestCase):
  110. @qubes.tests.skipUnlessDom0
  111. def test_000_init_empty(self):
  112. # pylint: disable=no-self-use,unused-variable,bare-except
  113. try:
  114. os.unlink('/tmp/qubestest.xml')
  115. except:
  116. pass
  117. app = qubes.Qubes.create_empty_store('/tmp/qubestest.xml')
  118. @qubes.tests.skipUnlessGit
  119. def test_900_example_xml_in_doc(self):
  120. self.assertXMLIsValid(
  121. lxml.etree.parse(open(
  122. os.path.join(qubes.tests.in_git, 'doc/example.xml'), 'rb')),
  123. 'qubes.rng')