test_backup_01.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. from PyQt4 import QtGui, QtTest, QtCore
  27. from qubesadmin import Qubes
  28. import qubesmanager.backup as backup_gui
  29. class BackupTest(unittest.TestCase):
  30. def setUp(self):
  31. super(BackupTest, self).setUp()
  32. # mock up nonexistence of saved backup settings
  33. self.patcher = unittest.mock.patch('builtins.open')
  34. self.mock_open = self.patcher.start()
  35. self.mock_open.side_effect = FileNotFoundError()
  36. self.addCleanup(self.patcher.stop)
  37. self.qapp = Qubes()
  38. self.qtapp = QtGui.QApplication(sys.argv)
  39. self.dialog = backup_gui.BackupVMsWindow(self.qtapp, self.qapp)
  40. def tearDown(self):
  41. del self.dialog
  42. del self.qtapp
  43. del self.qapp
  44. super(BackupTest, self).tearDown()
  45. def test_00_window_loads(self):
  46. self.assertTrue(self.dialog.select_vms_widget is not None)
  47. def test_01_vms_load_correctly(self):
  48. all_vms = len([vm for vm in self.qapp.domains
  49. if not vm.features.get('internal', False)])
  50. selected_vms = self.dialog.select_vms_widget.selected_list.count()
  51. available_vms = self.dialog.select_vms_widget.available_list.count()
  52. self.assertEqual(all_vms, available_vms + selected_vms)
  53. def test_02_correct_defaults(self):
  54. # backup is compressed
  55. self.assertTrue(self.dialog.compress_checkbox.isChecked(),
  56. "Compress backup should be checked by default")
  57. # correct VMs are selected
  58. include_in_backups_no = len([vm for vm in self.qapp.domains
  59. if not vm.features.get('internal', False)
  60. and getattr(vm, 'include_in_backups', True)])
  61. selected_no = self.dialog.select_vms_widget.selected_list.count()
  62. self.assertEqual(include_in_backups_no, selected_no,
  63. "Incorrect VMs selected by default")
  64. # passphrase is empty
  65. self.assertEqual(self.dialog.passphrase_line_edit.text(), "",
  66. "Passphrase should be empty")
  67. # save defaults
  68. self.assertTrue(self.dialog.save_profile_checkbox.isChecked(),
  69. "By default, profile should be saved")
  70. # Check if target vms are selected
  71. # Check if no default file loads correctly - another file??
  72. # TODO: make a separate backup testing file to test various backup defaults
  73. if __name__ == "__main__":
  74. ha_syslog = logging.handlers.SysLogHandler('/dev/log')
  75. ha_syslog.setFormatter(
  76. logging.Formatter('%(name)s[%(process)d]: %(message)s'))
  77. logging.root.addHandler(ha_syslog)
  78. unittest.main()