class SnapshotInterfaceTestCase(unittest.TestCase):
    """Test DBus interface of the Snapshot module."""

    def setUp(self):
        """Set up the module."""
        self.module = SnapshotModule()
        self.interface = SnapshotInterface(self.module)

    def is_requested_test(self):
        """Test IsRequested."""
        self.assertEqual(self.interface.IsRequested(SNAPSHOT_WHEN_PRE_INSTALL), False)
        self.assertEqual(self.interface.IsRequested(SNAPSHOT_WHEN_POST_INSTALL), False)

        self.module._requests = [Mock(when=SNAPSHOT_WHEN_PRE_INSTALL)]
        self.assertEqual(self.interface.IsRequested(SNAPSHOT_WHEN_PRE_INSTALL), True)
        self.assertEqual(self.interface.IsRequested(SNAPSHOT_WHEN_POST_INSTALL), False)

        self.module._requests = [Mock(when=SNAPSHOT_WHEN_POST_INSTALL)]
        self.assertEqual(self.interface.IsRequested(SNAPSHOT_WHEN_PRE_INSTALL), False)
        self.assertEqual(self.interface.IsRequested(SNAPSHOT_WHEN_POST_INSTALL), True)

        self.module._requests = [Mock(when=SNAPSHOT_WHEN_PRE_INSTALL),
                                 Mock(when=SNAPSHOT_WHEN_POST_INSTALL)]
        self.assertEqual(self.interface.IsRequested(SNAPSHOT_WHEN_PRE_INSTALL), True)
        self.assertEqual(self.interface.IsRequested(SNAPSHOT_WHEN_POST_INSTALL), True)

    @patch('pyanaconda.dbus.DBus.publish_object')
    def create_with_task_test(self, publisher):
        """Test CreateWithTask."""
        with self.assertRaises(UnavailableStorageError):
            self.interface.CreateWithTask(SNAPSHOT_WHEN_PRE_INSTALL)

        self.module.on_storage_reset(Mock())
        task_path = self.interface.CreateWithTask(SNAPSHOT_WHEN_PRE_INSTALL)

        publisher.assert_called_once()
        object_path, obj = publisher.call_args[0]

        self.assertEqual(task_path, object_path)
        self.assertIsInstance(obj, TaskInterface)

        self.assertIsInstance(obj.implementation, SnapshotCreateTask)
        self.assertEqual(obj.implementation._storage, self.module.storage)
        self.assertEqual(obj.implementation._requests, [])
        self.assertEqual(obj.implementation._when, SNAPSHOT_WHEN_PRE_INSTALL)

    @patch('pyanaconda.modules.storage.snapshot.snapshot.get_snapshot_device')
    def verify_requests_test(self, device_getter):
        """Test the verify_requests method."""
        report_error = Mock()
        report_warning = Mock()
        self.module._requests = [Mock(when=SNAPSHOT_WHEN_POST_INSTALL)]

        # Test passing check.
        self.module.verify_requests(Mock(), Mock(), report_error, report_warning)
        report_error.assert_not_called()
        report_warning.assert_not_called()

        # Test failing check.
        device_getter.side_effect = KickstartParseError("Fake error")
        self.module.verify_requests(Mock(), Mock(), report_error, report_warning)
        report_error.assert_called_once_with("Fake error")
        report_warning.assert_not_called()
class SnapshotInterfaceTestCase(unittest.TestCase):
    """Test DBus interface of the Snapshot module."""
    def setUp(self):
        """Set up the module."""
        self.module = SnapshotModule()
        self.interface = SnapshotInterface(self.module)

    def is_requested_test(self):
        """Test IsRequested."""
        self.assertEqual(self.interface.IsRequested(SNAPSHOT_WHEN_PRE_INSTALL),
                         False)
        self.assertEqual(
            self.interface.IsRequested(SNAPSHOT_WHEN_POST_INSTALL), False)

        self.module._requests = [Mock(when=SNAPSHOT_WHEN_PRE_INSTALL)]
        self.assertEqual(self.interface.IsRequested(SNAPSHOT_WHEN_PRE_INSTALL),
                         True)
        self.assertEqual(
            self.interface.IsRequested(SNAPSHOT_WHEN_POST_INSTALL), False)

        self.module._requests = [Mock(when=SNAPSHOT_WHEN_POST_INSTALL)]
        self.assertEqual(self.interface.IsRequested(SNAPSHOT_WHEN_PRE_INSTALL),
                         False)
        self.assertEqual(
            self.interface.IsRequested(SNAPSHOT_WHEN_POST_INSTALL), True)

        self.module._requests = [
            Mock(when=SNAPSHOT_WHEN_PRE_INSTALL),
            Mock(when=SNAPSHOT_WHEN_POST_INSTALL)
        ]
        self.assertEqual(self.interface.IsRequested(SNAPSHOT_WHEN_PRE_INSTALL),
                         True)
        self.assertEqual(
            self.interface.IsRequested(SNAPSHOT_WHEN_POST_INSTALL), True)

    @patch('pyanaconda.dbus.DBus.publish_object')
    def create_with_task_test(self, publisher):
        """Test CreateWithTask."""
        with self.assertRaises(UnavailableStorageError):
            self.interface.CreateWithTask(SNAPSHOT_WHEN_PRE_INSTALL)

        self.module.on_storage_reset(Mock())
        task_path = self.interface.CreateWithTask(SNAPSHOT_WHEN_PRE_INSTALL)

        publisher.assert_called_once()
        object_path, obj = publisher.call_args[0]

        self.assertEqual(task_path, object_path)
        self.assertIsInstance(obj, TaskInterface)

        self.assertIsInstance(obj.implementation, SnapshotCreateTask)
        self.assertEqual(obj.implementation._storage, self.module.storage)
        self.assertEqual(obj.implementation._requests, [])
        self.assertEqual(obj.implementation._when, SNAPSHOT_WHEN_PRE_INSTALL)

    @patch('pyanaconda.modules.storage.snapshot.snapshot.get_snapshot_device')
    def verify_requests_test(self, device_getter):
        """Test the verify_requests method."""
        report_error = Mock()
        report_warning = Mock()
        self.module._requests = [Mock(when=SNAPSHOT_WHEN_POST_INSTALL)]

        # Test passing check.
        self.module.verify_requests(Mock(), Mock(), report_error,
                                    report_warning)
        report_error.assert_not_called()
        report_warning.assert_not_called()

        # Test failing check.
        device_getter.side_effect = KickstartParseError("Fake error")
        self.module.verify_requests(Mock(), Mock(), report_error,
                                    report_warning)
        report_error.assert_called_once_with("Fake error")
        report_warning.assert_not_called()
