From 8e35eedc3d3abb85a6a4b618baaab088056921c4 Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Sat, 1 Jul 2023 10:33:21 +0000 Subject: [PATCH] Batch: create_compute_environment() now validates instanceRole and minvCpu (#6470) --- moto/batch/models.py | 8 ++++ tests/test_batch/test_batch_compute_envs.py | 44 +++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/moto/batch/models.py b/moto/batch/models.py index 5aaeab2a1..31e782ca2 100644 --- a/moto/batch/models.py +++ b/moto/batch/models.py @@ -1272,6 +1272,10 @@ class BatchBackend(BaseBackend): if "FARGATE" not in cr["type"]: # Most parameters are not applicable to jobs that are running on Fargate resources: # non exhaustive list: minvCpus, instanceTypes, imageId, ec2KeyPair, instanceRole, tags + if "instanceRole" not in cr: + raise ClientException( + "Error executing request, Exception : Instance role is required." + ) for profile in self.iam_backend.get_instance_profiles(): if profile.arn == cr["instanceRole"]: break @@ -1280,6 +1284,10 @@ class BatchBackend(BaseBackend): f"could not find instanceRole {cr['instanceRole']}" ) + if "minvCpus" not in cr: + raise ClientException( + "Error executing request, Exception : Resource minvCpus is required." + ) if int(cr["minvCpus"]) < 0: raise InvalidParameterValueException("minvCpus must be positive") if int(cr["maxvCpus"]) < int(cr["minvCpus"]): diff --git a/tests/test_batch/test_batch_compute_envs.py b/tests/test_batch/test_batch_compute_envs.py index e493d7754..1c016cbbb 100644 --- a/tests/test_batch/test_batch_compute_envs.py +++ b/tests/test_batch/test_batch_compute_envs.py @@ -395,3 +395,47 @@ def test_create_fargate_managed_compute_environment(compute_env_type): # Should have created 1 ECS cluster all_clusters = ecs_client.list_clusters()["clusterArns"] assert our_env["ecsClusterArn"] in all_clusters + + +@mock_ec2 +@mock_ecs +@mock_iam +@mock_batch +def test_create_ec2_managed_compute_environment__without_required_params(): + ec2_client, iam_client, _, _, batch_client = _get_clients() + _, subnet_id, _, iam_arn = _setup(ec2_client, iam_client) + + with pytest.raises(ClientError) as exc: + batch_client.create_compute_environment( + computeEnvironmentName="ec2-env", + type="MANAGED", + state="ENABLED", + computeResources={"type": "EC2", "maxvCpus": 1, "subnets": [subnet_id]}, + serviceRole=iam_arn, + ) + err = exc.value.response["Error"] + assert err["Code"] == "ClientException" + assert ( + "Error executing request, Exception : Instance role is required." + in err["Message"] + ) + + with pytest.raises(ClientError) as exc: + batch_client.create_compute_environment( + computeEnvironmentName="ec2-env", + type="MANAGED", + state="ENABLED", + computeResources={ + "type": "EC2", + "maxvCpus": 1, + "subnets": [subnet_id], + "instanceRole": iam_arn.replace("role", "instance-profile"), + }, + serviceRole=iam_arn, + ) + err = exc.value.response["Error"] + assert err["Code"] == "ClientException" + assert ( + "Error executing request, Exception : Resource minvCpus is required." + in err["Message"] + )