Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Orchestrator failed with assert result["result"] == HTTPOk.status_code #628

Merged
merged 1 commit into from
Jun 13, 2024
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
13 changes: 9 additions & 4 deletions src/aleph/vm/orchestrator/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,15 @@ async def check_ipv6(session: ClientSession, vm_id: ItemHash) -> bool:
async def check_internet(session: ClientSession, vm_id: ItemHash) -> bool:
"""Check that the VM has internet connectivity. This requires DNS, IP, HTTP and TLS to work."""
try:
result: dict = await get_json_from_vm(session, vm_id, "/internet")
assert result["result"] == HTTPOk.status_code
assert "Server" in result["headers"]
return True
response: dict = await get_json_from_vm(session, vm_id, "/internet")

# The HTTP Header "Server" must always be present in the result.
if "Server" not in response["headers"]:
hoh marked this conversation as resolved.
Show resolved Hide resolved
raise ValueError("Server header not found in the result.")

# The diagnostic VM returns HTTP 200 with {"result": False} when cannot connect to the internet.
# else it forwards the return code if its own test endpoint.
return response.get("result") == HTTPOk.status_code
except ClientResponseError:
return False

Expand Down
39 changes: 39 additions & 0 deletions tests/supervisor/test_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from unittest.mock import AsyncMock, MagicMock, Mock

import pytest
from aleph_message.models import ItemHash

from aleph.vm.orchestrator.status import check_internet


@pytest.mark.asyncio
async def test_check_internet_no_server_header():
vm_id = ItemHash("cafecafecafecafecafecafecafecafecafecafecafecafecafecafecafecafe")

mock_session = Mock()
mock_session.get = MagicMock()
mock_session.get.__aenter__.return_value.json = AsyncMock(return_value={"result": 200})

# A "Server" header is always expected in the result from the VM.
# If it is not present, the diagnostic VM is not working correctly
# and an error must be raised.
with pytest.raises(ValueError):
await check_internet(mock_session, vm_id)


@pytest.mark.asyncio
async def test_check_internet_wrong_result_code():
vm_id = ItemHash("cafecafecafecafecafecafecafecafecafecafecafecafecafecafecafecafe")

mock_session = Mock()
mock_session.get = MagicMock()

mock_session.get.return_value.__aenter__.return_value.json = AsyncMock(
return_value={"result": 200, "headers": {"Server": "nginx"}}
)
assert await check_internet(mock_session, vm_id) is True

mock_session.get.return_value.__aenter__.return_value.json = AsyncMock(
return_value={"result": 400, "headers": {"Server": "nginx"}}
)
assert await check_internet(mock_session, vm_id) is False