Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow running airflow against sqlite in-memory DB for tests #37144

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions airflow/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,6 @@ def configure_vars():
global PLUGINS_FOLDER
global DONOT_MODIFY_HANDLERS
SQL_ALCHEMY_CONN = conf.get("database", "SQL_ALCHEMY_CONN")
if SQL_ALCHEMY_CONN.startswith("sqlite") and not SQL_ALCHEMY_CONN.startswith("sqlite:////"):
from airflow.exceptions import AirflowConfigException

raise AirflowConfigException(
f"Cannot use relative path: `{SQL_ALCHEMY_CONN}` to connect to sqlite. "
"Please use absolute path such as `sqlite:////tmp/airflow.db`."
)

DAGS_FOLDER = os.path.expanduser(conf.get("core", "DAGS_FOLDER"))

Expand Down Expand Up @@ -231,6 +224,20 @@ def configure_orm(disable_connection_pool=False, pool_class=None):
"""Configure ORM using SQLAlchemy."""
from airflow.utils.log.secrets_masker import mask_secret

if (
SQL_ALCHEMY_CONN
and SQL_ALCHEMY_CONN.startswith("sqlite")
and not SQL_ALCHEMY_CONN.startswith("sqlite:////")
# In memory is not useful for production, but useful for writing tests against Airflow for extensions
and SQL_ALCHEMY_CONN != "sqlite://"
):
from airflow.exceptions import AirflowConfigException

raise AirflowConfigException(
f"Cannot use relative path: `{SQL_ALCHEMY_CONN}` to connect to sqlite. "
"Please use absolute path such as `sqlite:////tmp/airflow.db`."
)

global Session
global engine
if os.environ.get("_AIRFLOW_SKIP_DB_TESTS") == "true":
Expand Down
24 changes: 17 additions & 7 deletions tests/core/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
# under the License.
from __future__ import annotations

import contextlib
import os
import sys
import tempfile
from unittest import mock
from unittest.mock import MagicMock, call, patch
from unittest.mock import MagicMock, call

import pytest

Expand Down Expand Up @@ -216,11 +217,20 @@ def test_uses_updated_session_timeout_config_by_default(self):
assert session_lifetime_config == default_timeout_minutes


def test_sqlite_relative_path():
@pytest.mark.parametrize(
["value", "expectation"],
[
(
"sqlite:///./relative_path.db",
pytest.raises(AirflowConfigException, match=r"Cannot use relative path:"),
),
# Should not raise an exception
("sqlite://", contextlib.nullcontext()),
],
)
def test_sqlite_relative_path(monkeypatch, value, expectation):
from airflow import settings

with patch("airflow.settings.conf.get") as conf_get_mock:
conf_get_mock.return_value = "sqlite:///./relative_path.db"
with pytest.raises(AirflowConfigException) as exc:
settings.configure_vars()
assert "Cannot use relative path:" in str(exc.value)
monkeypatch.setattr(settings, "SQL_ALCHEMY_CONN", value)
with expectation:
settings.configure_orm()