class LiveImageSourceTestCase(unittest.TestCase):
    """Test the live image source module."""
    def setUp(self):
        self.module = LiveImageSourceModule()

    def type_test(self):
        """Test the type property."""
        self.assertEqual(SourceType.LIVE_IMAGE, self.module.type)

    def network_required_test(self):
        """Test the network_required property."""
        self.assertEqual(self.module.network_required, False)

        self.module.configuration.url = "file://my/path"
        self.assertEqual(self.module.network_required, False)

        self.module.configuration.url = "http://my/path"
        self.assertEqual(self.module.network_required, True)

        self.module.configuration.url = "https://my/path"
        self.assertEqual(self.module.network_required, True)

    def is_local_test(self):
        """Test the is_local property."""
        self.module.configuration.url = "file://my/path"
        self.assertEqual(self.module.is_local, True)

        self.module.configuration.url = "http://my/path"
        self.assertEqual(self.module.is_local, False)

    def get_state_test(self):
        """Test the source state."""
        self.assertEqual(SourceState.NOT_APPLICABLE, self.module.get_state())

    def required_space_test(self):
        """Test the required_space property."""
        self.assertEqual(self.module.required_space, 1024 * 1024 * 1024)

        self.module._required_space = 12345
        self.assertEqual(self.module.required_space, 12345)

    def set_up_with_tasks_test(self):
        """Test the set-up tasks."""
        self.module.configuration.url = "file://my/path"
        tasks = self.module.set_up_with_tasks()
        self.assertEqual(len(tasks), 1)
        self.assertIsInstance(tasks[0], SetUpLocalImageSourceTask)

        self.module.configuration.url = "http://my/path"
        tasks = self.module.set_up_with_tasks()
        self.assertEqual(len(tasks), 1)
        self.assertIsInstance(tasks[0], SetUpRemoteImageSourceTask)

    @patch.object(SetUpLocalImageSourceTask, "run")
    def handle_setup_task_result_test(self, runner):
        """Test the handler of the set-up tasks."""
        self.module.configuration.url = "file://my/path"
        runner.return_value = SetupImageResult(12345)

        tasks = self.module.set_up_with_tasks()
        for task in tasks:
            task.run_with_signals()

        runner.assert_called_once_with()
        self.assertEqual(self.module.required_space, 12345)

    def tear_down_with_tasks_test(self):
        """Test the tear-down tasks."""
        self.assertEqual(self.module.tear_down_with_tasks(), [])

    def repr_test(self):
        """Test the string representation."""
        self.module.configuration.url = "file://my/path"
        self.assertEqual(
            repr(self.module),
            str("Source("
                "type='LIVE_IMAGE', "
                "url='file://my/path'"
                ")"))
class LiveImageSourceTestCase(unittest.TestCase):
    """Test the live image source module."""
    def setUp(self):
        self.module = LiveImageSourceModule()

    def test_type(self):
        """Test the type property."""
        assert SourceType.LIVE_IMAGE == self.module.type

    def test_network_required(self):
        """Test the network_required property."""
        assert self.module.network_required is False

        self.module.configuration.url = "file://my/path"
        assert self.module.network_required is False

        self.module.configuration.url = "http://my/path"
        assert self.module.network_required is True

        self.module.configuration.url = "https://my/path"
        assert self.module.network_required is True

    def test_is_local(self):
        """Test the is_local property."""
        self.module.configuration.url = "file://my/path"
        assert self.module.is_local is True

        self.module.configuration.url = "http://my/path"
        assert self.module.is_local is False

    def test_get_state(self):
        """Test the source state."""
        assert SourceState.NOT_APPLICABLE == self.module.get_state()

    def test_required_space(self):
        """Test the required_space property."""
        assert self.module.required_space == 1024 * 1024 * 1024

        self.module._required_space = 12345
        assert self.module.required_space == 12345

    def test_set_up_with_tasks(self):
        """Test the set-up tasks."""
        self.module.configuration.url = "file://my/path"
        tasks = self.module.set_up_with_tasks()
        assert len(tasks) == 1
        assert isinstance(tasks[0], SetUpLocalImageSourceTask)

        self.module.configuration.url = "http://my/path"
        tasks = self.module.set_up_with_tasks()
        assert len(tasks) == 1
        assert isinstance(tasks[0], SetUpRemoteImageSourceTask)

    @patch.object(SetUpLocalImageSourceTask, "run")
    def test_handle_setup_task_result(self, runner):
        """Test the handler of the set-up tasks."""
        self.module.configuration.url = "file://my/path"
        runner.return_value = SetupImageResult(12345)

        tasks = self.module.set_up_with_tasks()
        for task in tasks:
            task.run_with_signals()

        runner.assert_called_once_with()
        assert self.module.required_space == 12345

    def test_tear_down_with_tasks(self):
        """Test the tear-down tasks."""
        assert self.module.tear_down_with_tasks() == []

    def test_repr(self):
        """Test the string representation."""
        self.module.configuration.url = "file://my/path"
        assert repr(self.module) == str("Source("
                                        "type='LIVE_IMAGE', "
                                        "url='file://my/path'"
                                        ")")