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
6 changes: 6 additions & 0 deletions src/aleph/vm/orchestrator/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,12 @@ def authenticate_api_request(request: web.Request) -> bool:


async def update_allocations(request: web.Request):
"""Main entry for the start of persistence VM and instance, called by the Scheduler,


auth via the SETTINGS.ALLOCATION_TOKEN_HASH sent in header X-Auth-Signature.
Receive a list of vm and instance that should be present and then match that state by stopping and launching VMs
"""
if not authenticate_api_request(request):
return web.HTTPUnauthorized(text="Authentication token received is invalid")

Expand Down
55 changes: 53 additions & 2 deletions tests/supervisor/test_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from unittest import mock

import pytest
from aiohttp import web

Expand Down Expand Up @@ -70,3 +68,56 @@ async def test_system_usage_mock(aiohttp_client, mocker):
assert resp["properties"]["cpu"]["vendor"] == "AuthenticAMD"
assert resp["cpu"]["load_average"] == {"load1": 1.0, "load15": 3.0, "load5": 2.0}
assert resp["cpu"]["count"] == 200


@pytest.mark.asyncio
async def test_allocation_invalid_auth_token(aiohttp_client):
"""Test that the allocation endpoint fails when an invalid auth token is provided."""
settings.ALLOCATION_TOKEN_HASH = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08" # = "test"
app = setup_webapp()
client = await aiohttp_client(app)
response = await client.post(
"/control/allocations",
json={"persistent_vms": []},
headers={"X-Auth-Signature": "notTest"},
)
assert response.status == 401
assert await response.text() == "Authentication token received is invalid"


@pytest.mark.asyncio
async def test_allocation_missing_auth_token(aiohttp_client):
"""Test that the allocation endpoint fails when auth token is not provided."""
app = setup_webapp()
client = await aiohttp_client(app)
response: web.Response = await client.post(
"/control/allocations",
json={"persistent_vms": []},
)
assert response.status == 401
assert await response.text() == "Authentication token is missing"


@pytest.mark.asyncio
async def test_allocation_valid_token(aiohttp_client):
"""Test that the allocation endpoint fails when an invalid auth is provided.

This is a very simple test that don't start or stop any VM so the mock is minimal"""

class FakeVmPool:
def get_persistent_executions(self):
return []

settings.ALLOCATION_TOKEN_HASH = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08" # = "test"
app = setup_webapp()
app["vm_pool"] = FakeVmPool()
app["pubsub"] = FakeVmPool()
client = await aiohttp_client(app)

response: web.Response = await client.post(
"/control/allocations",
json={"persistent_vms": []},
headers={"X-Auth-Signature": "test"},
)
assert response.status == 200
assert await response.json() == {"success": True, "successful": [], "failing": [], "errors": {}}