Skip to content

Commit 5fe46ac

Browse files
committed
Problem: allocation endpoints was not tested
Solution: Start by adding some simple tests We don't test the full allocation and deallocation here. just auth
1 parent b800612 commit 5fe46ac

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/aleph/vm/orchestrator/views/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,12 @@ def authenticate_api_request(request: web.Request) -> bool:
341341

342342

343343
async def update_allocations(request: web.Request):
344+
"""Main entry for the start of persistence VM and instance, called by the CCN,
345+
346+
347+
auth via the SETTINGS.ALLOCATION_TOKEN_HASH sent in header X-Auth-Signature.
348+
Receive a list of vm and instance that should be present and then match that state by stopping and launching VMs
349+
"""
344350
if not authenticate_api_request(request):
345351
return web.HTTPUnauthorized(text="Authentication token received is invalid")
346352

tests/supervisor/test_views.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,56 @@ async def test_system_usage(aiohttp_client):
3838
resp = await response.json()
3939
assert "cpu" in resp
4040
assert resp["cpu"]["count"] > 0
41+
42+
43+
@pytest.mark.asyncio
44+
async def test_allocation_invalid_auth_token(aiohttp_client):
45+
"""Test that the allocation endpoint fails when an invalid auth token is provided."""
46+
settings.ALLOCATION_TOKEN_HASH = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08" # = "test"
47+
app = setup_webapp()
48+
client = await aiohttp_client(app)
49+
response = await client.post(
50+
"/control/allocations",
51+
json={"persistent_vms": []},
52+
headers={"X-Auth-Signature": "notTest"},
53+
)
54+
assert response.status == 401
55+
assert await response.text() == "Authentication token received is invalid"
56+
57+
58+
@pytest.mark.asyncio
59+
async def test_allocation_missing_auth_token(aiohttp_client):
60+
"""Test that the allocation endpoint fails when auth token is not provided."""
61+
app = setup_webapp()
62+
client = await aiohttp_client(app)
63+
response: web.Response = await client.post(
64+
"/control/allocations",
65+
json={"persistent_vms": []},
66+
)
67+
assert response.status == 401
68+
assert await response.text() == "Authentication token is missing"
69+
70+
71+
@pytest.mark.asyncio
72+
async def test_allocation_valid_token(aiohttp_client):
73+
"""Test that the allocation endpoint fails when an invalid auth is provided.
74+
75+
This is a very simple test that don't start or stop any VM so the mock is minimal"""
76+
77+
class FakeVmPool:
78+
def get_persistent_executions(self):
79+
return []
80+
81+
settings.ALLOCATION_TOKEN_HASH = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08" # = "test"
82+
app = setup_webapp()
83+
app["vm_pool"] = FakeVmPool()
84+
app["pubsub"] = FakeVmPool()
85+
client = await aiohttp_client(app)
86+
87+
response: web.Response = await client.post(
88+
"/control/allocations",
89+
json={"persistent_vms": []},
90+
headers={"X-Auth-Signature": "test"},
91+
)
92+
assert response.status == 200
93+
assert await response.json() == {"success": True, "successful": [], "failing": [], "errors": {}}

0 commit comments

Comments
 (0)