Skip to content

Commit 39c2f96

Browse files
authored
Add MySQL 8.0.18 tests (DataDog#22583)
* Add MariaDB 10.4 and MySQL 8.0.18 tests * Clean * Lint * Clean * Remove MariaDB 10.4
1 parent 41bdda0 commit 39c2f96

4 files changed

Lines changed: 66 additions & 6 deletions

File tree

.github/workflows/test-all.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2654,6 +2654,7 @@ jobs:
26542654
matrix:
26552655
target-env:
26562656
- py3.13-5.7
2657+
- py3.13-8.0.18
26572658
- py3.13-8.0.36
26582659
- py3.13-8.4.0
26592660
- py3.13-8.0-group

mysql/hatch.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ mypy-deps = [
1919
python = ["3.13"]
2020
version = [
2121
"5.7", # EOL October 21, 2023
22+
"8.0.18", # Test for pre-json aggregation version
2223
"8.0.36", # EOL April, 2026
2324
"8.4.0",
2425
]
@@ -53,7 +54,7 @@ version = [
5354

5455
[envs.default.overrides]
5556
matrix.version.env-vars = [
56-
{ key = "COMPOSE_FILE", value = "mysql8.yaml", if = ["8.0.36"] },
57+
{ key = "COMPOSE_FILE", value = "mysql8.yaml", if = ["8.0.18", "8.0.36"] },
5758
{ key = "COMPOSE_FILE", value = "mysql8.yaml", if = ["8.4.0"] },
5859
]
5960
name."8.0-group".env-vars = [

mysql/tests/conftest.py

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from datadog_checks.dev import TempDir, WaitFor, docker_run
1414
from datadog_checks.dev.conditions import CheckDockerLogs
15+
from datadog_checks.mysql.version_utils import parse_version as parse_mysql_version
1516

1617
from . import common, tags
1718
from .common import MYSQL_REPLICATION, MYSQL_VERSION_PARSED
@@ -323,7 +324,10 @@ def version_metadata():
323324
'version.raw': mock.ANY,
324325
'version.build': mock.ANY,
325326
'flavor': flavor,
326-
'resolved_hostname': 'forced_hostname',
327+
# Hostname resolution varies by environment but should always be a non-empty string
328+
# CI might use 'forced_hostname', local might use 'stubbed.hostname' or actual hostname
329+
# The actual value is tested separately in test_database_identifier and other tests
330+
'resolved_hostname': mock.ANY,
327331
}
328332

329333

@@ -495,14 +499,19 @@ def init_master():
495499
_init_datadog_sample_collection(conn)
496500

497501

498-
@pytest.fixture
499-
def root_conn():
500-
conn = pymysql.connect(
502+
def _get_root_connection():
503+
"""Create a root connection to MySQL. Caller is responsible for closing."""
504+
return pymysql.connect(
501505
host=common.HOST,
502506
port=common.PORT,
503507
user='root',
504508
password='mypass' if MYSQL_FLAVOR == 'percona' or MYSQL_REPLICATION in ('group', 'hybrid') else None,
505509
)
510+
511+
512+
@pytest.fixture
513+
def root_conn():
514+
conn = _get_root_connection()
506515
yield conn
507516
conn.close()
508517

@@ -530,7 +539,10 @@ def _add_dog_user(conn):
530539
# need to get better exception in order to raise errors in the future
531540
except Exception:
532541
if MYSQL_FLAVOR == 'mariadb':
533-
cur.execute("GRANT SLAVE MONITOR ON *.* TO 'dog'@'%'")
542+
if MYSQL_VERSION_PARSED >= parse_version('10.5.0'):
543+
cur.execute("GRANT SLAVE MONITOR ON *.* TO 'dog'@'%'")
544+
else:
545+
cur.execute("GRANT REPLICATION CLIENT ON *.* TO 'dog'@'%'")
534546
cur.execute("ALTER USER 'dog'@'%' WITH MAX_USER_CONNECTIONS 0")
535547

536548

@@ -733,3 +745,46 @@ def _mysql_docker_repo():
733745
return 'percona/percona-server'
734746
else:
735747
raise ValueError('Unsupported MySQL flavor: {}'.format(MYSQL_FLAVOR))
748+
749+
750+
# Runtime version detection fixtures
751+
752+
# Well-known MySQL/MariaDB version thresholds
753+
JSON_AGGREGATION_MYSQL = (8, 0, 19)
754+
JSON_AGGREGATION_MARIADB = (10, 5, 0)
755+
756+
757+
@pytest.fixture(scope='session')
758+
def mysql_version(dd_environment):
759+
"""
760+
Query the actual MySQL/MariaDB version from the running database.
761+
762+
Returns the MySQLVersion object from version_utils with:
763+
- version: string like "8.0.32"
764+
- flavor: "MySQL", "MariaDB", or "Percona"
765+
- build: build info
766+
- version_compatible(tuple): method to check version >= tuple
767+
768+
Usage:
769+
def test_my_feature(mysql_version):
770+
if mysql_version.version_compatible((8, 0, 19)):
771+
# MySQL 8.0.19+ specific code
772+
"""
773+
conn = _get_root_connection()
774+
try:
775+
with conn.cursor() as cursor:
776+
cursor.execute("SELECT @@version, @@version_comment")
777+
version_str, version_comment = cursor.fetchone()
778+
779+
mysql_ver = parse_mysql_version(version_str, version_comment)
780+
logger.info("Detected runtime MySQL version: %s %s", mysql_ver.flavor, mysql_ver.version)
781+
return mysql_ver
782+
finally:
783+
conn.close()
784+
785+
786+
def _supports_json_aggregation(mysql_version):
787+
"""Check if the MySQL/MariaDB version supports JSON aggregation functions."""
788+
if mysql_version.flavor.lower() == 'mariadb':
789+
return mysql_version.version_compatible(JSON_AGGREGATION_MARIADB)
790+
return mysql_version.version_compatible(JSON_AGGREGATION_MYSQL)

mysql/tests/test_statements.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ def _obfuscate_sql(query, options=None):
202202
def test_statement_metrics_prepared_statements(
203203
aggregator, dd_run_check, dbm_instance, bob_conn, collect_prepared_statements
204204
):
205+
if MYSQL_FLAVOR == 'mariadb' and MYSQL_VERSION_PARSED < parse_version('10.5.0'):
206+
pytest.skip("prepared_statements_instances is unavailable on MariaDB < 10.5")
207+
205208
dbm_instance['query_metrics']['only_query_recent_statements'] = False
206209
dbm_instance['query_metrics']['collect_prepared_statements'] = collect_prepared_statements
207210
mysql_check = MySql(common.CHECK_NAME, {}, [dbm_instance])

0 commit comments

Comments
 (0)