2021-08-02 15:08:30 +05:30
|
|
|
# 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.
|
|
|
|
|
2021-08-01 14:27:44 -07:00
|
|
|
import time
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
import requests
|
|
|
|
from requests.exceptions import ConnectionError
|
|
|
|
from sqlalchemy.engine import create_engine
|
|
|
|
from sqlalchemy.inspection import inspect
|
|
|
|
|
2021-10-26 23:18:43 +02:00
|
|
|
from metadata.generated.schema.api.data.createDatabase import (
|
|
|
|
CreateDatabaseEntityRequest,
|
|
|
|
)
|
|
|
|
from metadata.generated.schema.api.data.createTable import CreateTableEntityRequest
|
|
|
|
from metadata.generated.schema.api.services.createDatabaseService import (
|
|
|
|
CreateDatabaseServiceEntityRequest,
|
|
|
|
)
|
2021-11-03 21:02:34 +01:00
|
|
|
from metadata.generated.schema.entity.data.database import Database
|
2021-10-26 23:18:43 +02:00
|
|
|
from metadata.generated.schema.entity.data.table import Column
|
2021-11-03 21:02:34 +01:00
|
|
|
from metadata.generated.schema.entity.services.databaseService import DatabaseService
|
2021-10-26 23:18:43 +02:00
|
|
|
from metadata.generated.schema.type.entityReference import EntityReference
|
2021-11-03 21:02:34 +01:00
|
|
|
from metadata.ingestion.ometa.ometa_api import OpenMetadata
|
|
|
|
from metadata.ingestion.ometa.openmetadata_rest import MetadataServerConfig
|
2021-10-23 19:58:26 -07:00
|
|
|
|
2021-08-01 14:27:44 -07:00
|
|
|
|
|
|
|
def is_responsive(url):
|
|
|
|
try:
|
|
|
|
response = requests.get(url)
|
|
|
|
if response.status_code == 200:
|
|
|
|
return True
|
|
|
|
except ConnectionError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2021-11-03 21:02:34 +01:00
|
|
|
def create_delete_table(client: OpenMetadata):
|
|
|
|
databases = client.list_entities(entity=Database).entities
|
2021-10-26 23:18:43 +02:00
|
|
|
columns = [
|
|
|
|
Column(name="id", dataType="INT", dataLength=1),
|
|
|
|
Column(name="name", dataType="VARCHAR", dataLength=1),
|
|
|
|
]
|
2021-08-01 14:27:44 -07:00
|
|
|
table = CreateTableEntityRequest(
|
2021-10-26 23:18:43 +02:00
|
|
|
name="test1", columns=columns, database=databases[0].id
|
|
|
|
)
|
2021-11-03 21:02:34 +01:00
|
|
|
created_table = client.create_or_update(table)
|
2021-10-26 23:18:43 +02:00
|
|
|
if table.name.__root__ == created_table.name.__root__:
|
2021-08-01 14:27:44 -07:00
|
|
|
requests.delete(
|
2021-10-26 23:18:43 +02:00
|
|
|
"http://localhost:8585/api/v1/tables/{}".format(created_table.id.__root__)
|
|
|
|
)
|
2021-08-01 14:27:44 -07:00
|
|
|
return 1
|
|
|
|
else:
|
|
|
|
requests.delete(
|
2021-10-26 23:18:43 +02:00
|
|
|
"http://localhost:8585/api/v1/tables/{}".format(created_table.id.__root__)
|
|
|
|
)
|
2021-08-01 14:27:44 -07:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2021-11-03 21:02:34 +01:00
|
|
|
def create_delete_database(client: OpenMetadata):
|
2021-10-26 23:18:43 +02:00
|
|
|
data = {
|
|
|
|
"jdbc": {
|
|
|
|
"connectionUrl": "mysql://localhost/catalog_db",
|
|
|
|
"driverClass": "jdbc",
|
|
|
|
},
|
|
|
|
"name": "temp_local_mysql",
|
|
|
|
"serviceType": "MySQL",
|
|
|
|
"description": "local mysql env",
|
|
|
|
}
|
2021-08-01 14:27:44 -07:00
|
|
|
create_mysql_service = CreateDatabaseServiceEntityRequest(**data)
|
2021-11-03 21:02:34 +01:00
|
|
|
mysql_service = client.create_or_update(create_mysql_service)
|
2021-08-01 14:27:44 -07:00
|
|
|
create_database_request = CreateDatabaseEntityRequest(
|
2021-10-26 23:18:43 +02:00
|
|
|
name="dwh", service=EntityReference(id=mysql_service.id, type="databaseService")
|
|
|
|
)
|
2021-11-03 21:02:34 +01:00
|
|
|
created_database = client.create_or_update(create_database_request)
|
2021-08-01 14:27:44 -07:00
|
|
|
resp = create_delete_table(client)
|
|
|
|
print(resp)
|
2021-11-03 21:02:34 +01:00
|
|
|
client.delete(entity=Database, entity_id=str(created_database.id.__root__))
|
|
|
|
client.delete(entity=DatabaseService, entity_id=str(mysql_service.id.__root__))
|
2021-08-01 14:27:44 -07:00
|
|
|
return resp
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def catalog_service(docker_ip, docker_services):
|
|
|
|
"""Ensure that Docker service is up and responsive."""
|
|
|
|
port = docker_services.port_for("db", 3306)
|
|
|
|
print("Mysql is running on port {}".format(port))
|
|
|
|
url = "http://localhost:8585"
|
2021-10-23 19:58:26 -07:00
|
|
|
time.sleep(30)
|
2021-08-01 14:27:44 -07:00
|
|
|
docker_services.wait_until_responsive(
|
2021-10-23 19:58:26 -07:00
|
|
|
timeout=30.0, pause=0.5, check=lambda: is_responsive(url)
|
2021-08-01 14:27:44 -07:00
|
|
|
)
|
|
|
|
return url
|
|
|
|
|
|
|
|
|
|
|
|
def test_check_tables(catalog_service):
|
2021-10-23 19:58:26 -07:00
|
|
|
metadata_config = MetadataServerConfig.parse_obj(
|
2021-10-26 23:18:43 +02:00
|
|
|
{"api_endpoint": catalog_service + "/api", "auth_provider_type": "no-auth"}
|
2021-10-23 19:58:26 -07:00
|
|
|
)
|
2021-11-03 21:02:34 +01:00
|
|
|
client = OpenMetadata(metadata_config)
|
2021-10-23 19:58:26 -07:00
|
|
|
assert create_delete_database(client)
|
2021-08-01 14:27:44 -07:00
|
|
|
|
2021-10-26 23:18:43 +02:00
|
|
|
|
2021-08-01 14:27:44 -07:00
|
|
|
def test_read_schema():
|
|
|
|
url = "mysql+pymysql://catalog_user:catalog_password@localhost:3307"
|
2021-10-23 19:58:26 -07:00
|
|
|
# pool_recycle to avoid the occasional "Lost connection to MySQL server during query" error
|
|
|
|
# when host machine is slow
|
|
|
|
engine = create_engine(url, pool_recycle=1)
|
2021-08-01 14:27:44 -07:00
|
|
|
inspector = inspect(engine)
|
|
|
|
schemas = []
|
|
|
|
for schema in inspector.get_schema_names():
|
|
|
|
schemas.append(schema)
|
|
|
|
if "catalog_test" in schemas:
|
|
|
|
assert 1
|
|
|
|
else:
|
|
|
|
assert 0
|