2018-03-28 17:42:42 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
#
|
|
|
|
# The Qubes OS Project, https://www.qubes-os.org/
|
|
|
|
#
|
2018-09-16 18:04:56 +02:00
|
|
|
# Copyright (C) 2016 Marta Marczykowska-Górecka
|
|
|
|
# <marmarta@invisiblethingslab.com>
|
2018-03-28 17:42:42 +02:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
#
|
2019-04-30 15:58:11 +02:00
|
|
|
import asyncio
|
|
|
|
import contextlib
|
2018-03-28 17:42:42 +02:00
|
|
|
import logging.handlers
|
|
|
|
import unittest
|
|
|
|
import unittest.mock
|
2019-04-30 15:58:11 +02:00
|
|
|
|
|
|
|
import subprocess
|
|
|
|
import datetime
|
|
|
|
import time
|
2018-03-28 17:42:42 +02:00
|
|
|
|
2019-10-15 00:00:18 +02:00
|
|
|
from PyQt5 import QtTest, QtCore, QtWidgets
|
2020-07-30 02:40:40 +02:00
|
|
|
from PyQt5.QtCore import (Qt, QSize)
|
|
|
|
from PyQt5.QtGui import (QIcon)
|
|
|
|
|
2019-04-30 15:58:11 +02:00
|
|
|
from qubesadmin import Qubes, events, exc
|
2018-03-28 17:42:42 +02:00
|
|
|
import qubesmanager.qube_manager as qube_manager
|
2019-10-15 00:00:18 +02:00
|
|
|
from qubesmanager.tests import init_qtapp
|
2018-03-28 17:42:42 +02:00
|
|
|
|
2019-10-17 00:16:01 +02:00
|
|
|
|
2020-08-14 12:38:21 +02:00
|
|
|
icon_size = qube_manager.icon_size
|
|
|
|
|
2020-07-30 02:40:40 +02:00
|
|
|
|
2018-03-28 17:42:42 +02:00
|
|
|
class QubeManagerTest(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
super(QubeManagerTest, self).setUp()
|
2019-10-15 00:00:18 +02:00
|
|
|
self.qtapp, self.loop = init_qtapp()
|
2018-03-28 17:42:42 +02:00
|
|
|
|
2019-10-15 00:00:18 +02:00
|
|
|
self.mock_qprogress = unittest.mock.patch(
|
|
|
|
'PyQt5.QtWidgets.QProgressDialog')
|
2019-04-30 15:58:11 +02:00
|
|
|
self.mock_qprogress.start()
|
|
|
|
|
|
|
|
self.addCleanup(self.mock_qprogress.stop)
|
2018-03-28 17:42:42 +02:00
|
|
|
|
|
|
|
self.qapp = Qubes()
|
2019-04-30 15:58:11 +02:00
|
|
|
self.dispatcher = events.EventsDispatcher(self.qapp)
|
|
|
|
|
|
|
|
self.dialog = qube_manager.VmManagerWindow(
|
|
|
|
self.qtapp, self.qapp, self.dispatcher)
|
2018-03-28 17:42:42 +02:00
|
|
|
|
2019-04-30 15:58:11 +02:00
|
|
|
def test_000_window_loads(self):
|
|
|
|
self.assertTrue(self.dialog.table is not None, "Window did not load")
|
2018-03-28 17:42:42 +02:00
|
|
|
|
2019-04-30 15:58:11 +02:00
|
|
|
def test_001_correct_vms_listed(self):
|
2018-03-28 17:42:42 +02:00
|
|
|
vms_in_table = []
|
2019-04-30 15:58:11 +02:00
|
|
|
|
2020-07-30 02:40:40 +02:00
|
|
|
for row in range(self.dialog.table.model().rowCount()):
|
|
|
|
vm = self._get_table_vm(row)
|
2019-04-30 15:58:11 +02:00
|
|
|
self.assertIsNotNone(vm)
|
|
|
|
vms_in_table.append(vm.name)
|
|
|
|
|
|
|
|
# check that name is listed correctly
|
|
|
|
name_item = self._get_table_item(row, "Name")
|
2020-07-30 02:40:40 +02:00
|
|
|
self.assertEqual(name_item, vm.name,
|
2019-04-30 15:58:11 +02:00
|
|
|
"Incorrect VM name for {}".format(vm.name))
|
2018-03-28 17:42:42 +02:00
|
|
|
|
|
|
|
actual_vms = [vm.name for vm in self.qapp.domains]
|
|
|
|
|
|
|
|
self.assertEqual(len(vms_in_table), len(actual_vms),
|
|
|
|
"Incorrect number of VMs loaded")
|
|
|
|
self.assertListEqual(sorted(vms_in_table), sorted(actual_vms),
|
|
|
|
"Incorrect VMs loaded")
|
2019-04-30 15:58:11 +02:00
|
|
|
|
|
|
|
def test_002_correct_template_listed(self):
|
2020-07-30 02:40:40 +02:00
|
|
|
for row in range(self.dialog.table.model().rowCount()):
|
|
|
|
vm = self._get_table_vm(row)
|
2019-04-30 15:58:11 +02:00
|
|
|
# check that template is listed correctly
|
|
|
|
template_item = self._get_table_item(row, "Template")
|
|
|
|
if getattr(vm, "template", None):
|
|
|
|
self.assertEqual(vm.template,
|
2020-07-30 02:40:40 +02:00
|
|
|
template_item,
|
2019-04-30 15:58:11 +02:00
|
|
|
"Incorrect template for {}".format(vm.name))
|
|
|
|
else:
|
2020-07-30 02:40:40 +02:00
|
|
|
self.assertEqual(vm.klass, template_item,
|
2019-04-30 15:58:11 +02:00
|
|
|
"Incorrect class for {}".format(vm.name))
|
|
|
|
|
|
|
|
def test_003_correct_netvm_listed(self):
|
2020-07-30 02:40:40 +02:00
|
|
|
for row in range(self.dialog.table.model().rowCount()):
|
|
|
|
vm = self._get_table_vm(row)
|
2019-04-30 15:58:11 +02:00
|
|
|
|
|
|
|
# check that netvm is listed correctly
|
|
|
|
netvm_item = self._get_table_item(row, "NetVM")
|
|
|
|
netvm_value = getattr(vm, "netvm", None)
|
2020-07-30 02:40:40 +02:00
|
|
|
|
|
|
|
if not netvm_value:
|
|
|
|
netvm_value = "n/a"
|
|
|
|
|
2019-04-30 15:58:11 +02:00
|
|
|
if netvm_value and hasattr(vm, "netvm") \
|
|
|
|
and vm.property_is_default("netvm"):
|
|
|
|
netvm_value = "default ({})".format(netvm_value)
|
|
|
|
|
|
|
|
self.assertEqual(netvm_value,
|
2020-07-30 02:40:40 +02:00
|
|
|
netvm_item,
|
2019-04-30 15:58:11 +02:00
|
|
|
"Incorrect netvm for {}".format(vm.name))
|
|
|
|
|
|
|
|
def test_004_correct_disk_usage_listed(self):
|
2020-07-30 02:40:40 +02:00
|
|
|
for row in range(self.dialog.table.model().rowCount()):
|
|
|
|
vm = self._get_table_vm(row)
|
2019-04-30 15:58:11 +02:00
|
|
|
|
2020-07-30 02:40:40 +02:00
|
|
|
size_item = self._get_table_item(row, "Disk Usage")
|
2019-04-30 15:58:11 +02:00
|
|
|
if vm.klass == 'AdminVM':
|
|
|
|
size_value = "n/a"
|
|
|
|
else:
|
|
|
|
size_value = round(vm.get_disk_utilization() / (1024 * 1024), 2)
|
|
|
|
size_value = str(size_value) + " MiB"
|
|
|
|
|
|
|
|
self.assertEqual(size_value,
|
2020-07-30 02:40:40 +02:00
|
|
|
size_item,
|
2019-04-30 15:58:11 +02:00
|
|
|
"Incorrect size for {}".format(vm.name))
|
|
|
|
|
|
|
|
def test_005_correct_internal_listed(self):
|
2020-07-30 02:40:40 +02:00
|
|
|
for row in range(self.dialog.table.model().rowCount()):
|
|
|
|
vm = self._get_table_vm(row)
|
2019-04-30 15:58:11 +02:00
|
|
|
|
|
|
|
internal_item = self._get_table_item(row, "Internal")
|
|
|
|
internal_value = "Yes" if vm.features.get('internal', False) else ""
|
|
|
|
|
2020-07-30 02:40:40 +02:00
|
|
|
self.assertEqual(internal_item, internal_value,
|
2019-04-30 15:58:11 +02:00
|
|
|
"Incorrect internal value for {}".format(vm.name))
|
|
|
|
|
|
|
|
def test_006_correct_ip_listed(self):
|
2020-07-30 02:40:40 +02:00
|
|
|
for row in range(self.dialog.table.model().rowCount()):
|
|
|
|
vm = self._get_table_vm(row)
|
2019-04-30 15:58:11 +02:00
|
|
|
|
|
|
|
ip_item = self._get_table_item(row, "IP")
|
|
|
|
if hasattr(vm, 'ip'):
|
|
|
|
ip_value = getattr(vm, 'ip')
|
|
|
|
ip_value = "" if ip_value is None else ip_value
|
|
|
|
else:
|
|
|
|
ip_value = "n/a"
|
|
|
|
|
2020-07-30 02:40:40 +02:00
|
|
|
self.assertEqual(ip_value, ip_item,
|
2019-04-30 15:58:11 +02:00
|
|
|
"Incorrect ip value for {}".format(vm.name))
|
|
|
|
|
|
|
|
def test_007_incl_in_backups_listed(self):
|
2020-07-30 02:40:40 +02:00
|
|
|
for row in range(self.dialog.table.model().rowCount()):
|
|
|
|
vm = self._get_table_vm(row)
|
2019-04-30 15:58:11 +02:00
|
|
|
|
2021-01-09 05:55:34 +01:00
|
|
|
incl_backups_item = self._get_table_item(row, "Backup", Qt.CheckStateRole) == Qt.Checked
|
2019-04-30 15:58:11 +02:00
|
|
|
incl_backups_value = getattr(vm, 'include_in_backups', False)
|
|
|
|
|
|
|
|
self.assertEqual(
|
2020-07-30 02:40:40 +02:00
|
|
|
incl_backups_value, incl_backups_item,
|
2019-04-30 15:58:11 +02:00
|
|
|
"Incorrect include in backups value for {}".format(vm.name))
|
|
|
|
|
|
|
|
def test_008_last_backup_listed(self):
|
2020-07-30 02:40:40 +02:00
|
|
|
for row in range(self.dialog.table.model().rowCount()):
|
|
|
|
vm = self._get_table_vm(row)
|
2019-04-30 15:58:11 +02:00
|
|
|
|
|
|
|
last_backup_item = self._get_table_item(row, "Last backup")
|
|
|
|
last_backup_value = getattr(vm, 'backup_timestamp', None)
|
|
|
|
|
|
|
|
if last_backup_value:
|
|
|
|
last_backup_value = str(
|
|
|
|
datetime.datetime.fromtimestamp(last_backup_value))
|
|
|
|
|
|
|
|
self.assertEqual(
|
2020-07-30 02:40:40 +02:00
|
|
|
last_backup_value, last_backup_item,
|
2019-04-30 15:58:11 +02:00
|
|
|
"Incorrect last backup value for {}".format(vm.name))
|
|
|
|
|
|
|
|
def test_009_def_dispvm_listed(self):
|
2020-07-30 02:40:40 +02:00
|
|
|
for row in range(self.dialog.table.model().rowCount()):
|
|
|
|
vm = self._get_table_vm(row)
|
2019-04-30 15:58:11 +02:00
|
|
|
|
|
|
|
def_dispvm_item = self._get_table_item(row, "Default DispVM")
|
2019-10-17 00:16:01 +02:00
|
|
|
if vm.property_is_default("default_dispvm"):
|
|
|
|
def_dispvm_value = "default ({})".format(
|
2021-01-09 05:55:34 +01:00
|
|
|
vm.property_get_default("default_dispvm"))
|
2019-10-17 00:16:01 +02:00
|
|
|
else:
|
|
|
|
def_dispvm_value = getattr(vm, "default_dispvm", None)
|
2019-04-30 15:58:11 +02:00
|
|
|
|
|
|
|
self.assertEqual(
|
2020-07-30 02:40:40 +02:00
|
|
|
def_dispvm_value, def_dispvm_item,
|
2019-04-30 15:58:11 +02:00
|
|
|
"Incorrect default dispvm value for {}".format(vm.name))
|
|
|
|
|
|
|
|
def test_010_is_dvm_template_listed(self):
|
2020-07-30 02:40:40 +02:00
|
|
|
for row in range(self.dialog.table.model().rowCount()):
|
|
|
|
vm = self._get_table_vm(row)
|
2019-04-30 15:58:11 +02:00
|
|
|
|
|
|
|
is_dvm_template_item = self._get_table_item(row, "Is DVM Template")
|
|
|
|
is_dvm_template_value = "Yes" if \
|
|
|
|
getattr(vm, "template_for_dispvms", False) else ""
|
|
|
|
|
|
|
|
self.assertEqual(
|
2020-07-30 02:40:40 +02:00
|
|
|
is_dvm_template_value, is_dvm_template_item,
|
2019-04-30 15:58:11 +02:00
|
|
|
"Incorrect is DVM template value for {}".format(vm.name))
|
|
|
|
|
|
|
|
def test_011_is_label_correct(self):
|
2020-07-30 02:40:40 +02:00
|
|
|
for row in range(self.dialog.table.model().rowCount()):
|
|
|
|
vm = self._get_table_vm(row)
|
2020-08-14 12:38:21 +02:00
|
|
|
icon = QIcon.fromTheme(getattr(vm, 'icon', 'appvm-black'))
|
2020-07-30 02:40:40 +02:00
|
|
|
icon = icon.pixmap(icon_size)
|
2019-04-30 15:58:11 +02:00
|
|
|
|
2020-07-30 02:40:40 +02:00
|
|
|
label_pixmap = self._get_table_item(row, "Label", Qt.DecorationRole)
|
2019-04-30 15:58:11 +02:00
|
|
|
|
2020-07-30 02:40:40 +02:00
|
|
|
self.assertEqual(label_pixmap.toImage(), icon.toImage())
|
2019-04-30 15:58:11 +02:00
|
|
|
|
2020-07-30 02:40:40 +02:00
|
|
|
def test_012_is_state_correct(self):
|
|
|
|
for row in range(self.dialog.table.model().rowCount()):
|
|
|
|
vm = self._get_table_vm(row)
|
2019-04-30 15:58:11 +02:00
|
|
|
|
2020-07-30 02:40:40 +02:00
|
|
|
displayed_power_state = self._get_table_item(row, "State")['power']
|
2019-04-30 15:58:11 +02:00
|
|
|
|
|
|
|
self.assertEqual(
|
2020-07-30 02:40:40 +02:00
|
|
|
displayed_power_state, vm.get_power_state(),
|
2019-04-30 15:58:11 +02:00
|
|
|
"Wrong power state displayed for {}".format(vm.name))
|
|
|
|
|
|
|
|
def test_013_incorrect_settings_file(self):
|
|
|
|
mock_settings = unittest.mock.MagicMock(spec=QtCore.QSettings)
|
|
|
|
|
|
|
|
settings_result_dict = {"view/sort_column": "Cthulhu",
|
|
|
|
"view/sort_order": "Fhtagn",
|
|
|
|
"view/menubar_visible": "R'lyeh"
|
|
|
|
}
|
|
|
|
|
|
|
|
mock_settings.side_effect = (
|
|
|
|
lambda x, *args, **kwargs: settings_result_dict.get(x))
|
|
|
|
|
2019-10-15 00:00:18 +02:00
|
|
|
with unittest.mock.patch('PyQt5.QtCore.QSettings.value',
|
|
|
|
mock_settings),\
|
|
|
|
unittest.mock.patch('PyQt5.QtWidgets.QMessageBox.warning')\
|
2019-04-30 20:52:56 +02:00
|
|
|
as mock_warning:
|
2019-04-30 15:58:11 +02:00
|
|
|
self.dialog = qube_manager.VmManagerWindow(
|
|
|
|
self.qtapp, self.qapp, self.dispatcher)
|
2019-04-30 20:52:56 +02:00
|
|
|
self.assertEqual(mock_warning.call_count, 1)
|
2019-04-30 15:58:11 +02:00
|
|
|
|
|
|
|
def test_100_sorting(self):
|
2020-07-30 02:40:40 +02:00
|
|
|
col = self.dialog.qubes_model.columns_indices.index("Template")
|
|
|
|
self.dialog.table.sortByColumn(col, QtCore.Qt.AscendingOrder)
|
2019-04-30 15:58:11 +02:00
|
|
|
self.__check_sorting("Template")
|
|
|
|
|
2020-07-30 02:40:40 +02:00
|
|
|
col = self.dialog.qubes_model.columns_indices.index("Name")
|
|
|
|
self.dialog.table.sortByColumn(col, QtCore.Qt.AscendingOrder)
|
2019-04-30 15:58:11 +02:00
|
|
|
self.__check_sorting("Name")
|
|
|
|
|
2020-07-30 02:40:40 +02:00
|
|
|
@unittest.mock.patch('qubesmanager.qube_manager.QSettings.setValue')
|
|
|
|
def test_101_hide_column(self, mock_settings):
|
|
|
|
model = self.dialog.qubes_model
|
|
|
|
action_no = model.columns_indices.index('Is DVM Template')
|
|
|
|
self.dialog.menu_view.actions()[action_no].trigger()
|
2019-04-30 15:58:11 +02:00
|
|
|
mock_settings.assert_called_with('columns/Is DVM Template', True)
|
2020-07-30 02:40:40 +02:00
|
|
|
|
|
|
|
self.dialog.menu_view.actions()[action_no].trigger()
|
|
|
|
mock_settings.assert_called_with('columns/Is DVM Template', False)
|
2018-03-28 17:42:42 +02:00
|
|
|
|
|
|
|
@unittest.mock.patch('qubesmanager.settings.VMSettingsWindow')
|
2019-04-30 15:58:11 +02:00
|
|
|
def test_200_vm_open_settings(self, mock_window):
|
2018-03-28 17:42:42 +02:00
|
|
|
selected_vm = self._select_non_admin_vm()
|
|
|
|
self.assertIsNotNone(selected_vm, "No valid non-admin VM found")
|
|
|
|
widget = self.dialog.toolbar.widgetForAction(
|
|
|
|
self.dialog.action_settings)
|
|
|
|
QtTest.QTest.mouseClick(widget,
|
|
|
|
QtCore.Qt.LeftButton)
|
2019-04-30 15:58:11 +02:00
|
|
|
mock_window.assert_called_once_with(
|
2020-06-30 14:44:20 +02:00
|
|
|
selected_vm, qapp=self.qtapp, init_page="basic", qubesapp=self.qapp)
|
2019-04-30 15:58:11 +02:00
|
|
|
|
|
|
|
def test_201_vm_open_settings_admin(self):
|
|
|
|
self._select_admin_vm()
|
|
|
|
|
|
|
|
self.assertFalse(self.dialog.action_settings.isEnabled(),
|
|
|
|
"Settings not disabled for admin VM")
|
|
|
|
self.assertFalse(self.dialog.action_editfwrules.isEnabled(),
|
2020-07-30 02:40:40 +02:00
|
|
|
"Editfw not disabled for admin VM")
|
2019-04-30 15:58:11 +02:00
|
|
|
self.assertFalse(self.dialog.action_appmenus.isEnabled(),
|
2020-07-30 02:40:40 +02:00
|
|
|
"Appmenus not disabled for admin VM")
|
2018-03-28 17:42:42 +02:00
|
|
|
|
|
|
|
@unittest.mock.patch('qubesmanager.settings.VMSettingsWindow')
|
2019-04-30 15:58:11 +02:00
|
|
|
def test_202_vm_open_firewall(self, mock_window):
|
2018-03-28 17:42:42 +02:00
|
|
|
selected_vm = self._select_non_admin_vm()
|
|
|
|
self.assertIsNotNone(selected_vm, "No valid non-admin VM found")
|
|
|
|
widget = self.dialog.toolbar.widgetForAction(
|
|
|
|
self.dialog.action_editfwrules)
|
|
|
|
QtTest.QTest.mouseClick(widget,
|
|
|
|
QtCore.Qt.LeftButton)
|
2019-04-30 15:58:11 +02:00
|
|
|
mock_window.assert_called_once_with(
|
2020-06-30 14:44:20 +02:00
|
|
|
selected_vm, qapp=self.qtapp, init_page="firewall",
|
|
|
|
qubesapp=self.qapp)
|
2019-04-30 15:58:11 +02:00
|
|
|
|
|
|
|
@unittest.mock.patch('qubesmanager.settings.VMSettingsWindow')
|
|
|
|
def test_203_vm_open_apps(self, mock_window):
|
|
|
|
selected_vm = self._select_non_admin_vm()
|
|
|
|
self.assertIsNotNone(selected_vm, "No valid non-admin VM found")
|
|
|
|
widget = self.dialog.toolbar.widgetForAction(
|
|
|
|
self.dialog.action_appmenus)
|
|
|
|
QtTest.QTest.mouseClick(widget,
|
|
|
|
QtCore.Qt.LeftButton)
|
|
|
|
mock_window.assert_called_once_with(
|
2020-06-30 14:44:20 +02:00
|
|
|
selected_vm, qapp=self.qtapp, init_page="applications",
|
|
|
|
qubesapp=self.qapp)
|
2019-04-30 15:58:11 +02:00
|
|
|
|
2020-10-08 02:48:40 +02:00
|
|
|
@unittest.mock.patch('PyQt5.QtWidgets.QMessageBox.warning')
|
|
|
|
def test_204_vm_keyboard(self, mock_message):
|
2019-04-30 15:58:11 +02:00
|
|
|
selected_vm = self._select_non_admin_vm(running=True)
|
|
|
|
self.assertIsNotNone(selected_vm, "No valid non-admin VM found")
|
2021-01-09 05:55:34 +01:00
|
|
|
if 'supported-feature.keyboard-layout' not in selected_vm.features:
|
|
|
|
self.skipTest("VM {!s} does not support new layout change".format(selected_vm))
|
2019-04-30 15:58:11 +02:00
|
|
|
widget = self.dialog.toolbar.widgetForAction(
|
|
|
|
self.dialog.action_set_keyboard_layout)
|
|
|
|
with unittest.mock.patch.object(selected_vm, 'run') as mock_run:
|
|
|
|
QtTest.QTest.mouseClick(widget,
|
|
|
|
QtCore.Qt.LeftButton)
|
|
|
|
mock_run.assert_called_once_with("qubes-change-keyboard-layout")
|
2020-10-08 02:48:40 +02:00
|
|
|
self.assertEqual(mock_message.call_count, 0,
|
|
|
|
"VM does not support new layout change")
|
2019-04-30 15:58:11 +02:00
|
|
|
|
2020-10-08 02:48:40 +02:00
|
|
|
@unittest.mock.patch('PyQt5.QtWidgets.QMessageBox.warning')
|
|
|
|
def test_205_vm_keyboard_not_running(self, mock_message):
|
2019-04-30 15:58:11 +02:00
|
|
|
selected_vm = self._select_non_admin_vm(running=False)
|
|
|
|
self.assertIsNotNone(selected_vm, "No valid non-admin VM found")
|
|
|
|
widget = self.dialog.toolbar.widgetForAction(
|
|
|
|
self.dialog.action_set_keyboard_layout)
|
|
|
|
with unittest.mock.patch.object(selected_vm, 'run') as mock_run:
|
|
|
|
QtTest.QTest.mouseClick(widget,
|
|
|
|
QtCore.Qt.LeftButton)
|
|
|
|
self.assertEqual(mock_run.call_count, 0,
|
|
|
|
"Keyboard change called on a halted VM")
|
|
|
|
|
|
|
|
def test_206_dom0_keyboard(self):
|
|
|
|
self._select_admin_vm()
|
|
|
|
self.assertFalse(self.dialog.action_set_keyboard_layout.isEnabled())
|
|
|
|
|
2019-10-15 00:00:18 +02:00
|
|
|
@unittest.mock.patch("PyQt5.QtWidgets.QMessageBox.question",
|
|
|
|
return_value=QtWidgets.QMessageBox.Yes)
|
2019-04-30 15:58:11 +02:00
|
|
|
def test_207_update_vm_not_running(self, _):
|
|
|
|
selected_vm = self._select_templatevm(running=False)
|
|
|
|
self.assertIsNotNone(selected_vm, "No valid template VM found")
|
|
|
|
|
|
|
|
widget = self.dialog.toolbar.widgetForAction(
|
|
|
|
self.dialog.action_updatevm)
|
|
|
|
|
|
|
|
with unittest.mock.patch('qubesmanager.qube_manager.UpdateVMThread') \
|
|
|
|
as mock_update:
|
|
|
|
QtTest.QTest.mouseClick(widget,
|
|
|
|
QtCore.Qt.LeftButton)
|
|
|
|
mock_update.assert_called_once_with(selected_vm)
|
|
|
|
mock_update().start.assert_called_once_with()
|
|
|
|
|
|
|
|
def test_208_update_vm_admin(self):
|
|
|
|
selected_vm = self._select_admin_vm()
|
|
|
|
self.assertIsNotNone(selected_vm, "No valid admin VM found")
|
|
|
|
|
|
|
|
widget = self.dialog.toolbar.widgetForAction(
|
|
|
|
self.dialog.action_updatevm)
|
|
|
|
|
|
|
|
with unittest.mock.patch('qubesmanager.qube_manager.UpdateVMThread') \
|
|
|
|
as mock_update:
|
|
|
|
QtTest.QTest.mouseClick(widget,
|
|
|
|
QtCore.Qt.LeftButton)
|
|
|
|
mock_update.assert_called_once_with(selected_vm)
|
|
|
|
mock_update().start.assert_called_once_with()
|
|
|
|
|
2019-10-15 00:00:18 +02:00
|
|
|
@unittest.mock.patch("PyQt5.QtWidgets.QInputDialog.getText",
|
2019-04-30 15:58:11 +02:00
|
|
|
return_value=("command to run", True))
|
|
|
|
def test_209_run_command_in_vm(self, _):
|
|
|
|
selected_vm = self._select_non_admin_vm()
|
|
|
|
|
|
|
|
self.assertIsNotNone(selected_vm, "No valid non-admin VM found")
|
|
|
|
|
|
|
|
with unittest.mock.patch('qubesmanager.qube_manager.RunCommandThread') \
|
|
|
|
as mock_thread:
|
|
|
|
self.dialog.action_run_command_in_vm.trigger()
|
|
|
|
mock_thread.assert_called_once_with(selected_vm, "command to run")
|
|
|
|
mock_thread().finished.connect.assert_called_once_with(
|
|
|
|
self.dialog.clear_threads)
|
|
|
|
mock_thread().start.assert_called_once_with()
|
|
|
|
|
|
|
|
def test_210_run_command_in_adminvm(self):
|
|
|
|
self._select_admin_vm()
|
|
|
|
|
|
|
|
self.assertFalse(self.dialog.action_run_command_in_vm.isEnabled(),
|
|
|
|
"Should not be able to run commands for dom0")
|
|
|
|
|
2019-10-15 00:00:18 +02:00
|
|
|
@unittest.mock.patch("PyQt5.QtWidgets.QMessageBox.warning")
|
2019-04-30 15:58:11 +02:00
|
|
|
def test_211_pausevm(self, mock_warn):
|
|
|
|
selected_vm = self._select_non_admin_vm(running=True)
|
|
|
|
|
|
|
|
self.assertTrue(self.dialog.action_pausevm.isEnabled(),
|
|
|
|
"Pause not enabled for a running VM")
|
|
|
|
|
|
|
|
with unittest.mock.patch.object(selected_vm, 'pause') as mock_pause:
|
|
|
|
self.dialog.action_pausevm.trigger()
|
|
|
|
mock_pause.assert_called_once_with()
|
|
|
|
|
|
|
|
mock_pause.side_effect = exc.QubesException('Error')
|
|
|
|
self.dialog.action_pausevm.trigger()
|
|
|
|
self.assertEqual(mock_warn.call_count, 1)
|
|
|
|
|
|
|
|
def test_212_resumevm(self):
|
|
|
|
selected_vm = self._select_non_admin_vm(running=False)
|
|
|
|
|
|
|
|
with unittest.mock.patch.object(selected_vm, 'get_power_state')\
|
|
|
|
as mock_state, \
|
|
|
|
unittest.mock.patch.object(selected_vm, 'unpause')\
|
|
|
|
as mock_unpause:
|
|
|
|
mock_state.return_value = 'Paused'
|
|
|
|
self.dialog.action_resumevm.trigger()
|
|
|
|
mock_unpause.assert_called_once_with()
|
|
|
|
|
|
|
|
with unittest.mock.patch('qubesmanager.qube_manager.StartVMThread') \
|
|
|
|
as mock_thread:
|
|
|
|
self.dialog.action_resumevm.trigger()
|
|
|
|
mock_thread.assert_called_once_with(selected_vm)
|
|
|
|
mock_thread().finished.connect.assert_called_once_with(
|
|
|
|
self.dialog.clear_threads)
|
|
|
|
mock_thread().start.assert_called_once_with()
|
|
|
|
|
|
|
|
def test_213_resume_running_vm(self):
|
|
|
|
self._select_non_admin_vm(running=True)
|
|
|
|
self.assertFalse(self.dialog.action_resumevm.isEnabled())
|
|
|
|
|
2019-10-15 00:00:18 +02:00
|
|
|
@unittest.mock.patch("PyQt5.QtWidgets.QMessageBox.question",
|
|
|
|
return_value=QtWidgets.QMessageBox.Yes)
|
|
|
|
@unittest.mock.patch('PyQt5.QtCore.QTimer.singleShot')
|
2019-04-30 15:58:11 +02:00
|
|
|
@unittest.mock.patch('qubesmanager.qube_manager.VmShutdownMonitor')
|
|
|
|
def test_214_shutdownvm(self, mock_monitor, mock_timer, _):
|
|
|
|
selected_vm = self._select_non_admin_vm(running=True)
|
|
|
|
|
|
|
|
with unittest.mock.patch.object(selected_vm, 'shutdown')\
|
|
|
|
as mock_shutdown:
|
|
|
|
self.dialog.action_shutdownvm.trigger()
|
|
|
|
mock_shutdown.assert_called_once_with()
|
|
|
|
mock_monitor.assert_called_once_with(
|
|
|
|
selected_vm,
|
|
|
|
unittest.mock.ANY, unittest.mock.ANY,
|
|
|
|
unittest.mock.ANY, unittest.mock.ANY)
|
|
|
|
mock_timer.assert_called_once_with(unittest.mock.ANY,
|
|
|
|
unittest.mock.ANY)
|
|
|
|
|
|
|
|
def test_215_shutdown_halted_vm(self):
|
|
|
|
self._select_non_admin_vm(running=False)
|
|
|
|
|
|
|
|
self.assertFalse(self.dialog.action_shutdownvm.isEnabled())
|
|
|
|
|
|
|
|
@unittest.mock.patch('qubesmanager.create_new_vm.NewVmDlg')
|
|
|
|
def test_216_create_vm(self, mock_new_vm):
|
|
|
|
action = self.dialog.action_createvm
|
|
|
|
self.assertTrue(action.isEnabled())
|
|
|
|
|
|
|
|
action.trigger()
|
|
|
|
|
|
|
|
self.assertEqual(mock_new_vm.call_count, 1,
|
|
|
|
"Create New VM window did not appear")
|
|
|
|
|
|
|
|
def test_217_remove_admin_vm(self):
|
|
|
|
self._select_admin_vm()
|
|
|
|
|
|
|
|
self.assertFalse(self.dialog.action_removevm.isEnabled())
|
|
|
|
|
2020-07-30 02:40:40 +02:00
|
|
|
@unittest.mock.patch("qubesmanager.qube_manager.QMessageBox")
|
2019-04-30 15:58:11 +02:00
|
|
|
@unittest.mock.patch('qubesadmin.utils.vm_dependencies')
|
|
|
|
def test_218_remove_vm_dependencies(self, mock_dependencies, mock_msgbox):
|
|
|
|
mock_vm = unittest.mock.Mock(spec=['name'],
|
2019-10-17 00:16:01 +02:00
|
|
|
**{'name.return_value': 'test-vm'})
|
2019-04-30 15:58:11 +02:00
|
|
|
mock_dependencies.return_value = [(mock_vm, "test_prop")]
|
|
|
|
|
2020-07-30 02:40:40 +02:00
|
|
|
action = self.dialog.action_removevm
|
|
|
|
self._select_non_admin_vm()
|
2019-04-30 15:58:11 +02:00
|
|
|
action.trigger()
|
2020-07-30 02:40:40 +02:00
|
|
|
|
2019-04-30 15:58:11 +02:00
|
|
|
mock_msgbox().show.assert_called_with()
|
|
|
|
|
2019-10-15 00:00:18 +02:00
|
|
|
@unittest.mock.patch('PyQt5.QtWidgets.QMessageBox.warning')
|
|
|
|
@unittest.mock.patch("PyQt5.QtWidgets.QInputDialog.getText")
|
2019-04-30 15:58:11 +02:00
|
|
|
@unittest.mock.patch('qubesadmin.utils.vm_dependencies')
|
|
|
|
def test_219_remove_vm_no_depencies(
|
|
|
|
self, mock_dependencies, mock_input, mock_warning):
|
|
|
|
action = self.dialog.action_removevm
|
|
|
|
selected_vm = self._select_non_admin_vm(running=False)
|
|
|
|
|
|
|
|
# test with no dependencies
|
|
|
|
mock_dependencies.return_value = None
|
|
|
|
|
|
|
|
with unittest.mock.patch('qubesmanager.common_threads.RemoveVMThread')\
|
|
|
|
as mock_thread:
|
2020-07-30 02:40:40 +02:00
|
|
|
mock_input.return_value = (selected_vm, False)
|
2019-04-30 15:58:11 +02:00
|
|
|
action.trigger()
|
|
|
|
self.assertEqual(mock_thread.call_count, 0,
|
|
|
|
"VM removed despite user clicking 'cancel")
|
|
|
|
|
|
|
|
mock_input.return_value = ("wrong_name", True)
|
|
|
|
action.trigger()
|
|
|
|
self.assertEqual(mock_warning.call_count, 1)
|
|
|
|
self.assertEqual(mock_thread.call_count, 0,
|
|
|
|
"VM removed despite user not confirming the name")
|
|
|
|
|
|
|
|
mock_input.return_value = (selected_vm.name, True)
|
|
|
|
action.trigger()
|
|
|
|
mock_thread.assert_called_once_with(selected_vm)
|
|
|
|
mock_thread().finished.connect.assert_called_once_with(
|
|
|
|
self.dialog.clear_threads)
|
|
|
|
mock_thread().start.assert_called_once_with()
|
|
|
|
|
|
|
|
def test_220_restartvm_halted_vm(self):
|
|
|
|
self._select_non_admin_vm(running=False)
|
|
|
|
self.assertFalse(self.dialog.action_restartvm.isEnabled())
|
|
|
|
|
2019-10-15 00:00:18 +02:00
|
|
|
@unittest.mock.patch('PyQt5.QtCore.QTimer.singleShot')
|
2019-04-30 15:58:11 +02:00
|
|
|
@unittest.mock.patch('qubesmanager.qube_manager.VmShutdownMonitor')
|
2019-10-15 00:00:18 +02:00
|
|
|
@unittest.mock.patch("PyQt5.QtWidgets.QMessageBox.question",
|
|
|
|
return_value=QtWidgets.QMessageBox.Yes)
|
2019-04-30 15:58:11 +02:00
|
|
|
def test_221_restartvm_running_vm(self, _msgbox, mock_monitor, _qtimer):
|
|
|
|
selected_vm = self._select_non_admin_vm(running=True)
|
|
|
|
|
|
|
|
action = self.dialog.action_restartvm
|
|
|
|
|
|
|
|
# currently the VM is running
|
|
|
|
with unittest.mock.patch.object(selected_vm, 'shutdown')\
|
|
|
|
as mock_shutdown:
|
|
|
|
action.trigger()
|
|
|
|
mock_shutdown.assert_called_once_with()
|
|
|
|
mock_monitor.assert_called_once_with(
|
|
|
|
selected_vm, unittest.mock.ANY,
|
|
|
|
unittest.mock.ANY, True, unittest.mock.ANY)
|
|
|
|
|
|
|
|
@unittest.mock.patch('qubesmanager.qube_manager.StartVMThread')
|
2019-10-15 00:00:18 +02:00
|
|
|
@unittest.mock.patch("PyQt5.QtWidgets.QMessageBox.question",
|
|
|
|
return_value=QtWidgets.QMessageBox.Yes)
|
2019-04-30 15:58:11 +02:00
|
|
|
def test_222_restartvm_shutdown_meantime(self, _, mock_thread):
|
|
|
|
selected_vm = self._select_non_admin_vm(running=True)
|
|
|
|
|
|
|
|
action = self.dialog.action_restartvm
|
|
|
|
|
|
|
|
# it was shutdown in the meantime
|
|
|
|
with unittest.mock.patch.object(
|
|
|
|
selected_vm, 'is_running', **{'return_value': False}):
|
|
|
|
action.trigger()
|
|
|
|
mock_thread.assert_called_once_with(selected_vm)
|
|
|
|
mock_thread().finished.connect.assert_called_once_with(
|
|
|
|
self.dialog.clear_threads)
|
|
|
|
mock_thread().start.assert_called_once_with()
|
|
|
|
|
|
|
|
@unittest.mock.patch('qubesmanager.qube_manager.UpdateVMThread')
|
|
|
|
def test_223_updatevm_running(self, mock_thread):
|
|
|
|
selected_vm = self._select_non_admin_vm(running=True)
|
|
|
|
|
|
|
|
self.dialog.action_updatevm.trigger()
|
|
|
|
|
|
|
|
mock_thread.assert_called_once_with(selected_vm)
|
|
|
|
mock_thread().finished.connect.assert_called_once_with(
|
|
|
|
self.dialog.clear_threads)
|
|
|
|
mock_thread().start.assert_called_once_with()
|
|
|
|
|
2019-10-15 00:00:18 +02:00
|
|
|
@unittest.mock.patch("PyQt5.QtWidgets.QMessageBox.question",
|
|
|
|
return_value=QtWidgets.QMessageBox.Yes)
|
2019-04-30 15:58:11 +02:00
|
|
|
@unittest.mock.patch('qubesmanager.qube_manager.UpdateVMThread')
|
|
|
|
def test_224_updatevm_halted(self, mock_thread, _):
|
|
|
|
selected_vm = self._select_non_admin_vm(running=False)
|
|
|
|
|
|
|
|
self.dialog.action_updatevm.trigger()
|
|
|
|
|
|
|
|
mock_thread.assert_called_once_with(selected_vm)
|
|
|
|
mock_thread().finished.connect.assert_called_once_with(
|
|
|
|
self.dialog.clear_threads)
|
|
|
|
mock_thread().start.assert_called_once_with()
|
|
|
|
|
2019-10-15 00:00:18 +02:00
|
|
|
@unittest.mock.patch("PyQt5.QtWidgets.QMessageBox.question",
|
|
|
|
return_value=QtWidgets.QMessageBox.Yes)
|
2019-04-30 15:58:11 +02:00
|
|
|
def test_224_killvm(self, _):
|
|
|
|
selected_vm = self._select_non_admin_vm(running=True)
|
|
|
|
action = self.dialog.action_killvm
|
|
|
|
|
|
|
|
with unittest.mock.patch.object(selected_vm, 'kill') as mock_kill:
|
|
|
|
action.trigger()
|
|
|
|
mock_kill.assert_called_once_with()
|
|
|
|
|
2019-10-15 00:00:18 +02:00
|
|
|
@unittest.mock.patch("PyQt5.QtWidgets.QMessageBox.question",
|
|
|
|
return_value=QtWidgets.QMessageBox.Cancel)
|
2019-04-30 15:58:11 +02:00
|
|
|
def test_225_killvm_cancel(self, _):
|
|
|
|
selected_vm = self._select_non_admin_vm(running=True)
|
|
|
|
action = self.dialog.action_killvm
|
|
|
|
|
|
|
|
with unittest.mock.patch.object(selected_vm, 'kill') as mock_kill:
|
|
|
|
action.trigger()
|
|
|
|
self.assertEqual(mock_kill.call_count, 0,
|
|
|
|
"Ignored Cancel on kill VM")
|
|
|
|
|
|
|
|
@unittest.mock.patch('qubesmanager.global_settings.GlobalSettingsWindow')
|
|
|
|
def test_226_global_settings(self, mock_settings):
|
|
|
|
self._select_non_admin_vm()
|
|
|
|
self.dialog.action_global_settings.trigger()
|
|
|
|
self.assertEqual(mock_settings.call_count, 1,
|
|
|
|
"Global Settings not opened")
|
|
|
|
|
|
|
|
self._select_admin_vm()
|
|
|
|
self.dialog.action_global_settings.trigger()
|
|
|
|
self.assertEqual(mock_settings.call_count, 2,
|
|
|
|
"Global Settings not opened for the second time")
|
|
|
|
|
|
|
|
@unittest.mock.patch('qubesmanager.backup.BackupVMsWindow')
|
|
|
|
def test_227_backup(self, mock_backup):
|
|
|
|
self.dialog.action_backup.trigger()
|
|
|
|
self.assertTrue(self.dialog.action_backup.isEnabled())
|
|
|
|
self.assertEqual(mock_backup.call_count, 1,
|
|
|
|
"Backup window does not appear")
|
|
|
|
|
|
|
|
@unittest.mock.patch('qubesmanager.restore.RestoreVMsWindow')
|
|
|
|
def test_228_restore(self, mock_restore):
|
|
|
|
self.dialog.action_restore.trigger()
|
|
|
|
self.assertTrue(self.dialog.action_restore.isEnabled())
|
|
|
|
self.assertEqual(mock_restore.call_count, 1,
|
|
|
|
"Backup window does not appear")
|
|
|
|
|
|
|
|
@unittest.mock.patch('qubesmanager.qube_manager.AboutDialog')
|
|
|
|
def test_229_about_qubes(self, mock_about):
|
|
|
|
self.assertTrue(self.dialog.action_about_qubes.isEnabled())
|
|
|
|
self.dialog.action_about_qubes.trigger()
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
mock_about.call_count, 1, "About window does not appear")
|
|
|
|
|
|
|
|
def test_230_exit_action(self):
|
|
|
|
self.assertTrue(self.dialog.action_exit.isEnabled())
|
|
|
|
with unittest.mock.patch.object(self.dialog, 'close') as mock_close:
|
|
|
|
self.dialog.action_exit.trigger()
|
|
|
|
mock_close.assert_called_once_with()
|
|
|
|
|
|
|
|
@unittest.mock.patch('subprocess.check_call')
|
|
|
|
def test_231_template_manager(self, mock_subprocess):
|
|
|
|
self.assertTrue(self.dialog.action_manage_templates.isEnabled())
|
|
|
|
|
|
|
|
self.dialog.action_manage_templates.trigger()
|
|
|
|
mock_subprocess.assert_called_once_with('qubes-template-manager')
|
|
|
|
|
2020-08-04 17:01:34 +02:00
|
|
|
@unittest.mock.patch('qubesmanager.clone_vm.CloneVMDlg')
|
|
|
|
def test_232_clonevm(self, mock_clone):
|
2019-04-30 15:58:11 +02:00
|
|
|
action = self.dialog.action_clonevm
|
|
|
|
|
|
|
|
self._select_admin_vm()
|
|
|
|
self.assertFalse(action.isEnabled())
|
|
|
|
|
|
|
|
selected_vm = self._select_non_admin_vm()
|
|
|
|
self.assertTrue(action.isEnabled())
|
|
|
|
|
|
|
|
action.trigger()
|
2020-08-04 17:01:34 +02:00
|
|
|
mock_clone.assert_called_once_with(self.qtapp, self.qapp,
|
|
|
|
src_vm=selected_vm)
|
2019-04-30 15:58:11 +02:00
|
|
|
|
|
|
|
def test_233_search_action(self):
|
|
|
|
self.qtapp.setActiveWindow(self.dialog.searchbox)
|
|
|
|
self.dialog.action_search.trigger()
|
|
|
|
self.assertTrue(self.dialog.searchbox.hasFocus())
|
|
|
|
|
|
|
|
# input text
|
|
|
|
self.dialog.searchbox.setText("sys")
|
|
|
|
# click outside the widget
|
|
|
|
QtTest.QTest.mouseClick(self.dialog.table, QtCore.Qt.LeftButton)
|
|
|
|
# click the widget, check if it is correctly activated and the whole
|
|
|
|
# text was selected
|
|
|
|
QtTest.QTest.mouseClick(self.dialog.searchbox, QtCore.Qt.LeftButton)
|
|
|
|
self.assertTrue(self.dialog.searchbox.hasFocus())
|
|
|
|
self.assertEqual(self.dialog.searchbox.selectedText(), "sys")
|
|
|
|
|
|
|
|
def test_234_searchbox(self):
|
|
|
|
# look for sys
|
|
|
|
self.dialog.searchbox.setText("sys")
|
|
|
|
expected_number = \
|
|
|
|
len([vm for vm in self.qapp.domains if "sys" in vm.name])
|
|
|
|
actual_number = self._count_visible_table_rows()
|
|
|
|
self.assertEqual(expected_number, actual_number,
|
|
|
|
"Incorrect number of vms shown for 'sys'")
|
|
|
|
|
|
|
|
# clear search
|
|
|
|
self.dialog.searchbox.setText("")
|
|
|
|
expected_number = len([vm for vm in self.qapp.domains])
|
|
|
|
actual_number = self._count_visible_table_rows()
|
|
|
|
self.assertEqual(expected_number, actual_number,
|
|
|
|
"Incorrect number of vms shown for cleared search box")
|
|
|
|
|
|
|
|
def test_235_hide_show_toolbars(self):
|
2019-10-15 00:00:18 +02:00
|
|
|
with unittest.mock.patch('PyQt5.QtCore.QSettings.setValue')\
|
2019-04-30 15:58:11 +02:00
|
|
|
as mock_setvalue:
|
|
|
|
self.dialog.action_menubar.trigger()
|
|
|
|
mock_setvalue.assert_called_with('view/menubar_visible', False)
|
|
|
|
self.dialog.action_toolbar.trigger()
|
|
|
|
mock_setvalue.assert_called_with('view/toolbar_visible', False)
|
|
|
|
|
|
|
|
self.assertFalse(self.dialog.menubar.isVisible(),
|
|
|
|
"Menubar not hidden correctly")
|
|
|
|
self.assertFalse(self.dialog.toolbar.isVisible(),
|
|
|
|
"Toolbar not hidden correctly")
|
|
|
|
|
|
|
|
def test_236_clear_searchbox(self):
|
|
|
|
self.dialog.searchbox.setText("text")
|
|
|
|
|
|
|
|
self.assertEqual(self.dialog.searchbox.text(), "text")
|
|
|
|
|
|
|
|
QtTest.QTest.keyPress(self.dialog, QtCore.Qt.Key_Escape)
|
|
|
|
|
|
|
|
self.assertEqual(self.dialog.searchbox.text(), "",
|
|
|
|
"Escape failed to clear searchbox")
|
|
|
|
|
|
|
|
expected_number = len([vm for vm in self.qapp.domains])
|
|
|
|
actual_number = self._count_visible_table_rows()
|
|
|
|
self.assertEqual(expected_number, actual_number,
|
|
|
|
"Incorrect number of vms shown for cleared search box")
|
|
|
|
|
2019-10-15 00:00:18 +02:00
|
|
|
@unittest.mock.patch('PyQt5.QtWidgets.QMessageBox.information')
|
|
|
|
@unittest.mock.patch('PyQt5.QtWidgets.QMessageBox.warning')
|
2019-04-30 15:58:11 +02:00
|
|
|
def test_300_clear_threads(self, mock_warning, mock_info):
|
|
|
|
mock_thread_finished_ok = unittest.mock.Mock(
|
|
|
|
spec=['isFinished', 'msg', 'msg_is_success'],
|
|
|
|
msg=None, msg_is_success=False,
|
|
|
|
**{'isFinished.return_value': True})
|
|
|
|
mock_thread_not_finished = unittest.mock.Mock(
|
|
|
|
spec=['isFinished', 'msg', 'msg_is_success'],
|
|
|
|
msg=None, msg_is_success=False,
|
|
|
|
**{'isFinished.return_value': False})
|
|
|
|
mock_thread_finished_error = unittest.mock.Mock(
|
|
|
|
spec=['isFinished', 'msg', 'msg_is_success'],
|
|
|
|
msg=("Error", "Error"), msg_is_success=False,
|
|
|
|
**{'isFinished.return_value': True})
|
|
|
|
mock_thread_fin_error_success = unittest.mock.Mock(
|
|
|
|
spec=['isFinished', 'msg', 'msg_is_success'],
|
|
|
|
msg=("Done", "Done"), msg_is_success=True,
|
|
|
|
**{'isFinished.return_value': True})
|
|
|
|
|
|
|
|
# single finished thread
|
|
|
|
self.dialog.threads_list = [mock_thread_not_finished,
|
|
|
|
mock_thread_finished_ok]
|
|
|
|
self.dialog.clear_threads()
|
|
|
|
self.assertEqual(mock_warning.call_count, 0)
|
|
|
|
self.assertEqual(mock_info.call_count, 0)
|
|
|
|
self.assertEqual(len(self.dialog.threads_list), 1)
|
|
|
|
|
|
|
|
# an error thread and some in-progress ones
|
|
|
|
self.dialog.threads_list = [mock_thread_not_finished,
|
|
|
|
mock_thread_not_finished,
|
|
|
|
mock_thread_finished_error]
|
|
|
|
self.dialog.clear_threads()
|
|
|
|
self.assertEqual(mock_warning.call_count, 1)
|
|
|
|
self.assertEqual(mock_info.call_count, 0)
|
|
|
|
self.assertEqual(len(self.dialog.threads_list), 2)
|
|
|
|
|
|
|
|
# an error-success thread and some in-progress ones
|
|
|
|
self.dialog.threads_list = [mock_thread_not_finished,
|
|
|
|
mock_thread_not_finished,
|
|
|
|
mock_thread_fin_error_success,
|
|
|
|
mock_thread_finished_error]
|
|
|
|
self.dialog.clear_threads()
|
|
|
|
self.assertEqual(mock_warning.call_count, 1)
|
|
|
|
self.assertEqual(mock_info.call_count, 1)
|
|
|
|
self.assertEqual(len(self.dialog.threads_list), 3)
|
|
|
|
|
|
|
|
def test_400_event_domain_added(self):
|
2020-07-30 02:40:40 +02:00
|
|
|
number_of_vms = self.dialog.table.model().rowCount()
|
2018-03-28 17:42:42 +02:00
|
|
|
|
2019-10-17 00:16:01 +02:00
|
|
|
self.addCleanup(subprocess.call, ["qvm-remove", "-f", "test-vm"])
|
2018-03-28 17:42:42 +02:00
|
|
|
|
2019-04-30 15:58:11 +02:00
|
|
|
self._run_command_and_process_events(
|
2019-10-17 00:16:01 +02:00
|
|
|
["qvm-create", "--label", "red", "test-vm"])
|
2019-04-30 15:58:11 +02:00
|
|
|
|
|
|
|
# a single row was added to the table
|
2020-07-30 02:40:40 +02:00
|
|
|
self.assertEqual(self.dialog.table.model().rowCount(), number_of_vms+1)
|
2019-04-30 15:58:11 +02:00
|
|
|
|
|
|
|
# table contains the correct vms
|
|
|
|
vms_in_table = self._create_set_of_current_vms()
|
|
|
|
|
|
|
|
vms_in_system = set([vm.name for vm in self.qapp.domains])
|
|
|
|
|
|
|
|
self.assertEqual(vms_in_table, vms_in_system, "Table not updated "
|
|
|
|
"correctly after add")
|
|
|
|
|
|
|
|
# check if sorting works
|
|
|
|
self.__check_sorting("Name")
|
|
|
|
|
|
|
|
# try opening settings for the added vm
|
2020-07-30 02:40:40 +02:00
|
|
|
for row in range(self.dialog.table.model().rowCount()):
|
2019-04-30 15:58:11 +02:00
|
|
|
name = self._get_table_item(row, "Name")
|
2020-07-30 02:40:40 +02:00
|
|
|
if name == "test-vm":
|
|
|
|
index = self.dialog.table.model().index(row, 0)
|
|
|
|
self.dialog.table.setCurrentIndex(index)
|
2019-04-30 15:58:11 +02:00
|
|
|
break
|
|
|
|
with unittest.mock.patch('qubesmanager.settings.VMSettingsWindow')\
|
|
|
|
as mock_settings:
|
|
|
|
self.dialog.action_settings.trigger()
|
|
|
|
mock_settings.assert_called_once_with(
|
2020-06-30 14:44:20 +02:00
|
|
|
self.qapp.domains["test-vm"], qapp = self.qtapp,
|
|
|
|
init_page = "basic", qubesapp = self.qapp)
|
2019-04-30 15:58:11 +02:00
|
|
|
|
|
|
|
def test_401_event_domain_removed(self):
|
|
|
|
initial_vms = self._create_set_of_current_vms()
|
|
|
|
|
|
|
|
self._run_command_and_process_events(
|
2019-10-17 00:16:01 +02:00
|
|
|
["qvm-create", "--label", "red", "test-vm"])
|
2019-04-30 15:58:11 +02:00
|
|
|
|
|
|
|
current_vms = self._create_set_of_current_vms()
|
|
|
|
self.assertEqual(len(initial_vms) + 1, len(current_vms))
|
|
|
|
|
|
|
|
self._run_command_and_process_events(
|
2019-10-17 00:16:01 +02:00
|
|
|
["qvm-remove", "--force", "test-vm"])
|
2019-04-30 15:58:11 +02:00
|
|
|
current_vms = self._create_set_of_current_vms()
|
|
|
|
self.assertEqual(initial_vms, current_vms)
|
|
|
|
|
|
|
|
# check if sorting works
|
|
|
|
self.__check_sorting("Name")
|
|
|
|
|
|
|
|
def test_403_event_dispvm_added(self):
|
|
|
|
initial_vms = self._create_set_of_current_vms()
|
|
|
|
|
|
|
|
dispvm_template = None
|
|
|
|
|
|
|
|
for vm in self.qapp.domains:
|
|
|
|
if getattr(vm, "template_for_dispvms", False):
|
|
|
|
dispvm_template = vm.name
|
|
|
|
break
|
|
|
|
self.assertIsNotNone(dispvm_template,
|
|
|
|
"Cannot find a template for dispVMs")
|
|
|
|
|
|
|
|
# this requires very long timeout, because it takes time for the
|
|
|
|
# dispvm to vanish
|
|
|
|
self._run_command_and_process_events(
|
2020-08-14 12:38:21 +02:00
|
|
|
["qvm-run", "--dispvm", dispvm_template, "true"], timeout=60,
|
2020-09-29 00:43:16 +02:00
|
|
|
additional_timeout=60)
|
2019-04-30 15:58:11 +02:00
|
|
|
|
|
|
|
final_vms = self._create_set_of_current_vms()
|
|
|
|
|
|
|
|
self.assertEqual(initial_vms, final_vms,
|
|
|
|
"Failed handling of a created-and-removed dispvm")
|
|
|
|
|
|
|
|
def test_404_crashing_dispvm(self):
|
|
|
|
initial_vms = self._create_set_of_current_vms()
|
|
|
|
|
|
|
|
dispvm_template = None
|
|
|
|
|
|
|
|
for vm in self.qapp.domains:
|
|
|
|
if getattr(vm, "template_for_dispvms", False):
|
|
|
|
dispvm_template = vm.name
|
|
|
|
break
|
|
|
|
|
|
|
|
self.assertIsNotNone(dispvm_template,
|
|
|
|
"Cannot find a template for dispVMs")
|
|
|
|
|
|
|
|
current_memory = getattr(self.qapp.domains[dispvm_template], "memory")
|
|
|
|
self.addCleanup(
|
|
|
|
subprocess.call,
|
|
|
|
["qvm-prefs", dispvm_template, "memory", str(current_memory)])
|
|
|
|
subprocess.check_call(
|
|
|
|
["qvm-prefs", dispvm_template, "memory", "600000"])
|
|
|
|
|
|
|
|
self._run_command_and_process_events(
|
2020-08-14 12:38:21 +02:00
|
|
|
["qvm-run", "--dispvm", dispvm_template, "true"], timeout=30,
|
|
|
|
additional_timeout=15)
|
2019-04-30 15:58:11 +02:00
|
|
|
|
|
|
|
final_vms = self._create_set_of_current_vms()
|
|
|
|
|
|
|
|
self.assertEqual(initial_vms, final_vms,
|
|
|
|
"Failed handling of dispvm that crashed on start")
|
|
|
|
|
|
|
|
def test_405_prop_change_label(self):
|
|
|
|
target_vm_name = "work"
|
|
|
|
vm_row = self._find_vm_row(target_vm_name)
|
|
|
|
|
2020-07-30 02:40:40 +02:00
|
|
|
current_label = self._get_table_item(vm_row, "Label", Qt.DecorationRole)
|
2019-04-30 15:58:11 +02:00
|
|
|
|
|
|
|
self.addCleanup(
|
|
|
|
subprocess.call, ["qvm-prefs", target_vm_name, "label", "blue"])
|
|
|
|
self._run_command_and_process_events(
|
2020-09-29 00:43:16 +02:00
|
|
|
["qvm-prefs", target_vm_name, "label", "red"], timeout=20)
|
2019-04-30 15:58:11 +02:00
|
|
|
|
2020-07-30 02:40:40 +02:00
|
|
|
new_label = self._get_table_item(vm_row, "Label", Qt.DecorationRole)
|
2019-04-30 15:58:11 +02:00
|
|
|
|
2020-07-30 02:40:40 +02:00
|
|
|
self.assertNotEqual(current_label.toImage(), new_label.toImage(),
|
|
|
|
"Label icon did not change")
|
|
|
|
|
|
|
|
icon = QIcon.fromTheme(self.qapp.domains[target_vm_name].label.icon)
|
|
|
|
icon = icon.pixmap(icon_size)
|
|
|
|
|
|
|
|
self.assertEqual(new_label.toImage(), icon.toImage(), "Incorrect label")
|
2019-04-30 15:58:11 +02:00
|
|
|
|
|
|
|
def test_406_prop_change_template(self):
|
|
|
|
target_vm_name = "work"
|
|
|
|
vm_row = self._find_vm_row(target_vm_name)
|
|
|
|
|
2020-07-30 02:40:40 +02:00
|
|
|
old_template = self._get_table_item(vm_row, "Template")
|
2019-04-30 15:58:11 +02:00
|
|
|
new_template = None
|
|
|
|
for vm in self.qapp.domains:
|
|
|
|
if vm.klass == 'TemplateVM' and vm.name != old_template:
|
|
|
|
new_template = vm.name
|
|
|
|
break
|
|
|
|
|
|
|
|
self.addCleanup(
|
|
|
|
subprocess.call,
|
|
|
|
["qvm-prefs", target_vm_name, "template", old_template])
|
|
|
|
self._run_command_and_process_events(
|
|
|
|
["qvm-prefs", target_vm_name, "template", new_template])
|
|
|
|
|
|
|
|
self.assertNotEqual(old_template,
|
2020-07-30 02:40:40 +02:00
|
|
|
self._get_table_item(vm_row, "Template"),
|
2019-04-30 15:58:11 +02:00
|
|
|
"Template did not change")
|
|
|
|
self.assertEqual(
|
2020-07-30 02:40:40 +02:00
|
|
|
self._get_table_item(vm_row, "Template"),
|
2019-04-30 15:58:11 +02:00
|
|
|
self.qapp.domains[target_vm_name].template.name,
|
|
|
|
"Incorrect template")
|
|
|
|
|
|
|
|
def test_407_prop_change_netvm(self):
|
|
|
|
target_vm_name = "work"
|
|
|
|
vm_row = self._find_vm_row(target_vm_name)
|
|
|
|
|
2020-07-30 02:40:40 +02:00
|
|
|
old_netvm = self._get_table_item(vm_row, "NetVM")
|
2019-04-30 15:58:11 +02:00
|
|
|
new_netvm = None
|
|
|
|
for vm in self.qapp.domains:
|
|
|
|
if getattr(vm, "provides_network", False) and vm.name != old_netvm:
|
|
|
|
new_netvm = vm.name
|
|
|
|
break
|
|
|
|
|
|
|
|
self.addCleanup(
|
|
|
|
subprocess.call, ["qvm-prefs", target_vm_name, "netvm", old_netvm])
|
|
|
|
self._run_command_and_process_events(
|
|
|
|
["qvm-prefs", target_vm_name, "netvm", new_netvm])
|
|
|
|
|
|
|
|
self.assertNotEqual(old_netvm,
|
2020-07-30 02:40:40 +02:00
|
|
|
self._get_table_item(vm_row, "NetVM"),
|
2019-04-30 15:58:11 +02:00
|
|
|
"NetVM did not change")
|
|
|
|
self.assertEqual(
|
2020-07-30 02:40:40 +02:00
|
|
|
self._get_table_item(vm_row, "NetVM"),
|
2019-04-30 15:58:11 +02:00
|
|
|
self.qapp.domains[target_vm_name].netvm.name,
|
|
|
|
"Incorrect NetVM")
|
|
|
|
|
|
|
|
@unittest.expectedFailure
|
|
|
|
def test_408_prop_change_internal(self):
|
|
|
|
target_vm_name = "work"
|
|
|
|
vm_row = self._find_vm_row(target_vm_name)
|
|
|
|
|
|
|
|
self.addCleanup(subprocess.call,
|
|
|
|
["qvm-features", "--unset", "work", "interal"])
|
|
|
|
self._run_command_and_process_events(
|
|
|
|
["qvm-features", "work", "interal", "1"])
|
|
|
|
|
|
|
|
self.assertEqual(
|
2020-07-30 02:40:40 +02:00
|
|
|
self._get_table_item(vm_row, "Internal"),
|
2019-04-30 15:58:11 +02:00
|
|
|
"Yes",
|
|
|
|
"Incorrect value for internal VM")
|
|
|
|
|
|
|
|
self._run_command_and_process_events(
|
|
|
|
["qvm-features", "--unset", "work", "interal"])
|
|
|
|
|
|
|
|
self.assertEqual(
|
2020-07-30 02:40:40 +02:00
|
|
|
self._get_table_item(vm_row, "Internal"),
|
2019-04-30 15:58:11 +02:00
|
|
|
"",
|
|
|
|
"Incorrect value for non-internal VM")
|
|
|
|
|
|
|
|
def test_409_prop_change_ip(self):
|
|
|
|
target_vm_name = "work"
|
|
|
|
vm_row = self._find_vm_row(target_vm_name)
|
|
|
|
|
2020-07-30 02:40:40 +02:00
|
|
|
old_ip = self._get_table_item(vm_row, "IP")
|
2019-04-30 15:58:11 +02:00
|
|
|
new_ip = old_ip.replace(".0.", ".5.")
|
|
|
|
|
|
|
|
self.addCleanup(
|
|
|
|
subprocess.call, ["qvm-prefs", target_vm_name, "ip", old_ip])
|
|
|
|
self._run_command_and_process_events(
|
|
|
|
["qvm-prefs", target_vm_name, "ip", new_ip])
|
|
|
|
|
|
|
|
self.assertNotEqual(old_ip,
|
2020-07-30 02:40:40 +02:00
|
|
|
self._get_table_item(vm_row, "IP"),
|
2019-04-30 15:58:11 +02:00
|
|
|
"IP did not change")
|
|
|
|
self.assertEqual(
|
2020-07-30 02:40:40 +02:00
|
|
|
self._get_table_item(vm_row, "IP"),
|
2019-04-30 15:58:11 +02:00
|
|
|
self.qapp.domains[target_vm_name].ip,
|
|
|
|
"Incorrect IP")
|
|
|
|
|
|
|
|
def test_410_prop_change_in_backups(self):
|
|
|
|
target_vm_name = "work"
|
|
|
|
vm_row = self._find_vm_row(target_vm_name)
|
|
|
|
|
|
|
|
old_value = self.qapp.domains[target_vm_name].include_in_backups
|
|
|
|
new_value = not old_value
|
|
|
|
|
|
|
|
self.addCleanup(
|
|
|
|
subprocess.call,
|
|
|
|
["qvm-prefs", target_vm_name, "include_in_backups", str(old_value)])
|
|
|
|
self._run_command_and_process_events(
|
|
|
|
["qvm-prefs", target_vm_name, "include_in_backups", str(new_value)])
|
|
|
|
|
|
|
|
self.assertEqual(
|
2020-07-30 02:40:40 +02:00
|
|
|
self._get_table_item(vm_row, "Internal"),
|
2019-04-30 15:58:11 +02:00
|
|
|
"Yes" if new_value else "",
|
|
|
|
"Incorrect value for include_in_backups")
|
|
|
|
|
|
|
|
def test_411_prop_change_last_backup(self):
|
|
|
|
target_vm_name = "work"
|
|
|
|
target_timestamp = "2015-01-01 17:00:00"
|
|
|
|
vm_row = self._find_vm_row(target_vm_name)
|
|
|
|
|
2020-07-30 02:40:40 +02:00
|
|
|
old_value = self._get_table_item(vm_row, "Last backup")
|
2019-04-30 15:58:11 +02:00
|
|
|
new_value = datetime.datetime.strptime(
|
|
|
|
target_timestamp, "%Y-%m-%d %H:%M:%S")
|
|
|
|
|
|
|
|
self.addCleanup(
|
|
|
|
subprocess.call,
|
|
|
|
["qvm-prefs", '-D', target_vm_name, "backup_timestamp"])
|
|
|
|
self._run_command_and_process_events(
|
|
|
|
["qvm-prefs", target_vm_name, "backup_timestamp",
|
|
|
|
str(int(new_value.timestamp()))])
|
|
|
|
|
|
|
|
self.assertNotEqual(old_value,
|
2020-07-30 02:40:40 +02:00
|
|
|
self._get_table_item(vm_row, "Last backup"),
|
2019-04-30 15:58:11 +02:00
|
|
|
"Last backup date did not change")
|
|
|
|
self.assertEqual(
|
2020-07-30 02:40:40 +02:00
|
|
|
self._get_table_item(vm_row, "Last backup"),
|
2019-04-30 15:58:11 +02:00
|
|
|
target_timestamp,
|
|
|
|
"Incorrect Last backup date")
|
|
|
|
|
|
|
|
def test_412_prop_change_defdispvm(self):
|
|
|
|
target_vm_name = "work"
|
|
|
|
vm_row = self._find_vm_row(target_vm_name)
|
|
|
|
|
2020-07-30 02:40:40 +02:00
|
|
|
|
2019-04-30 15:58:11 +02:00
|
|
|
old_default_dispvm =\
|
2020-07-30 02:40:40 +02:00
|
|
|
self._get_table_item(vm_row, "Default DispVM")
|
2019-04-30 15:58:11 +02:00
|
|
|
new_default_dispvm = None
|
|
|
|
for vm in self.qapp.domains:
|
|
|
|
if getattr(vm, "template_for_dispvms", False) and vm.name !=\
|
|
|
|
old_default_dispvm:
|
|
|
|
new_default_dispvm = vm.name
|
|
|
|
break
|
|
|
|
|
|
|
|
self.addCleanup(
|
2020-07-30 02:40:40 +02:00
|
|
|
subprocess.call,
|
|
|
|
["qvm-prefs", target_vm_name, "default_dispvm", old_default_dispvm])
|
2019-04-30 15:58:11 +02:00
|
|
|
self._run_command_and_process_events(
|
|
|
|
["qvm-prefs", target_vm_name, "default_dispvm", new_default_dispvm])
|
|
|
|
|
|
|
|
self.assertNotEqual(
|
|
|
|
old_default_dispvm,
|
2020-07-30 02:40:40 +02:00
|
|
|
self._get_table_item(vm_row, "Default DispVM"),
|
2019-04-30 15:58:11 +02:00
|
|
|
"Default DispVM did not change")
|
|
|
|
|
|
|
|
self.assertEqual(
|
2020-07-30 02:40:40 +02:00
|
|
|
self._get_table_item(vm_row, "Default DispVM"),
|
2019-04-30 15:58:11 +02:00
|
|
|
self.qapp.domains[target_vm_name].default_dispvm.name,
|
|
|
|
"Incorrect Default DispVM")
|
|
|
|
|
|
|
|
def test_413_prop_change_templ_disp(self):
|
|
|
|
target_vm_name = "work"
|
|
|
|
vm_row = self._find_vm_row(target_vm_name)
|
|
|
|
|
|
|
|
self.addCleanup(
|
|
|
|
subprocess.call,
|
|
|
|
["qvm-prefs", "--default", target_vm_name, "template_for_dispvms"])
|
|
|
|
self._run_command_and_process_events(
|
|
|
|
["qvm-prefs", target_vm_name, "template_for_dispvms", "True"])
|
|
|
|
|
|
|
|
self.assertEqual(
|
2020-07-30 02:40:40 +02:00
|
|
|
self._get_table_item(vm_row, "Is DVM Template"),
|
2019-04-30 15:58:11 +02:00
|
|
|
"Yes",
|
|
|
|
"Incorrect value for DVM Template")
|
|
|
|
|
|
|
|
self._run_command_and_process_events(
|
|
|
|
["qvm-prefs", "--default", target_vm_name, "template_for_dispvms"])
|
|
|
|
|
|
|
|
self.assertEqual(
|
2020-07-30 02:40:40 +02:00
|
|
|
self._get_table_item(vm_row, "Is DVM Template"),
|
2019-04-30 15:58:11 +02:00
|
|
|
"",
|
|
|
|
"Incorrect value for not DVM Template")
|
|
|
|
|
|
|
|
def test_414_vm_state_change(self):
|
|
|
|
target_vm_name = "work"
|
|
|
|
vm_row = self._find_vm_row(target_vm_name)
|
|
|
|
|
|
|
|
self.assertFalse(self.qapp.domains[target_vm_name].is_running())
|
|
|
|
|
|
|
|
self.addCleanup(
|
|
|
|
subprocess.call,
|
2019-12-04 21:05:45 +01:00
|
|
|
["qvm-shutdown", "--wait", target_vm_name])
|
2019-04-30 15:58:11 +02:00
|
|
|
self._run_command_and_process_events(
|
2019-12-04 21:05:45 +01:00
|
|
|
["qvm-start", target_vm_name], timeout=60)
|
2019-04-30 15:58:11 +02:00
|
|
|
|
2020-07-30 02:40:40 +02:00
|
|
|
displayed_state = self._get_table_item(vm_row, "State")
|
2019-04-30 15:58:11 +02:00
|
|
|
|
2020-07-30 02:40:40 +02:00
|
|
|
self.assertEqual(displayed_state['power'], 'Running',
|
2019-04-30 15:58:11 +02:00
|
|
|
"Power state failed to update on start")
|
|
|
|
|
|
|
|
self._run_command_and_process_events(
|
2019-12-04 21:05:45 +01:00
|
|
|
["qvm-shutdown", "--wait", target_vm_name], timeout=30)
|
2019-04-30 15:58:11 +02:00
|
|
|
|
2020-07-30 02:40:40 +02:00
|
|
|
displayed_state = self._get_table_item(vm_row, "State")
|
2019-04-30 15:58:11 +02:00
|
|
|
|
2020-07-30 02:40:40 +02:00
|
|
|
self.assertEqual(displayed_state['power'], 'Halted',
|
2019-04-30 15:58:11 +02:00
|
|
|
"Power state failed to update on shutdown")
|
|
|
|
|
|
|
|
def test_415_template_vm_started(self):
|
|
|
|
# check whether changing state of a template_vm causes all other
|
|
|
|
# vms depending on it to check theirs
|
|
|
|
target_vm_name = None
|
|
|
|
for vm in self.qapp.domains:
|
|
|
|
if vm.klass == 'TemplateVM':
|
|
|
|
for vm2 in self.qapp.domains:
|
|
|
|
if getattr(vm2, 'template', None) == vm.name:
|
|
|
|
target_vm_name = vm.name
|
|
|
|
break
|
|
|
|
if target_vm_name:
|
|
|
|
break
|
|
|
|
|
2020-07-30 02:40:40 +02:00
|
|
|
for i in range(self.dialog.table.model().rowCount()):
|
|
|
|
self._get_table_vminfo(i).update = unittest.mock.Mock()
|
2019-04-30 15:58:11 +02:00
|
|
|
|
|
|
|
self.addCleanup(
|
|
|
|
subprocess.call,
|
2019-12-04 21:05:45 +01:00
|
|
|
["qvm-shutdown", "--wait", target_vm_name])
|
2019-04-30 15:58:11 +02:00
|
|
|
self._run_command_and_process_events(
|
2019-12-04 21:05:45 +01:00
|
|
|
["qvm-start", target_vm_name], timeout=60)
|
2019-04-30 15:58:11 +02:00
|
|
|
|
2020-07-30 02:40:40 +02:00
|
|
|
for i in range(self.dialog.table.model().rowCount()):
|
|
|
|
call_count = self._get_table_vminfo(
|
|
|
|
i).update.call_count
|
|
|
|
if self._get_table_item(i, "Template") == target_vm_name:
|
2019-04-30 15:58:11 +02:00
|
|
|
self.assertGreater(call_count, 0)
|
2020-07-30 02:40:40 +02:00
|
|
|
elif self._get_table_item(i, "Name") == target_vm_name:
|
2019-04-30 15:58:11 +02:00
|
|
|
self.assertGreater(call_count, 0)
|
|
|
|
else:
|
|
|
|
self.assertEqual(call_count, 0)
|
|
|
|
|
2021-01-09 05:55:34 +01:00
|
|
|
@unittest.mock.patch('qubesmanager.log_dialog.LogDialog')
|
|
|
|
def test_500_logs(self, mock_log_dialog):
|
2019-04-30 15:58:11 +02:00
|
|
|
self._select_admin_vm()
|
|
|
|
|
2021-01-09 05:55:34 +01:00
|
|
|
self.dialog.action_show_logs.trigger()
|
|
|
|
mock_log_dialog.assert_called_once()
|
|
|
|
dom0_logs = mock_log_dialog.mock_calls[0][1][1]
|
|
|
|
for c in dom0_logs:
|
|
|
|
self.assertIn("hypervisor", c,
|
2019-04-30 15:58:11 +02:00
|
|
|
"Log for dom0 does not contain 'hypervisor'")
|
|
|
|
|
2021-01-09 05:55:34 +01:00
|
|
|
mock_log_dialog.reset_mock()
|
2019-04-30 15:58:11 +02:00
|
|
|
|
2021-01-09 05:55:34 +01:00
|
|
|
selected_vm = self._select_non_admin_vm(running=True).name
|
2019-04-30 15:58:11 +02:00
|
|
|
|
2021-01-09 05:55:34 +01:00
|
|
|
self.dialog.action_show_logs.trigger()
|
|
|
|
mock_log_dialog.assert_called_once()
|
|
|
|
vm_logs = mock_log_dialog.mock_calls[0][1][1]
|
|
|
|
for c in vm_logs:
|
2019-04-30 15:58:11 +02:00
|
|
|
self.assertIn(
|
|
|
|
selected_vm,
|
2021-01-09 05:55:34 +01:00
|
|
|
c,
|
2019-04-30 15:58:11 +02:00
|
|
|
"Log for {} does not contain its name".format(selected_vm))
|
|
|
|
|
|
|
|
self.assertNotEqual(dom0_logs, vm_logs,
|
|
|
|
"Same logs found for dom0 and non-adminVM")
|
|
|
|
|
|
|
|
def _find_vm_row(self, vm_name):
|
2020-07-30 02:40:40 +02:00
|
|
|
for row in range(self.dialog.table.model().rowCount()):
|
2019-04-30 15:58:11 +02:00
|
|
|
name = self._get_table_item(row, "Name")
|
2020-07-30 02:40:40 +02:00
|
|
|
if name == vm_name:
|
2019-04-30 15:58:11 +02:00
|
|
|
return row
|
|
|
|
return None
|
|
|
|
|
|
|
|
def _count_visible_table_rows(self):
|
|
|
|
result = 0
|
2020-07-30 02:40:40 +02:00
|
|
|
for i in range(self.dialog.table.model().rowCount()):
|
2019-04-30 15:58:11 +02:00
|
|
|
if not self.dialog.table.isRowHidden(i):
|
|
|
|
result += 1
|
|
|
|
return result
|
|
|
|
|
2020-08-14 12:38:21 +02:00
|
|
|
def _run_command_and_process_events(self, command, timeout=5,
|
|
|
|
additional_timeout=None):
|
2019-04-30 15:58:11 +02:00
|
|
|
"""
|
|
|
|
helper function to run a given command and process eventsDispatcher
|
|
|
|
events
|
|
|
|
:param command: list of strings, containing the command and all its
|
|
|
|
parameters
|
2019-10-17 00:16:01 +02:00
|
|
|
:param timeout: default 5 seconds
|
2020-08-14 12:38:21 +02:00
|
|
|
:param additional_timeout: default none
|
2019-04-30 15:58:11 +02:00
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
asyncio.set_event_loop(self.loop)
|
|
|
|
|
|
|
|
future1 = asyncio.ensure_future(self.dispatcher.listen_for_events())
|
2019-10-17 00:16:01 +02:00
|
|
|
self.loop.run_until_complete(asyncio.sleep(0))
|
2020-10-07 16:30:59 +02:00
|
|
|
|
2019-04-30 15:58:11 +02:00
|
|
|
future2 = asyncio.create_subprocess_exec(*command,
|
|
|
|
stdout=subprocess.DEVNULL,
|
|
|
|
stderr=subprocess.DEVNULL)
|
|
|
|
|
2020-10-07 16:30:59 +02:00
|
|
|
future2 = self.loop.run_until_complete(future2).wait()
|
|
|
|
|
2020-08-14 12:38:21 +02:00
|
|
|
if additional_timeout:
|
|
|
|
(done, pending) = self.loop.run_until_complete(
|
|
|
|
asyncio.wait({future1, future2}, timeout=timeout,
|
|
|
|
return_when=asyncio.FIRST_COMPLETED))
|
|
|
|
(done, pending) = self.loop.run_until_complete(
|
|
|
|
asyncio.wait(pending, timeout=additional_timeout))
|
|
|
|
else:
|
|
|
|
(done, pending) = self.loop.run_until_complete(
|
|
|
|
asyncio.wait({future1, future2}, timeout=timeout))
|
2019-04-30 15:58:11 +02:00
|
|
|
|
|
|
|
for task in pending:
|
|
|
|
with contextlib.suppress(asyncio.CancelledError):
|
|
|
|
task.cancel()
|
|
|
|
|
|
|
|
self.loop.call_soon(self.loop.stop)
|
|
|
|
self.loop.run_forever()
|
|
|
|
|
|
|
|
def _create_set_of_current_vms(self):
|
|
|
|
result = set()
|
2020-07-30 02:40:40 +02:00
|
|
|
for i in range(self.dialog.table.model().rowCount()):
|
|
|
|
result.add(self._get_table_item(i, "Name"))
|
2019-04-30 15:58:11 +02:00
|
|
|
return result
|
|
|
|
|
|
|
|
def _select_admin_vm(self):
|
2020-07-30 02:40:40 +02:00
|
|
|
for row in range(self.dialog.table.model().rowCount()):
|
|
|
|
template = self._get_table_item(row, "Template")
|
|
|
|
if template == 'AdminVM':
|
|
|
|
index = self.dialog.table.model().index(row, 0)
|
|
|
|
self.dialog.table.setCurrentIndex(index)
|
|
|
|
return index.data(Qt.UserRole).vm
|
2018-03-28 17:42:42 +02:00
|
|
|
return None
|
|
|
|
|
2019-04-30 15:58:11 +02:00
|
|
|
def _select_non_admin_vm(self, running=None):
|
2020-07-30 02:40:40 +02:00
|
|
|
for row in range(self.dialog.table.model().rowCount()):
|
|
|
|
template = self._get_table_item(row, "Template")
|
|
|
|
vm = self._get_table_vm(row)
|
|
|
|
if template != 'AdminVM' and \
|
2019-04-30 15:58:11 +02:00
|
|
|
(running is None
|
2020-07-30 02:40:40 +02:00
|
|
|
or (running and vm.is_running())
|
|
|
|
or (not running and not vm.is_running())):
|
|
|
|
index = self.dialog.table.model().index(row, 0)
|
|
|
|
self.dialog.table.setCurrentIndex(index)
|
|
|
|
return vm
|
2019-04-30 15:58:11 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
def _select_templatevm(self, running=None):
|
2020-07-30 02:40:40 +02:00
|
|
|
for row in range(self.dialog.table.model().rowCount()):
|
|
|
|
template = self._get_table_item(row, "Template")
|
|
|
|
vm = self._get_table_vm(row)
|
|
|
|
if template == 'TemplateVM' and \
|
2019-04-30 15:58:11 +02:00
|
|
|
(running is None
|
2020-07-30 02:40:40 +02:00
|
|
|
or (running and vm.is_running())
|
|
|
|
or (not running and not vm.is_running())):
|
|
|
|
index = self.dialog.table.model().index(row, 0)
|
|
|
|
self.dialog.table.setCurrentIndex(index)
|
|
|
|
return vm
|
2019-04-30 15:58:11 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
def __check_sorting(self, column_name):
|
|
|
|
last_text = None
|
|
|
|
last_vm = None
|
2020-07-30 02:40:40 +02:00
|
|
|
for row in range(self.dialog.table.model().rowCount()):
|
|
|
|
vm = self._get_table_item(row, "Name")
|
|
|
|
text = self._get_table_item(row, column_name)
|
2019-04-30 15:58:11 +02:00
|
|
|
|
|
|
|
if row == 0:
|
|
|
|
self.assertEqual(vm, "dom0", "dom0 is not sorted first")
|
|
|
|
elif last_text is None:
|
|
|
|
last_text = text
|
|
|
|
last_vm = vm
|
|
|
|
else:
|
|
|
|
if last_text == text:
|
|
|
|
self.assertGreater(
|
2020-07-30 02:40:40 +02:00
|
|
|
vm.lower(), last_vm.lower(),
|
2019-04-30 15:58:11 +02:00
|
|
|
"Incorrect sorting for {}".format(column_name))
|
|
|
|
else:
|
|
|
|
self.assertGreater(
|
2020-07-30 02:40:40 +02:00
|
|
|
text.lower(), last_text.lower(),
|
2019-04-30 15:58:11 +02:00
|
|
|
"Incorrect sorting for {}".format(column_name))
|
|
|
|
last_text = text
|
|
|
|
last_vm = vm
|
|
|
|
|
2020-07-30 02:40:40 +02:00
|
|
|
def _get_table_vminfo(self, row):
|
|
|
|
model = self.dialog.table.model()
|
|
|
|
return model.index(row, 0).data(Qt.UserRole)
|
2019-04-30 15:58:11 +02:00
|
|
|
|
2020-07-30 02:40:40 +02:00
|
|
|
def _get_table_vm(self, row):
|
|
|
|
model = self.dialog.table.model()
|
|
|
|
return model.index(row, 0).data(Qt.UserRole).vm
|
2019-04-30 15:58:11 +02:00
|
|
|
|
2020-09-29 00:43:16 +02:00
|
|
|
def _get_table_item(self, row, column_name, role=Qt.DisplayRole):
|
2020-07-30 02:40:40 +02:00
|
|
|
model = self.dialog.table.model()
|
|
|
|
column = self.dialog.qubes_model.columns_indices.index(column_name)
|
|
|
|
return model.index(row, column).data(role)
|
2019-04-30 15:58:11 +02:00
|
|
|
|
2020-08-14 12:38:21 +02:00
|
|
|
|
2019-04-30 15:58:11 +02:00
|
|
|
class QubeManagerThreadTest(unittest.TestCase):
|
|
|
|
def test_01_startvm_thread(self):
|
|
|
|
vm = unittest.mock.Mock(spec=['start'])
|
|
|
|
|
|
|
|
thread = qube_manager.StartVMThread(vm)
|
|
|
|
thread.run()
|
|
|
|
|
|
|
|
vm.start.assert_called_once_with()
|
|
|
|
|
|
|
|
def test_02_startvm_thread_error(self):
|
|
|
|
vm = unittest.mock.Mock(
|
|
|
|
spec=['start'],
|
|
|
|
**{'start.side_effect': exc.QubesException('Error')})
|
|
|
|
|
|
|
|
thread = qube_manager.StartVMThread(vm)
|
|
|
|
thread.run()
|
|
|
|
|
|
|
|
self.assertIsNotNone(thread.msg)
|
|
|
|
|
|
|
|
def test_10_run_command_thread(self):
|
|
|
|
vm = unittest.mock.Mock(spec=['run'])
|
|
|
|
|
|
|
|
thread = qube_manager.RunCommandThread(vm, "test_command")
|
|
|
|
thread.run()
|
|
|
|
|
|
|
|
vm.run.assert_called_once_with("test_command")
|
|
|
|
|
|
|
|
def test_11_run_command_thread_error(self):
|
|
|
|
vm = unittest.mock.Mock(spec=['run'],
|
|
|
|
**{'run.side_effect': ChildProcessError})
|
|
|
|
|
|
|
|
thread = qube_manager.RunCommandThread(vm, "test_command")
|
|
|
|
thread.run()
|
|
|
|
|
|
|
|
self.assertIsNotNone(thread.msg)
|
|
|
|
|
|
|
|
@unittest.mock.patch('subprocess.check_call')
|
|
|
|
def test_20_update_vm_thread_dom0(self, check_call):
|
2020-08-05 00:22:04 +02:00
|
|
|
vm = unittest.mock.Mock(spec=['klass'])
|
|
|
|
vm.klass = 'AdminVM'
|
2019-04-30 15:58:11 +02:00
|
|
|
thread = qube_manager.UpdateVMThread(vm)
|
|
|
|
thread.run()
|
|
|
|
|
|
|
|
check_call.assert_called_once_with(
|
|
|
|
["/usr/bin/qubes-dom0-update", "--clean", "--gui"])
|
|
|
|
|
|
|
|
@unittest.mock.patch('builtins.open')
|
|
|
|
@unittest.mock.patch('subprocess.call')
|
|
|
|
def test_21_update_vm_thread_running(self, mock_call, mock_open):
|
|
|
|
vm = unittest.mock.Mock(
|
2020-08-05 00:22:04 +02:00
|
|
|
spec=['klass', 'is_running', 'run_service_for_stdio', 'run_service'],
|
2019-04-30 15:58:11 +02:00
|
|
|
**{'is_running.return_value': True})
|
|
|
|
|
2020-08-05 00:22:04 +02:00
|
|
|
vm.klass = 'AppVM'
|
2019-04-30 15:58:11 +02:00
|
|
|
vm.run_service_for_stdio.return_value = (b'changed=no\n', None)
|
|
|
|
|
|
|
|
thread = qube_manager.UpdateVMThread(vm)
|
|
|
|
|
|
|
|
thread.run()
|
|
|
|
|
|
|
|
mock_open.assert_called_with(
|
|
|
|
'/usr/libexec/qubes-manager/dsa-4371-update', 'rb')
|
|
|
|
|
|
|
|
vm.run_service_for_stdio.assert_called_once_with(
|
|
|
|
"qubes.VMShell", user='root', input=unittest.mock.ANY)
|
|
|
|
|
|
|
|
vm.run_service.assert_called_once_with(
|
|
|
|
"qubes.InstallUpdatesGUI", user="root", wait=False)
|
|
|
|
|
|
|
|
self.assertEqual(mock_call.call_count, 0)
|
|
|
|
|
|
|
|
@unittest.mock.patch('builtins.open')
|
|
|
|
@unittest.mock.patch('subprocess.call')
|
|
|
|
def test_22_update_vm_thread_not_running(self, mock_call, mock_open):
|
|
|
|
vm = unittest.mock.Mock(
|
2020-08-05 00:22:04 +02:00
|
|
|
spec=['klass', 'is_running', 'run_service_for_stdio',
|
2019-04-30 15:58:11 +02:00
|
|
|
'run_service', 'start', 'name'],
|
|
|
|
**{'is_running.return_value': False})
|
|
|
|
|
2020-08-05 00:22:04 +02:00
|
|
|
vm.klass = 'AppVM'
|
2019-04-30 15:58:11 +02:00
|
|
|
vm.run_service_for_stdio.return_value = (b'changed=yes\n', None)
|
|
|
|
|
|
|
|
thread = qube_manager.UpdateVMThread(vm)
|
|
|
|
thread.run()
|
|
|
|
|
|
|
|
mock_open.assert_called_with(
|
|
|
|
'/usr/libexec/qubes-manager/dsa-4371-update', 'rb')
|
|
|
|
|
|
|
|
vm.start.assert_called_once_with()
|
|
|
|
|
|
|
|
vm.run_service_for_stdio.assert_called_once_with(
|
|
|
|
"qubes.VMShell", user='root', input=unittest.mock.ANY)
|
|
|
|
|
|
|
|
vm.run_service.assert_called_once_with(
|
|
|
|
"qubes.InstallUpdatesGUI", user="root", wait=False)
|
|
|
|
|
|
|
|
self.assertEqual(mock_call.call_count, 1)
|
|
|
|
|
|
|
|
@unittest.mock.patch('builtins.open')
|
|
|
|
@unittest.mock.patch('subprocess.check_call')
|
|
|
|
def test_23_update_vm_thread_error(self, *_args):
|
|
|
|
vm = unittest.mock.Mock(
|
2020-08-05 00:22:04 +02:00
|
|
|
spec=['klass', 'is_running'],
|
2019-04-30 15:58:11 +02:00
|
|
|
**{'is_running.side_effect': ChildProcessError})
|
|
|
|
|
2020-08-05 00:22:04 +02:00
|
|
|
vm.klass = 'AppVM'
|
2019-04-30 15:58:11 +02:00
|
|
|
|
|
|
|
thread = qube_manager.UpdateVMThread(vm)
|
|
|
|
thread.run()
|
|
|
|
|
|
|
|
self.assertIsNotNone(thread.msg)
|
|
|
|
|
|
|
|
|
|
|
|
class VMShutdownMonitorTest(unittest.TestCase):
|
2020-07-30 03:01:47 +02:00
|
|
|
@unittest.mock.patch('qubesmanager.qube_manager.QMessageBox')
|
2019-10-15 00:00:18 +02:00
|
|
|
@unittest.mock.patch('PyQt5.QtCore.QTimer')
|
2019-04-30 15:58:11 +02:00
|
|
|
def test_01_vm_shutdown_correct(self, mock_timer, mock_question):
|
|
|
|
mock_vm = unittest.mock.Mock()
|
|
|
|
mock_vm.is_running.return_value = False
|
|
|
|
|
|
|
|
monitor = qube_manager.VmShutdownMonitor(mock_vm)
|
|
|
|
monitor.restart_vm_if_needed = unittest.mock.Mock()
|
|
|
|
|
|
|
|
monitor.check_if_vm_has_shutdown()
|
|
|
|
|
|
|
|
self.assertEqual(mock_question.call_count, 0)
|
|
|
|
self.assertEqual(mock_timer.call_count, 0)
|
|
|
|
monitor.restart_vm_if_needed.assert_called_once_with()
|
|
|
|
|
2020-07-30 03:01:47 +02:00
|
|
|
@unittest.mock.patch('qubesmanager.qube_manager.QMessageBox')
|
2019-10-15 00:00:18 +02:00
|
|
|
@unittest.mock.patch('PyQt5.QtCore.QTimer.singleShot')
|
2019-04-30 15:58:11 +02:00
|
|
|
def test_02_vm_not_shutdown_wait(self, mock_timer, mock_question):
|
2019-10-17 00:16:01 +02:00
|
|
|
mock_question().clickedButton.return_value = 1
|
|
|
|
mock_question().addButton.return_value = 0
|
|
|
|
|
2019-04-30 15:58:11 +02:00
|
|
|
mock_vm = unittest.mock.Mock()
|
|
|
|
mock_vm.is_running.return_value = True
|
|
|
|
mock_vm.start_time = datetime.datetime.now().timestamp() - 3000
|
|
|
|
|
|
|
|
monitor = qube_manager.VmShutdownMonitor(mock_vm, shutdown_time=1)
|
|
|
|
time.sleep(3)
|
|
|
|
|
|
|
|
monitor.check_if_vm_has_shutdown()
|
|
|
|
|
|
|
|
self.assertEqual(mock_timer.call_count, 1)
|
|
|
|
|
2020-07-30 03:01:47 +02:00
|
|
|
@unittest.mock.patch('qubesmanager.qube_manager.QMessageBox')
|
2019-10-15 00:00:18 +02:00
|
|
|
@unittest.mock.patch('PyQt5.QtCore.QTimer.singleShot')
|
2019-04-30 15:58:11 +02:00
|
|
|
def test_03_vm_kill(self, mock_timer, mock_question):
|
2019-10-17 00:16:01 +02:00
|
|
|
mock_question().clickedButton.return_value = 1
|
|
|
|
mock_question().addButton.return_value = 1
|
|
|
|
|
2019-04-30 15:58:11 +02:00
|
|
|
mock_vm = unittest.mock.Mock()
|
|
|
|
mock_vm.is_running.return_value = True
|
|
|
|
mock_vm.start_time = datetime.datetime.now().timestamp() - 3000
|
|
|
|
|
|
|
|
monitor = qube_manager.VmShutdownMonitor(mock_vm, shutdown_time=1)
|
|
|
|
time.sleep(3)
|
|
|
|
monitor.restart_vm_if_needed = unittest.mock.Mock()
|
|
|
|
|
|
|
|
monitor.check_if_vm_has_shutdown()
|
|
|
|
|
|
|
|
self.assertEqual(mock_timer.call_count, 0)
|
|
|
|
mock_vm.kill.assert_called_once_with()
|
|
|
|
monitor.restart_vm_if_needed.assert_called_once_with()
|
|
|
|
|
2020-07-30 03:01:47 +02:00
|
|
|
@unittest.mock.patch('qubesmanager.qube_manager.QMessageBox')
|
2019-10-15 00:00:18 +02:00
|
|
|
@unittest.mock.patch('PyQt5.QtCore.QTimer.singleShot')
|
2019-04-30 15:58:11 +02:00
|
|
|
def test_04_check_later(self, mock_timer, mock_question):
|
|
|
|
mock_vm = unittest.mock.Mock()
|
|
|
|
mock_vm.is_running.return_value = True
|
|
|
|
mock_vm.start_time = datetime.datetime.now().timestamp() - 3000
|
|
|
|
|
|
|
|
monitor = qube_manager.VmShutdownMonitor(mock_vm, shutdown_time=3000)
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
monitor.check_if_vm_has_shutdown()
|
|
|
|
|
|
|
|
self.assertEqual(mock_question.call_count, 0)
|
|
|
|
self.assertEqual(mock_timer.call_count, 1)
|
|
|
|
|
|
|
|
|
2018-03-28 17:42:42 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
ha_syslog = logging.handlers.SysLogHandler('/dev/log')
|
|
|
|
ha_syslog.setFormatter(
|
|
|
|
logging.Formatter('%(name)s[%(process)d]: %(message)s'))
|
|
|
|
logging.root.addHandler(ha_syslog)
|
|
|
|
unittest.main()
|