test_qube_manager.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/python3
  2. #
  3. # The Qubes OS Project, https://www.qubes-os.org/
  4. #
  5. # Copyright (C) 2016 Marta Marczykowska-Górecka
  6. # <marmarta@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. #
  22. import logging.handlers
  23. import sys
  24. import unittest
  25. import unittest.mock
  26. import gc
  27. from PyQt4 import QtGui, QtTest, QtCore
  28. from qubesadmin import Qubes
  29. import qubesmanager.qube_manager as qube_manager
  30. class QubeManagerTest(unittest.TestCase):
  31. def setUp(self):
  32. super(QubeManagerTest, self).setUp()
  33. # # todo: mockup no settings file
  34. # self.patcher = unittest.mock.patch('builtins.open')
  35. # self.mock_open = self.patcher.start()
  36. # self.mock_open.side_effect = FileNotFoundError()
  37. # self.addCleanup(self.patcher.stop)
  38. self.qapp = Qubes()
  39. self.qtapp = QtGui.QApplication(sys.argv)
  40. self.dialog = qube_manager.VmManagerWindow(self.qtapp, self.qapp)
  41. def tearDown(self):
  42. del self.dialog
  43. del self.qtapp
  44. del self.qapp
  45. super(QubeManagerTest, self).tearDown()
  46. gc.collect()
  47. # 0 - Check if the window was displayed and populated correctly
  48. def test_00_window_loads(self):
  49. self.assertTrue(self.dialog.table is not None)
  50. @unittest.expectedFailure
  51. def test_01_table_populates_correctly(self):
  52. vms_in_table = []
  53. for row in range(self.dialog.table.rowCount()):
  54. item = self.dialog.table.item(row,
  55. self.dialog.columns_indices["Name"])
  56. self.assertIsNotNone(item)
  57. vms_in_table.append(item.text())
  58. actual_vms = [vm.name for vm in self.qapp.domains]
  59. self.assertEqual(len(vms_in_table), len(actual_vms),
  60. "Incorrect number of VMs loaded")
  61. self.assertListEqual(sorted(vms_in_table), sorted(actual_vms),
  62. "Incorrect VMs loaded")
  63. # todos:
  64. # did settings load correctly
  65. # did settings save corectly
  66. @unittest.mock.patch('qubesmanager.settings.VMSettingsWindow')
  67. def test_20_vm_open_settings(self, mock_window):
  68. selected_vm = self._select_non_admin_vm()
  69. self.assertIsNotNone(selected_vm, "No valid non-admin VM found")
  70. widget = self.dialog.toolbar.widgetForAction(
  71. self.dialog.action_settings)
  72. QtTest.QTest.mouseClick(widget,
  73. QtCore.Qt.LeftButton)
  74. mock_window.assert_called_once_with(selected_vm, self.qtapp, "basic")
  75. @unittest.mock.patch('qubesmanager.settings.VMSettingsWindow')
  76. def test_21_vm_firewall_settings(self, mock_window):
  77. selected_vm = self._select_non_admin_vm()
  78. self.assertIsNotNone(selected_vm, "No valid non-admin VM found")
  79. widget = self.dialog.toolbar.widgetForAction(
  80. self.dialog.action_editfwrules)
  81. QtTest.QTest.mouseClick(widget,
  82. QtCore.Qt.LeftButton)
  83. mock_window.assert_called_once_with(selected_vm, self.qtapp, "firewall")
  84. # test whether pause/start/resume works
  85. @unittest.mock.patch('qubesmanager.qubesadmin.vm.QubesVM.pause')
  86. @unittest.mock.patch('qubesmanager.qubesadmin.vm.QubesVM.is_running')
  87. @unittest.mock.patch('qubesmanager.qubesadmin.vm.QubesVM.get_power_state')
  88. def _select_non_admin_vm(self):
  89. for row in range(self.dialog.table.rowCount()):
  90. template = self.dialog.table.item(
  91. row, self.dialog.columns_indices["Template"])
  92. if template.text() != 'AdminVM':
  93. self.dialog.table.setCurrentItem(template)
  94. return template.vm
  95. return None
  96. if __name__ == "__main__":
  97. ha_syslog = logging.handlers.SysLogHandler('/dev/log')
  98. ha_syslog.setFormatter(
  99. logging.Formatter('%(name)s[%(process)d]: %(message)s'))
  100. logging.root.addHandler(ha_syslog)
  101. unittest.main()