Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ def validate_python_code(
{
"expected_checksum": checksum,
"actual_checksum": calculated_checksum,
"code_text": code_text,
}
),
)
Expand Down
5 changes: 2 additions & 3 deletions airbyte_cdk/test/standard_tests/declarative_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@


def md5_checksum(file_path: Path) -> str:
"""Helper function to calculate the MD5 checksum of a file.
"""Calculate the MD5 checksum of the file text, as injected by `create_connector`.

This is used to calculate the checksum of the `components.py` file, if it exists.
"""
with open(file_path, "rb") as file:
return md5(file.read()).hexdigest()
return md5(file_path.read_text().encode()).hexdigest()


class DeclarativeSourceTestSuite(SourceTestSuiteBase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,23 @@ def test_invalid_checksum_fails_to_run(
datetime.datetime.now() - datetime.timedelta(days=2)
).strftime("%Y-%m-%d")

actual_checksum = py_components_config_dict["__injected_components_py_checksums"][hash_type]
py_components_config_dict["__injected_components_py_checksums"][hash_type] = "invalid_checksum"

with NamedTemporaryFile(delete=False, suffix=".json") as temp_config_file:
json_str = json.dumps(py_components_config_dict)
Path(temp_config_file.name).write_text(json_str)
temp_config_file.flush()
with pytest.raises(AirbyteCodeTamperedError):
with pytest.raises(AirbyteCodeTamperedError) as exc_info:
source = create_declarative_source(
["check", "--config", temp_config_file.name],
)

# No components code in the message: it lands in failure reasons and logs
assert str(exc_info.value) == f"{hash_type} checksum does not match." + str(
{"expected_checksum": "invalid_checksum", "actual_checksum": actual_checksum}
)


@pytest.mark.parametrize(
"env_value, should_raise",
Expand Down
13 changes: 13 additions & 0 deletions unit_tests/test/test_standard_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
from airbyte_cdk.sources.declarative.concurrent_declarative_source import (
ConcurrentDeclarativeSource,
)
from airbyte_cdk.sources.declarative.parsers.custom_code_compiler import validate_python_code
from airbyte_cdk.sources.source import Source
from airbyte_cdk.test.models.scenario import ConnectorTestScenario
from airbyte_cdk.test.standard_tests._job_runner import IConnector
from airbyte_cdk.test.standard_tests.declarative_sources import md5_checksum
from airbyte_cdk.test.standard_tests.docker_base import DockerConnectorTestSuite
from airbyte_cdk.test.standard_tests.pytest_hooks import _scenario_test_ids

Expand Down Expand Up @@ -108,3 +110,14 @@ def test_dedup_scenarios_conflicting_statuses_raise() -> None:
def test_scenario_test_ids(config_paths: list[Path], expected_ids: list[str]) -> None:
scenarios = [ConnectorTestScenario(config_path=path) for path in config_paths]
assert _scenario_test_ids(scenarios) == expected_ids


def test_md5_checksum_matches_injected_text_with_crlf(tmp_path: Path) -> None:
"""Assert the checksum covers `read_text()`, which `create_connector` injects, not raw bytes."""
components_py_path = tmp_path / "components.py"
components_py_path.write_bytes(b"def sample_function():\r\n return 1\r\n")

validate_python_code(
code_text=components_py_path.read_text(),
checksums={"md5": md5_checksum(components_py_path)},
)
Loading