Skip to content

Commit

Permalink
Avoid side-effects for providers-manager related tests (#37414)
Browse files Browse the repository at this point in the history
When running tests in v2-8-tests some of the tests failed with
package-name key not found in providers manager. This was because
some other tests modified dictionaries stored in ProvidersManager
and added there entries that missed the keys.

This PR adds a a new fixture `cleanup_providers_manager` - that will
reinitialize the ProvidersManager before the test and - more importantly
will clean it up after. This way all the entries added manually should
be removed for other tests and all the tests that do not explicitly
initialize providers manager should fail if they need it.

(cherry picked from commit 7461ac7)
  • Loading branch information
potiuk authored and ephraimbuddy committed Feb 22, 2024
1 parent 85d0a95 commit 7586d91
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
11 changes: 4 additions & 7 deletions tests/always/test_providers_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,9 @@

class TestProviderManager:
@pytest.fixture(autouse=True)
def inject_fixtures(self, caplog):
def inject_fixtures(self, caplog, cleanup_providers_manager):
self._caplog = caplog

@pytest.fixture(autouse=True, scope="function")
def clean(self):
"""The tests depend on a clean state of a ProvidersManager."""
ProvidersManager().__init__()

def test_providers_are_loaded(self):
with self._caplog.at_level(logging.WARNING):
provider_manager = ProvidersManager()
Expand Down Expand Up @@ -93,8 +88,9 @@ def test_hooks_deprecation_warnings_not_generated(self):
assert [] == [w.message for w in warning_records.list if "hook-class-names" in str(w.message)]

def test_warning_logs_generated(self):
providers_manager = ProvidersManager()
providers_manager._hooks_lazy_dict = LazyDictWithCache()
with self._caplog.at_level(logging.WARNING):
providers_manager = ProvidersManager()
providers_manager._provider_dict["apache-airflow-providers-sftp"] = ProviderInfo(
version="0.0.1",
data={
Expand Down Expand Up @@ -165,6 +161,7 @@ def test_already_registered_conn_type_in_provide(self):

def test_providers_manager_register_plugins(self):
providers_manager = ProvidersManager()
providers_manager._provider_dict = LazyDictWithCache()
providers_manager._provider_dict["apache-airflow-providers-apache-hive"] = ProviderInfo(
version="0.0.1",
data={
Expand Down
2 changes: 1 addition & 1 deletion tests/api_connexion/endpoints/test_provider_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def configured_app(minimal_app_for_api):

class TestBaseProviderEndpoint:
@pytest.fixture(autouse=True)
def setup_attrs(self, configured_app) -> None:
def setup_attrs(self, configured_app, cleanup_providers_manager) -> None:
self.app = configured_app
self.client = self.app.test_client() # type:ignore

Expand Down
2 changes: 1 addition & 1 deletion tests/cli/commands/test_info_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class TestInfoCommandMockHttpx:
("database", "sql_alchemy_conn"): "postgresql+psycopg2://postgres:airflow@postgres/airflow",
}
)
def test_show_info_anonymize_fileio(self, setup_parser):
def test_show_info_anonymize_fileio(self, setup_parser, cleanup_providers_manager):
with mock.patch("airflow.cli.commands.info_command.httpx.post") as post:
post.return_value = httpx.Response(
status_code=200,
Expand Down
12 changes: 12 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,18 @@ def close_all_sqlalchemy_sessions():
close_all_sessions()


@pytest.fixture()
def cleanup_providers_manager():
from airflow.providers_manager import ProvidersManager

ProvidersManager()._cleanup()
ProvidersManager().initialize_providers_configuration()
try:
yield
finally:
ProvidersManager()._cleanup()


# The code below is a modified version of capture-warning code from
# https://github.com/athinkingape/pytest-capture-warnings

Expand Down

0 comments on commit 7586d91

Please sign in to comment.