예제 #1
0
def _bootstrap_subprocess(package_name: str, conf_logging: Dict[str, Any]):
    # pylint: disable=import-outside-toplevel,protected-access,cyclic-import
    from kedro.framework.session.session import _register_all_project_hooks

    hook_manager = get_hook_manager()
    _register_all_project_hooks(hook_manager, package_name)
    logging.config.dictConfig(conf_logging)
예제 #2
0
    def test_hooks_registered_when_session_created(self, mocker, request,
                                                   caplog, logging_hooks,
                                                   num_plugins):
        hook_manager = get_hook_manager()
        assert not hook_manager.get_plugins()

        load_setuptools_entrypoints = mocker.patch.object(
            hook_manager,
            "load_setuptools_entrypoints",
            return_value=num_plugins)
        distinfo = [("plugin_obj_1", MockDistInfo("test-project-a", "0.1"))]
        list_distinfo_mock = mocker.patch.object(hook_manager,
                                                 "list_plugin_distinfo",
                                                 return_value=distinfo)

        # call a fixture which creates a session
        request.getfixturevalue("mock_session_with_hooks")
        assert hook_manager.is_registered(logging_hooks)

        load_setuptools_entrypoints.assert_called_once_with("kedro.hooks")
        list_distinfo_mock.assert_called_once_with()

        if num_plugins:
            log_messages = [record.getMessage() for record in caplog.records]
            plugin = f"{distinfo[0][1].project_name}-{distinfo[0][1].version}"
            expected_msg = (
                f"Registered hooks from {num_plugins} installed plugin(s): {plugin}"
            )
            assert expected_msg in log_messages
예제 #3
0
    def test_disabling_auto_discovered_hooks(
        self, mocker, caplog, tmp_path, mock_package_name, naughty_plugin, good_plugin,
    ):
        hook_manager = get_hook_manager()
        assert not hook_manager.get_plugins()

        distinfo = [("plugin_obj_1", naughty_plugin), ("plugin_obj_2", good_plugin)]
        list_distinfo_mock = mocker.patch.object(
            hook_manager, "list_plugin_distinfo", return_value=distinfo
        )
        mocker.patch.object(
            hook_manager, "load_setuptools_entrypoints", return_value=len(distinfo)
        )
        unregister_mock = mocker.patch.object(hook_manager, "unregister")

        KedroSession.create(
            mock_package_name, tmp_path, extra_params={"params:key": "value"}
        )
        list_distinfo_mock.assert_called_once_with()
        unregister_mock.assert_called_once_with(plugin=distinfo[0][0])

        # check the logs
        log_messages = [record.getMessage() for record in caplog.records]
        expected_msg = (
            f"Registered hooks from 1 installed plugin(s): "
            f"{good_plugin.project_name}-{good_plugin.version}"
        )
        assert expected_msg in log_messages

        expected_msg = (
            f"Hooks are disabled for plugin(s): "
            f"{naughty_plugin.project_name}-{naughty_plugin.version}"
        )
        assert expected_msg in log_messages
예제 #4
0
    def test_assert_register_hooks(self, request, logging_hooks):
        hook_manager = get_hook_manager()
        assert not hook_manager.is_registered(logging_hooks)

        # call the fixture to construct the session
        request.getfixturevalue("mock_session_with_hooks")

        assert hook_manager.is_registered(logging_hooks)
예제 #5
0
    def test_calling_register_hooks_twice(self, project_hooks):
        """Calling hook registration multiple times should not raise"""
        hook_manager = get_hook_manager()

        assert hook_manager.is_registered(project_hooks)
        _register_hooks(hook_manager, (project_hooks,))
        _register_hooks(hook_manager, (project_hooks,))
        assert hook_manager.is_registered(project_hooks)
예제 #6
0
    def test_calling_register_hooks_twice(self, logging_hooks):
        """Calling hook registration multiple times should not raise"""
        hook_manager = get_hook_manager()

        # hooks already registered when fixture 'mock_session_with_hooks' was called
        assert hook_manager.is_registered(logging_hooks)
        _register_hooks(hook_manager, (logging_hooks, RequiredHooks()))
        _register_hooks(hook_manager, (logging_hooks, RequiredHooks()))
        assert hook_manager.is_registered(logging_hooks)
