Skip to content
Draft
Show file tree
Hide file tree
Changes from 5 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
7 changes: 6 additions & 1 deletion ddtrace/testing/internal/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ def get_known_tests(self) -> set[TestRef]:
for page_number in range(max_pages):
# First page: empty page_info lets backend use its default max (10k).
# Subsequent pages: only send page_state.
page_info: dict[str, t.Any] = {} if page_state is None else {"page_state": page_state}
# AIDEV-NOTE: `page_info` is reused below to hold the *response* page_info after
# the request is sent. To fix: rename this to `request_page_info` and the response
# assignment to `response_page_info` (same pattern as _api_client.py fetch_known_tests).
page_info: dict[str, t.Any] = {"size": 2_000} if page_state is None else {"page_state": page_state}
Comment thread
gnufede marked this conversation as resolved.
Outdated
log.warning("Known tests request page %d: sending page_info=%r", page_number, page_info)

try:
request_data: dict[str, t.Any] = {
Expand Down Expand Up @@ -188,6 +192,7 @@ def get_known_tests(self) -> set[TestRef]:
known_test_ids.add(TestRef(suite_ref, test))

page_info = attributes.get("page_info")
log.warning("Known tests response page %d: received page_info=%r", page_number, page_info)
if not page_info:
break
if not isinstance(page_info, dict):
Expand Down
1 change: 1 addition & 0 deletions ddtrace/testing/internal/session_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def __init__(self, session: TestSession) -> None:
telemetry_api=self.telemetry_api,
)
self.settings = self.api_client.get_settings()
self.settings.known_tests_enabled = True # DEBUG
self.override_settings_with_env_vars()

# Manifest mode disables test skipping: cached skippable decisions should not
Expand Down
79 changes: 79 additions & 0 deletions tests/testing/internal/test_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,85 @@ def test_get_known_tests_pagination_sends_page_info_correctly(self, mock_telemet
TestRef(SuiteRef(ModuleRef("mod2"), "suite2.py"), "test_b"),
}

def test_get_known_tests_pagination_full_request_payload(self, mock_telemetry: Mock) -> None:
"""Second-page request must carry the same fields as the first, with only page_info differing."""
page1_response = {
"data": {
"attributes": {
"tests": {"mod1": {"suite1.py": ["test_a"]}},
"page_info": {"has_next": True, "cursor": "cursor-abc"},
},
"id": "page1-id",
"type": "ci_app_libraries_tests",
}
}
page2_response = {
"data": {
"attributes": {
"tests": {"mod2": {"suite2.py": ["test_b"]}},
},
"id": "page2-id",
"type": "ci_app_libraries_tests",
}
}
mock_connector = mock_backend_connector().build()
mock_connector.post_json.side_effect = [
BackendResult(response=Mock(status=200), parsed_response=page1_response),
BackendResult(response=Mock(status=200), parsed_response=page2_response),
]
mock_connector_setup = Mock()
mock_connector_setup.get_connector_for_subdomain.return_value = mock_connector

api_client = APIClient(
service="my-service",
env="my-env",
env_tags={
GitTag.REPOSITORY_URL: "http://github.com/org/repo.git",
GitTag.COMMIT_SHA: "deadbeef",
GitTag.BRANCH: "main",
GitTag.COMMIT_MESSAGE: "a commit",
},
itr_skipping_level=ITRSkippingLevel.TEST,
configurations={"os.platform": "Linux", "os.architecture": "x86_64"},
connector_setup=mock_connector_setup,
telemetry_api=mock_telemetry,
)

with patch("uuid.uuid4", return_value=uuid.UUID("00000000-0000-0000-0000-000000000000")):
known_tests = api_client.get_known_tests()

assert len(mock_connector.post_json.call_args_list) == 2

expected_shared_attributes = {
"service": "my-service",
"env": "my-env",
"repository_url": "http://github.com/org/repo.git",
"configurations": {"os.platform": "Linux", "os.architecture": "x86_64"},
}

page1_payload = mock_connector.post_json.call_args_list[0][0][1]
assert page1_payload == {
"data": {
"id": "00000000-0000-0000-0000-000000000000",
"type": "ci_app_libraries_tests_request",
"attributes": {**expected_shared_attributes, "page_info": {}},
}
}

page2_payload = mock_connector.post_json.call_args_list[1][0][1]
assert page2_payload == {
"data": {
"id": "00000000-0000-0000-0000-000000000000",
"type": "ci_app_libraries_tests_request",
"attributes": {**expected_shared_attributes, "page_info": {"page_state": "cursor-abc"}},
}
}

assert known_tests == {
TestRef(SuiteRef(ModuleRef("mod1"), "suite1.py"), "test_a"),
TestRef(SuiteRef(ModuleRef("mod2"), "suite2.py"), "test_b"),
}

def test_get_known_tests_max_pages_limit_bails_and_disables_known_tests(
self, mock_telemetry: Mock, caplog: pytest.LogCaptureFixture, monkeypatch: pytest.MonkeyPatch
) -> None:
Expand Down
Loading