Skip to content

Commit 8ef1d36

Browse files
ec2: Add dry run error handling for fleet creation
1 parent 1922cdd commit 8ef1d36

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

moto/ec2/responses/fleets.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ def create_fleet(self) -> ActionResult:
6868

6969
tag_specifications = self._get_param("TagSpecifications", [])
7070

71+
self.error_on_dryrun()
72+
7173
request = self.ec2_backend.create_fleet(
7274
on_demand_options=on_demand_options,
7375
spot_options=spot_options,

tests/test_ec2/test_fleets.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import boto3
44
import pytest
5+
from botocore.client import ClientError
56

67
from moto import mock_aws
78
from moto.core.types import Base64EncodedString
89
from tests import EXAMPLE_AMI_ID
10+
from tests.test_ec2.helpers import assert_dryrun_error
911

1012
from . import ec2_aws_verified
1113

@@ -68,6 +70,52 @@ def test_launch_template_is_created_properly(ec2_client=None):
6870
assert template["LatestVersionNumber"] == 1
6971

7072

73+
@pytest.mark.aws_verified
74+
@ec2_aws_verified()
75+
def test_create_fleet_dryrun(ec2_client=None):
76+
with launch_template_context() as ctxt:
77+
# Attempting to create a fleet with DryRun=True should raise an error
78+
with pytest.raises(ClientError) as ex:
79+
fleets_before = len(ctxt.ec2.describe_fleets()["Fleets"])
80+
reservations_before = len(ctxt.ec2.describe_instances()["Reservations"])
81+
82+
ctxt.ec2.create_fleet(
83+
DryRun=True,
84+
ExcessCapacityTerminationPolicy="terminate",
85+
LaunchTemplateConfigs=[
86+
{
87+
"LaunchTemplateSpecification": {
88+
"LaunchTemplateId": ctxt.lt_id,
89+
"Version": "1",
90+
},
91+
},
92+
],
93+
TargetCapacitySpecification={
94+
"DefaultTargetCapacityType": "spot",
95+
"OnDemandTargetCapacity": 0,
96+
"SpotTargetCapacity": 1,
97+
"TotalTargetCapacity": 1,
98+
},
99+
SpotOptions={
100+
"AllocationStrategy": "lowest-price",
101+
},
102+
Type="maintain",
103+
ValidFrom="2020-01-01T00:00:00Z",
104+
ValidUntil="2020-12-31T00:00:00Z",
105+
)
106+
107+
# Verify the error is the expected DryRun error
108+
assert_dryrun_error(ex)
109+
110+
# Verify no fleets were created
111+
fleets_res = ctxt.ec2.describe_fleets()
112+
assert len(fleets_res["Fleets"]) == fleets_before
113+
114+
# Verify no instances were created
115+
instances_res = ctxt.ec2.describe_instances()
116+
assert len(instances_res["Reservations"]) == reservations_before
117+
118+
71119
@mock_aws
72120
def test_create_spot_fleet_with_lowest_price():
73121
conn = boto3.client("ec2", region_name="us-west-2")

0 commit comments

Comments
 (0)