예제 #7
0
    def test_calling_register_hooks_twice(self, mock_session_with_hooks, logging_hooks):
        """Calling hook registration multiple times should not raise"""
        hook_manager = get_hook_manager()
        package_name = mock_session_with_hooks._package_name

        # hooks already registered when fixture 'mock_session_with_hooks' was called
        assert hook_manager.is_registered(logging_hooks)
        _register_all_project_hooks(hook_manager, package_name)
        _register_all_project_hooks(hook_manager, package_name)
        assert hook_manager.is_registered(logging_hooks)
예제 #8
0
    def test_disabling_auto_discovered_hooks(
        self, mocker, request, caplog, mock_settings_import
    ):
        hook_manager = get_hook_manager()
        assert not hook_manager.get_plugins()

        # pretend that some setuptools plugins were autodiscovered
        naughty_plugin = MockDistInfo("test-project-a", "0.1")
        good_plugin = MockDistInfo("test-project-b", "0.2")

        distinfo = [("plugin_obj_1", naughty_plugin), ("plugin_obj_2", good_plugin)]
        list_distinfo_mock = mocker.patch.object(
            hook_manager, "list_plugin_distinfo", return_value=distinfo
        )
        mocker.patch.object(
            hook_manager, "load_setuptools_entrypoints", return_value=len(distinfo)
        )
        unregister_mock = mocker.patch.object(hook_manager, "unregister")

        # pretend that we disabled hooks for plugin 'test-project-a'
        mock_settings_import.return_value.DISABLE_HOOKS_FOR_PLUGINS = (
            naughty_plugin.project_name,
        )

        # call a fixture which creates a session
        request.getfixturevalue("mock_session_with_hooks")
        list_distinfo_mock.assert_called_once_with()
        unregister_mock.assert_called_once_with(plugin=distinfo[0][0])

        # check the logs
        log_messages = [record.getMessage() for record in caplog.records]
        expected_msg = (
            f"Registered hooks from 1 installed plugin(s): "
            f"{good_plugin.project_name}-{good_plugin.version}"
        )
        assert expected_msg in log_messages

        expected_msg = (
            f"Hooks are disabled for plugin(s): "
            f"{naughty_plugin.project_name}-{naughty_plugin.version}"
        )
        assert expected_msg in log_messages
예제 #9
0
    def test_disabling_auto_discovered_hooks(self, mocker, caplog,
                                             logging_hooks, tmp_path):
        hook_manager = get_hook_manager()
        assert not hook_manager.get_plugins()

        # pretend that some setuptools plugins were autodiscovered
        naughty_plugin = MockDistInfo("test-project-a", "0.1")
        good_plugin = MockDistInfo("test-project-b", "0.2")

        distinfo = [("plugin_obj_1", naughty_plugin),
                    ("plugin_obj_2", good_plugin)]
        list_distinfo_mock = mocker.patch.object(hook_manager,
                                                 "list_plugin_distinfo",
                                                 return_value=distinfo)
        mocker.patch.object(hook_manager,
                            "load_setuptools_entrypoints",
                            return_value=len(distinfo))
        unregister_mock = mocker.patch.object(hook_manager, "unregister")

        mocker.patch("kedro.framework.project._validate_module")
        logging_hooks.queue_listener.start()
        configure_project(MOCK_PACKAGE_NAME)
        KedroSession.create(MOCK_PACKAGE_NAME,
                            tmp_path,
                            extra_params={"params:key": "value"})
        logging_hooks.queue_listener.stop()
        list_distinfo_mock.assert_called_once_with()
        unregister_mock.assert_called_once_with(plugin=distinfo[0][0])

        # check the logs
        log_messages = [record.getMessage() for record in caplog.records]
        expected_msg = (f"Registered hooks from 1 installed plugin(s): "
                        f"{good_plugin.project_name}-{good_plugin.version}")
        assert expected_msg in log_messages

        expected_msg = (
            f"Hooks are disabled for plugin(s): "
            f"{naughty_plugin.project_name}-{naughty_plugin.version}")
        assert expected_msg in log_messages
예제 #10
0
def clear_hook_manager():
    yield
    hook_manager = get_hook_manager()
    plugins = hook_manager.get_plugins()
    for plugin in plugins:
        hook_manager.unregister(plugin)