test_create_new_vm.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. import qubesadmin
  26. from PyQt5 import QtTest, QtCore
  27. from qubesadmin import Qubes
  28. from qubesmanager.tests import init_qtapp
  29. from qubesmanager import create_new_vm
  30. class NewVmTest(unittest.TestCase):
  31. def setUp(self):
  32. super(NewVmTest, self).setUp()
  33. self.qtapp, self.loop = init_qtapp()
  34. self.qapp = Qubes()
  35. # mock up the Create VM Thread to avoid changing system state
  36. self.patcher_thread = unittest.mock.patch(
  37. 'qubesmanager.create_new_vm.CreateVMThread')
  38. self.mock_thread = self.patcher_thread.start()
  39. self.addCleanup(self.patcher_thread.stop)
  40. # mock the progress dialog to speed testing up
  41. self.patcher_progress = unittest.mock.patch(
  42. 'PyQt5.QtWidgets.QProgressDialog')
  43. self.mock_progress = self.patcher_progress.start()
  44. self.addCleanup(self.patcher_progress.stop)
  45. self.dialog = create_new_vm.NewVmDlg(self.qtapp, self.qapp)
  46. def tearDown(self):
  47. self.dialog.close()
  48. self.qtapp.processEvents()
  49. yield from self.loop.sleep(1)
  50. super(NewVmTest, self).tearDown()
  51. def test_00_window_loads(self):
  52. self.assertGreater(self.dialog.template_vm.count(), 0,
  53. "No templates shown")
  54. self.assertGreater(self.dialog.netvm.count(), 0, "No netvm listed")
  55. def test_01_cancel_works(self):
  56. self.__click_cancel()
  57. self.assertEqual(self.mock_thread.call_count, 0,
  58. "Attempted to create VM on cancel")
  59. def test_02_create_simple_vm(self):
  60. self.dialog.name.setText("testvm")
  61. self.__click_ok()
  62. self.mock_thread.assert_called_once_with(
  63. self.qapp, "AppVM", "testvm",
  64. unittest.mock.ANY, qubesadmin.DEFAULT,
  65. {'provides_network': False})
  66. self.mock_thread().start.assert_called_once_with()
  67. def test_03_label(self):
  68. for i in range(self.dialog.label.count()):
  69. if self.dialog.label.itemText(i) == 'blue':
  70. self.dialog.label.setCurrentIndex(i)
  71. break
  72. self.dialog.name.setText("testvm")
  73. self.__click_ok()
  74. self.mock_thread.assert_called_once_with(
  75. self.qapp, "AppVM", "testvm",
  76. self.qapp.labels['blue'], qubesadmin.DEFAULT,
  77. unittest.mock.ANY)
  78. self.mock_thread().start.assert_called_once_with()
  79. def test_04_template(self):
  80. template = None
  81. for i in range(self.dialog.template_vm.count()):
  82. if not self.dialog.template_vm.itemText(i).startswith('default'):
  83. self.dialog.template_vm.setCurrentIndex(i)
  84. template = self.dialog.template_vm.currentText()
  85. break
  86. self.dialog.name.setText("testvm")
  87. self.__click_ok()
  88. self.mock_thread.assert_called_once_with(
  89. self.qapp, "AppVM", "testvm",
  90. unittest.mock.ANY, template,
  91. unittest.mock.ANY)
  92. def test_05_netvm(self):
  93. netvm = None
  94. for i in range(self.dialog.netvm.count()):
  95. if not self.dialog.netvm.itemText(i).startswith('default'):
  96. self.dialog.netvm.setCurrentIndex(i)
  97. netvm = self.dialog.netvm.currentText()
  98. break
  99. self.dialog.name.setText("testvm")
  100. self.__click_ok()
  101. self.mock_thread.assert_called_once_with(
  102. self.qapp, "AppVM", "testvm",
  103. unittest.mock.ANY, unittest.mock.ANY,
  104. {'netvm': netvm, 'provides_network': False})
  105. def test_06_provides_network(self):
  106. self.dialog.provides_network.setChecked(True)
  107. self.dialog.name.setText("testvm")
  108. self.__click_ok()
  109. self.mock_thread.assert_called_once_with(
  110. self.qapp, "AppVM", "testvm",
  111. unittest.mock.ANY, unittest.mock.ANY,
  112. {'provides_network': True})
  113. @unittest.mock.patch('subprocess.check_call')
  114. def test_07_launch_settings(self, mock_call):
  115. self.dialog.launch_settings.setChecked(True)
  116. self.dialog.name.setText("testvm")
  117. self.__click_ok()
  118. # make sure the thread is not reporting an error
  119. self.mock_thread.assert_called_once_with(
  120. self.qapp, "AppVM", "testvm",
  121. unittest.mock.ANY, unittest.mock.ANY,
  122. unittest.mock.ANY)
  123. self.mock_thread().msg = None
  124. self.dialog.create_finished()
  125. mock_call.assert_called_once_with(['qubes-vm-settings', "testvm"])
  126. def test_08_progress_hides(self):
  127. self.dialog.name.setText("testvm")
  128. self.__click_ok()
  129. self.mock_thread.assert_called_once_with(
  130. self.qapp, "AppVM", "testvm",
  131. unittest.mock.ANY, unittest.mock.ANY,
  132. unittest.mock.ANY)
  133. # make sure the thread is not reporting an error
  134. self.mock_thread().start.assert_called_once_with()
  135. self.mock_thread().msg = None
  136. self.mock_progress().show.assert_called_once_with()
  137. self.dialog.create_finished()
  138. self.mock_progress().hide.assert_called_once_with()
  139. def test_09_standalone_clone(self):
  140. self.dialog.name.setText("testvm")
  141. for i in range(self.dialog.vm_type.count()):
  142. opt_text = self.dialog.vm_type.itemText(i).lower()
  143. if "standalone" in opt_text and "template" in opt_text and\
  144. "not based" not in opt_text and "empty" not in opt_text:
  145. self.dialog.vm_type.setCurrentIndex(i)
  146. break
  147. self.__click_ok()
  148. self.mock_thread.assert_called_once_with(
  149. self.qapp, "StandaloneVM", "testvm",
  150. unittest.mock.ANY, unittest.mock.ANY,
  151. unittest.mock.ANY)
  152. @unittest.mock.patch('subprocess.check_call')
  153. def test_10_standalone_empty(self, mock_call):
  154. self.dialog.name.setText("testvm")
  155. for i in range(self.dialog.vm_type.count()):
  156. opt_text = self.dialog.vm_type.itemText(i).lower()
  157. if "standalone" in opt_text and\
  158. ("not based" in opt_text or "empty" in opt_text):
  159. self.dialog.vm_type.setCurrentIndex(i)
  160. break
  161. self.__click_ok()
  162. self.mock_thread.assert_called_once_with(
  163. self.qapp, "StandaloneVM", "testvm",
  164. unittest.mock.ANY, None,
  165. unittest.mock.ANY)
  166. self.mock_thread().msg = None
  167. self.dialog.create_finished()
  168. mock_call.assert_called_once_with(['qubes-vm-boot-from-device',
  169. 'testvm'])
  170. @unittest.mock.patch('subprocess.check_call')
  171. def test_11_standalone_empty_not_install(self, mock_call):
  172. self.dialog.name.setText("testvm")
  173. for i in range(self.dialog.vm_type.count()):
  174. opt_text = self.dialog.vm_type.itemText(i).lower()
  175. if "standalone" in opt_text and\
  176. ("not based" in opt_text or "empty" in opt_text):
  177. self.dialog.vm_type.setCurrentIndex(i)
  178. break
  179. self.dialog.install_system.setChecked(False)
  180. self.__click_ok()
  181. self.mock_thread.assert_called_once_with(
  182. self.qapp, "StandaloneVM", "testvm",
  183. unittest.mock.ANY, None,
  184. unittest.mock.ANY)
  185. self.mock_thread().msg = None
  186. self.dialog.create_finished()
  187. self.assertEqual(mock_call.call_count, 0)
  188. def test_12_setting_change(self):
  189. # cannot install system on a template-based appvm
  190. for i in range(self.dialog.vm_type.count()):
  191. opt_text = self.dialog.vm_type.itemText(i).lower()
  192. if "appvm" in opt_text and "standalone" not in opt_text:
  193. self.dialog.vm_type.setCurrentIndex(i)
  194. break
  195. self.assertFalse(self.dialog.install_system.isEnabled())
  196. self.assertTrue(self.dialog.launch_settings.isEnabled())
  197. self.assertTrue(self.dialog.template_vm.isEnabled())
  198. # or on a standalone vm cloned from a template
  199. for i in range(self.dialog.vm_type.count()):
  200. opt_text = self.dialog.vm_type.itemText(i).lower()
  201. if "standalone" in opt_text and "template" in opt_text and\
  202. "not based" not in opt_text and "empty" not in opt_text:
  203. self.dialog.vm_type.setCurrentIndex(i)
  204. break
  205. self.assertFalse(self.dialog.install_system.isEnabled())
  206. self.assertTrue(self.dialog.launch_settings.isEnabled())
  207. self.assertTrue(self.dialog.template_vm.isEnabled())
  208. # cannot set a template but can install system on a truly empty AppVM
  209. for i in range(self.dialog.vm_type.count()):
  210. opt_text = self.dialog.vm_type.itemText(i).lower()
  211. if "standalone" in opt_text and\
  212. ("not based" in opt_text or "empty" in opt_text):
  213. self.dialog.vm_type.setCurrentIndex(i)
  214. break
  215. self.assertTrue(self.dialog.install_system.isEnabled())
  216. self.assertTrue(self.dialog.launch_settings.isEnabled())
  217. self.assertFalse(self.dialog.template_vm.isEnabled())
  218. def __click_ok(self):
  219. okwidget = self.dialog.buttonBox.button(
  220. self.dialog.buttonBox.Ok)
  221. QtTest.QTest.mouseClick(okwidget, QtCore.Qt.LeftButton)
  222. def __click_cancel(self):
  223. cancelwidget = self.dialog.buttonBox.button(
  224. self.dialog.buttonBox.Cancel)
  225. QtTest.QTest.mouseClick(cancelwidget, QtCore.Qt.LeftButton)
  226. # class CreatteVMThreadTest(unittest.TestCase):
  227. if __name__ == "__main__":
  228. ha_syslog = logging.handlers.SysLogHandler('/dev/log')
  229. ha_syslog.setFormatter(
  230. logging.Formatter('%(name)s[%(process)d]: %(message)s'))
  231. logging.root.addHandler(ha_syslog)
  232. unittest.main()