-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy path_job_runner.py
More file actions
146 lines (123 loc) · 5.51 KB
/
_job_runner.py
File metadata and controls
146 lines (123 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Copyright (c) 2025 Airbyte, Inc., all rights reserved.
"""Job runner for Airbyte Standard Tests."""
import logging
import tempfile
import uuid
from dataclasses import asdict
from pathlib import Path
from typing import Any, Callable, Literal
import orjson
from typing_extensions import Protocol, runtime_checkable
from airbyte_cdk.models import (
ConfiguredAirbyteCatalog,
Status,
)
from airbyte_cdk.test import entrypoint_wrapper
from airbyte_cdk.test.models import (
ConnectorTestScenario,
)
@runtime_checkable
class IConnector(Protocol):
"""A connector that can be run in a test scenario.
Note: We currently use 'spec' to determine if we have a connector object.
In the future, it would be preferred to leverage a 'launch' method instead,
directly on the connector (which doesn't yet exist).
"""
def spec(self, logger: logging.Logger) -> Any:
"""Connectors should have a `spec` method."""
def run_test_job(
connector: IConnector | type[IConnector] | Callable[[], IConnector],
verb: Literal["spec", "read", "check", "discover"],
*,
connector_root: Path,
test_scenario: ConnectorTestScenario | None = None,
catalog: ConfiguredAirbyteCatalog | dict[str, Any] | None = None,
) -> entrypoint_wrapper.EntrypointOutput:
"""Run a test scenario from provided CLI args and return the result."""
# Use default (empty) scenario if not provided:
test_scenario = test_scenario or ConnectorTestScenario()
if not connector:
raise ValueError("Connector is required")
if catalog and isinstance(catalog, ConfiguredAirbyteCatalog):
# Convert the catalog to a dict if it's already a ConfiguredAirbyteCatalog.
catalog = asdict(catalog)
connector_obj: IConnector
if isinstance(connector, type) or callable(connector):
# If the connector is a class or a factory lambda, instantiate it.
connector_obj = connector()
elif isinstance(connector, IConnector):
connector_obj = connector
else:
raise ValueError(
f"Invalid connector input: {type(connector)}",
)
args: list[str] = [verb]
config_dict = test_scenario.get_config_dict(
empty_if_missing=True,
connector_root=connector_root,
)
if config_dict and verb != "spec":
# Write the config to a temp json file and pass the path to the file as an argument.
config_path = (
Path(tempfile.gettempdir()) / "airbyte-test" / f"temp_config_{uuid.uuid4().hex}.json"
)
config_path.parent.mkdir(parents=True, exist_ok=True)
config_path.write_text(orjson.dumps(config_dict).decode())
args += ["--config", str(config_path)]
catalog_path: Path | None = None
if verb not in ["discover", "check"]:
# We need a catalog for read.
if catalog:
# Write the catalog to a temp json file and pass the path to the file as an argument.
catalog_path = (
Path(tempfile.gettempdir())
/ "airbyte-test"
/ f"temp_catalog_{uuid.uuid4().hex}.json"
)
catalog_path.parent.mkdir(parents=True, exist_ok=True)
catalog_path.write_text(orjson.dumps(catalog).decode())
elif test_scenario.configured_catalog_path:
catalog_path = Path(test_scenario.configured_catalog_path)
if catalog_path:
args += ["--catalog", str(catalog_path)]
# This is a bit of a hack because the source needs the catalog early.
# Because it *also* can fail, we have to redundantly wrap it in a try/except block.
result: entrypoint_wrapper.EntrypointOutput = entrypoint_wrapper._run_command( # noqa: SLF001 # Non-public API
source=connector_obj, # type: ignore [arg-type]
args=args,
expected_outcome=test_scenario.expected_outcome,
)
if result.errors and test_scenario.expected_outcome.expect_success():
raise result.as_exception()
if verb == "check":
# Check is expected to fail gracefully without an exception.
# Instead, we assert that we have a CONNECTION_STATUS message with
# a failure status.
assert len(result.connection_status_messages) == 1, (
"Expected exactly one CONNECTION_STATUS message. Got "
f"{len(result.connection_status_messages)}:\n"
+ "\n".join([str(msg) for msg in result.connection_status_messages])
+ result.get_formatted_error_message()
)
if test_scenario.expected_outcome.expect_exception():
conn_status = result.connection_status_messages[0].connectionStatus
assert conn_status, (
"Expected CONNECTION_STATUS message to be present. Got: \n"
+ "\n".join([str(msg) for msg in result.connection_status_messages])
)
assert conn_status.status == Status.FAILED, (
"Expected CONNECTION_STATUS message to be FAILED. Got: \n"
+ "\n".join([str(msg) for msg in result.connection_status_messages])
)
return result
# For all other verbs, we assert check that an exception is raised (or not).
if test_scenario.expected_outcome.expect_exception():
if not result.errors:
raise AssertionError("Expected exception but got none.")
return result
if test_scenario.expected_outcome.expect_success():
assert not result.errors, (
f"Test job failed with {len(result.errors)} error(s): \n"
+ result.get_formatted_error_message()
)
return result