From 890c3b4954a864f5362dc8d8645b8d0751de9331 Mon Sep 17 00:00:00 2001 From: Adam Richie-Halford Date: Tue, 14 Jul 2020 03:29:49 -0700 Subject: [PATCH] Make batch.utils.lowercase_first_key() recursive (#3124) * Make batch.utils.lowercase_first_key() recursive * Reformat using black * Add test of recursive lowercase_first_key() * Fix typo in ttest_batch/test_cloud_formation.py --- moto/batch/utils.py | 10 +++++++++- tests/test_batch/test_cloudformation.py | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/moto/batch/utils.py b/moto/batch/utils.py index ce9b2ffe8..d9f79e236 100644 --- a/moto/batch/utils.py +++ b/moto/batch/utils.py @@ -21,6 +21,14 @@ def lowercase_first_key(some_dict): new_dict = {} for key, value in some_dict.items(): new_key = key[0].lower() + key[1:] - new_dict[new_key] = value + try: + if isinstance(value, dict): + new_dict[new_key] = lowercase_first_key(value) + elif all([isinstance(v, dict) for v in value]): + new_dict[new_key] = [lowercase_first_key(v) for v in value] + else: + new_dict[new_key] = value + except TypeError: + new_dict[new_key] = value return new_dict diff --git a/tests/test_batch/test_cloudformation.py b/tests/test_batch/test_cloudformation.py index a6baedb38..cc51b79f3 100644 --- a/tests/test_batch/test_cloudformation.py +++ b/tests/test_batch/test_cloudformation.py @@ -234,6 +234,7 @@ def test_create_job_def_cf(): "Vcpus": 2, "Memory": 2000, "Command": ["echo", "Hello world"], + "LinuxParameters": {"Devices": [{"HostPath": "test-path"}]}, }, "RetryStrategy": {"Attempts": 1}, }, @@ -262,3 +263,17 @@ def test_create_job_def_cf(): job_def_resource["PhysicalResourceId"].startswith("arn:aws:batch:") job_def_resource["PhysicalResourceId"].should.contain("test_stack-JobDef") job_def_resource["PhysicalResourceId"].should.contain("job-definition/") + + # Test the linux parameter device host path + # This ensures that batch is parsing the parameter dictionaries + # correctly by recursively converting the first character of all + # dict keys to lowercase. + batch_conn = boto3.client("batch", DEFAULT_REGION) + response = batch_conn.describe_job_definitions( + jobDefinitions=[job_def_resource["PhysicalResourceId"]] + ) + job_def_linux_device_host_path = response.get("jobDefinitions")[0][ + "containerProperties" + ]["linuxParameters"]["devices"][0]["hostPath"] + + job_def_linux_device_host_path.should.equal("test-path")