test_create_new_vm.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 and "template" in opt_text and\
  138. "not based" not in opt_text and "empty" not in opt_text:
  139. self.dialog.vm_type.setCurrentIndex(i)
  140. break
  141. self.__click_ok()
  142. self.mock_thread.assert_called_once_with(
  143. self.qapp, "StandaloneVM", "test-vm",
  144. unittest.mock.ANY, unittest.mock.ANY,
  145. unittest.mock.ANY, unittest.mock.ANY)
  146. @unittest.mock.patch('subprocess.check_call')
  147. def test_10_standalone_empty(self, mock_call):
  148. self.dialog.name.setText("test-vm")
  149. for i in range(self.dialog.vm_type.count()):
  150. opt_text = self.dialog.vm_type.itemText(i).lower()
  151. if "standalone" in opt_text and\
  152. ("not based" in opt_text or "empty" in opt_text):
  153. self.dialog.vm_type.setCurrentIndex(i)
  154. break
  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 and\
  170. ("not based" in opt_text or "empty" in opt_text):
  171. self.dialog.vm_type.setCurrentIndex(i)
  172. break
  173. self.dialog.install_system.setChecked(False)
  174. self.__click_ok()
  175. self.mock_thread.assert_called_once_with(
  176. self.qapp, "StandaloneVM", "test-vm",
  177. unittest.mock.ANY, None,
  178. unittest.mock.ANY, unittest.mock.ANY)
  179. self.mock_thread().msg = None
  180. self.dialog.create_finished()
  181. self.assertEqual(mock_call.call_count, 0)
  182. def test_12_setting_change(self):
  183. # cannot install system on a template-based appvm
  184. for i in range(self.dialog.vm_type.count()):
  185. opt_text = self.dialog.vm_type.itemText(i).lower()
  186. if "appvm" in opt_text and "standalone" not in opt_text:
  187. self.dialog.vm_type.setCurrentIndex(i)
  188. break
  189. self.assertFalse(self.dialog.install_system.isEnabled())
  190. self.assertTrue(self.dialog.launch_settings.isEnabled())
  191. self.assertTrue(self.dialog.template_vm.isEnabled())
  192. # or on a standalone vm cloned from a template
  193. for i in range(self.dialog.vm_type.count()):
  194. opt_text = self.dialog.vm_type.itemText(i).lower()
  195. if "standalone" in opt_text and "template" in opt_text and\
  196. "not based" not in opt_text and "empty" not in opt_text:
  197. self.dialog.vm_type.setCurrentIndex(i)
  198. break
  199. self.assertFalse(self.dialog.install_system.isEnabled())
  200. self.assertTrue(self.dialog.launch_settings.isEnabled())
  201. self.assertTrue(self.dialog.template_vm.isEnabled())
  202. # cannot set a template but can install system on a truly empty AppVM
  203. for i in range(self.dialog.vm_type.count()):
  204. opt_text = self.dialog.vm_type.itemText(i).lower()
  205. if "standalone" in opt_text and\
  206. ("not based" in opt_text or "empty" in opt_text):
  207. self.dialog.vm_type.setCurrentIndex(i)
  208. break
  209. self.assertTrue(self.dialog.install_system.isEnabled())
  210. self.assertTrue(self.dialog.launch_settings.isEnabled())
  211. self.assertFalse(self.dialog.template_vm.isEnabled())
  212. def __click_ok(self):
  213. okwidget = self.dialog.buttonBox.button(
  214. self.dialog.buttonBox.Ok)
  215. QtTest.QTest.mouseClick(okwidget, QtCore.Qt.LeftButton)
  216. def __click_cancel(self):
  217. cancelwidget = self.dialog.buttonBox.button(
  218. self.dialog.buttonBox.Cancel)
  219. QtTest.QTest.mouseClick(cancelwidget, QtCore.Qt.LeftButton)
  220. # class CreatteVMThreadTest(unittest.TestCase):
  221. if __name__ == "__main__":
  222. ha_syslog = logging.handlers.SysLogHandler('/dev/log')
  223. ha_syslog.setFormatter(
  224. logging.Formatter('%(name)s[%(process)d]: %(message)s'))
  225. logging.root.addHandler(ha_syslog)
  226. unittest.main()