Skip to content

Commit 3198758

Browse files
committed
adjust
1 parent 6953507 commit 3198758

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

packages/aws-library/tests/test_ec2_client.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,74 @@ async def test_stop_start_instances(
418418
else:
419419
assert getattr(s, f.name) == getattr(c, f.name)
420420

421+
# stop them now
422+
await simcore_ec2_api.stop_instances(created_instances)
423+
await _assert_instances_in_ec2(
424+
ec2_client,
425+
expected_num_reservations=1,
426+
expected_num_instances=num_instances,
427+
expected_instance_type=ec2_instance_config.type,
428+
expected_tags=ec2_instance_config.tags,
429+
expected_state="stopped",
430+
)
431+
432+
433+
async def test_start_instances_with_insufficient_instance_capacity(
434+
simcore_ec2_api: SimcoreEC2API,
435+
ec2_client: EC2Client,
436+
faker: Faker,
437+
ec2_instance_config: EC2InstanceConfig,
438+
mocker: MockerFixture,
439+
):
440+
# we have nothing running now in ec2
441+
await _assert_no_instances_in_ec2(ec2_client)
442+
# create some instance
443+
_NUM_INSTANCES = 10
444+
num_instances = faker.pyint(min_value=1, max_value=_NUM_INSTANCES)
445+
created_instances = await simcore_ec2_api.launch_instances(
446+
ec2_instance_config,
447+
min_number_of_instances=num_instances,
448+
number_of_instances=num_instances,
449+
)
450+
await _assert_instances_in_ec2(
451+
ec2_client,
452+
expected_num_reservations=1,
453+
expected_num_instances=num_instances,
454+
expected_instance_type=ec2_instance_config.type,
455+
expected_tags=ec2_instance_config.tags,
456+
expected_state="running",
457+
)
458+
# stop the instances
459+
await simcore_ec2_api.stop_instances(created_instances)
460+
await _assert_instances_in_ec2(
461+
ec2_client,
462+
expected_num_reservations=1,
463+
expected_num_instances=num_instances,
464+
expected_instance_type=ec2_instance_config.type,
465+
expected_tags=ec2_instance_config.tags,
466+
expected_state="stopped",
467+
)
468+
469+
# Mock the EC2 client to simulate InsufficientInstanceCapacity on first subnet
470+
async def mock_start_instances(*args, **kwargs) -> Any:
471+
# no more machines, simulate insufficient capacity
472+
error_response: dict[str, Any] = {
473+
"Error": {
474+
"Code": "InsufficientInstanceCapacity",
475+
"Message": "An error occurred (InsufficientInstanceCapacity) when calling the StartInstances operation (reached max retries: 4): We currently do not have sufficient g4dn.4xlarge capacity in the Availability Zone you requested (us-east-1a). Our system will be working on provisioning additional capacity. You can currently get g4dn.4xlarge capacity by not specifying an Availability Zone in your request or choosing us-east-1b, us-east-1c, us-east-1d, us-east-1f",
476+
},
477+
}
478+
raise botocore.exceptions.ClientError(error_response, "StartInstances") # type: ignore
479+
480+
# Apply the mock
481+
mocker.patch.object(
482+
simcore_ec2_api.client, "start_instances", side_effect=mock_start_instances
483+
)
484+
485+
# start the instances now
486+
with pytest.raises(EC2InsufficientCapacityError):
487+
await simcore_ec2_api.start_instances(created_instances)
488+
421489

422490
async def test_terminate_instance(
423491
simcore_ec2_api: SimcoreEC2API,

0 commit comments

Comments
 (0)