1515 CpuProperties ,
1616 GpuDeviceClass ,
1717 HypervisorType ,
18- MachineResources ,
1918)
2019from multidict import CIMultiDict , CIMultiDictProxy
2120
@@ -262,28 +261,48 @@ def create_mock_shutil():
262261 return MagicMock (which = MagicMock (return_value = "/root/.cargo/bin/sevctl" , move = MagicMock (return_value = "/fake/path" )))
263262
264263
265- def create_mock_client ():
264+ def create_mock_client (payment_type = "superfluid" ):
266265 mock_client = AsyncMock (
267266 get_message = AsyncMock (return_value = True ),
268267 get_stored_content = AsyncMock (
269268 return_value = Dict (filename = "fake_tac" , hash = "0xfake_tac" , url = "https://fake.tac.com" )
270269 ),
270+ get_estimated_price = AsyncMock (
271+ return_value = MagicMock (
272+ required_tokens = 0.00001527777777777777 if payment_type == "superfluid" else 1000 ,
273+ payment_type = payment_type ,
274+ )
275+ ),
271276 )
272277 mock_client_class = MagicMock ()
273278 mock_client_class .return_value .__aenter__ = AsyncMock (return_value = mock_client )
274279 return mock_client_class , mock_client
275280
276281
277- def create_mock_auth_client (mock_account ):
282+ def create_mock_auth_client (mock_account , payment_type = "superfluid" , payment_types = None ):
283+
284+ def response_get_program_price (ptype ):
285+ return MagicMock (
286+ required_tokens = 0.00001527777777777777 if ptype == "superfluid" else 1000 ,
287+ payment_type = ptype ,
288+ )
289+
278290 mock_response_get_message = create_mock_instance_message (mock_account , payg = True )
279291 mock_response_create_instance = MagicMock (item_hash = FAKE_VM_HASH )
280292 mock_auth_client = AsyncMock (
281293 get_messages = AsyncMock (),
282294 get_message = AsyncMock (return_value = mock_response_get_message ),
283295 create_instance = AsyncMock (return_value = [mock_response_create_instance , 200 ]),
284- get_program_price = AsyncMock ( return_value = MagicMock ( required_tokens = 0.0001 )) ,
296+ get_program_price = None ,
285297 forget = AsyncMock (return_value = (MagicMock (), 200 )),
286298 )
299+ if payment_types :
300+ mock_auth_client .get_program_price = AsyncMock (
301+ side_effect = [response_get_program_price (pt ) for pt in payment_types ]
302+ )
303+ else :
304+ mock_auth_client .get_program_price = AsyncMock (return_value = response_get_program_price (payment_type ))
305+
287306 mock_auth_client_class = MagicMock ()
288307 mock_auth_client_class .return_value .__aenter__ = AsyncMock (return_value = mock_auth_client )
289308 return mock_auth_client_class , mock_auth_client
@@ -421,23 +440,23 @@ async def test_create_instance(args, expected):
421440 mock_validate_ssh_pubkey_file = create_mock_validate_ssh_pubkey_file ()
422441 mock_load_account = create_mock_load_account ()
423442 mock_account = mock_load_account .return_value
424- mock_client_class , _ = create_mock_client ()
425- mock_auth_client_class , mock_auth_client = create_mock_auth_client (mock_account )
443+ mock_get_balance = AsyncMock (return_value = dict (available_amount = 100000 ))
444+ mock_client_class , mock_client = create_mock_client (payment_type = args ["payment_type" ])
445+ mock_auth_client_class , mock_auth_client = create_mock_auth_client (mock_account , payment_type = args ["payment_type" ])
426446 mock_vm_client_class , mock_vm_client = create_mock_vm_client ()
427447 mock_crn_info = create_mock_crn_info ()
428448 mock_validated_int_prompt = MagicMock (return_value = 1 )
429449 mock_wait_for_processed_instance = AsyncMock ()
430- mock_update_flow = AsyncMock (return_value = "fake_flow_hash" )
431450 mock_wait_for_confirmed_flow = AsyncMock ()
432451
433452 @patch ("aleph_client.commands.instance.validate_ssh_pubkey_file" , mock_validate_ssh_pubkey_file )
434453 @patch ("aleph_client.commands.instance._load_account" , mock_load_account )
454+ @patch ("aleph_client.commands.instance.get_balance" , mock_get_balance )
435455 @patch ("aleph_client.commands.instance.AlephHttpClient" , mock_client_class )
436456 @patch ("aleph_client.commands.instance.AuthenticatedAlephHttpClient" , mock_auth_client_class )
437457 @patch ("aleph_client.commands.instance.CRNInfo" , mock_crn_info )
438458 @patch ("aleph_client.commands.instance.validated_int_prompt" , mock_validated_int_prompt )
439459 @patch ("aleph_client.commands.instance.wait_for_processed_instance" , mock_wait_for_processed_instance )
440- @patch ("aleph_client.commands.instance.update_flow" , mock_update_flow )
441460 @patch ("aleph_client.commands.instance.wait_for_confirmed_flow" , mock_wait_for_confirmed_flow )
442461 @patch ("aleph_client.commands.instance.VmClient" , mock_vm_client_class )
443462 async def create_instance (instance_spec ):
@@ -446,16 +465,18 @@ async def create_instance(instance_spec):
446465 ssh_pubkey_file = FAKE_PUBKEY_FILE ,
447466 name = "mock_instance" ,
448467 hypervisor = HypervisorType .qemu ,
449- rootfs_size = 20480 ,
450- vcpus = 1 ,
451- memory = 2048 ,
468+ compute_units = 1 ,
469+ vcpus = None ,
470+ memory = None ,
471+ rootfs_size = None ,
452472 timeout_seconds = settings .DEFAULT_VM_TIMEOUT ,
453473 skip_volume = True ,
454474 persistent_volume = None ,
455475 ephemeral_volume = None ,
456476 immutable_volume = None ,
457477 crn_auto_tac = True ,
458478 channel = settings .DEFAULT_CHANNEL ,
479+ address = None ,
459480 crn_hash = None ,
460481 crn_url = None ,
461482 confidential = False ,
@@ -471,10 +492,13 @@ async def create_instance(instance_spec):
471492 returned = await create_instance (args )
472493 mock_load_account .assert_called_once ()
473494 mock_validate_ssh_pubkey_file .return_value .read_text .assert_called_once ()
495+ mock_client .get_estimated_price .assert_called_once ()
474496 mock_auth_client .create_instance .assert_called_once ()
475- if args ["payment_type" ] == "superfluid" :
497+ if args ["payment_type" ] == "hold" :
498+ mock_get_balance .assert_called_once ()
499+ elif args ["payment_type" ] == "superfluid" :
476500 mock_wait_for_processed_instance .assert_called_once ()
477- mock_update_flow .assert_called_once ()
501+ mock_account . manage_flow .assert_called_once ()
478502 mock_wait_for_confirmed_flow .assert_called_once ()
479503 mock_vm_client .start_instance .assert_called_once ()
480504 assert returned == expected
@@ -485,8 +509,10 @@ async def test_list_instances():
485509 mock_load_account = create_mock_load_account ()
486510 mock_account = mock_load_account .return_value
487511 mock_client_class , mock_client = create_mock_client ()
488- mock_auth_client_class , mock_auth_client = create_mock_auth_client (mock_account )
489512 mock_instance_messages = create_mock_instance_messages (mock_account )
513+ mock_auth_client_class , mock_auth_client = create_mock_auth_client (
514+ mock_account , payment_types = [vm .content .payment .type for vm in mock_instance_messages .return_value ]
515+ )
490516
491517 @patch ("aleph_client.commands.instance._load_account" , mock_load_account )
492518 @patch ("aleph_client.commands.files.AlephHttpClient" , mock_client_class )
@@ -503,7 +529,7 @@ async def list_instance():
503529 mock_instance_messages .assert_called_once ()
504530 mock_auth_client .get_messages .assert_called_once ()
505531 mock_auth_client .get_program_price .assert_called ()
506- assert mock_auth_client .get_program_price .call_count == 4
532+ assert mock_auth_client .get_program_price .call_count == 5
507533 assert mock_client .get_stored_content .call_count == 1
508534
509535 await list_instance ()
@@ -531,7 +557,7 @@ async def delete_instance():
531557 )
532558 mock_auth_client .get_message .assert_called_once ()
533559 mock_vm_client .erase_instance .assert_called_once ()
534- mock_account .delete_flow .assert_awaited_once ()
560+ mock_account .manage_flow .assert_awaited_once ()
535561 mock_auth_client .forget .assert_called_once ()
536562
537563 await delete_instance ()
@@ -732,10 +758,8 @@ async def coco_start():
732758 payment_chain = "AVAX" ,
733759 crn_hash = FAKE_CRN_HASH ,
734760 crn_url = FAKE_CRN_URL ,
735- vcpus = 1 ,
736- memory = 2048 ,
737761 rootfs = FAKE_STORE_HASH ,
738- rootfs_size = 20480 ,
762+ compute_units = 1 ,
739763 ),
740764 dict (vm_id = FAKE_VM_HASH ), # coco_from_hash
741765 ],
@@ -771,14 +795,16 @@ async def coco_create(instance_spec):
771795 crn_hash = None ,
772796 crn_url = None ,
773797 ssh_pubkey_file = FAKE_PUBKEY_FILE ,
798+ address = None ,
774799 name = "mock_instance" ,
775800 vm_secret = "fake_secret" ,
801+ compute_units = None ,
776802 vcpus = None ,
777803 memory = None ,
804+ rootfs_size = None ,
778805 timeout_seconds = settings .DEFAULT_VM_TIMEOUT ,
779806 gpu = False ,
780807 rootfs = None ,
781- rootfs_size = None ,
782808 skip_volume = True ,
783809 persistent_volume = None ,
784810 ephemeral_volume = None ,
0 commit comments