Skip to content

Commit

Permalink
fixed mssql tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sushi30 committed Sep 19, 2024
1 parent 85a186c commit 604834d
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 36 deletions.
2 changes: 1 addition & 1 deletion ingestion/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@
VERSIONS["grpc-tools"],
VERSIONS["neo4j"],
"testcontainers==3.7.1;python_version<'3.9'",
"testcontainers==4.8.0;python_version>='3.9'",
"testcontainers==4.8.1;python_version>='3.9'",
"minio==7.2.5",
*plugins["mlflow"],
*plugins["datalake-s3"],
Expand Down
95 changes: 68 additions & 27 deletions ingestion/tests/integration/sql_server/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import os
import shutil
import tempfile

import pytest
from sqlalchemy import create_engine, text
from testcontainers.mssql import SqlServerContainer

from _openmetadata_testutils.helpers.docker import copy_dir_to_container, try_bind
from metadata.generated.schema.api.services.createDatabaseService import (
CreateDatabaseServiceRequest,
Expand All @@ -18,28 +16,61 @@
DatabaseService,
DatabaseServiceType,
)
from sqlalchemy import create_engine, text
from testcontainers.mssql import SqlServerContainer

from ..conftest import ingestion_config as base_ingestion_config


@pytest.fixture(scope="session")
def mssql_container(tmp_path_factory):
def db_name():
return "AdventureWorksLT2022"


class CustomSqlServerConainer(SqlServerContainer):
def start(self) -> "DbContainer":
dockerfile = f"""
FROM {self.image}
USER root
RUN mkdir -p /data
RUN chown mssql /data
USER mssql
"""
temp_dir = os.path.join(tempfile.gettempdir(), "mssql")
os.makedirs(temp_dir, exist_ok=True)
temp_dockerfile_path = os.path.join(temp_dir, "Dockerfile")
with open(temp_dockerfile_path, "w") as temp_dockerfile:
temp_dockerfile.write(dockerfile)
self.get_docker_client().build(temp_dir, tag=self.image)
return super().start()

def _configure(self) -> None:
super()._configure()
self.with_env("SQL_SA_PASSWORD", self.password)


