|
| 1 | +import time |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from pyatlan.cache.role_cache import RoleCache |
| 6 | +from pyatlan.client.atlan import AtlanClient |
| 7 | +from pyatlan.model.assets import Asset, Connection, Process, SparkJob |
| 8 | +from pyatlan.model.audit import AuditSearchRequest |
| 9 | +from pyatlan.model.enums import OpenLineageEventType |
| 10 | +from pyatlan.model.open_lineage.event import OpenLineageEvent |
| 11 | +from pyatlan.model.open_lineage.job import OpenLineageJob |
| 12 | +from pyatlan.model.open_lineage.run import OpenLineageRun |
| 13 | +from tests.integration.client import TestId, delete_asset |
| 14 | + |
| 15 | +MODULE_NAME = TestId.make_unique("OL") |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture(scope="module") |
| 19 | +def connection(client: AtlanClient): |
| 20 | + admin_role_guid = RoleCache.get_id_for_name("$admin") |
| 21 | + assert admin_role_guid |
| 22 | + response = client.open_lineage.create_connection( |
| 23 | + name=MODULE_NAME, admin_roles=[admin_role_guid] |
| 24 | + ) |
| 25 | + result = response.assets_created(asset_type=Connection)[0] |
| 26 | + yield client.asset.get_by_guid( |
| 27 | + result.guid, asset_type=Connection, ignore_relationships=False |
| 28 | + ) |
| 29 | + delete_asset(client, asset_type=Connection, guid=result.guid) |
| 30 | + |
| 31 | + |
| 32 | +def test_open_lineage_integration(connection: Connection, client: AtlanClient): |
| 33 | + |
| 34 | + assert connection is not None |
| 35 | + assert connection.name == MODULE_NAME |
| 36 | + |
| 37 | + namespace = "snowflake://abc123.snowflakecomputing.com" |
| 38 | + producer = "https://your.orchestrator/unique/id/123" |
| 39 | + job = OpenLineageJob.creator( |
| 40 | + connection_name=MODULE_NAME, job_name="dag_123", producer=producer |
| 41 | + ) |
| 42 | + run = OpenLineageRun.creator(job=job) |
| 43 | + id = job.create_input(namespace=namespace, asset_name="OPS.DEFAULT.RUN_STATS") |
| 44 | + od = job.create_output(namespace=namespace, asset_name="OPS.DEFAULT.FULL_STATS") |
| 45 | + od.to_fields = [ |
| 46 | + { |
| 47 | + "COLUMN": [ |
| 48 | + id.from_field(field_name="COLUMN"), |
| 49 | + id.from_field(field_name="ONE"), |
| 50 | + id.from_field(field_name="TWO"), |
| 51 | + ] |
| 52 | + }, |
| 53 | + { |
| 54 | + "ANOTHER": [ |
| 55 | + id.from_field(field_name="THREE"), |
| 56 | + ] |
| 57 | + }, |
| 58 | + ] |
| 59 | + start = OpenLineageEvent.creator(run=run, event_type=OpenLineageEventType.START) |
| 60 | + start.inputs = [ |
| 61 | + id, |
| 62 | + job.create_input(namespace=namespace, asset_name="SOME.OTHER.TBL"), |
| 63 | + job.create_input(namespace=namespace, asset_name="AN.OTHER.TBL"), |
| 64 | + ] |
| 65 | + start.outputs = [ |
| 66 | + od, |
| 67 | + job.create_output(namespace=namespace, asset_name="AN.OTHER.VIEW"), |
| 68 | + ] |
| 69 | + start.emit() |
| 70 | + |
| 71 | + complete = OpenLineageEvent.creator( |
| 72 | + run=run, event_type=OpenLineageEventType.COMPLETE |
| 73 | + ) |
| 74 | + complete.emit() |
| 75 | + |
| 76 | + assert job |
| 77 | + assert start.event_type == OpenLineageEventType.START |
| 78 | + assert complete.event_type == OpenLineageEventType.COMPLETE |
| 79 | + |
| 80 | + # Awaiting the creation and storage of the Job asset in the backend |
| 81 | + time.sleep(30) |
| 82 | + |
| 83 | + job_qualified_name = f"{connection.qualified_name}/{job.name}" |
| 84 | + |
| 85 | + # Use the audit search, similar to UI calls, |
| 86 | + # to retrieve complete information (process, inputs, outputs) about Spark jobs |
| 87 | + results = client.audit.search( |
| 88 | + AuditSearchRequest.by_qualified_name( |
| 89 | + type_name=SparkJob.__name__, |
| 90 | + qualified_name=job_qualified_name, |
| 91 | + ) |
| 92 | + ) |
| 93 | + assert results and results.current_page() and len(results.current_page()) > 0 |
| 94 | + job_asset = results.current_page()[0] |
| 95 | + assert ( |
| 96 | + job_asset |
| 97 | + and job_asset.detail |
| 98 | + and isinstance(job_asset.detail, Asset) |
| 99 | + and job_asset.detail.relationship_attributes |
| 100 | + ) |
| 101 | + assert job_asset.detail.name == job.name |
| 102 | + assert job_asset.detail.qualified_name == job_qualified_name |
| 103 | + |
| 104 | + inputs = job_asset.detail.relationship_attributes.get("inputs") |
| 105 | + outputs = job_asset.detail.relationship_attributes.get("outputs") |
| 106 | + process = job_asset.detail.relationship_attributes.get("process") |
| 107 | + |
| 108 | + assert inputs |
| 109 | + assert outputs |
| 110 | + assert process |
| 111 | + |
| 112 | + input_qns = { |
| 113 | + input.get("uniqueAttributes", {}).get("qualifiedName") for input in inputs |
| 114 | + } |
| 115 | + assert f"{connection.qualified_name}/OPS/DEFAULT/RUN_STATS" in input_qns |
| 116 | + assert f"{connection.qualified_name}/SOME/OTHER/TBL" in input_qns |
| 117 | + assert f"{connection.qualified_name}/AN/OTHER/TBL" in input_qns |
| 118 | + |
| 119 | + outputs_qns = { |
| 120 | + output.get("uniqueAttributes", {}).get("qualifiedName") for output in outputs |
| 121 | + } |
| 122 | + assert f"{connection.qualified_name}/OPS/DEFAULT/FULL_STATS" in outputs_qns |
| 123 | + assert f"{connection.qualified_name}/AN/OTHER/VIEW" in outputs_qns |
| 124 | + assert ( |
| 125 | + process.get("uniqueAttributes", {}).get("qualifiedName") |
| 126 | + == f"{connection.qualified_name}/dag_123/process" |
| 127 | + ) |
| 128 | + delete_asset(client, asset_type=Process, guid=process.get("guid")) |
| 129 | + delete_asset(client, asset_type=SparkJob, guid=job_asset.detail.guid) |
0 commit comments