def test_should_not_warning_about_fab_plugins(self):
        class AirflowAdminViewsPlugin(AirflowPlugin):
            name = "test_admin_views_plugin"

            appbuilder_views = [mock.MagicMock()]

        class AirflowAdminMenuLinksPlugin(AirflowPlugin):
            name = "test_menu_links_plugin"

            appbuilder_menu_items = [mock.MagicMock()]

        with mock_plugin_manager(plugins=[
                AirflowAdminViewsPlugin(),
                AirflowAdminMenuLinksPlugin()
        ]):
            from airflow import plugins_manager

            # assert not logs
            with self.assertRaises(AssertionError), self.assertLogs(
                    plugins_manager.log):
                plugins_manager.initialize_web_ui_plugins()
    def test_should_not_warning_about_fab_and_flask_admin_plugins(self, caplog):
        class AirflowAdminViewsPlugin(AirflowPlugin):
            name = "test_admin_views_plugin"

            admin_views = [mock.MagicMock()]
            appbuilder_views = [mock.MagicMock()]

        class AirflowAdminMenuLinksPlugin(AirflowPlugin):
            name = "test_menu_links_plugin"

            menu_links = [mock.MagicMock()]
            appbuilder_menu_items = [mock.MagicMock()]

        with mock_plugin_manager(
            plugins=[AirflowAdminViewsPlugin(), AirflowAdminMenuLinksPlugin()]
        ), caplog.at_level(logging.WARNING, logger='airflow.plugins_manager'):
            from airflow import plugins_manager

            plugins_manager.initialize_web_ui_plugins()

        assert caplog.record_tuples == []
示例#3
0
    def test_should_load_plugins_from_property(self, caplog):
        class AirflowTestPropertyPlugin(AirflowPlugin):
            name = "test_property_plugin"

            @property
            def hooks(self):
                class TestPropertyHook(BaseHook):
                    pass

                return [TestPropertyHook]

        with mock_plugin_manager(plugins=[AirflowTestPropertyPlugin()]):
            from airflow import plugins_manager

            plugins_manager.ensure_plugins_loaded()

            assert 'AirflowTestPropertyPlugin' in str(plugins_manager.plugins)
            assert 'TestPropertyHook' in str(plugins_manager.registered_hooks)

        assert caplog.records[0].levelname == 'INFO'
        assert caplog.records[0].msg == 'Loading %d plugin(s) took %.2f seconds'
    def test_registering_plugin_macros(self, request):
        """
        Tests whether macros that originate from plugins are being registered correctly.
        """
        from airflow import macros
        from airflow.plugins_manager import integrate_macros_plugins

        def cleanup_macros():
            """Reloads the airflow.macros module such that the symbol table is reset after the test."""
            # We're explicitly deleting the module from sys.modules and importing it again
            # using import_module() as opposed to using importlib.reload() because the latter
            # does not undo the changes to the airflow.macros module that are being caused by
            # invoking integrate_macros_plugins()
            del sys.modules['airflow.macros']
            importlib.import_module('airflow.macros')

        request.addfinalizer(cleanup_macros)

        def custom_macro():
            return 'foo'

        class MacroPlugin(AirflowPlugin):
            name = 'macro_plugin'
            macros = [custom_macro]

        with mock_plugin_manager(plugins=[MacroPlugin()]):
            # Ensure the macros for the plugin have been integrated.
            integrate_macros_plugins()
            # Test whether the modules have been created as expected.
            plugin_macros = importlib.import_module(
                f"airflow.macros.{MacroPlugin.name}")
            for macro in MacroPlugin.macros:
                # Verify that the macros added by the plugin are being set correctly
                # on the plugin's macro module.
                assert hasattr(plugin_macros, macro.__name__)
            # Verify that the symbol table in airflow.macros has been updated with an entry for
            # this plugin, this is necessary in order to allow the plugin's macros to be used when
            # rendering templates.
            assert hasattr(macros, MacroPlugin.name)
 def test_get_plugins_return_200(self):
     mock_plugin = AirflowPlugin()
     mock_plugin.name = "test_plugin"
     with mock_plugin_manager(plugins=[mock_plugin]):
         response = self.client.get(
             "api/v1/plugins", environ_overrides={'REMOTE_USER': "******"})
     assert response.status_code == 200
     assert response.json == {
         'plugins': [{
             'appbuilder_menu_items': [],
             'appbuilder_views': [],
             'executors': [],
             'flask_blueprints': [],
             'global_operator_extra_links': [],
             'hooks': [],
             'macros': [],
             'operator_extra_links': [],
             'source': None,
             'name': 'test_plugin',
         }],
         'total_entries':
         1,
     }
 def test_get_plugins_return_200_if_no_plugins(self):
     with mock_plugin_manager(plugins=[]):
         response = self.client.get(
             "api/v1/plugins", environ_overrides={'REMOTE_USER': "******"})
     assert response.status_code == 200