Example #3
0
class SnapshotInterfaceTestCase(unittest.TestCase):
    """Test DBus interface of the Snapshot module."""
    def setUp(self):
        """Set up the module."""
        self.module = SnapshotModule()
        self.interface = SnapshotInterface(self.module)

    def is_requested_test(self):
        """Test IsRequested."""
        self.assertEqual(self.interface.IsRequested(SNAPSHOT_WHEN_PRE_INSTALL),
                         False)
        self.assertEqual(
            self.interface.IsRequested(SNAPSHOT_WHEN_POST_INSTALL), False)

        self.module._requests = [Mock(when=SNAPSHOT_WHEN_PRE_INSTALL)]
        self.assertEqual(self.interface.IsRequested(SNAPSHOT_WHEN_PRE_INSTALL),
                         True)
        self.assertEqual(
            self.interface.IsRequested(SNAPSHOT_WHEN_POST_INSTALL), False)

        self.module._requests = [Mock(when=SNAPSHOT_WHEN_POST_INSTALL)]
        self.assertEqual(self.interface.IsRequested(SNAPSHOT_WHEN_PRE_INSTALL),
                         False)
        self.assertEqual(
            self.interface.IsRequested(SNAPSHOT_WHEN_POST_INSTALL), True)

        self.module._requests = [
            Mock(when=SNAPSHOT_WHEN_PRE_INSTALL),
            Mock(when=SNAPSHOT_WHEN_POST_INSTALL)
        ]
        self.assertEqual(self.interface.IsRequested(SNAPSHOT_WHEN_PRE_INSTALL),
                         True)
        self.assertEqual(
            self.interface.IsRequested(SNAPSHOT_WHEN_POST_INSTALL), True)

    @patch('pyanaconda.dbus.DBus.publish_object')
    def validate_with_task_test(self, publisher):
        """Test ValidateWithTask."""
        with self.assertRaises(UnavailableStorageError):
            self.interface.ValidateWithTask(SNAPSHOT_WHEN_POST_INSTALL)

        self.module.on_storage_reset(Mock())
        task_path = self.interface.ValidateWithTask(SNAPSHOT_WHEN_POST_INSTALL)

        publisher.assert_called_once()
        object_path, obj = publisher.call_args[0]

        self.assertEqual(task_path, object_path)
        self.assertIsInstance(obj, TaskInterface)

        self.assertIsInstance(obj.implementation, SnapshotValidateTask)
        self.assertEqual(obj.implementation._storage, self.module.storage)
        self.assertEqual(obj.implementation._requests, [])
        self.assertEqual(obj.implementation._when, SNAPSHOT_WHEN_POST_INSTALL)

    @patch('pyanaconda.dbus.DBus.publish_object')
    def create_with_task_test(self, publisher):
        """Test CreateWithTask."""
        with self.assertRaises(UnavailableStorageError):
            self.interface.CreateWithTask(SNAPSHOT_WHEN_PRE_INSTALL)

        self.module.on_storage_reset(Mock())
        task_path = self.interface.CreateWithTask(SNAPSHOT_WHEN_PRE_INSTALL)

        publisher.assert_called_once()
        object_path, obj = publisher.call_args[0]

        self.assertEqual(task_path, object_path)
        self.assertIsInstance(obj, TaskInterface)

        self.assertIsInstance(obj.implementation, SnapshotCreateTask)
        self.assertEqual(obj.implementation._storage, self.module.storage)
        self.assertEqual(obj.implementation._requests, [])
        self.assertEqual(obj.implementation._when, SNAPSHOT_WHEN_PRE_INSTALL)