Back to Repositories

Testing Plugin Manager State Isolation in Apache Airflow

This test suite implements mocking utilities for Airflow’s plugin manager, providing controlled testing environments for plugin-related functionality. It enables isolation of plugin manager state and prevents test interference through careful management of global variables.

Test Coverage Overview

The test coverage focuses on mocking the Airflow plugin manager’s state and functionality.

Key areas covered include:
  • Plugin loading and initialization
  • Version-specific attribute handling
  • Global variable state management
  • Error handling for plugin imports

Implementation Analysis

The testing approach utilizes Python’s contextmanager pattern to create isolated testing environments.

Technical implementation includes:
  • Context manager for state protection
  • Dynamic attribute patching based on Airflow version
  • Mock object injection for plugin management
  • ExitStack for managing multiple context managers

Technical Details

Testing tools and configuration:
  • unittest.mock for mocking functionality
  • contextlib.ExitStack for context management
  • Version-specific attribute lists (PLUGINS_MANAGER_NULLABLE_ATTRIBUTES)
  • Custom mock_plugin_manager context manager

Best Practices Demonstrated

The test implementation showcases several testing best practices in Python.

Notable practices include:
  • Proper isolation of test cases
  • State preservation and restoration
  • Clean separation of concerns
  • Comprehensive error handling
  • Version-aware implementation

apache/airflow

tests_common/test_utils/mock_plugins.py

            
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
from __future__ import annotations

from contextlib import ExitStack, contextmanager
from unittest import mock

from airflow import __version__ as airflow_version

PLUGINS_MANAGER_NULLABLE_ATTRIBUTES = [
    "plugins",
    "macros_modules",
    "admin_views",
    "flask_blueprints",
    "fastapi_apps",
    "menu_links",
    "flask_appbuilder_views",
    "flask_appbuilder_menu_links",
    "global_operator_extra_links",
    "operator_extra_links",
    "registered_operator_link_classes",
    "timetable_classes",
    "hook_lineage_reader_classes",
]


PLUGINS_MANAGER_NULLABLE_ATTRIBUTES_V2_10 = [
    "plugins",
    "macros_modules",
    "admin_views",
    "flask_blueprints",
    "menu_links",
    "flask_appbuilder_views",
    "flask_appbuilder_menu_links",
    "global_operator_extra_links",
    "operator_extra_links",
    "registered_operator_link_classes",
    "timetable_classes",
    "hook_lineage_reader_classes",
]


@contextmanager
def mock_plugin_manager(plugins=None, **kwargs):
    """
    Protects the initial state and sets the default state for the airflow.plugins module.

    You can also overwrite variables by passing a keyword argument.

    airflow.plugins_manager uses many global variables. To avoid side effects, this decorator performs
    the following operations:

    1. saves variables state,
    2. set variables to default value,
    3. executes context code,
    4. restores the state of variables to the state from point 1.

    Use this context if you want your test to not have side effects in airflow.plugins_manager, and
    other tests do not affect the results of this test.
    """
    illegal_arguments = set(kwargs.keys()) - set(PLUGINS_MANAGER_NULLABLE_ATTRIBUTES) - {"import_errors"}
    if illegal_arguments:
        raise TypeError(
            f"TypeError: mock_plugin_manager got an unexpected keyword arguments: {illegal_arguments}"
        )
    # Handle plugins specially
    with ExitStack() as exit_stack:

        def mock_loaded_plugins():
            exit_stack.enter_context(mock.patch("airflow.plugins_manager.plugins", plugins or []))

        exit_stack.enter_context(
            mock.patch(
                "airflow.plugins_manager.load_plugins_from_plugin_directory", side_effect=mock_loaded_plugins
            )
        )

        if airflow_version <= "3":
            ATTR_TO_PATCH = PLUGINS_MANAGER_NULLABLE_ATTRIBUTES_V2_10
        else:
            ATTR_TO_PATCH = PLUGINS_MANAGER_NULLABLE_ATTRIBUTES

        for attr in ATTR_TO_PATCH:
            exit_stack.enter_context(mock.patch(f"airflow.plugins_manager.{attr}", kwargs.get(attr)))

        # Always start the block with an empty plugins, so ensure_plugins_loaded runs.
        exit_stack.enter_context(mock.patch("airflow.plugins_manager.plugins", None))
        exit_stack.enter_context(
            mock.patch("airflow.plugins_manager.import_errors", kwargs.get("import_errors", {}))
        )

        yield