diff --git a/moto/ec2/responses/fleets.py b/moto/ec2/responses/fleets.py index b59f8ec5113b..03163aad7309 100644 --- a/moto/ec2/responses/fleets.py +++ b/moto/ec2/responses/fleets.py @@ -68,6 +68,8 @@ def create_fleet(self) -> ActionResult: tag_specifications = self._get_param("TagSpecifications", []) + self.error_on_dryrun() + request = self.ec2_backend.create_fleet( on_demand_options=on_demand_options, spot_options=spot_options, diff --git a/tests/test_ec2/test_fleets.py b/tests/test_ec2/test_fleets.py index d22ab85624e4..f3d15bc87247 100644 --- a/tests/test_ec2/test_fleets.py +++ b/tests/test_ec2/test_fleets.py @@ -2,10 +2,12 @@ import boto3 import pytest +from botocore.client import ClientError from moto import mock_aws from moto.core.types import Base64EncodedString from tests import EXAMPLE_AMI_ID +from tests.test_ec2.helpers import assert_dryrun_error from . import ec2_aws_verified @@ -68,6 +70,52 @@ def test_launch_template_is_created_properly(ec2_client=None): assert template["LatestVersionNumber"] == 1 +@pytest.mark.aws_verified +@ec2_aws_verified() +def test_create_fleet_dryrun(ec2_client=None): + with launch_template_context() as ctxt: + # Attempting to create a fleet with DryRun=True should raise an error + with pytest.raises(ClientError) as ex: + fleets_before = len(ctxt.ec2.describe_fleets()["Fleets"]) + reservations_before = len(ctxt.ec2.describe_instances()["Reservations"]) + + ctxt.ec2.create_fleet( + DryRun=True, + ExcessCapacityTerminationPolicy="terminate", + LaunchTemplateConfigs=[ + { + "LaunchTemplateSpecification": { + "LaunchTemplateId": ctxt.lt_id, + "Version": "1", + }, + }, + ], + TargetCapacitySpecification={ + "DefaultTargetCapacityType": "spot", + "OnDemandTargetCapacity": 0, + "SpotTargetCapacity": 1, + "TotalTargetCapacity": 1, + }, + SpotOptions={ + "AllocationStrategy": "lowest-price", + }, + Type="maintain", + ValidFrom="2020-01-01T00:00:00Z", + ValidUntil="2020-12-31T00:00:00Z", + ) + + # Verify the error is the expected DryRun error + assert_dryrun_error(ex) + + # Verify no fleets were created + fleets_res = ctxt.ec2.describe_fleets() + assert len(fleets_res["Fleets"]) == fleets_before + + # Verify no instances were created + instances_res = ctxt.ec2.describe_instances() + assert len(instances_res["Reservations"]) == reservations_before + + @mock_aws def test_create_spot_fleet_with_lowest_price(): conn = boto3.client("ec2", region_name="us-west-2")