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
This commit is contained in:
Adam Richie-Halford 2020-07-14 03:29:49 -07:00 committed by GitHub
parent f31f8e08c1
commit 890c3b4954
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 1 deletions

View File

@ -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

View File

@ -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")