test_clone_vm.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #!/usr/bin/python3
  2. #
  3. # The Qubes OS Project, https://www.qubes-os.org/
  4. #
  5. # Copyright (C) 2020 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 clone_vm
  29. # TODO: test when you do give a src vm
  30. class CloneVMTest(unittest.TestCase):
  31. def setUp(self):
  32. super(CloneVMTest, 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.common_threads.CloneVMThread')
  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. # mock the progress dialog to speed testing up
  46. self.patcher_warning = unittest.mock.patch(
  47. 'PyQt5.QtWidgets.QMessageBox.warning')
  48. self.mock_warning = self.patcher_warning.start()
  49. self.addCleanup(self.patcher_warning.stop)
  50. self.dialog = clone_vm.CloneVMDlg(self.qtapp, self.qapp)
  51. def test_00_window_loads(self):
  52. self.assertGreater(self.dialog.src_vm.count(), 0,
  53. "No source vms shown")
  54. self.assertGreater(self.dialog.label.count(), 0, "No labels listed")
  55. self.assertGreater(self.dialog.storage_pool.count(), 0,
  56. "No pools listed")
  57. self.assertTrue(self.dialog.src_vm.isEnabled(),
  58. "source vm dialog not active")
  59. def test_01_cancel_works(self):
  60. self.__click_cancel()
  61. self.assertEqual(self.mock_thread.call_count, 0,
  62. "Attempted to create VM on cancel")
  63. def test_02_name_correctly_updates(self):
  64. src_name = self.dialog.src_vm.currentText()
  65. target_name = self.dialog.name.text()
  66. self.assertTrue(target_name.startswith(src_name),
  67. "target name does not contain source name")
  68. self.assertTrue('clone' in target_name,
  69. "target name does not contain >clone<")
  70. self.dialog.src_vm.setCurrentIndex(self.dialog.src_vm.currentIndex()+1)
  71. src_name = self.dialog.src_vm.currentText()
  72. target_name = self.dialog.name.text()
  73. self.assertTrue(target_name.startswith(src_name),
  74. "target name does not contain source name")
  75. self.assertTrue('clone' in target_name,
  76. "target name does not contain >clone<")
  77. def test_03_label_correctly_updates(self):
  78. src_label = self.dialog.src_vm.currentData().label.name
  79. target_label = self.dialog.label.currentText()
  80. self.assertEqual(src_label, target_label, "incorrect start label")
  81. while self.dialog.src_vm.currentData().label.name == src_label:
  82. self.dialog.src_vm.setCurrentIndex(
  83. self.dialog.src_vm.currentIndex() + 1)
  84. src_label = self.dialog.src_vm.currentData().label.name
  85. target_label = self.dialog.label.currentText()
  86. self.assertEqual(src_label, target_label,
  87. "label did not change correctly")
  88. def test_04_clone_first_vm(self):
  89. self.dialog.name.setText("clone-test")
  90. src_vm = self.qapp.domains[self.dialog.src_vm.currentText()]
  91. self.__click_ok()
  92. self.mock_thread.assert_called_once_with(
  93. src_vm, "clone-test", pool=None, label=src_vm.label)
  94. self.mock_thread().start.assert_called_once_with()
  95. def test_05_clone_other_vm(self):
  96. self.dialog.src_vm.setCurrentIndex(self.dialog.src_vm.currentIndex()+1)
  97. src_vm = self.qapp.domains[self.dialog.src_vm.currentText()]
  98. dst_name = self.dialog.name.text()
  99. self.__click_ok()
  100. self.mock_thread.assert_called_once_with(
  101. src_vm, dst_name, pool=None, label=src_vm.label)
  102. self.mock_thread().start.assert_called_once_with()
  103. def test_06_clone_label(self):
  104. src_vm = self.qapp.domains[self.dialog.src_vm.currentText()]
  105. dst_name = self.dialog.name.text()
  106. while self.dialog.label.currentText() != 'blue':
  107. self.dialog.label.setCurrentIndex(
  108. self.dialog.label.currentIndex()+1)
  109. self.__click_ok()
  110. self.mock_thread.assert_called_once_with(
  111. src_vm, dst_name, pool=None, label=self.qapp.labels['blue'])
  112. self.mock_thread().start.assert_called_once_with()
  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("clone-test")
  117. self.__click_ok()
  118. self.mock_thread.assert_called_once_with(
  119. unittest.mock.ANY, "clone-test", pool=None,
  120. label=unittest.mock.ANY)
  121. self.mock_thread().msg = ("Success", "Success")
  122. self.dialog.clone_finished()
  123. mock_call.assert_called_once_with(['qubes-vm-settings', "clone-test"])
  124. def test_08_progress_hides(self):
  125. self.dialog.name.setText("clone-test")
  126. self.__click_ok()
  127. self.mock_thread.assert_called_once_with(
  128. unittest.mock.ANY, "clone-test", pool=None,
  129. label=unittest.mock.ANY)
  130. # make sure the thread is not reporting an error
  131. self.mock_thread().start.assert_called_once_with()
  132. self.mock_thread().msg = ("Success", "Success")
  133. self.mock_progress().show.assert_called_once_with()
  134. self.dialog.clone_finished()
  135. self.mock_progress().hide.assert_called_once_with()
  136. def test_09_pool_nondefault(self):
  137. while 'default' in self.dialog.storage_pool.currentText():
  138. self.dialog.storage_pool.setCurrentIndex(
  139. self.dialog.storage_pool.currentIndex()+1)
  140. selected_pool = self.dialog.storage_pool.currentText()
  141. self.__click_ok()
  142. self.mock_thread.assert_called_once_with(
  143. unittest.mock.ANY, unittest.mock.ANY,
  144. pool=selected_pool,
  145. label=unittest.mock.ANY)
  146. self.mock_thread().start.assert_called_once_with()
  147. def __click_ok(self):
  148. okwidget = self.dialog.buttonBox.button(
  149. self.dialog.buttonBox.Ok)
  150. QtTest.QTest.mouseClick(okwidget, QtCore.Qt.LeftButton)
  151. def __click_cancel(self):
  152. cancelwidget = self.dialog.buttonBox.button(
  153. self.dialog.buttonBox.Cancel)
  154. QtTest.QTest.mouseClick(cancelwidget, QtCore.Qt.LeftButton)
  155. class CloneVMTestSrcVM(unittest.TestCase):
  156. def setUp(self):
  157. super(CloneVMTestSrcVM, self).setUp()
  158. self.qtapp, self.loop = init_qtapp()
  159. self.qapp = Qubes()
  160. # mock up the Create VM Thread to avoid changing system state
  161. self.patcher_thread = unittest.mock.patch(
  162. 'qubesmanager.common_threads.CloneVMThread')
  163. self.mock_thread = self.patcher_thread.start()
  164. self.addCleanup(self.patcher_thread.stop)
  165. # mock the progress dialog to speed testing up
  166. self.patcher_progress = unittest.mock.patch(
  167. 'PyQt5.QtWidgets.QProgressDialog')
  168. self.mock_progress = self.patcher_progress.start()
  169. self.addCleanup(self.patcher_progress.stop)
  170. # mock the progress dialog to speed testing up
  171. self.patcher_warning = unittest.mock.patch(
  172. 'PyQt5.QtWidgets.QMessageBox.warning')
  173. self.mock_warning = self.patcher_warning.start()
  174. self.addCleanup(self.patcher_warning.stop)
  175. self.src_vm = next(
  176. domain for domain in self.qapp.domains
  177. if domain.klass != 'AdminVM')
  178. self.dialog = clone_vm.CloneVMDlg(self.qtapp, self.qapp,
  179. src_vm=self.src_vm)
  180. def test_00_window_loads(self):
  181. self.assertEqual(self.dialog.src_vm.currentText(), self.src_vm.name)
  182. self.assertEqual(self.dialog.src_vm.currentData(), self.src_vm)
  183. self.assertFalse(self.dialog.src_vm.isEnabled(),
  184. "source vm dialog active")
  185. self.assertEqual(self.dialog.label.currentText(),
  186. self.src_vm.label.name)
  187. def test_01_simple_clone(self):
  188. self.dialog.name.setText("clone-test")
  189. self.__click_ok()
  190. self.mock_thread.assert_called_once_with(
  191. self.src_vm, "clone-test", pool=None, label=self.src_vm.label)
  192. self.mock_thread().start.assert_called_once_with()
  193. def __click_ok(self):
  194. okwidget = self.dialog.buttonBox.button(
  195. self.dialog.buttonBox.Ok)
  196. QtTest.QTest.mouseClick(okwidget, QtCore.Qt.LeftButton)
  197. if __name__ == "__main__":
  198. ha_syslog = logging.handlers.SysLogHandler('/dev/log')
  199. ha_syslog.setFormatter(
  200. logging.Formatter('%(name)s[%(process)d]: %(message)s'))
  201. logging.root.addHandler(ha_syslog)
  202. unittest.main()