Skip to content

Commit

Permalink
Merge branch 'main' into python-apt-only
Browse files Browse the repository at this point in the history
  • Loading branch information
lengau authored Feb 26, 2025
2 parents 8d1e882 + 4a17a58 commit a99d737
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 69 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repos:
- id: fix-byte-order-marker
- id: mixed-line-ending
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.9.5"
rev: "v0.9.7"
hooks:
# Run the linter
- id: ruff
Expand Down
33 changes: 24 additions & 9 deletions craft_application/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,22 +226,26 @@ def stop_service(fetch_process: subprocess.Popen[str]) -> None:
fetch_process.kill()


def create_session(*, strict: bool) -> SessionData:
def create_session(*, strict: bool, timeout: float = 5.0) -> SessionData:
"""Create a new fetch-service session.
:param strict: Whether the created session should be strict.
:param timeout: Maximum time to wait for the response from the fetch-service
:return: a SessionData object containing the session's id and token.
"""
json = {"policy": "strict" if strict else "permissive"}
data = _service_request("post", "session", json=json).json()
data = _service_request("post", "session", json=json, timeout=timeout).json()

return SessionData.unmarshal(data=data)


def teardown_session(session_data: SessionData) -> dict[str, Any]:
def teardown_session(
session_data: SessionData, timeout: float = 10.0
) -> dict[str, Any]:
"""Stop and cleanup a running fetch-service session.
:param session_data: the data of a previously-created session.
:param timeout: Maximum time to wait for the response from the fetch-service
:return: A dict containing the session's report (the contents and format
of this dict are still subject to change).
"""
Expand All @@ -250,17 +254,25 @@ def teardown_session(session_data: SessionData) -> dict[str, Any]:

# Revoke token
_revoke_data = _service_request(
"delete", f"session/{session_id}/token", json={"token": session_token}
"delete",
f"session/{session_id}/token",
json={"token": session_token},
timeout=timeout,
).json()

# Get session report
session_report = _service_request("get", f"session/{session_id}", json={}).json()
session_report = _service_request(
"get",
f"session/{session_id}",
json={},
timeout=timeout,
).json()

# Delete session
_service_request("delete", f"session/{session_id}")
_service_request("delete", f"session/{session_id}", timeout=timeout)

# Delete session resources
_service_request("delete", f"resources/{session_id}")
_service_request("delete", f"resources/{session_id}", timeout=timeout)

return cast(dict[str, Any], session_report)

Expand Down Expand Up @@ -302,7 +314,10 @@ def verify_installed() -> None:


def _service_request(
verb: str, endpoint: str, json: dict[str, Any] | None = None
verb: str,
endpoint: str,
json: dict[str, Any] | None = None,
timeout: float = 10.0,
) -> requests.Response:
headers = {
"Content-type": "application/json",
Expand All @@ -315,7 +330,7 @@ def _service_request(
auth=auth,
headers=headers,
json=json, # Use defaults
timeout=0.1,
timeout=timeout,
)
response.raise_for_status()
except requests.RequestException as err:
Expand Down
8 changes: 8 additions & 0 deletions docs/reference/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
Changelog
*********

4.x.x (YYYY-MMM-DD)
-------------------

Application
===========

- Increase timeout in fetch-service queries.

4.9.1 (2025-Feb-12)
-------------------

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ apt = [

[dependency-groups]
dev = [
"coverage[toml]==7.6.10",
"coverage[toml]==7.6.12",
"hypothesis>=6.0",
"pyfakefs~=5.3",
"pytest==8.3.4",
Expand Down
15 changes: 13 additions & 2 deletions tests/unit/test_fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,16 @@ def test_start_service_not_installed(mocker):
("strict", "expected_policy"), [(True, "strict"), (False, "permissive")]
)
def test_create_session(strict, expected_policy):
create_session_timeout = 5.0
responses.add(
responses.POST,
f"http://localhost:{CONTROL}/session",
json={"id": "my-session-id", "token": "my-session-token"},
status=200,
match=[matchers.json_params_matcher({"policy": expected_policy})],
match=[
matchers.json_params_matcher({"policy": expected_policy}),
matchers.request_kwargs_matcher({"timeout": create_session_timeout}),
],
)

session_data = fetch.create_session(strict=strict)
Expand All @@ -171,30 +175,37 @@ def test_create_session(strict, expected_policy):
@assert_requests
def test_teardown_session():
session_data = fetch.SessionData(id="my-session-id", token="my-session-token")
default_timeout = 10.0

# Call to delete token
responses.delete(
f"http://localhost:{CONTROL}/session/{session_data.session_id}/token",
match=[matchers.json_params_matcher({"token": session_data.token})],
match=[
matchers.json_params_matcher({"token": session_data.token}),
matchers.request_kwargs_matcher({"timeout": default_timeout}),
],
json={},
status=200,
)
# Call to get session report
responses.get(
f"http://localhost:{CONTROL}/session/{session_data.session_id}",
json={},
match=[matchers.request_kwargs_matcher({"timeout": default_timeout})],
status=200,
)
# Call to delete session
responses.delete(
f"http://localhost:{CONTROL}/session/{session_data.session_id}",
json={},
match=[matchers.request_kwargs_matcher({"timeout": default_timeout})],
status=200,
)
# Call to delete session resources
responses.delete(
f"http://localhost:{CONTROL}/resources/{session_data.session_id}",
json={},
match=[matchers.request_kwargs_matcher({"timeout": default_timeout})],
status=200,
)

Expand Down
Loading

0 comments on commit a99d737

Please sign in to comment.