Skip to content

Commit 0e656ba

Browse files
committed
Fix: Orchestrator failed with assert result["result"] == HTTPOk.status_code
Problem: The diagnostic VM returned HTTP 200 with {"result": False} when it could not connect to the internet. Since this is an OK return code, `raise_for_status` did not raise an error and an assertion error was raised. Solution: Test that the returned status code also corresponds to HTTP OK.
1 parent b63d248 commit 0e656ba

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/aleph/vm/orchestrator/status.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,17 @@ async def check_ipv6(session: ClientSession, vm_id: ItemHash) -> bool:
118118
async def check_internet(session: ClientSession, vm_id: ItemHash) -> bool:
119119
"""Check that the VM has internet connectivity. This requires DNS, IP, HTTP and TLS to work."""
120120
try:
121-
result: dict = await get_json_from_vm(session, vm_id, "/internet")
122-
assert result["result"] == HTTPOk.status_code
123-
assert "Server" in result["headers"]
121+
response: dict = await get_json_from_vm(session, vm_id, "/internet")
122+
123+
# The HTTP Header "Server" must always be present in the result.
124+
if "Server" not in response["headers"]:
125+
raise ValueError("Server header not found in the result.")
126+
127+
# The diagnostic VM returns HTTP 200 with {"result": False} when cannot connect to the internet.
128+
# else it forwards the return code if its own test endpoint.
129+
if response.get("result") != HTTPOk.status_code:
130+
return False
131+
124132
return True
125133
except ClientResponseError:
126134
return False

tests/supervisor/test_status.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from unittest.mock import AsyncMock, MagicMock, Mock
2+
3+
import pytest
4+
from aleph_message.models import ItemHash
5+
6+
from aleph.vm.orchestrator.status import check_internet
7+
8+
9+
@pytest.mark.asyncio
10+
async def test_check_internet_no_server_header():
11+
vm_id = ItemHash("cafecafecafecafecafecafecafecafecafecafecafecafecafecafecafecafe")
12+
13+
mock_session = Mock()
14+
mock_session.get = MagicMock()
15+
mock_session.get.__aenter__.return_value.json = AsyncMock(return_value={"result": 200})
16+
17+
# A "Server" header is always expected in the result from the VM.
18+
# If it is not present, the diagnostic VM is not working correctly
19+
# and an error must be raised.
20+
with pytest.raises(ValueError):
21+
await check_internet(mock_session, vm_id)
22+
23+
24+
@pytest.mark.asyncio
25+
async def test_check_internet_wrong_result_code():
26+
vm_id = ItemHash("cafecafecafecafecafecafecafecafecafecafecafecafecafecafecafecafe")
27+
28+
mock_session = Mock()
29+
mock_session.get = MagicMock()
30+
31+
mock_session.get.return_value.__aenter__.return_value.json = AsyncMock(
32+
return_value={"result": 200, "headers": {"Server": "nginx"}}
33+
)
34+
assert await check_internet(mock_session, vm_id) is True
35+
36+
mock_session.get.return_value.__aenter__.return_value.json = AsyncMock(
37+
return_value={"result": 400, "headers": {"Server": "nginx"}}
38+
)
39+
assert await check_internet(mock_session, vm_id) is False

0 commit comments

Comments
 (0)