From 59ce86c5c7419cf9fa261c860fe4c4c561f023a7 Mon Sep 17 00:00:00 2001 From: rafcio19 Date: Thu, 21 Mar 2024 21:23:26 +0100 Subject: [PATCH] Batch: improve parameter validation (#7505) --- moto/batch/models.py | 6 ++++++ tests/test_batch/test_batch_compute_envs.py | 19 +++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/moto/batch/models.py b/moto/batch/models.py index 59c6eb955..a7a35fd08 100644 --- a/moto/batch/models.py +++ b/moto/batch/models.py @@ -1219,6 +1219,12 @@ class BatchBackend(BaseBackend): f"A compute environment already exists with the name {compute_environment_name}" ) + if not service_role: + raise ClientException( + f"Error executing request, Exception : ServiceRole is required.," + f" RequestId: {mock_random.uuid4()}" + ) + # Look for IAM role try: self.iam_backend.get_role_by_arn(service_role) diff --git a/tests/test_batch/test_batch_compute_envs.py b/tests/test_batch/test_batch_compute_envs.py index 7443d73f7..43129da92 100644 --- a/tests/test_batch/test_batch_compute_envs.py +++ b/tests/test_batch/test_batch_compute_envs.py @@ -374,9 +374,11 @@ 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) + env_name = "ec2-env" + with pytest.raises(ClientError) as exc: batch_client.create_compute_environment( - computeEnvironmentName="ec2-env", + computeEnvironmentName=env_name, type="MANAGED", state="ENABLED", computeResources={"type": "EC2", "maxvCpus": 1, "subnets": [subnet_id]}, @@ -391,7 +393,7 @@ def test_create_ec2_managed_compute_environment__without_required_params(): with pytest.raises(ClientError) as exc: batch_client.create_compute_environment( - computeEnvironmentName="ec2-env", + computeEnvironmentName=env_name, type="MANAGED", state="ENABLED", computeResources={ @@ -408,3 +410,16 @@ def test_create_ec2_managed_compute_environment__without_required_params(): "Error executing request, Exception : Resource minvCpus is required." in err["Message"] ) + + with pytest.raises(ClientError) as exc: + batch_client.create_compute_environment( + computeEnvironmentName=env_name, + type="UNMANGED", + state="ENABLED", + ) + err = exc.value.response["Error"] + assert err["Code"] == "ClientException" + assert ( + "Error executing request, Exception : ServiceRole is required.," + in err["Message"] + )