@pytest.fixture(scope="session")
def mssql_container(tmp_path_factory, db_name):
container = SqlServerContainer(
"mcr.microsoft.com/mssql/server:2022-latest", dbname="AdventureWorks"
"mcr.microsoft.com/mssql/server:2022-latest", dbname="master"
)
data_dir = tmp_path_factory.mktemp("data")
shutil.copy(
os.path.join(os.path.dirname(__file__), "data", "AdventureWorks2017.bak"),
os.path.join(os.path.dirname(__file__), "data", f"{db_name}.bak"),
str(data_dir),
)
with open(data_dir / "install.sql", "w") as f:
f.write(
"""
f"""
USE [master]
RESTORE DATABASE [AdventureWorks]
FROM DISK = '/data/AdventureWorks2017.bak'
WITH MOVE 'AdventureWorks2017' TO '/var/opt/mssql/data/AdventureWorks.mdf',
MOVE 'AdventureWorks2017_log' TO '/var/opt/mssql/data/AdventureWorks_log.ldf'
RESTORE FILELISTONLY
FROM DISK = '/data/{db_name}.bak';
GO
RESTORE DATABASE [{db_name}]
FROM DISK = '/data/{db_name}.bak'
WITH MOVE '{db_name}_Data' TO '/var/opt/mssql/data/{db_name}.mdf',
MOVE '{db_name}_Log' TO '/var/opt/mssql/data/{db_name}.ldf';
GO
"""
)
Expand All @@ -49,17 +80,22 @@ def mssql_container(tmp_path_factory):
copy_dir_to_container(str(data_dir), docker_container, "/data")
res = docker_container.exec_run(
[
"/opt/mssql-tools/bin/sqlcmd",
"-S",
"localhost",
"-U",
container.username,
"-P",
container.password,
"-d",
"master",
"-i",
"/data/install.sql",
"bash",
"-c",
" ".join(
[
"/opt/mssql-tools*/bin/sqlcmd",
"-U",
container.username,
"-P",
f"'{container.password}'",
"-d",
"master",
"-i",
"/data/install.sql",
"-C",
]
),
]
)
if res[0] != 0:
Expand All @@ -72,7 +108,7 @@ def mssql_container(tmp_path_factory):
transaciton = conn.begin()
conn.execute(
text(
"SELECT * INTO AdventureWorks.HumanResources.DepartmenCopy FROM AdventureWorks.HumanResources.Department;"
f"SELECT * INTO {db_name}.SalesLT.CustomerCopy FROM {db_name}.SalesLT.Customer;"
)
)
transaciton.commit()
Expand All @@ -91,7 +127,7 @@ def scheme(request):


@pytest.fixture(scope="module")
def create_service_request(mssql_container, scheme, tmp_path_factory):
def create_service_request(mssql_container, scheme, tmp_path_factory, db_name):
return CreateDatabaseServiceRequest(
name="docker_test_" + tmp_path_factory.mktemp("mssql").name + "_" + scheme.name,
serviceType=DatabaseServiceType.Mssql,
Expand All @@ -101,7 +137,7 @@ def create_service_request(mssql_container, scheme, tmp_path_factory):
password=mssql_container.password,
hostPort="localhost:"
+ mssql_container.get_exposed_port(mssql_container.port),
database="AdventureWorks",
database=db_name,
scheme=scheme,
ingestAllDatabases=True,
connectionOptions={
Expand All @@ -115,12 +151,17 @@ def create_service_request(mssql_container, scheme, tmp_path_factory):

@pytest.fixture(scope="module")
def ingestion_config(
db_service, tmp_path_factory, workflow_config, sink_config, base_ingestion_config
db_service,
tmp_path_factory,
workflow_config,
sink_config,
base_ingestion_config,
db_name,
):
base_ingestion_config["source"]["sourceConfig"]["config"][
"databaseFilterPattern"
] = {
"includes": ["TestDB", "AdventureWorks"],
"includes": ["TestDB", db_name],
}
return base_ingestion_config

Expand Down
Binary file not shown.
Binary file not shown.
8 changes: 4 additions & 4 deletions ingestion/tests/integration/sql_server/test_lineage.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ def language_config(mssql_container, request):


@pytest.fixture()
def lineage_config(language_config, db_service, workflow_config, sink_config):
def lineage_config(language_config, db_service, workflow_config, sink_config, db_name):
return {
"source": {
"type": "mssql-lineage",
"serviceName": db_service.fullyQualifiedName.root,
"sourceConfig": {
"config": {
"type": "DatabaseLineage",
"databaseFilterPattern": {"includes": ["TestDB", "AdventureWorks"]},
"databaseFilterPattern": {"includes": ["TestDB", db_name]},
},
},
},
Expand All @@ -52,13 +52,13 @@ def test_lineage(
lineage_config,
db_service,
metadata,
):
db_name):
search_cache.clear()
run_workflow(MetadataWorkflow, ingestion_config)
run_workflow(MetadataWorkflow, lineage_config)
department_table = metadata.get_by_name(
Table,
f"{db_service.fullyQualifiedName.root}.AdventureWorks.HumanResources.Department",
f"{db_service.fullyQualifiedName.root}.{db_name}.SalesLT.Customer",
nullable=False,
)
lineage = metadata.get_lineage_by_id(Table, department_table.id.root)
Expand Down
4 changes: 2 additions & 2 deletions ingestion/tests/integration/sql_server/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ def test_ingest_metadata(
ingestion_config,
db_service,
metadata,
):
db_name):
run_workflow(MetadataWorkflow, ingestion_config)
table: Table = metadata.get_by_name(
Table,
f"{db_service.fullyQualifiedName.root}.AdventureWorks.HumanResources.Department",
f"{db_service.fullyQualifiedName.root}.{db_name}.SalesLT.Customer",
)
assert table is not None
assert table.columns[0].name.root == "DepartmentID"
Expand Down
4 changes: 2 additions & 2 deletions ingestion/tests/integration/sql_server/test_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


@pytest.fixture()
def usage_config(db_service, workflow_config):
def usage_config(db_service, workflow_config, db_name):
return {
"source": {
"type": "mssql-usage",
Expand All @@ -19,7 +19,7 @@ def usage_config(db_service, workflow_config):
"config": {
"queryLogDuration": 2,
"resultLimit": 1000,
"databaseFilterPattern": {"includes": ["TestDB", "AdventureWorks"]},
"databaseFilterPattern": {"includes": ["TestDB", db_name]},
},
},
},
Expand Down

0 comments on commit 604834d

Please sign in to comment.