From 8b0a6f3d27d70cbca7ec8dcf03510e60b05a6872 Mon Sep 17 00:00:00 2001 From: Brian Pandola Date: Mon, 22 Nov 2021 16:47:35 -0800 Subject: [PATCH] Allow batch job definition tags to be updated (#4620) Fixes #4618 --- moto/batch/models.py | 9 +++--- tests/test_batch/test_batch_jobs.py | 44 +++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/moto/batch/models.py b/moto/batch/models.py index f6c0b9d55..fa62305c7 100644 --- a/moto/batch/models.py +++ b/moto/batch/models.py @@ -295,7 +295,7 @@ class JobDefinition(CloudFormationModel): if vcpus < 1: raise ClientException("container vcpus limit must be greater than 0") - def update(self, parameters, _type, container_properties, retry_strategy): + def update(self, parameters, _type, container_properties, retry_strategy, tags): if parameters is None: parameters = self.parameters @@ -316,6 +316,7 @@ class JobDefinition(CloudFormationModel): region_name=self._region, revision=self.revision, retry_strategy=retry_strategy, + tags=tags, ) def describe(self): @@ -1276,9 +1277,9 @@ class BatchBackend(BaseBackend): retry_strategy = retry_strategy["attempts"] except Exception: raise ClientException("retryStrategy is malformed") + if not tags: + tags = {} if job_def is None: - if not tags: - tags = {} job_def = JobDefinition( def_name, parameters, @@ -1291,7 +1292,7 @@ class BatchBackend(BaseBackend): else: # Make new jobdef job_def = job_def.update( - parameters, _type, container_properties, retry_strategy + parameters, _type, container_properties, retry_strategy, tags ) self._job_definitions[job_def.arn] = job_def diff --git a/tests/test_batch/test_batch_jobs.py b/tests/test_batch/test_batch_jobs.py index ade9b4261..5abe209ad 100644 --- a/tests/test_batch/test_batch_jobs.py +++ b/tests/test_batch/test_batch_jobs.py @@ -667,3 +667,47 @@ def prepare_job(batch_client, commands, iam_arn, job_def_name): ) job_def_arn = resp["jobDefinitionArn"] return job_def_arn, queue_arn + + +@mock_batch +def test_update_job_definition(): + _, _, _, _, batch_client = _get_clients() + + tags = [ + {"Foo1": "bar1", "Baz1": "buzz1"}, + {"Foo2": "bar2", "Baz2": "buzz2"}, + ] + + container_props = { + "image": "amazonlinux", + "memory": 1024, + "vcpus": 2, + } + + batch_client.register_job_definition( + jobDefinitionName="test-job", + type="container", + tags=tags[0], + parameters={}, + containerProperties=container_props, + ) + + container_props["memory"] = 2048 + batch_client.register_job_definition( + jobDefinitionName="test-job", + type="container", + tags=tags[1], + parameters={}, + containerProperties=container_props, + ) + + job_defs = batch_client.describe_job_definitions(jobDefinitionName="test-job")[ + "jobDefinitions" + ] + job_defs.should.have.length_of(2) + + job_defs[0]["containerProperties"]["memory"].should.equal(1024) + job_defs[0]["tags"].should.equal(tags[0]) + + job_defs[1]["containerProperties"]["memory"].should.equal(2048) + job_defs[1]["tags"].should.equal(tags[1])