test_create_new_vm.py 11 KB

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