init.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #!/usr/bin/python2 -O
  2. import sys
  3. import unittest
  4. import lxml.etree
  5. import qubes
  6. import qubes.events
  7. import qubes.vm
  8. import qubes.tests
  9. class TC_10_Label(qubes.tests.QubesTestCase):
  10. def test_000_constructor(self):
  11. label = qubes.Label(1, '#cc0000', 'red')
  12. self.assertEqual(label.index, 1)
  13. self.assertEqual(label.color, '#cc0000')
  14. self.assertEqual(label.name, 'red')
  15. self.assertEqual(label.icon, 'appvm-red')
  16. self.assertEqual(label.icon_dispvm, 'dispvm-red')
  17. def test_001_fromxml(self):
  18. xml = lxml.etree.XML('''
  19. <qubes version="3">
  20. <labels>
  21. <label id="label-1" color="#cc0000">red</label>
  22. </labels>
  23. </qubes>
  24. ''')
  25. node = xml.xpath('//label')[0]
  26. label = qubes.Label.fromxml(node)
  27. self.assertEqual(label.index, 1)
  28. self.assertEqual(label.color, '#cc0000')
  29. self.assertEqual(label.name, 'red')
  30. self.assertEqual(label.icon, 'appvm-red')
  31. self.assertEqual(label.icon_dispvm, 'dispvm-red')
  32. class TestHolder(qubes.PropertyHolder):
  33. testprop1 = qubes.property('testprop1', order=0)
  34. testprop2 = qubes.property('testprop2', order=1, save_via_ref=True)
  35. testprop3 = qubes.property('testprop3', order=2, default='testdefault')
  36. testprop4 = qubes.property('testprop4', order=3)
  37. class TC_00_PropertyHolder(qubes.tests.QubesTestCase):
  38. def assertXMLEqual(self, xml1, xml2):
  39. self.assertEqual(xml1.tag, xml2.tag)
  40. self.assertEqual(xml1.text, xml2.text)
  41. self.assertEqual(sorted(xml1.keys()), sorted(xml2.keys()))
  42. for key in xml1.keys():
  43. self.assertEqual(xml1.get(key), xml2.get(key))
  44. def setUp(self):
  45. xml = lxml.etree.XML('''
  46. <qubes version="3">
  47. <properties>
  48. <property name="testprop1">testvalue1</property>
  49. <property name="testprop2" ref="testref2" />
  50. </properties>
  51. </qubes>
  52. ''')
  53. self.holder = TestHolder(xml)
  54. def test_000_load_properties(self):
  55. self.holder.load_properties()
  56. self.assertEquals(self.holder.testprop1, 'testvalue1')
  57. self.assertEquals(self.holder.testprop2, 'testref2')
  58. self.assertEquals(self.holder.testprop3, 'testdefault')
  59. with self.assertRaises(AttributeError):
  60. self.holder.testprop4
  61. def test_001_save_properties(self):
  62. self.holder.load_properties()
  63. elements = self.holder.save_properties()
  64. elements_with_defaults = self.holder.save_properties(with_defaults=True)
  65. self.assertEqual(len(elements), 2)
  66. self.assertEqual(len(elements_with_defaults), 3)
  67. expected_prop1 = lxml.etree.Element('property', name='testprop1')
  68. expected_prop1.text = 'testvalue1'
  69. self.assertXMLEqual(elements_with_defaults[0], expected_prop1)
  70. expected_prop2 = lxml.etree.Element('property', name='testprop2', ref='testref2')
  71. self.assertXMLEqual(elements_with_defaults[1], expected_prop2)
  72. expected_prop3 = lxml.etree.Element('property', name='testprop3')
  73. expected_prop3.text = 'testdefault'
  74. self.assertXMLEqual(elements_with_defaults[2], expected_prop3)
  75. class TestVM(qubes.vm.BaseVM):
  76. qid = qubes.property('qid', type=int)
  77. name = qubes.property('name')
  78. netid = qid
  79. class TestApp(qubes.events.Emitter):
  80. pass
  81. class TC_11_VMCollection(qubes.tests.QubesTestCase):
  82. def setUp(self):
  83. # XXX passing None may be wrong in the future
  84. self.vms = qubes.VMCollection(TestApp())
  85. self.testvm1 = TestVM(None, None, qid=1, name='testvm1')
  86. self.testvm2 = TestVM(None, None, qid=2, name='testvm2')
  87. def test_000_contains(self):
  88. self.vms._dict = {1: self.testvm1}
  89. self.assertIn(1, self.vms)
  90. self.assertIn('testvm1', self.vms)
  91. self.assertIn(self.testvm1, self.vms)
  92. self.assertNotIn(2, self.vms)
  93. self.assertNotIn('testvm2', self.vms)
  94. self.assertNotIn(self.testvm2, self.vms)
  95. def test_001_getitem(self):
  96. self.vms._dict = {1: self.testvm1}
  97. self.assertIs(self.vms[1], self.testvm1)
  98. self.assertIs(self.vms['testvm1'], self.testvm1)
  99. self.assertIs(self.vms[self.testvm1], self.testvm1)
  100. def test_002_add(self):
  101. self.vms.add(self.testvm1)
  102. self.assertIn(1, self.vms)
  103. with self.assertRaises(TypeError):
  104. self.vms.add(object())
  105. testvm_qid_collision = TestVM(None, None, name='testvm2', qid=1)
  106. testvm_name_collision = TestVM(None, None, name='testvm1', qid=2)
  107. with self.assertRaises(ValueError):
  108. self.vms.add(testvm_qid_collision)
  109. with self.assertRaises(ValueError):
  110. self.vms.add(testvm_name_collision)
  111. def test_003_qids(self):
  112. self.vms.add(self.testvm1)
  113. self.vms.add(self.testvm2)
  114. self.assertItemsEqual(self.vms.qids(), [1, 2])
  115. self.assertItemsEqual(self.vms.keys(), [1, 2])
  116. def test_004_names(self):
  117. self.vms.add(self.testvm1)
  118. self.vms.add(self.testvm2)
  119. self.assertItemsEqual(self.vms.names(), ['testvm1', 'testvm2'])
  120. def test_005_vms(self):
  121. self.vms.add(self.testvm1)
  122. self.vms.add(self.testvm2)
  123. self.assertItemsEqual(self.vms.vms(), [self.testvm1, self.testvm2])
  124. self.assertItemsEqual(self.vms.values(), [self.testvm1, self.testvm2])
  125. def test_006_items(self):
  126. self.vms.add(self.testvm1)
  127. self.vms.add(self.testvm2)
  128. self.assertItemsEqual(self.vms.items(), [(1, self.testvm1), (2, self.testvm2)])
  129. def test_007_len(self):
  130. self.vms.add(self.testvm1)
  131. self.vms.add(self.testvm2)
  132. self.assertEqual(len(self.vms), 2)
  133. def test_008_delitem(self):
  134. self.vms.add(self.testvm1)
  135. self.vms.add(self.testvm2)
  136. del self.vms['testvm2']
  137. self.assertItemsEqual(self.vms.vms(), [self.testvm1])
  138. def test_100_get_new_unused_qid(self):
  139. self.vms.add(self.testvm1)
  140. self.vms.add(self.testvm2)
  141. self.vms.get_new_unused_qid()
  142. def test_101_get_new_unused_netid(self):
  143. self.vms.add(self.testvm1)
  144. self.vms.add(self.testvm2)
  145. self.vms.get_new_unused_netid()
  146. # def test_200_get_vms_based_on(self):
  147. # pass
  148. # def test_201_get_vms_connected_to(self):
  149. # pass
  150. class TC_20_Qubes(qubes.tests.QubesTestCase):
  151. pass