7
7
from unittest .mock import AsyncMock , MagicMock , patch
8
8
9
9
import pytest
10
+ import typer
10
11
from aiohttp import InvalidURL
11
12
from aleph_message .models import Chain , ItemHash
12
13
from aleph_message .models .execution .base import Payment , PaymentType
@@ -380,7 +381,7 @@ def create_mock_vm_coco_client():
380
381
"coco_superfluid_evm" ,
381
382
"gpu_superfluid_evm" ,
382
383
],
383
- argnames = "args, expected" ,
384
+ argnames = "args, expected, should_raise " ,
384
385
argvalues = [
385
386
( # regular_hold_evm
386
387
{
@@ -392,6 +393,7 @@ def create_mock_vm_coco_client():
392
393
"immutable_volume" : [f"mount=/opt/packages,ref={ FAKE_STORE_HASH } " ],
393
394
},
394
395
(FAKE_VM_HASH , None , "ETH" ),
396
+ False ,
395
397
),
396
398
( # regular_superfluid_evm
397
399
{
@@ -401,6 +403,7 @@ def create_mock_vm_coco_client():
401
403
"crn_url" : FAKE_CRN_URL ,
402
404
},
403
405
(FAKE_VM_HASH , FAKE_CRN_URL , "AVAX" ),
406
+ False ,
404
407
),
405
408
( # regular_hold_sol
406
409
{
@@ -409,6 +412,7 @@ def create_mock_vm_coco_client():
409
412
"rootfs" : "debian12" ,
410
413
},
411
414
(FAKE_VM_HASH , None , "SOL" ),
415
+ False ,
412
416
),
413
417
( # coco_hold_sol
414
418
{
@@ -418,7 +422,8 @@ def create_mock_vm_coco_client():
418
422
"crn_url" : FAKE_CRN_URL ,
419
423
"confidential" : True ,
420
424
},
421
- (FAKE_VM_HASH , FAKE_CRN_URL , "SOL" ),
425
+ None ,
426
+ True ,
422
427
),
423
428
( # coco_hold_evm
424
429
{
@@ -428,7 +433,8 @@ def create_mock_vm_coco_client():
428
433
"crn_url" : FAKE_CRN_URL ,
429
434
"confidential" : True ,
430
435
},
431
- (FAKE_VM_HASH , FAKE_CRN_URL , "ETH" ),
436
+ None ,
437
+ True ,
432
438
),
433
439
( # coco_superfluid_evm
434
440
{
@@ -439,6 +445,7 @@ def create_mock_vm_coco_client():
439
445
"confidential" : True ,
440
446
},
441
447
(FAKE_VM_HASH , FAKE_CRN_URL , "BASE" ),
448
+ False ,
442
449
),
443
450
( # gpu_superfluid_evm
444
451
{
@@ -449,11 +456,12 @@ def create_mock_vm_coco_client():
449
456
"gpu" : True ,
450
457
},
451
458
(FAKE_VM_HASH , FAKE_CRN_URL , "BASE" ),
459
+ False ,
452
460
),
453
461
],
454
462
)
455
463
@pytest .mark .asyncio
456
- async def test_create_instance (args , expected ):
464
+ async def test_create_instance (args , expected , should_raise ):
457
465
mock_validate_ssh_pubkey_file = create_mock_validate_ssh_pubkey_file ()
458
466
mock_load_account = create_mock_load_account ()
459
467
mock_account = mock_load_account .return_value
@@ -496,28 +504,34 @@ async def create_instance(instance_spec):
496
504
all_args .update (instance_spec )
497
505
return await create (** all_args )
498
506
499
- returned = await create_instance (args )
500
- # Basic assertions for all cases
501
- mock_load_account .assert_called_once ()
502
- mock_validate_ssh_pubkey_file .return_value .read_text .assert_called_once ()
503
- mock_client .get_estimated_price .assert_called_once ()
504
- mock_auth_client .create_instance .assert_called_once ()
505
- # Payment type specific assertions
506
- if args ["payment_type" ] == "hold" :
507
- mock_get_balance .assert_called_once ()
508
- elif args ["payment_type" ] == "superfluid" :
509
- assert mock_account .manage_flow .call_count == 2
510
- assert mock_wait_for_confirmed_flow .call_count == 2
511
- # CRN related assertions
512
- if args ["payment_type" ] == "superfluid" or args .get ("confidential" ) or args .get ("gpu" ):
513
- mock_fetch_latest_crn_version .assert_called ()
514
- if not args .get ("gpu" ):
515
- mock_fetch_crn_info .assert_called_once ()
516
- else :
517
- mock_crn_table .return_value .run_async .assert_called_once ()
518
- mock_wait_for_processed_instance .assert_called_once ()
519
- mock_vm_client .start_instance .assert_called_once ()
520
- assert returned == expected
507
+ if should_raise :
508
+ with pytest .raises (typer .Exit ) as exc_info :
509
+ await create_instance (args )
510
+ assert exc_info .value .exit_code == 1
511
+
512
+ else :
513
+ returned = await create_instance (args )
514
+ # Basic assertions for all cases
515
+ mock_load_account .assert_called_once ()
516
+ mock_validate_ssh_pubkey_file .return_value .read_text .assert_called_once ()
517
+ mock_client .get_estimated_price .assert_called_once ()
518
+ mock_auth_client .create_instance .assert_called_once ()
519
+ # Payment type specific assertions
520
+ if args ["payment_type" ] == "hold" :
521
+ mock_get_balance .assert_called_once ()
522
+ elif args ["payment_type" ] == "superfluid" :
523
+ assert mock_account .manage_flow .call_count == 2
524
+ assert mock_wait_for_confirmed_flow .call_count == 2
525
+ # CRN related assertions
526
+ if args ["payment_type" ] == "superfluid" or args .get ("confidential" ) or args .get ("gpu" ):
527
+ mock_fetch_latest_crn_version .assert_called ()
528
+ if not args .get ("gpu" ):
529
+ mock_fetch_crn_info .assert_called_once ()
530
+ else :
531
+ mock_crn_table .return_value .run_async .assert_called_once ()
532
+ mock_wait_for_processed_instance .assert_called_once ()
533
+ mock_vm_client .start_instance .assert_called_once ()
534
+ assert returned == expected
521
535
522
536
523
537
@pytest .mark .asyncio
0 commit comments