From 4c2adbca009aa8924cb03364f08b62d782bcb09c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marta=20Marczykowska-G=C3=B3recka?= Date: Thu, 17 Oct 2019 00:16:01 +0200 Subject: [PATCH] Tests fixed for new features and bugfixes After switching to Qt5, a bunch of fixes and changes were introduced. This fixes tests to take them into account. --- qubesmanager/tests/test_backup.py | 92 ++++++++----- qubesmanager/tests/test_backup_utils.py | 15 +-- qubesmanager/tests/test_create_new_vm.py | 70 +++++----- qubesmanager/tests/test_global_settings.py | 6 - qubesmanager/tests/test_qube_manager.py | 55 ++++---- qubesmanager/tests/test_vm_settings.py | 150 ++++++++++----------- 6 files changed, 194 insertions(+), 194 deletions(-) diff --git a/qubesmanager/tests/test_backup.py b/qubesmanager/tests/test_backup.py index 341d1b5..013033e 100644 --- a/qubesmanager/tests/test_backup.py +++ b/qubesmanager/tests/test_backup.py @@ -27,10 +27,26 @@ from PyQt5 import QtTest, QtCore, QtWidgets from qubesadmin import Qubes, events, utils, exc from qubesmanager import backup from qubesmanager.tests import init_qtapp -import asyncio class BackupTest(unittest.TestCase): + @classmethod + def setUpClass(cls): + qapp = Qubes() + + cls.dom0_name = "dom0" + + cls.vms = [] + cls.running_vm = None + + for vm in qapp.domains: + if vm.klass != "AdminVM" and vm.is_running(): + cls.running_vm = vm.name + if vm.klass != "AdminVM" and vm.get_disk_utilization() > 0: + cls.vms.append(vm.name) + if cls.running_vm and len(cls.vms) >= 3: + break + def setUp(self): super(BackupTest, self).setUp() self.qtapp, self.loop = init_qtapp() @@ -55,9 +71,7 @@ class BackupTest(unittest.TestCase): self.dialog.show() def tearDown(self): - self.dialog.close() - self.qtapp.processEvents() - yield from asyncio.sleep(1) + self.dialog.done(0) super(BackupTest, self).tearDown() def test_00_window_loads(self): @@ -110,7 +124,7 @@ class BackupTest(unittest.TestCase): self.dialog.select_vms_widget.available_list.count(), "Remove All VMs does not work") - self._select_vm("work") + self._select_vm(self.vms[0]) self.assertEqual(self.dialog.select_vms_widget.selected_list.count(), 1, "Select a single VM does not work") @@ -179,12 +193,16 @@ class BackupTest(unittest.TestCase): def test_07_disk_space_correct(self): for i in range(self.dialog.select_vms_widget.available_list.count()): item = self.dialog.select_vms_widget.available_list.item(i) - if item.vm.name == "dom0" or item.vm.get_disk_utilization() > 0: + if item.vm.name == self.dom0_name or \ + item.vm.get_disk_utilization() > 0: self.assertGreater( item.size, 0, "{} size incorrectly reported as 0".format(item.vm.name)) def test_08_total_size_correct(self): + if len(self.vms) < 3: + self.skipTest("Insufficient number of VMs with positive " + "disk utilization") # select nothing self.dialog.select_vms_widget.remove_all_button.click() self.assertEqual(self.dialog.total_size_label.text(), "0", @@ -192,27 +210,27 @@ class BackupTest(unittest.TestCase): current_size = 0 # select a single VM - self._select_vm("sys-net") + self._select_vm(self.vms[0]) - current_size += self.qapp.domains["sys-net"].get_disk_utilization() + current_size += self.qapp.domains[self.vms[0]].get_disk_utilization() self.assertEqual(self.dialog.total_size_label.text(), utils.size_to_human(current_size), "Size incorrectly listed for a single VM") # add two more - self._select_vm("sys-firewall") - self._select_vm("work") + self._select_vm(self.vms[1]) + self._select_vm(self.vms[2]) - current_size += self.qapp.domains["sys-firewall"].get_disk_utilization() - current_size += self.qapp.domains["work"].get_disk_utilization() + current_size += self.qapp.domains[self.vms[1]].get_disk_utilization() + current_size += self.qapp.domains[self.vms[2]].get_disk_utilization() self.assertEqual(self.dialog.total_size_label.text(), utils.size_to_human(current_size), "Size incorrectly listed for several VMs") # remove one - self._deselect_vm("sys-net") - current_size -= self.qapp.domains["sys-net"].get_disk_utilization() + self._deselect_vm(self.vms[0]) + current_size -= self.qapp.domains[self.vms[0]].get_disk_utilization() self.assertEqual(self.dialog.total_size_label.text(), utils.size_to_human(current_size), "Size incorrectly listed for several VMs") @@ -225,7 +243,7 @@ class BackupTest(unittest.TestCase): is self.dialog.select_vms_page) self.dialog.select_vms_widget.remove_all_button.click() - self._select_vm("work") + self._select_vm(self.vms[0]) self._click_next() @@ -233,16 +251,16 @@ class BackupTest(unittest.TestCase): is self.dialog.select_dir_page) # setup backup - self._select_location("dom0") + self._select_location(self.dom0_name) self.dialog.dir_line_edit.setText("/home") self.dialog.passphrase_line_edit.setText("pass") self.dialog.passphrase_line_edit_verify.setText("pass") self.dialog.save_profile_checkbox.setChecked(True) self.dialog.turn_off_checkbox.setChecked(False) self.dialog.compress_checkbox.setChecked(False) - expected_settings = {'destination_vm': "dom0", + expected_settings = {'destination_vm': self.dom0_name, 'destination_path': "/home", - 'include': ["work"], + 'include': [self.vms[0]], 'passphrase_text': "pass", 'compression': False} with unittest.mock.patch.object(self.dialog.textEdit, 'setText')\ @@ -260,7 +278,8 @@ class BackupTest(unittest.TestCase): # make sure the backup is executed self._click_next() - self.mock_thread.assert_called_once_with(self.qapp.domains["dom0"]) + self.mock_thread.assert_called_once_with( + self.qapp.domains[self.dom0_name]) self.mock_thread().start.assert_called_once_with() @unittest.mock.patch('qubesmanager.backup_utils.write_backup_profile') @@ -271,9 +290,9 @@ class BackupTest(unittest.TestCase): is self.dialog.select_vms_page) self.dialog.select_vms_widget.remove_all_button.click() - self._select_vm("work") - self._select_vm("sys-net") - self._select_vm("dom0") + self._select_vm(self.dom0_name) + self._select_vm(self.vms[0]) + self._select_vm(self.vms[1]) self._click_next() @@ -281,16 +300,17 @@ class BackupTest(unittest.TestCase): is self.dialog.select_dir_page) # setup backup - self._select_location("sys-net") + self._select_location(self.running_vm) self.dialog.dir_line_edit.setText("/home") self.dialog.passphrase_line_edit.setText("longerPassPhrase") self.dialog.passphrase_line_edit_verify.setText("longerPassPhrase") self.dialog.save_profile_checkbox.setChecked(False) self.dialog.turn_off_checkbox.setChecked(False) self.dialog.compress_checkbox.setChecked(True) - expected_settings = {'destination_vm': "sys-net", + expected_settings = {'destination_vm': self.running_vm, 'destination_path': "/home", - 'include': ["dom0", "sys-net", "work"], + 'include': sorted([self.dom0_name, self.vms[0], + self.vms[1]]), 'passphrase_text': "longerPassPhrase", 'compression': True} with unittest.mock.patch.object(self.dialog.textEdit, 'setText')\ @@ -308,16 +328,17 @@ class BackupTest(unittest.TestCase): # make sure the backup is executed self._click_next() - self.mock_thread.assert_called_once_with(self.qapp.domains["sys-net"]) + self.mock_thread.assert_called_once_with( + self.qapp.domains[self.running_vm]) self.mock_thread().start.assert_called_once_with() @unittest.mock.patch('qubesmanager.backup_utils.load_backup_profile') def test_20_loading_settings(self, mock_load): mock_load.return_value = { - 'destination_vm': "sys-net", + 'destination_vm': self.running_vm, 'destination_path': "/home", - 'include': ["dom0", "sys-net", "work"], + 'include': [self.dom0_name, self.vms[0], self.vms[1]], 'passphrase_text': "longerPassPhrase", 'compression': True } @@ -331,7 +352,8 @@ class BackupTest(unittest.TestCase): self.dialog.show() # check if settings were loaded - self.assertEqual(self.dialog.appvm_combobox.currentText(), "sys-net", + self.assertEqual(self.dialog.appvm_combobox.currentText(), + self.running_vm, "Destination VM not loaded") self.assertEqual(self.dialog.dir_line_edit.text(), "/home", "Destination path not loaded") @@ -396,7 +418,7 @@ class BackupTest(unittest.TestCase): self.assertTrue(self.dialog.currentPage() is self.dialog.select_dir_page) - self._select_location("dom0") + self._select_location(self.dom0_name) self.dialog.dir_line_edit.setText("/home") self.dialog.passphrase_line_edit.setText("pass") self.dialog.passphrase_line_edit_verify.setText("pass") @@ -418,7 +440,7 @@ class BackupTest(unittest.TestCase): self.assertTrue(self.dialog.currentPage() is self.dialog.select_dir_page) - self._select_location("dom0") + self._select_location(self.dom0_name) self.dialog.dir_line_edit.setText("/home") self.dialog.passphrase_line_edit.setText("pass") self.dialog.passphrase_line_edit_verify.setText("pass") @@ -446,7 +468,7 @@ class BackupTest(unittest.TestCase): self.assertTrue(self.dialog.currentPage() is self.dialog.select_dir_page) - self._select_location("dom0") + self._select_location(self.dom0_name) self.dialog.dir_line_edit.setText("/home") self.dialog.passphrase_line_edit.setText("pass") self.dialog.passphrase_line_edit_verify.setText("pass") @@ -486,7 +508,7 @@ class BackupTest(unittest.TestCase): self.assertTrue(self.dialog.currentPage() is self.dialog.select_dir_page) - self._select_location("dom0") + self._select_location(self.dom0_name) self.dialog.dir_line_edit.setText("/home") self.dialog.passphrase_line_edit.setText("pass") self.dialog.passphrase_line_edit_verify.setText("pass") @@ -525,7 +547,7 @@ class BackupTest(unittest.TestCase): self.assertTrue(self.dialog.currentPage() is self.dialog.select_dir_page) - self._select_location("dom0") + self._select_location(self.dom0_name) self.dialog.dir_line_edit.setText("/home") self.dialog.passphrase_line_edit.setText("pass") self.dialog.passphrase_line_edit_verify.setText("pass") @@ -563,7 +585,7 @@ class BackupTest(unittest.TestCase): self.assertTrue(self.dialog.currentPage() is self.dialog.select_dir_page) - self._select_location("dom0") + self._select_location(self.dom0_name) self.dialog.dir_line_edit.setText("/home") self.dialog.passphrase_line_edit.setText("pass") self.dialog.passphrase_line_edit_verify.setText("pass") diff --git a/qubesmanager/tests/test_backup_utils.py b/qubesmanager/tests/test_backup_utils.py index ae125c7..615b9f4 100644 --- a/qubesmanager/tests/test_backup_utils.py +++ b/qubesmanager/tests/test_backup_utils.py @@ -21,29 +21,18 @@ # import logging.handlers import unittest.mock -import quamash -import asyncio -import sys from PyQt5 import QtWidgets from qubesadmin import Qubes from qubesmanager import backup_utils - -qtapp = QtWidgets.QApplication(sys.argv) -loop = quamash.QEventLoop(qtapp) -asyncio.set_event_loop(loop) +from qubesmanager.tests import init_qtapp class BackupUtilsTest(unittest.TestCase): def setUp(self): super(BackupUtilsTest, self).setUp() + self.qtapp, self.loop = init_qtapp() self.qapp = Qubes() - self.loop = quamash.QEventLoop(qtapp) - - def tearDown(self): - qtapp.processEvents() - yield from loop.sleep(1) - super(BackupUtilsTest, self).tearDown() def test_01_fill_apvms(self): dialog = QtWidgets.QDialog() diff --git a/qubesmanager/tests/test_create_new_vm.py b/qubesmanager/tests/test_create_new_vm.py index 06d836f..e701f53 100644 --- a/qubesmanager/tests/test_create_new_vm.py +++ b/qubesmanager/tests/test_create_new_vm.py @@ -51,12 +51,6 @@ class NewVmTest(unittest.TestCase): self.dialog = create_new_vm.NewVmDlg(self.qtapp, self.qapp) - def tearDown(self): - self.dialog.close() - self.qtapp.processEvents() - yield from self.loop.sleep(1) - super(NewVmTest, self).tearDown() - def test_00_window_loads(self): self.assertGreater(self.dialog.template_vm.count(), 0, "No templates shown") @@ -68,13 +62,13 @@ class NewVmTest(unittest.TestCase): "Attempted to create VM on cancel") def test_02_create_simple_vm(self): - self.dialog.name.setText("testvm") + self.dialog.name.setText("test-vm") self.__click_ok() self.mock_thread.assert_called_once_with( - self.qapp, "AppVM", "testvm", + self.qapp, "AppVM", "test-vm", unittest.mock.ANY, qubesadmin.DEFAULT, - {'provides_network': False}) + {'provides_network': False}, unittest.mock.ANY) self.mock_thread().start.assert_called_once_with() def test_03_label(self): @@ -83,13 +77,13 @@ class NewVmTest(unittest.TestCase): self.dialog.label.setCurrentIndex(i) break - self.dialog.name.setText("testvm") + self.dialog.name.setText("test-vm") self.__click_ok() self.mock_thread.assert_called_once_with( - self.qapp, "AppVM", "testvm", + self.qapp, "AppVM", "test-vm", self.qapp.labels['blue'], qubesadmin.DEFAULT, - unittest.mock.ANY) + unittest.mock.ANY, unittest.mock.ANY) self.mock_thread().start.assert_called_once_with() def test_04_template(self): @@ -100,13 +94,13 @@ class NewVmTest(unittest.TestCase): template = self.dialog.template_vm.currentText() break - self.dialog.name.setText("testvm") + self.dialog.name.setText("test-vm") self.__click_ok() self.mock_thread.assert_called_once_with( - self.qapp, "AppVM", "testvm", + self.qapp, "AppVM", "test-vm", unittest.mock.ANY, template, - unittest.mock.ANY) + unittest.mock.ANY, unittest.mock.ANY) def test_05_netvm(self): netvm = None @@ -116,53 +110,53 @@ class NewVmTest(unittest.TestCase): netvm = self.dialog.netvm.currentText() break - self.dialog.name.setText("testvm") + self.dialog.name.setText("test-vm") self.__click_ok() self.mock_thread.assert_called_once_with( - self.qapp, "AppVM", "testvm", + self.qapp, "AppVM", "test-vm", unittest.mock.ANY, unittest.mock.ANY, - {'netvm': netvm, 'provides_network': False}) + {'netvm': netvm, 'provides_network': False}, unittest.mock.ANY) def test_06_provides_network(self): self.dialog.provides_network.setChecked(True) - self.dialog.name.setText("testvm") + self.dialog.name.setText("test-vm") self.__click_ok() self.mock_thread.assert_called_once_with( - self.qapp, "AppVM", "testvm", + self.qapp, "AppVM", "test-vm", unittest.mock.ANY, unittest.mock.ANY, - {'provides_network': True}) + {'provides_network': True}, unittest.mock.ANY) @unittest.mock.patch('subprocess.check_call') def test_07_launch_settings(self, mock_call): self.dialog.launch_settings.setChecked(True) - self.dialog.name.setText("testvm") + self.dialog.name.setText("test-vm") self.__click_ok() # make sure the thread is not reporting an error self.mock_thread.assert_called_once_with( - self.qapp, "AppVM", "testvm", + self.qapp, "AppVM", "test-vm", unittest.mock.ANY, unittest.mock.ANY, - unittest.mock.ANY) + unittest.mock.ANY, unittest.mock.ANY) self.mock_thread().msg = None self.dialog.create_finished() - mock_call.assert_called_once_with(['qubes-vm-settings', "testvm"]) + mock_call.assert_called_once_with(['qubes-vm-settings', "test-vm"]) def test_08_progress_hides(self): - self.dialog.name.setText("testvm") + self.dialog.name.setText("test-vm") self.__click_ok() self.mock_thread.assert_called_once_with( - self.qapp, "AppVM", "testvm", + self.qapp, "AppVM", "test-vm", unittest.mock.ANY, unittest.mock.ANY, - unittest.mock.ANY) + unittest.mock.ANY, unittest.mock.ANY) # make sure the thread is not reporting an error self.mock_thread().start.assert_called_once_with() @@ -175,7 +169,7 @@ class NewVmTest(unittest.TestCase): self.mock_progress().hide.assert_called_once_with() def test_09_standalone_clone(self): - self.dialog.name.setText("testvm") + self.dialog.name.setText("test-vm") for i in range(self.dialog.vm_type.count()): opt_text = self.dialog.vm_type.itemText(i).lower() if "standalone" in opt_text and "template" in opt_text and\ @@ -185,13 +179,13 @@ class NewVmTest(unittest.TestCase): self.__click_ok() self.mock_thread.assert_called_once_with( - self.qapp, "StandaloneVM", "testvm", + self.qapp, "StandaloneVM", "test-vm", unittest.mock.ANY, unittest.mock.ANY, - unittest.mock.ANY) + unittest.mock.ANY, unittest.mock.ANY) @unittest.mock.patch('subprocess.check_call') def test_10_standalone_empty(self, mock_call): - self.dialog.name.setText("testvm") + self.dialog.name.setText("test-vm") for i in range(self.dialog.vm_type.count()): opt_text = self.dialog.vm_type.itemText(i).lower() if "standalone" in opt_text and\ @@ -201,19 +195,19 @@ class NewVmTest(unittest.TestCase): self.__click_ok() self.mock_thread.assert_called_once_with( - self.qapp, "StandaloneVM", "testvm", + self.qapp, "StandaloneVM", "test-vm", unittest.mock.ANY, None, - unittest.mock.ANY) + unittest.mock.ANY, unittest.mock.ANY) self.mock_thread().msg = None self.dialog.create_finished() mock_call.assert_called_once_with(['qubes-vm-boot-from-device', - 'testvm']) + 'test-vm']) @unittest.mock.patch('subprocess.check_call') def test_11_standalone_empty_not_install(self, mock_call): - self.dialog.name.setText("testvm") + self.dialog.name.setText("test-vm") for i in range(self.dialog.vm_type.count()): opt_text = self.dialog.vm_type.itemText(i).lower() @@ -226,9 +220,9 @@ class NewVmTest(unittest.TestCase): self.__click_ok() self.mock_thread.assert_called_once_with( - self.qapp, "StandaloneVM", "testvm", + self.qapp, "StandaloneVM", "test-vm", unittest.mock.ANY, None, - unittest.mock.ANY) + unittest.mock.ANY, unittest.mock.ANY) self.mock_thread().msg = None self.dialog.create_finished() diff --git a/qubesmanager/tests/test_global_settings.py b/qubesmanager/tests/test_global_settings.py index e0f53c5..d3d6b80 100644 --- a/qubesmanager/tests/test_global_settings.py +++ b/qubesmanager/tests/test_global_settings.py @@ -43,12 +43,6 @@ class GlobalSettingsTest(unittest.TestCase): self.setattr_mock = self.setattr_patcher.start() self.addCleanup(self.setattr_patcher.stop) - def tearDown(self): - self.dialog.close() - self.qtapp.processEvents() - yield from self.loop.sleep(1) - super(GlobalSettingsTest, self).tearDown() - def test_00_settings_started(self): # non-empty drop-downs self.assertNotEqual( diff --git a/qubesmanager/tests/test_qube_manager.py b/qubesmanager/tests/test_qube_manager.py index 92f62e2..720b5f1 100644 --- a/qubesmanager/tests/test_qube_manager.py +++ b/qubesmanager/tests/test_qube_manager.py @@ -34,6 +34,7 @@ from qubesadmin import Qubes, events, exc import qubesmanager.qube_manager as qube_manager from qubesmanager.tests import init_qtapp + class QubeManagerTest(unittest.TestCase): def setUp(self): super(QubeManagerTest, self).setUp() @@ -51,12 +52,6 @@ class QubeManagerTest(unittest.TestCase): self.dialog = qube_manager.VmManagerWindow( self.qtapp, self.qapp, self.dispatcher) - def tearDown(self): - self.dialog.close() - self.qtapp.processEvents() - yield from self.loop.sleep(1) - super(QubeManagerTest, self).tearDown() - def test_000_window_loads(self): self.assertTrue(self.dialog.table is not None, "Window did not load") @@ -182,7 +177,11 @@ class QubeManagerTest(unittest.TestCase): vm = self._get_table_item(row, "Name").vm def_dispvm_item = self._get_table_item(row, "Default DispVM") - def_dispvm_value = getattr(vm, "default_dispvm", None) + if vm.property_is_default("default_dispvm"): + def_dispvm_value = "default ({})".format( + self.qapp.default_dispvm) + else: + def_dispvm_value = getattr(vm, "default_dispvm", None) self.assertEqual( str(def_dispvm_value), def_dispvm_item.text(), @@ -247,10 +246,12 @@ class QubeManagerTest(unittest.TestCase): def test_100_sorting(self): - self.dialog.table.sortByColumn(self.dialog.columns_indices["Template"]) + self.dialog.table.sortByColumn(self.dialog.columns_indices["Template"], + QtCore.Qt.AscendingOrder) self.__check_sorting("Template") - self.dialog.table.sortByColumn(self.dialog.columns_indices["Name"]) + self.dialog.table.sortByColumn(self.dialog.columns_indices["Name"], + QtCore.Qt.AscendingOrder) self.__check_sorting("Name") @unittest.mock.patch('qubesmanager.qube_manager.QtCore.QSettings.setValue') @@ -466,7 +467,7 @@ class QubeManagerTest(unittest.TestCase): action = self.dialog.action_removevm mock_vm = unittest.mock.Mock(spec=['name'], - **{'name.return_value': 'testvm'}) + **{'name.return_value': 'test-vm'}) mock_dependencies.return_value = [(mock_vm, "test_prop")] action.trigger() @@ -767,10 +768,10 @@ class QubeManagerTest(unittest.TestCase): def test_400_event_domain_added(self): number_of_vms = self.dialog.table.rowCount() - self.addCleanup(subprocess.call, ["qvm-remove", "-f", "testvm"]) + self.addCleanup(subprocess.call, ["qvm-remove", "-f", "test-vm"]) self._run_command_and_process_events( - ["qvm-create", "--label", "red", "testvm"]) + ["qvm-create", "--label", "red", "test-vm"]) # a single row was added to the table self.assertEqual(self.dialog.table.rowCount(), number_of_vms + 1) @@ -791,26 +792,26 @@ class QubeManagerTest(unittest.TestCase): # try opening settings for the added vm for row in range(self.dialog.table.rowCount()): name = self._get_table_item(row, "Name") - if name.text() == "testvm": + if name.text() == "test-vm": self.dialog.table.setCurrentItem(name) break with unittest.mock.patch('qubesmanager.settings.VMSettingsWindow')\ as mock_settings: self.dialog.action_settings.trigger() mock_settings.assert_called_once_with( - self.qapp.domains["testvm"], self.qtapp, "basic") + self.qapp.domains["test-vm"], self.qtapp, "basic") def test_401_event_domain_removed(self): initial_vms = self._create_set_of_current_vms() self._run_command_and_process_events( - ["qvm-create", "--label", "red", "testvm"]) + ["qvm-create", "--label", "red", "test-vm"]) current_vms = self._create_set_of_current_vms() self.assertEqual(len(initial_vms) + 1, len(current_vms)) self._run_command_and_process_events( - ["qvm-remove", "--force", "testvm"]) + ["qvm-remove", "--force", "test-vm"]) current_vms = self._create_set_of_current_vms() self.assertEqual(initial_vms, current_vms) @@ -1186,12 +1187,13 @@ class QubeManagerTest(unittest.TestCase): events :param command: list of strings, containing the command and all its parameters - :param timeout: default 20 seconds + :param timeout: default 5 seconds :return: """ asyncio.set_event_loop(self.loop) future1 = asyncio.ensure_future(self.dispatcher.listen_for_events()) + self.loop.run_until_complete(asyncio.sleep(0)) future2 = asyncio.create_subprocess_exec(*command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) @@ -1398,7 +1400,7 @@ class QubeManagerThreadTest(unittest.TestCase): class VMShutdownMonitorTest(unittest.TestCase): - @unittest.mock.patch('PyQt5.QtWidgets.QMessageBox.question') + @unittest.mock.patch('PyQt5.QtWidgets.QMessageBox') @unittest.mock.patch('PyQt5.QtCore.QTimer') def test_01_vm_shutdown_correct(self, mock_timer, mock_question): mock_vm = unittest.mock.Mock() @@ -1413,10 +1415,12 @@ class VMShutdownMonitorTest(unittest.TestCase): self.assertEqual(mock_timer.call_count, 0) monitor.restart_vm_if_needed.assert_called_once_with() - @unittest.mock.patch('PyQt5.QtWidgets.QMessageBox.question', - return_value=1) + @unittest.mock.patch('PyQt5.QtWidgets.QMessageBox') @unittest.mock.patch('PyQt5.QtCore.QTimer.singleShot') def test_02_vm_not_shutdown_wait(self, mock_timer, mock_question): + mock_question().clickedButton.return_value = 1 + mock_question().addButton.return_value = 0 + mock_vm = unittest.mock.Mock() mock_vm.is_running.return_value = True mock_vm.start_time = datetime.datetime.now().timestamp() - 3000 @@ -1426,13 +1430,14 @@ class VMShutdownMonitorTest(unittest.TestCase): monitor.check_if_vm_has_shutdown() - self.assertEqual(mock_question.call_count, 1) self.assertEqual(mock_timer.call_count, 1) - @unittest.mock.patch('PyQt5.QtWidgets.QMessageBox.question', - return_value=0) + @unittest.mock.patch('PyQt5.QtWidgets.QMessageBox') @unittest.mock.patch('PyQt5.QtCore.QTimer.singleShot') def test_03_vm_kill(self, mock_timer, mock_question): + mock_question().clickedButton.return_value = 1 + mock_question().addButton.return_value = 1 + mock_vm = unittest.mock.Mock() mock_vm.is_running.return_value = True mock_vm.start_time = datetime.datetime.now().timestamp() - 3000 @@ -1443,13 +1448,11 @@ class VMShutdownMonitorTest(unittest.TestCase): monitor.check_if_vm_has_shutdown() - self.assertEqual(mock_question.call_count, 1) self.assertEqual(mock_timer.call_count, 0) mock_vm.kill.assert_called_once_with() monitor.restart_vm_if_needed.assert_called_once_with() - @unittest.mock.patch('PyQt5.QtWidgets.QMessageBox.question', - return_value=0) + @unittest.mock.patch('PyQt5.QtWidgets.QMessageBox') @unittest.mock.patch('PyQt5.QtCore.QTimer.singleShot') def test_04_check_later(self, mock_timer, mock_question): mock_vm = unittest.mock.Mock() diff --git a/qubesmanager/tests/test_vm_settings.py b/qubesmanager/tests/test_vm_settings.py index d5ca782..646116f 100644 --- a/qubesmanager/tests/test_vm_settings.py +++ b/qubesmanager/tests/test_vm_settings.py @@ -23,10 +23,7 @@ import logging.handlers import unittest import unittest.mock -import gc -import asyncio - -from PyQt5 import QtTest, QtCore, QtWidgets +from PyQt5 import QtTest, QtCore from qubesadmin import Qubes import qubesmanager.settings as vm_settings from qubesmanager.tests import init_qtapp @@ -44,64 +41,63 @@ class VMSettingsTest(unittest.TestCase): self.addCleanup(self.mock_qprogress.stop) self.qapp = Qubes() - - if "testvm" in self.qapp.domains: - del self.qapp.domains["testvm"] + + if "test-vm" in self.qapp.domains: + del self.qapp.domains["test-vm"] def tearDown(self): - del self.qapp.domains["testvm"] - self.qtapp.processEvents() - yield from self.loop.sleep(1) - + if "test-vm" in self.qapp.domains: + del self.qapp.domains["test-vm"] super(VMSettingsTest, self).tearDown() def test_00_load_correct_tab(self): - self.vm = self.qapp.add_new_vm("AppVM", "testvm", "red") + self.vm = self.qapp.add_new_vm("AppVM", "test-vm", "red") self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "basic") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, init_page="basic") self.assertTrue( self.dialog.tabWidget.currentWidget() is self.dialog.basic_tab) self.dialog.deleteLater() self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "advanced") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, init_page="advanced") self.assertTrue( self.dialog.tabWidget.currentWidget() is self.dialog.advanced_tab) self.dialog.deleteLater() self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "firewall") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, init_page="firewall") self.assertTrue( self.dialog.tabWidget.currentWidget() is self.dialog.firewall_tab) self.dialog.deleteLater() self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "devices") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, init_page="devices") self.assertTrue( self.dialog.tabWidget.currentWidget() is self.dialog.devices_tab) self.dialog.deleteLater() self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "applications") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, + init_page="applications") self.assertTrue( self.dialog.tabWidget.currentWidget() is self.dialog.apps_tab) self.dialog.deleteLater() self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "services") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, init_page="services") self.assertTrue( self.dialog.tabWidget.currentWidget() is self.dialog.services_tab) self.dialog.deleteLater() def test_01_basic_tab_default(self): - self.vm = self.qapp.add_new_vm("AppVM", "testvm", "blue") + self.vm = self.qapp.add_new_vm("AppVM", "test-vm", "blue") # set the vm to have a default template and netvm self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "basic") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, init_page="basic") - self.assertEqual(self.dialog.vmname.text(), "testvm", + self.assertEqual(self.dialog.vmname.text(), "test-vm", "Name displayed incorrectly") self.assertTrue("blue" in self.dialog.vmlabel.currentText(), @@ -156,12 +152,12 @@ class VMSettingsTest(unittest.TestCase): "Incorrect max private root size") def test_02_basic_tab_nones(self): - self.vm = self.qapp.add_new_vm("StandaloneVM", "testvm", "blue") + self.vm = self.qapp.add_new_vm("StandaloneVM", "test-vm", "blue") # set the vm to have a default template and netvm self.vm.netvm = None self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "basic") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, init_page="basic") self.assertEqual("", self.dialog.template_name.currentText(), "No template incorrectly displayed") @@ -185,12 +181,11 @@ class VMSettingsTest(unittest.TestCase): "---", "Incorrect gateway displayed") - @unittest.expectedFailure def test_03_change_label(self): - # this test fails due to error where we check whether label is visible - self.vm = self.qapp.add_new_vm("AppVM", "testvm", "blue") + self.vm = self.qapp.add_new_vm("AppVM", "test-vm", "blue") self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "basic") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, init_page="basic") + self.dialog.show() new_label = self._set_noncurrent(self.dialog.vmlabel) self._click_ok() @@ -199,9 +194,9 @@ class VMSettingsTest(unittest.TestCase): "Label is not set correctly") def test_04_change_template(self): - self.vm = self.qapp.add_new_vm("AppVM", "testvm", "blue") + self.vm = self.qapp.add_new_vm("AppVM", "test-vm", "blue") self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "basic") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, init_page="basic") new_template = self._set_noncurrent(self.dialog.template_name) self._click_ok() @@ -210,9 +205,9 @@ class VMSettingsTest(unittest.TestCase): "Template is not set correctly") def test_05_change_networking(self): - self.vm = self.qapp.add_new_vm("AppVM", "testvm", "blue") + self.vm = self.qapp.add_new_vm("AppVM", "test-vm", "blue") self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "basic") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, init_page="basic") new_netvm = self._set_noncurrent(self.dialog.netVM) self._click_ok() @@ -221,9 +216,9 @@ class VMSettingsTest(unittest.TestCase): "NetVM is not set correctly") def test_06_change_networking_none(self): - self.vm = self.qapp.add_new_vm("AppVM", "testvm", "blue") + self.vm = self.qapp.add_new_vm("AppVM", "test-vm", "blue") self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "basic") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, init_page="basic") self._set_none(self.dialog.netVM) self._click_ok() @@ -232,7 +227,7 @@ class VMSettingsTest(unittest.TestCase): "None netVM is not set correctly") def test_07_change_networking_to_default(self): - self.vm = self.qapp.add_new_vm("AppVM", "testvm", "blue") + self.vm = self.qapp.add_new_vm("AppVM", "test-vm", "blue") for vm in self.qapp.domains: if getattr(vm, 'provides_network', False)\ @@ -241,7 +236,7 @@ class VMSettingsTest(unittest.TestCase): break self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "basic") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, init_page="basic") new_netvm = self._set_default(self.dialog.netVM) self._click_ok() @@ -250,11 +245,11 @@ class VMSettingsTest(unittest.TestCase): "NetVM is not set correctly") self.assertTrue(self.vm.property_is_default('netvm')) - @unittest.expectedFailure def test_08_basic_checkboxes_true(self): - self.vm = self.qapp.add_new_vm("AppVM", "testvm", "blue") + self.vm = self.qapp.add_new_vm("AppVM", "test-vm", "blue") self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "basic") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, init_page="basic") + self.dialog.show() self.dialog.include_in_backups.setChecked(True) self.dialog.autostart_vm.setChecked(True) @@ -269,11 +264,11 @@ class VMSettingsTest(unittest.TestCase): self.assertTrue(self.vm.debug, "Debug mode not set to true") - @unittest.expectedFailure def test_09_basic_checkboxes_false(self): - self.vm = self.qapp.add_new_vm("AppVM", "testvm", "blue") + self.vm = self.qapp.add_new_vm("AppVM", "test-vm", "blue") self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "basic") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, init_page="basic") + self.dialog.show() self.dialog.include_in_backups.setChecked(False) self.dialog.autostart_vm.setChecked(False) @@ -289,9 +284,9 @@ class VMSettingsTest(unittest.TestCase): "Debug mode not set to false") def test_10_increase_private_storage(self): - self.vm = self.qapp.add_new_vm("AppVM", "testvm", "blue") + self.vm = self.qapp.add_new_vm("AppVM", "test-vm", "blue") self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "basic") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, init_page="basic") current_storage = self.vm.volumes['private'].size // 1024**2 new_storage = current_storage + 512 @@ -308,16 +303,16 @@ class VMSettingsTest(unittest.TestCase): @unittest.mock.patch('PyQt5.QtWidgets.QInputDialog.getText') @unittest.mock.patch('qubesmanager.settings.RenameVMThread') def test_11_rename_vm(self, mock_thread, mock_input, _): - self.vm = self.qapp.add_new_vm("AppVM", "testvm", "blue") + self.vm = self.qapp.add_new_vm("AppVM", "test-vm", "blue") self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "basic") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, init_page="basic") self.assertTrue(self.dialog.rename_vm_button.isEnabled()) - mock_input.return_value = ("testvm2", True) + mock_input.return_value = ("test-vm2", True) self.dialog.rename_vm_button.click() - mock_thread.assert_called_with(self.vm, "testvm2", unittest.mock.ANY) + mock_thread.assert_called_with(self.vm, "test-vm2", unittest.mock.ANY) mock_thread().start.assert_called_with() # TODO: thread tests for rename @@ -326,16 +321,16 @@ class VMSettingsTest(unittest.TestCase): @unittest.mock.patch('PyQt5.QtWidgets.QInputDialog.getText') @unittest.mock.patch('qubesmanager.common_threads.CloneVMThread') def test_12_clone_vm(self, mock_thread, mock_input, _): - self.vm = self.qapp.add_new_vm("AppVM", "testvm", "blue") + self.vm = self.qapp.add_new_vm("AppVM", "test-vm", "blue") self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "basic") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, init_page="basic") self.assertTrue(self.dialog.clone_vm_button.isEnabled()) - mock_input.return_value = ("testvm2", True) + mock_input.return_value = ("test-vm2", True) self.dialog.clone_vm_button.click() - mock_thread.assert_called_with(self.vm, "testvm2") + mock_thread.assert_called_with(self.vm, "test-vm2") mock_thread().start.assert_called_with() @unittest.mock.patch('PyQt5.QtWidgets.QMessageBox.warning') @@ -343,19 +338,19 @@ class VMSettingsTest(unittest.TestCase): @unittest.mock.patch('PyQt5.QtWidgets.QInputDialog.getText') @unittest.mock.patch('qubesmanager.common_threads.RemoveVMThread') def test_13_remove_vm(self, mock_thread, mock_input, _, mock_warning): - self.vm = self.qapp.add_new_vm("AppVM", "testvm", "blue") + self.vm = self.qapp.add_new_vm("AppVM", "test-vm", "blue") self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "basic") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, init_page="basic") self.assertTrue(self.dialog.delete_vm_button.isEnabled()) # try with a wrong name - mock_input.return_value = ("testvm2", True) + mock_input.return_value = ("test-vm2", True) self.dialog.delete_vm_button.click() self.assertEqual(mock_warning.call_count, 1) # and now correct one - mock_input.return_value = ("testvm", True) + mock_input.return_value = ("test-vm", True) self.dialog.delete_vm_button.click() mock_thread.assert_called_with(self.vm) @@ -363,9 +358,9 @@ class VMSettingsTest(unittest.TestCase): # Advanced Tab def test_20_advanced_loads(self): - self.vm = self.qapp.add_new_vm("AppVM", "testvm", "blue") + self.vm = self.qapp.add_new_vm("AppVM", "test-vm", "blue") self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "advanced") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, init_page="advanced") self.assertEqual(self.dialog.init_mem.value(), self.vm.memory, "Incorrect initial memory") @@ -396,11 +391,11 @@ class VMSettingsTest(unittest.TestCase): self.assertTrue("PVH" in self.dialog.virt_mode.currentText()) def test_21_nondefaultmaxmem(self): - self.vm = self.qapp.add_new_vm("AppVM", "testvm", "blue") + self.vm = self.qapp.add_new_vm("AppVM", "test-vm", "blue") self.vm.maxmem = 5000 self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "advanced") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, init_page="advanced") self.assertEqual(self.dialog.max_mem_size.value(), 5000) @@ -412,7 +407,7 @@ class VMSettingsTest(unittest.TestCase): self.dialog.deleteLater() self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "advanced") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, init_page="advanced") self.assertFalse(self.dialog.include_in_balancing.isChecked()) self.dialog.include_in_balancing.setChecked(True) @@ -422,11 +417,11 @@ class VMSettingsTest(unittest.TestCase): self.assertEqual(self.vm.maxmem, 5000) def test_22_initmem(self): - self.vm = self.qapp.add_new_vm("AppVM", "testvm", "blue") + self.vm = self.qapp.add_new_vm("AppVM", "test-vm", "blue") self.vm.memory = 500 self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "advanced") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, init_page="advanced") self.assertEqual(self.dialog.init_mem.value(), 500, "Incorrect initial memory") @@ -436,11 +431,11 @@ class VMSettingsTest(unittest.TestCase): self.assertEqual(self.vm.memory, 600, "Setting initial memory failed") def test_23_vcpus(self): - self.vm = self.qapp.add_new_vm("AppVM", "testvm", "blue") + self.vm = self.qapp.add_new_vm("AppVM", "test-vm", "blue") self.vm.vcpus = 1 self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "advanced") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, init_page="advanced") self.assertEqual(self.dialog.vcpus.value(), 1, "Incorrect number of VCPUs") @@ -451,9 +446,10 @@ class VMSettingsTest(unittest.TestCase): "Incorrect number of VCPUs") def test_24_kernel(self): - self.vm = self.qapp.add_new_vm("AppVM", "testvm", "blue") + self.vm = self.qapp.add_new_vm("AppVM", "test-vm", "blue") self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "advanced") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, init_page="advanced") + self.dialog.show() new_kernel = self._set_noncurrent(self.dialog.kernel) self._click_ok() @@ -463,20 +459,22 @@ class VMSettingsTest(unittest.TestCase): self.dialog.deleteLater() self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "advanced") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, init_page="advanced") + self.dialog.show() self._set_default(self.dialog.kernel) self._click_ok() self.assertTrue(self.vm.property_is_default('kernel')) def test_25_virtmode_change(self): - self.vm = self.qapp.add_new_vm("AppVM", "testvm", "blue") + self.vm = self.qapp.add_new_vm("AppVM", "test-vm", "blue") modes = ["HVM", "PVH", "PV"] for mode in modes: self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "advanced") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, + init_page="advanced") self._set_value(self.dialog.virt_mode, mode) self._click_ok() @@ -486,17 +484,17 @@ class VMSettingsTest(unittest.TestCase): self.dialog.deleteLater() self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "advanced") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, init_page="advanced") self._set_default(self.dialog.virt_mode) self._click_ok() self.assertTrue(self.vm.property_is_default('virt_mode')) def test_26_default_dispvm(self): - self.vm = self.qapp.add_new_vm("AppVM", "testvm", "blue") + self.vm = self.qapp.add_new_vm("AppVM", "test-vm", "blue") self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "advanced") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, init_page="advanced") new_dvm = self._set_noncurrent(self.dialog.default_dispvm) self._click_ok() @@ -506,7 +504,7 @@ class VMSettingsTest(unittest.TestCase): self.dialog.deleteLater() self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "advanced") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, init_page="advanced") self._set_default(self.dialog.default_dispvm) self._click_ok() @@ -514,13 +512,13 @@ class VMSettingsTest(unittest.TestCase): @unittest.mock.patch('subprocess.check_call') def test_27_boot_cdrom(self, mock_call): - self.vm = self.qapp.add_new_vm("AppVM", "testvm", "blue") + self.vm = self.qapp.add_new_vm("AppVM", "test-vm", "blue") self.dialog = vm_settings.VMSettingsWindow( - self.vm, self.qtapp, "advanced") + self.vm, qapp=self.qtapp, qubesapp=self.qapp, init_page="advanced") self.dialog.boot_from_device_button.click() - mock_call.assert_called_with(['qubes-vm-boot-from-device', "testvm"]) + mock_call.assert_called_with(['qubes-vm-boot-from-device', "test-vm"]) def _click_ok(self): okwidget = self.dialog.buttonBox.button(