test_global_settings.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. #!/usr/bin/python3
  2. #
  3. # The Qubes OS Project, https://www.qubes-os.org/
  4. #
  5. # Copyright (C) 2016 Marta Marczykowska-Górecka
  6. # <marmarta@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 logging.handlers
  23. import unittest
  24. import unittest.mock
  25. from PyQt5 import QtTest, QtCore, QtWidgets
  26. from qubesadmin import Qubes
  27. from qubesmanager.tests import init_qtapp
  28. import qubesmanager.global_settings as global_settings
  29. class GlobalSettingsTest(unittest.TestCase):
  30. def setUp(self):
  31. super(GlobalSettingsTest, self).setUp()
  32. self.qtapp, self.loop = init_qtapp()
  33. self.qapp = Qubes()
  34. self.dialog = global_settings.GlobalSettingsWindow(self.qtapp,
  35. self.qapp)
  36. self.setattr_patcher = unittest.mock.patch.object(
  37. type(self.dialog.qvm_collection), "__setattr__")
  38. self.setattr_mock = self.setattr_patcher.start()
  39. self.addCleanup(self.setattr_patcher.stop)
  40. def test_00_settings_started(self):
  41. # non-empty drop-downs
  42. self.assertNotEqual(
  43. self.dialog.default_kernel_combo.currentText(), "",
  44. "Default kernel not listed")
  45. self.assertNotEqual(
  46. self.dialog.default_netvm_combo.currentText(), "",
  47. "Default netVM not listed")
  48. self.assertNotEqual(
  49. self.dialog.default_template_combo.currentText(),
  50. "", "Default template not listed")
  51. self.assertNotEqual(
  52. self.dialog.clock_vm_combo.currentText(), "",
  53. "ClockVM not listed")
  54. self.assertNotEqual(
  55. self.dialog.update_vm_combo.currentText(), "",
  56. "UpdateVM for dom0 not listed")
  57. self.assertNotEqual(
  58. self.dialog.default_dispvm_combo.currentText(), "",
  59. "Default DispVM not listed")
  60. # not empty memory settings
  61. self.assertTrue(len(self.dialog.min_vm_mem.text()) > 4,
  62. "Too short min mem value")
  63. self.assertTrue(len(self.dialog.dom0_mem_boost.text()) > 4,
  64. "Too short dom0 mem boost value")
  65. def test_01_load_correct_defs(self):
  66. # correctly selected default template
  67. selected_default_template = \
  68. self.dialog.default_template_combo.currentText()
  69. self.assertTrue(
  70. selected_default_template.startswith(
  71. str(getattr(self.qapp, 'default_template', '(none)'))),
  72. "Incorrect default template loaded")
  73. # correctly selected default NetVM
  74. selected_default_netvm = self.dialog.default_netvm_combo.currentText()
  75. self.assertTrue(selected_default_netvm.startswith(
  76. str(getattr(self.qapp, 'default_netvm', '(none)'))),
  77. "Incorrect default netVM loaded")
  78. # correctly selected default kernel
  79. selected_default_kernel = self.dialog.default_kernel_combo.currentText()
  80. self.assertTrue(selected_default_kernel.startswith(
  81. str(getattr(self.qapp, 'default_kernel', '(none)'))),
  82. "Incorrect default kernel loaded")
  83. # correct ClockVM
  84. selected_clockvm = self.dialog.clock_vm_combo.currentText()
  85. correct_clockvm = str(getattr(self.qapp, 'clockvm', "(none)"))
  86. self.assertTrue(selected_clockvm.startswith(correct_clockvm),
  87. "Incorrect clockVM loaded")
  88. # correct updateVM
  89. selected_updatevm = self.dialog.update_vm_combo.currentText()
  90. correct_updatevm = str(getattr(self.qapp, 'updatevm', "(none)"))
  91. self.assertTrue(selected_updatevm.startswith(correct_updatevm),
  92. "Incorrect updateVm loaded")
  93. # correct defaultDispVM
  94. selected_default_dispvm = self.dialog.default_dispvm_combo.currentText()
  95. correct_default_dispvm = \
  96. str(getattr(self.qapp, 'default_dispvm', "(none)"))
  97. self.assertTrue(
  98. selected_default_dispvm.startswith(correct_default_dispvm),
  99. "Incorrect defaultDispVM loaded")
  100. # update vm status
  101. self.assertEqual(self.qapp.check_updates_vm,
  102. self.dialog.updates_vm.isChecked(),
  103. "Incorrect check qube updates value loaded")
  104. def test_02_dom0_updates_load(self):
  105. # check dom0 updates
  106. try:
  107. dom0_updates = self.qapp.domains[
  108. 'dom0'].features['service.qubes-update-check']
  109. except KeyError:
  110. dom0_updates = True
  111. self.assertEqual(bool(dom0_updates),
  112. self.dialog.updates_dom0.isChecked(),
  113. "Incorrect dom0 updates value")
  114. def __set_noncurrent(self, widget):
  115. if widget.count() < 2:
  116. self.skipTest("not enough choices for " + widget.objectName())
  117. widget.setCurrentIndex(0)
  118. while widget.currentText().endswith("(current)") \
  119. or widget.currentText().startswith("(none)"):
  120. widget.setCurrentIndex(widget.currentIndex() + 1)
  121. return widget.currentText()
  122. def __set_none(self, widget):
  123. widget.setCurrentIndex(0)
  124. while not widget.currentText().startswith("(none)"):
  125. if widget.currentIndex() == widget.count():
  126. self.skipTest("none not available for " + widget.objectName())
  127. widget.setCurrentIndex(widget.currentIndex() + 1)
  128. def __click_ok(self):
  129. okwidget = self.dialog.buttonBox.button(
  130. self.dialog.buttonBox.Ok)
  131. QtTest.QTest.mouseClick(okwidget, QtCore.Qt.LeftButton)
  132. def __click_cancel(self):
  133. cancelwidget = self.dialog.buttonBox.button(
  134. self.dialog.buttonBox.Cancel)
  135. QtTest.QTest.mouseClick(cancelwidget, QtCore.Qt.LeftButton)
  136. def test_03_nothing_changed_ok(self):
  137. self.__click_ok()
  138. self.assertEqual(self.setattr_mock.call_count, 0,
  139. "Changes occurred despite no changes being made")
  140. def test_04_nothing_changed_cancel(self):
  141. self.__click_cancel()
  142. self.assertEqual(self.setattr_mock.call_count, 0,
  143. "Changes occurred despite no changes being made")
  144. def test_10_set_update_vm(self):
  145. new_updatevm_name = self.__set_noncurrent(self.dialog.update_vm_combo)
  146. self.__click_ok()
  147. self.setattr_mock.assert_called_once_with('updatevm', new_updatevm_name)
  148. def test_11_set_update_vm_to_none(self):
  149. self.__set_none(self.dialog.update_vm_combo)
  150. self.__click_ok()
  151. self.setattr_mock.assert_called_once_with('updatevm', None)
  152. def test_20_set_clock_vm(self):
  153. new_clockvm_name = self.__set_noncurrent(self.dialog.clock_vm_combo)
  154. self.__click_ok()
  155. self.setattr_mock.assert_called_once_with('clockvm', new_clockvm_name)
  156. def test_21_set_clock_vm_to_none(self):
  157. self.__set_none(self.dialog.clock_vm_combo)
  158. self.__click_ok()
  159. self.setattr_mock.assert_called_once_with('clockvm', None)
  160. def test_30_set_default_netvm(self):
  161. new_netvm_name = self.__set_noncurrent(self.dialog.default_netvm_combo)
  162. self.__click_ok()
  163. self.setattr_mock.assert_called_once_with('default_netvm',
  164. new_netvm_name)
  165. def test_31_set_default_netvm_to_none(self):
  166. self.__set_none(self.dialog.default_netvm_combo)
  167. self.__click_ok()
  168. self.setattr_mock.assert_called_once_with('default_netvm', None)
  169. def test_40_set_default_template(self):
  170. new_def_template_name = self.__set_noncurrent(
  171. self.dialog.default_template_combo)
  172. self.__click_ok()
  173. self.setattr_mock.assert_called_once_with('default_template',
  174. new_def_template_name)
  175. def test_50_set_default_kernel(self):
  176. new_def_kernel_name = self.__set_noncurrent(
  177. self.dialog.default_kernel_combo)
  178. self.__click_ok()
  179. self.setattr_mock.assert_called_once_with('default_kernel',
  180. new_def_kernel_name)
  181. def test_51_set_default_kernel_to_none(self):
  182. self.__set_none(self.dialog.default_kernel_combo)
  183. self.__click_ok()
  184. self.setattr_mock.assert_called_once_with('default_kernel',
  185. None)
  186. def test_60_set_dom0_updates_true(self):
  187. current_state = self.dialog.updates_dom0.isChecked()
  188. self.dialog.updates_dom0.setChecked(not current_state)
  189. with unittest.mock.patch.object(
  190. type(self.dialog.qvm_collection.domains['dom0'].features),
  191. '__setitem__') as mock_features:
  192. self.__click_ok()
  193. mock_features.assert_called_once_with('service.qubes-update-check',
  194. not current_state)
  195. def test_70_change_vm_updates(self):
  196. current_state = self.dialog.updates_vm.isChecked()
  197. self.dialog.updates_vm.setChecked(not current_state)
  198. self.__click_ok()
  199. self.setattr_mock.assert_called_once_with('check_updates_vm',
  200. not current_state)
  201. @unittest.mock.patch("PyQt5.QtWidgets.QMessageBox.question",
  202. return_value=QtWidgets.QMessageBox.Yes)
  203. @unittest.mock.patch('qubesadmin.features.Features.__setitem__')
  204. def test_72_set_all_vms_true(self, mock_features, msgbox):
  205. QtTest.QTest.mouseClick(self.dialog.enable_updates_all,
  206. QtCore.Qt.LeftButton)
  207. self.assertEqual(msgbox.call_count, 1,
  208. "Wrong number of confirmation window calls")
  209. call_list_expected = \
  210. [unittest.mock.call('service.qubes-update-check', True) for vm
  211. in self.qapp.domains if vm.klass != 'AdminVM']
  212. self.assertListEqual(call_list_expected,
  213. mock_features.call_args_list)
  214. @unittest.mock.patch("PyQt5.QtWidgets.QMessageBox.question",
  215. return_value=QtWidgets.QMessageBox.Yes)
  216. @unittest.mock.patch('qubesadmin.features.Features.__setitem__')
  217. def test_73_set_all_vms_false(self, mock_features, msgbox):
  218. QtTest.QTest.mouseClick(self.dialog.disable_updates_all,
  219. QtCore.Qt.LeftButton)
  220. self.assertEqual(msgbox.call_count, 1,
  221. "Wrong number of confirmation window calls")
  222. call_list_expected = \
  223. [unittest.mock.call('service.qubes-update-check', False) for vm
  224. in self.qapp.domains if vm.klass != 'AdminVM']
  225. self.assertListEqual(call_list_expected,
  226. mock_features.call_args_list)
  227. def test_80_set_default_dispvm(self):
  228. new_dispvm_name = self.__set_noncurrent(
  229. self.dialog.default_dispvm_combo)
  230. self.__click_ok()
  231. self.setattr_mock.assert_called_once_with('default_dispvm',
  232. new_dispvm_name)
  233. def test_81_set_default_dispvm_to_none(self):
  234. self.__set_none(self.dialog.default_dispvm_combo)
  235. self.__click_ok()
  236. self.setattr_mock.assert_called_once_with('default_dispvm', None)
  237. @unittest.mock.patch.object(
  238. type(Qubes()), '__getattr__',
  239. side_effect=(lambda x: False if x == 'check_updates_vm' else None))
  240. def test_90_test_all_set_none(self, mock_qubes):
  241. mock_qubes.configure_mock()
  242. self.dialog = global_settings.GlobalSettingsWindow(
  243. self.qtapp, self.qapp)
  244. self.assertEqual(self.dialog.update_vm_combo.currentText(),
  245. "(none) (current)",
  246. "UpdateVM displays as none incorrectly")
  247. self.assertEqual(self.dialog.clock_vm_combo.currentText(),
  248. "(none) (current)",
  249. "ClockVM displays as none incorrectly")
  250. self.assertEqual(self.dialog.default_netvm_combo.currentText(),
  251. "(none) (current)",
  252. "Default NetVM displays as none incorrectly")
  253. self.assertEqual(self.dialog.default_template_combo.currentText(),
  254. "(none) (current)",
  255. "Default template displays as none incorrectly")
  256. self.assertEqual(self.dialog.default_kernel_combo.currentText(),
  257. "(none) (current)",
  258. "Defautl kernel displays as none incorrectly")
  259. self.assertEqual(self.dialog.default_dispvm_combo.currentText(),
  260. "(none) (current)",
  261. "Default DispVM displays as none incorrectly")
  262. if __name__ == "__main__":
  263. ha_syslog = logging.handlers.SysLogHandler('/dev/log')
  264. ha_syslog.setFormatter(
  265. logging.Formatter('%(name)s[%(process)d]: %(message)s'))
  266. logging.root.addHandler(ha_syslog)
  267. unittest.main()
  268. # TODO: add tests for memory settings once memory is handled better