test_create_new_vm.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 test_00_window_loads(self):
  47. self.assertGreater(self.dialog.template_vm.count(), 0,
  48. "No templates shown")
  49. self.assertGreater(self.dialog.netvm.count(), 0, "No netvm listed")
  50. def test_01_cancel_works(self):
  51. self.__click_cancel()
  52. self.assertEqual(self.mock_thread.call_count, 0,
  53. "Attempted to create VM on cancel")
  54. def test_02_create_simple_vm(self):
  55. self.dialog.name.setText("test-vm")
  56. self.__click_ok()
  57. self.mock_thread.assert_called_once_with(
  58. self.qapp, "AppVM", "test-vm",
  59. unittest.mock.ANY, qubesadmin.DEFAULT,
  60. {'provides_network': False}, unittest.mock.ANY)
  61. self.mock_thread().start.assert_called_once_with()
  62. def test_03_label(self):
  63. for i in range(self.dialog.label.count()):
  64. if self.dialog.label.itemText(i) == 'blue':
  65. self.dialog.label.setCurrentIndex(i)
  66. break
  67. self.dialog.name.setText("test-vm")
  68. self.__click_ok()
  69. self.mock_thread.assert_called_once_with(
  70. self.qapp, "AppVM", "test-vm",
  71. self.qapp.labels['blue'], qubesadmin.DEFAULT,
  72. unittest.mock.ANY, unittest.mock.ANY)
  73. self.mock_thread().start.assert_called_once_with()
  74. def test_04_template(self):
  75. template = None
  76. for i in range(self.dialog.template_vm.count()):
  77. if not self.dialog.template_vm.itemText(i).startswith('default'):
  78. self.dialog.template_vm.setCurrentIndex(i)
  79. template = self.dialog.template_vm.currentText()
  80. break
  81. self.dialog.name.setText("test-vm")
  82. self.__click_ok()
  83. self.mock_thread.assert_called_once_with(
  84. self.qapp, "AppVM", "test-vm",
  85. unittest.mock.ANY, template,
  86. unittest.mock.ANY, unittest.mock.ANY)
  87. def test_05_netvm(self):
  88. netvm = None
  89. for i in range(self.dialog.netvm.count()):
  90. if not self.dialog.netvm.itemText(i).startswith('default'):
  91. self.dialog.netvm.setCurrentIndex(i)
  92. netvm = self.dialog.netvm.currentText()
  93. break
  94. self.dialog.name.setText("test-vm")
  95. self.__click_ok()
  96. self.mock_thread.assert_called_once_with(
  97. self.qapp, "AppVM", "test-vm",
  98. unittest.mock.ANY, unittest.mock.ANY,
  99. {'netvm': netvm, 'provides_network': False}, unittest.mock.ANY)
  100. def test_06_provides_network(self):
  101. self.dialog.provides_network.setChecked(True)
  102. self.dialog.name.setText("test-vm")
  103. self.__click_ok()
  104. self.mock_thread.assert_called_once_with(
  105. self.qapp, "AppVM", "test-vm",
  106. unittest.mock.ANY, unittest.mock.ANY,
  107. {'provides_network': True}, unittest.mock.ANY)
  108. @unittest.mock.patch('subprocess.check_call')
  109. def test_07_launch_settings(self, mock_call):
  110. self.dialog.launch_settings.setChecked(True)
  111. self.dialog.name.setText("test-vm")
  112. self.__click_ok()
  113. # make sure the thread is not reporting an error
  114. self.mock_thread.assert_called_once_with(
  115. self.qapp, "AppVM", "test-vm",
  116. unittest.mock.ANY, unittest.mock.ANY,
  117. unittest.mock.ANY, unittest.mock.ANY)
  118. self.mock_thread().msg = None
  119. self.dialog.create_finished()
  120. mock_call.assert_called_once_with(['qubes-vm-settings', "test-vm"])
  121. def test_08_progress_hides(self):
  122. self.dialog.name.setText("test-vm")
  123. self.__click_ok()
  124. self.mock_thread.assert_called_once_with(
  125. self.qapp, "AppVM", "test-vm",
  126. unittest.mock.ANY, unittest.mock.ANY,
  127. unittest.mock.ANY, unittest.mock.ANY)
  128. # make sure the thread is not reporting an error
  129. self.mock_thread().start.assert_called_once_with()
  130. self.mock_thread().msg = None
  131. self.mock_progress().show.assert_called_once_with()
  132. self.dialog.create_finished()
  133. self.mock_progress().hide.assert_called_once_with()
  134. def test_09_standalone_clone(self):
  135. self.dialog.name.setText("test-vm")
  136. for i in range(self.dialog.vm_type.count()):
  137. opt_text = self.dialog.vm_type.itemText(i).lower()
  138. if "standalone" in opt_text and "template" in opt_text and\
  139. "not based" not in opt_text and "empty" not in opt_text:
  140. self.dialog.vm_type.setCurrentIndex(i)
  141. break
  142. self.__click_ok()
  143. self.mock_thread.assert_called_once_with(
  144. self.qapp, "StandaloneVM", "test-vm",
  145. unittest.mock.ANY, unittest.mock.ANY,
  146. unittest.mock.ANY, unittest.mock.ANY)
  147. @unittest.mock.patch('subprocess.check_call')
  148. def test_10_standalone_empty(self, mock_call):
  149. self.dialog.name.setText("test-vm")
  150. for i in range(self.dialog.vm_type.count()):
  151. opt_text = self.dialog.vm_type.itemText(i).lower()
  152. if "standalone" in opt_text and\
  153. ("not based" in opt_text or "empty" in opt_text):
  154. self.dialog.vm_type.setCurrentIndex(i)
  155. break
  156. self.__click_ok()
  157. self.mock_thread.assert_called_once_with(
  158. self.qapp, "StandaloneVM", "test-vm",
  159. unittest.mock.ANY, None,
  160. unittest.mock.ANY, unittest.mock.ANY)
  161. self.mock_thread().msg = None
  162. self.dialog.create_finished()
  163. mock_call.assert_called_once_with(['qubes-vm-boot-from-device',
  164. 'test-vm'])
  165. @unittest.mock.patch('subprocess.check_call')
  166. def test_11_standalone_empty_not_install(self, mock_call):
  167. self.dialog.name.setText("test-vm")
  168. for i in range(self.dialog.vm_type.count()):
  169. opt_text = self.dialog.vm_type.itemText(i).lower()
  170. if "standalone" in opt_text and\
  171. ("not based" in opt_text or "empty" in opt_text):
  172. self.dialog.vm_type.setCurrentIndex(i)
  173. break
  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 and "standalone" not 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 and "template" in opt_text and\
  197. "not based" not in opt_text and "empty" not in opt_text:
  198. self.dialog.vm_type.setCurrentIndex(i)
  199. break
  200. self.assertFalse(self.dialog.install_system.isEnabled())
  201. self.assertTrue(self.dialog.launch_settings.isEnabled())
  202. self.assertTrue(self.dialog.template_vm.isEnabled())
  203. # cannot set a template but can install system on a truly empty AppVM
  204. for i in range(self.dialog.vm_type.count()):
  205. opt_text = self.dialog.vm_type.itemText(i).lower()
  206. if "standalone" in opt_text and\
  207. ("not based" in opt_text or "empty" in opt_text):
  208. self.dialog.vm_type.setCurrentIndex(i)
  209. break
  210. self.assertTrue(self.dialog.install_system.isEnabled())
  211. self.assertTrue(self.dialog.launch_settings.isEnabled())
  212. self.assertFalse(self.dialog.template_vm.isEnabled())
  213. def __click_ok(self):
  214. okwidget = self.dialog.buttonBox.button(
  215. self.dialog.buttonBox.Ok)
  216. QtTest.QTest.mouseClick(okwidget, QtCore.Qt.LeftButton)
  217. def __click_cancel(self):
  218. cancelwidget = self.dialog.buttonBox.button(
  219. self.dialog.buttonBox.Cancel)
  220. QtTest.QTest.mouseClick(cancelwidget, QtCore.Qt.LeftButton)
  221. # class CreatteVMThreadTest(unittest.TestCase):
  222. if __name__ == "__main__":
  223. ha_syslog = logging.handlers.SysLogHandler('/dev/log')
  224. ha_syslog.setFormatter(
  225. logging.Formatter('%(name)s[%(process)d]: %(message)s'))
  226. logging.root.addHandler(ha_syslog)
  227. unittest.main()