bf2343b7e4
Integration Tests - MySQL + Elasticsearch / Detect Changes (push) Has been cancelled
Integration Tests - MySQL + Elasticsearch / integration-tests-mysql-elasticsearch (push) Has been cancelled
Integration Tests - PostgreSQL + Elasticsearch + Redis / Detect Changes (push) Has been cancelled
Integration Tests - PostgreSQL + Elasticsearch + Redis / integration-tests-postgres-elasticsearch-redis (push) Has been cancelled
Integration Tests - PostgreSQL + OpenSearch / Detect Changes (push) Has been cancelled
Integration Tests - PostgreSQL + OpenSearch / integration-tests-postgres-opensearch (push) Has been cancelled
Java Checkstyle / java-checkstyle (push) Has been cancelled
Maven Collate Tests / maven-collate-ci (push) Has been cancelled
OpenMetadata Service Unit Tests / openmetadata-service-unit-tests-status (push) Has been cancelled
Publish Package to Maven Central Repository / publish-maven-packages (push) Has been cancelled
OpenMetadata Service Unit Tests / Detect Changes (push) Has been cancelled
OpenMetadata Service Unit Tests / openmetadata-service-unit-tests (push) Has been cancelled
OpenMetadata Service Unit Tests / k8s_operator-unit-tests (push) Has been cancelled
84 lines
3.5 KiB
Python
84 lines
3.5 KiB
Python
import os
|
|
import uuid
|
|
from subprocess import CalledProcessError
|
|
|
|
import pytest
|
|
from sqlalchemy import create_engine, text
|
|
from testcontainers.mysql import MySqlContainer
|
|
|
|
from _openmetadata_testutils.helpers.docker import try_bind
|
|
from metadata.generated.schema.api.services.createDatabaseService import (
|
|
CreateDatabaseServiceRequest,
|
|
)
|
|
from metadata.generated.schema.entity.services.databaseService import (
|
|
DatabaseServiceType,
|
|
)
|
|
|
|
|
|
@pytest.fixture(scope="package")
|
|
def mysql_container(tmp_path_factory):
|
|
"""Start a PostgreSQL container with the dvdrental database."""
|
|
test_db_tar_path = os.path.join(os.path.dirname(__file__), "data", "mysql", "test_db-1.0.7.tar.gz") # noqa: PTH118, PTH120
|
|
container = MySqlContainer(image="mysql:8.4.5", dbname="employees")
|
|
with try_bind(container, 3306, 3307) if not os.getenv("CI") else container as container:
|
|
docker_container = container.get_wrapped_container()
|
|
docker_container.exec_run(["mkdir", "-p", "/data"])
|
|
docker_container.put_archive("/data", open(test_db_tar_path, "rb")) # noqa: PTH123, SIM115
|
|
for command in (
|
|
[
|
|
"sh",
|
|
"-c",
|
|
f"cd /data/test_db && mysql -uroot -p{container.password} < employees.sql",
|
|
],
|
|
[
|
|
"sh",
|
|
"-c",
|
|
f'mysql -uroot -p{container.password} -e \'GRANT SELECT ON employees.* TO "test"@"%";\'',
|
|
],
|
|
):
|
|
res = docker_container.exec_run(command)
|
|
if res[0] != 0:
|
|
raise CalledProcessError(returncode=res[0], cmd=res, output=res[1].decode("utf-8"))
|
|
engine = create_engine(container.get_connection_url())
|
|
with engine.connect() as conn:
|
|
conn.execute(text("ALTER TABLE employees ADD COLUMN last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP"))
|
|
conn.execute(
|
|
text("UPDATE employees SET last_update = hire_date + INTERVAL FLOOR(1 + RAND() * 500000) SECOND")
|
|
)
|
|
conn.commit()
|
|
engine.dispose()
|
|
assert_dangling_connections(container, 1)
|
|
yield container
|
|
# Needs to be handled for Test Cases https://github.com/open-metadata/OpenMetadata/issues/21187
|
|
assert_dangling_connections(container, 9)
|
|
|
|
|
|
def assert_dangling_connections(container, max_connections):
|
|
engine = create_engine(container.get_connection_url())
|
|
with engine.connect() as conn:
|
|
result = conn.execute(text("SHOW PROCESSLIST"))
|
|
processes = result.fetchall()
|
|
# Count all connections except system processes (Daemon, Binlog Dump)
|
|
# Note: We include Sleep connections as they are still open connections
|
|
active_connections = len([p for p in processes if p[1] not in ["Daemon", "Binlog Dump"]])
|
|
|
|
assert active_connections <= max_connections, f"Found {active_connections} open connections to MySQL"
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def create_service_request(mysql_container):
|
|
return CreateDatabaseServiceRequest.model_validate(
|
|
{
|
|
"name": f"docker_test_mysql_{uuid.uuid4().hex[:8]}",
|
|
"serviceType": DatabaseServiceType.Mysql.value,
|
|
"connection": {
|
|
"config": {
|
|
"username": mysql_container.username,
|
|
"authType": {"password": mysql_container.password},
|
|
"hostPort": f"localhost:{mysql_container.get_exposed_port(mysql_container.port)}",
|
|
"databaseSchema": mysql_container.dbname,
|
|
}
|
|
},
|
|
}
|
|
)
|