Files
superset2/tests/unit_tests/db_engine_specs/test_init.py
T
rusackasandClaude Opus 4.8 67a193f1d9 fix(testcontainers): stop third-party dialect entry points from leaking global compiler state
sqlalchemy-monetdb mutates SQLAlchemy's shared, process-global
compiler.OPERATORS mapping in place on import instead of subclassing it
(OPERATORS = compiler.OPERATORS; OPERATORS[operators.ne] = " <> "). Once
Superset's own get_available_engine_specs() enumerates the "monetdb"
sqlalchemy.dialects entry point to build the "available databases" list
(which happens on every app boot, not just when MonetDB is actually
used), that changes `!=` rendering to `<>` for every dialect for the
rest of the process -- breaking test_where_operators identically across
the test-mysql/test-postgres/test-sqlite CI jobs, since installing the
monetdb extra now pulls sqlalchemy-monetdb into their shared dev
requirements.

Snapshot/restore sqlalchemy.sql.compiler.OPERATORS around each
third-party dialect entry point load so a misbehaving connector can't
leak global compiler state just because it was enumerated here.

Also fix the new mongodb testcontainers pagination test: pymongosql's
SQL-to-Mongo AST parser reads LIMIT/OFFSET off the compiled SQL text
ahead of parameter substitution, so a bound `LIMIT ?`/`OFFSET ?`
placeholder is silently dropped. Compile with literal_binds=True instead,
matching how Superset actually issues chart/SQL Lab queries.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-28 05:56:24 -07:00

217 lines
7.2 KiB
Python

