2022-07-07 08:28:34 -07:00
|
|
|
import subprocess
|
|
|
|
|
2021-04-01 12:15:05 -07:00
|
|
|
import pytest
|
2021-07-29 20:04:40 -07:00
|
|
|
from freezegun import freeze_time
|
2021-02-11 12:53:44 -08:00
|
|
|
|
2021-12-16 23:07:38 -05:00
|
|
|
from tests.test_helpers import mce_helpers
|
|
|
|
from tests.test_helpers.click_helpers import run_datahub_cmd
|
2021-04-13 17:30:24 -07:00
|
|
|
from tests.test_helpers.docker_helpers import wait_for_port
|
2021-02-09 01:02:05 -08:00
|
|
|
|
2021-07-29 20:04:40 -07:00
|
|
|
FROZEN_TIME = "2020-04-14 07:00:00"
|
2022-07-07 08:28:34 -07:00
|
|
|
MYSQL_PORT = 3306
|
2021-02-11 12:24:20 -08:00
|
|
|
|
2021-07-29 20:04:40 -07:00
|
|
|
|
2022-07-07 08:28:34 -07:00
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def test_resources_dir(pytestconfig):
|
|
|
|
return pytestconfig.rootpath / "tests/integration/mysql"
|
|
|
|
|
|
|
|
|
|
|
|
def is_mysql_up(container_name: str, port: int) -> bool:
|
|
|
|
"""A cheap way to figure out if mysql is responsive on a container"""
|
|
|
|
|
|
|
|
cmd = f"docker logs {container_name} 2>&1 | grep '/usr/sbin/mysqld: ready for connections.' | grep {port}"
|
|
|
|
ret = subprocess.run(
|
|
|
|
cmd,
|
|
|
|
shell=True,
|
|
|
|
)
|
|
|
|
return ret.returncode == 0
|
2021-02-09 01:02:05 -08:00
|
|
|
|
2022-07-07 08:28:34 -07:00
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def mysql_runner(docker_compose_runner, pytestconfig, test_resources_dir):
|
2021-04-13 17:30:24 -07:00
|
|
|
with docker_compose_runner(
|
|
|
|
test_resources_dir / "docker-compose.yml", "mysql"
|
|
|
|
) as docker_services:
|
2022-07-07 08:28:34 -07:00
|
|
|
wait_for_port(
|
|
|
|
docker_services,
|
|
|
|
"testmysql",
|
|
|
|
MYSQL_PORT,
|
|
|
|
timeout=120,
|
|
|
|
checker=lambda: is_mysql_up("testmysql", MYSQL_PORT),
|
2021-12-16 23:07:38 -05:00
|
|
|
)
|
2022-07-07 08:28:34 -07:00
|
|
|
yield docker_services
|
2022-03-09 12:29:58 -05:00
|
|
|
|
|
|
|
|
2023-07-07 19:05:50 -04:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"config_file,golden_file",
|
|
|
|
[
|
|
|
|
("mysql_to_file_with_db.yml", "mysql_mces_with_db_golden.json"),
|
|
|
|
("mysql_to_file_no_db.yml", "mysql_mces_no_db_golden.json"),
|
|
|
|
("mysql_profile_table_level_only.yml", "mysql_table_level_only.json"),
|
|
|
|
],
|
|
|
|
)
|
2022-03-09 12:29:58 -05:00
|
|
|
@freeze_time(FROZEN_TIME)
|
|
|
|
@pytest.mark.integration
|
2022-08-26 23:17:49 +05:30
|
|
|
def test_mysql_ingest_no_db(
|
2023-07-07 19:05:50 -04:00
|
|
|
mysql_runner,
|
|
|
|
pytestconfig,
|
|
|
|
test_resources_dir,
|
|
|
|
tmp_path,
|
|
|
|
mock_time,
|
|
|
|
config_file,
|
|
|
|
golden_file,
|
2022-08-26 23:17:49 +05:30
|
|
|
):
|
|
|
|
# Run the metadata ingestion pipeline.
|
2023-07-07 19:05:50 -04:00
|
|
|
config_file = (test_resources_dir / config_file).resolve()
|
2023-03-13 13:32:40 -07:00
|
|
|
run_datahub_cmd(["ingest", "-c", f"{config_file}"], tmp_path=tmp_path)
|
2022-08-26 23:17:49 +05:30
|
|
|
|
|
|
|
# Verify the output.
|
|
|
|
mce_helpers.check_golden_file(
|
|
|
|
pytestconfig,
|
|
|
|
output_path=tmp_path / "mysql_mces.json",
|
2023-07-07 19:05:50 -04:00
|
|
|
golden_path=test_resources_dir / golden_file,
|
2022-07-07 08:28:34 -07:00
|
|
|
)
|
2022-03-09 12:29:58 -05:00
|
|
|
|
|
|
|
|
2022-07-07 08:28:34 -07:00
|
|
|
@freeze_time(FROZEN_TIME)
|
|
|
|
@pytest.mark.integration
|
|
|
|
def test_mysql_ingest_with_db_alias(
|
|
|
|
mysql_runner, pytestconfig, test_resources_dir, tmp_path, mock_time
|
|
|
|
):
|
|
|
|
# Run the metadata ingestion pipeline.
|
|
|
|
config_file = (test_resources_dir / "mysql_to_file_dbalias.yml").resolve()
|
2023-03-13 13:32:40 -07:00
|
|
|
run_datahub_cmd(["ingest", "-c", f"{config_file}"], tmp_path=tmp_path)
|
2022-03-09 12:29:58 -05:00
|
|
|
|
2022-07-07 08:28:34 -07:00
|
|
|
# Verify the output.
|
|
|
|
# Assert that all events generated have instance specific urns
|
|
|
|
import re
|
|
|
|
|
|
|
|
urn_pattern = "^" + re.escape(
|
|
|
|
"urn:li:dataset:(urn:li:dataPlatform:mysql,foogalaxy."
|
|
|
|
)
|
|
|
|
mce_helpers.assert_mcp_entity_urn(
|
|
|
|
filter="ALL",
|
|
|
|
entity_type="dataset",
|
|
|
|
regex_pattern=urn_pattern,
|
|
|
|
file=tmp_path / "mysql_mces_dbalias.json",
|
|
|
|
)
|