Skip to content

Commit 16de0ba

Browse files
authored
Implement Hypervisor field on instances (#107)
* Problem: Instances with Qemu hypervisor cannot be created. Solution: Implement hypervisor field into create_instance method * Fix: Add test case for hypervisor field * Fix: Solved code quality issues * Fix: Solve code quality issues on tests also * Fix: Fixed incorrect import on abstract interface * Fix: Fixed incorrect import on abstract interface * Fix: Upgraded black version and refformatted issues * Fix: Solved issue in wrong field to test.
1 parent 53a3aaa commit 16de0ba

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

src/aleph/sdk/client/abstract.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
Payment,
2424
PostMessage,
2525
)
26+
from aleph_message.models.execution.environment import HypervisorType
2627
from aleph_message.models.execution.program import Encoding
2728
from aleph_message.status import MessageStatus
2829

@@ -373,6 +374,7 @@ async def create_instance(
373374
allow_amend: bool = False,
374375
internet: bool = True,
375376
aleph_api: bool = True,
377+
hypervisor: Optional[HypervisorType] = None,
376378
volumes: Optional[List[Mapping]] = None,
377379
volume_persistence: str = "host",
378380
ssh_keys: Optional[List[str]] = None,

src/aleph/sdk/client/authenticated_http.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from aleph_message.models.execution.base import Encoding, Payment, PaymentType
3030
from aleph_message.models.execution.environment import (
3131
FunctionEnvironment,
32+
HypervisorType,
3233
MachineResources,
3334
)
3435
from aleph_message.models.execution.instance import RootfsVolume
@@ -520,6 +521,7 @@ async def create_instance(
520521
allow_amend: bool = False,
521522
internet: bool = True,
522523
aleph_api: bool = True,
524+
hypervisor: Optional[HypervisorType] = None,
523525
volumes: Optional[List[Mapping]] = None,
524526
volume_persistence: str = "host",
525527
ssh_keys: Optional[List[str]] = None,
@@ -533,6 +535,7 @@ async def create_instance(
533535
timeout_seconds = timeout_seconds or settings.DEFAULT_VM_TIMEOUT
534536

535537
payment = payment or Payment(chain=Chain.ETH, type=PaymentType.hold)
538+
hypervisor = hypervisor or HypervisorType.firecracker
536539

537540
content = InstanceContent(
538541
address=address,
@@ -541,6 +544,7 @@ async def create_instance(
541544
reproducible=False,
542545
internet=internet,
543546
aleph_api=aleph_api,
547+
hypervisor=hypervisor,
544548
),
545549
variables=environment_variables,
546550
resources=MachineResources(

tests/unit/test_asynchronous.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
ProgramMessage,
1515
StoreMessage,
1616
)
17-
from aleph_message.models.execution.environment import MachineResources
17+
from aleph_message.models.execution.environment import HypervisorType, MachineResources
1818
from aleph_message.status import MessageStatus
1919

2020
from aleph.sdk.exceptions import InsufficientFundsError
@@ -116,6 +116,7 @@ async def test_create_instance(mock_session_with_post_success):
116116
receiver="0x4145f182EF2F06b45E50468519C1B92C60FBd4A0",
117117
type=PaymentType.superfluid,
118118
),
119+
hypervisor=HypervisorType.qemu,
119120
)
120121

121122
assert mock_session_with_post_success.http_session.post.called_once
@@ -144,6 +145,27 @@ async def test_create_instance_no_payment(mock_session_with_post_success):
144145
assert isinstance(instance_message, InstanceMessage)
145146

146147

148+
@pytest.mark.asyncio
149+
async def test_create_instance_no_hypervisor(mock_session_with_post_success):
150+
"""Test that an instance can be created with no hypervisor specified.
151+
It should in this case default to "firecracker".
152+
"""
153+
async with mock_session_with_post_success as session:
154+
instance_message, message_status = await session.create_instance(
155+
rootfs="cafecafecafecafecafecafecafecafecafecafecafecafecafecafecafecafe",
156+
rootfs_size=1,
157+
rootfs_name="rootfs",
158+
channel="TEST",
159+
metadata={"tags": ["test"]},
160+
hypervisor=None,
161+
)
162+
163+
assert instance_message.content.environment.hypervisor == HypervisorType.firecracker
164+
165+
assert mock_session_with_post_success.http_session.post.called_once
166+
assert isinstance(instance_message, InstanceMessage)
167+
168+
147169
@pytest.mark.asyncio
148170
async def test_forget(mock_session_with_post_success):
149171
async with mock_session_with_post_success as session:

0 commit comments

Comments
 (0)