# 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.
import pytest
from pytest_mock import MockerFixture
from sqlalchemy.engine.default import DefaultDialect
from superset.db_engine_specs import get_available_engine_specs
def test_get_available_engine_specs(mocker: MockerFixture) -> None:
"""
get_available_engine_specs should return all engine specs
"""
from superset.db_engine_specs.databricks import (
DatabricksHiveEngineSpec,
DatabricksNativeEngineSpec,
DatabricksODBCEngineSpec,
)
mocker.patch(
"superset.db_engine_specs.load_engine_specs",
return_value=iter(
[
DatabricksHiveEngineSpec,
DatabricksNativeEngineSpec,
DatabricksODBCEngineSpec,
]
),
)
assert list(get_available_engine_specs().keys()) == [
DatabricksHiveEngineSpec,
DatabricksNativeEngineSpec,
DatabricksODBCEngineSpec,
]
def test_get_available_engine_specs_skips_malformed_dialect_entry_point(
mocker: MockerFixture,
) -> None:
"""
A third-party ``sqlalchemy.dialects`` entry point that loads successfully but
does not resolve to a usable dialect (e.g. a module with no ``name`` or a
named class that does not implement the dialect contract) must be skipped.
Regression test: an unguarded ``dialect.name`` there aborted the whole
enumeration with ``AttributeError``, which 500s every page that builds the
bootstrap payload (e.g. ``/welcome/``), not just that one connector.
"""
import types
mocker.patch(
"superset.db_engine_specs.load_engine_specs",
return_value=iter([]),
)
malformed_ep = mocker.MagicMock()
malformed_ep.name = "bogus"
malformed_ep.value = "bogus_pkg:base"
# ``ep.load()`` returns a module (no ``name`` attribute), as a real
# ``name = pkg:submodule`` entry point would.
malformed_ep.load.return_value = types.ModuleType("bogus_pkg.base")
named_but_invalid_ep = mocker.MagicMock()
named_but_invalid_ep.name = "named_bogus"
named_but_invalid_ep.value = "bogus_pkg:NamedButInvalidDialect"
named_but_invalid_ep.load.return_value = type(
"NamedButInvalidDialect",
(),
{"name": "bogus", "driver": "bogus"},
)
def entry_points(group: str) -> list[object]:
return (
[malformed_ep, named_but_invalid_ep]
if group == "sqlalchemy.dialects"
else []
)
mocker.patch(
"superset.db_engine_specs.entry_points",
side_effect=entry_points,
)
warning = mocker.patch("superset.db_engine_specs.logger.warning")
# Must not raise (previously ``AttributeError`` on ``dialect.name``).
available = get_available_engine_specs()
assert isinstance(available, dict)
# The malformed entry point is skipped with a warning that identifies it.
assert any("bogus" in str(call) for call in warning.call_args_list)
assert any("named_bogus" in str(call) for call in warning.call_args_list)
def test_get_available_engine_specs_keeps_valid_third_party_dialect(
mocker: MockerFixture,
) -> None:
"""A valid SQLAlchemy 2.0-style dialect is included without calling dbapi()."""
import sqlalchemy.dialects
from superset.db_engine_specs.sqlite import SqliteEngineSpec
class ValidDialect(DefaultDialect):
name = "sqlite"
driver = "valid_driver"
mocker.patch.object(sqlalchemy.dialects, "__all__", [])
mocker.patch(
"superset.db_engine_specs.load_engine_specs",
return_value=iter([SqliteEngineSpec]),
)
entry_point = mocker.MagicMock()
entry_point.load.return_value = ValidDialect
mocker.patch(
"superset.db_engine_specs.entry_points",
return_value=[entry_point],
)
available = get_available_engine_specs()
assert available[SqliteEngineSpec] == {"valid_driver"}
def test_get_available_engine_specs_restores_compiler_operators(
mocker: MockerFixture,
) -> None:
"""
A third-party ``sqlalchemy.dialects`` entry point that mutates SQLAlchemy's
shared, process-global ``compiler.OPERATORS`` mapping on import (as
``sqlalchemy-monetdb`` does, in place, rather than subclassing) must not be
allowed to leak that change into every other dialect for the rest of the
process.
Regression test: enumerating a real "monetdb" entry point here (to build the
"available databases" list) silently changed ``!=`` rendering to ``<>`` for
postgres/mysql/sqlite/etc. too, for the remainder of the process.
"""
from sqlalchemy.sql import compiler as sqla_compiler, operators
mocker.patch(
"superset.db_engine_specs.load_engine_specs",
return_value=iter([]),
)
pristine = dict(sqla_compiler.OPERATORS)
assert pristine[operators.ne] != " <> "
class MisbehavingDialect(DefaultDialect):
name = "misbehaving"
driver = "misbehaving_driver"
def load_and_mutate_globally() -> type[MisbehavingDialect]:
# Mirrors sqlalchemy-monetdb's `base.py`: grabs a reference to the
# shared dict (not a copy) and mutates it in place.
sqla_compiler.OPERATORS[operators.ne] = " <> "
return MisbehavingDialect
entry_point = mocker.MagicMock()
entry_point.name = "misbehaving"
entry_point.load.side_effect = load_and_mutate_globally
mocker.patch(
"superset.db_engine_specs.entry_points",
return_value=[entry_point],
)
try:
get_available_engine_specs()
assert sqla_compiler.OPERATORS[operators.ne] == pristine[operators.ne]
finally:
sqla_compiler.OPERATORS.clear()
sqla_compiler.OPERATORS.update(pristine)
@pytest.mark.parametrize(
"app",
[{"DBS_AVAILABLE_DENYLIST": {"databricks": {"pyhive", "pyodbc"}}}],
indirect=True,
)
def test_get_available_engine_specs_with_denylist(mocker: MockerFixture) -> None:
"""
The denylist removes items from the db engine spec list
"""
from superset.db_engine_specs.databricks import (
DatabricksHiveEngineSpec,
DatabricksNativeEngineSpec,
DatabricksODBCEngineSpec,
)
mocker.patch(
"superset.db_engine_specs.load_engine_specs",
return_value=iter(
[
DatabricksHiveEngineSpec,
DatabricksNativeEngineSpec,
DatabricksODBCEngineSpec,
]
),
)
available = get_available_engine_specs()
assert list(available.keys()) == [DatabricksNativeEngineSpec]