From e020b06016fc2de6127782a1cb69e14b53b9c2f5 Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Tue, 28 Dec 2021 13:02:18 -0100 Subject: [PATCH] Batch:list_jobs() - extend list of return fields (#4727) --- moto/batch/models.py | 26 ++++++++++++++++++-------- moto/batch/responses.py | 6 +----- tests/test_batch/test_batch_jobs.py | 20 ++++++++++++++++++-- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/moto/batch/models.py b/moto/batch/models.py index bcbe85162..436fff1a3 100644 --- a/moto/batch/models.py +++ b/moto/batch/models.py @@ -415,6 +415,7 @@ class Job(threading.Thread, BaseModel, DockerModel): self.all_jobs = all_jobs self.stop = False + self.exit_code = None self.daemon = True self.name = "MOTO-BATCH-" + self.job_id @@ -422,18 +423,28 @@ class Job(threading.Thread, BaseModel, DockerModel): self._log_backend = log_backend self.log_stream_name = None - def describe(self): + def describe_short(self): result = { - "jobDefinition": self.job_definition.arn, "jobId": self.job_id, "jobName": self.job_name, - "jobQueue": self.job_queue.arn, - "status": self.job_state, - "dependsOn": self.depends_on if self.depends_on else [], "createdAt": datetime2int_milliseconds(self.job_created_at), + "status": self.job_state, + "jobDefinition": self.job_definition.arn, } + if self.job_stopped_reason is not None: + result["statusReason"] = self.job_stopped_reason if result["status"] not in ["SUBMITTED", "PENDING", "RUNNABLE", "STARTING"]: result["startedAt"] = datetime2int_milliseconds(self.job_started_at) + if self.job_stopped: + result["stoppedAt"] = datetime2int_milliseconds(self.job_stopped_at) + if self.exit_code is not None: + result["container"] = {"exitCode": self.exit_code} + return result + + def describe(self): + result = self.describe_short() + result["jobQueue"] = self.job_queue.arn + result["dependsOn"] = self.depends_on if self.depends_on else [] if self.job_stopped: result["stoppedAt"] = datetime2int_milliseconds(self.job_stopped_at) result["container"] = {} @@ -452,8 +463,6 @@ class Job(threading.Thread, BaseModel, DockerModel): "environment", [] ) result["container"]["logStreamName"] = self.log_stream_name - if self.job_stopped_reason is not None: - result["statusReason"] = self.job_stopped_reason if self.timeout: result["timeout"] = self.timeout return result @@ -630,7 +639,8 @@ class Job(threading.Thread, BaseModel, DockerModel): self._log_backend.put_log_events(log_group, stream_name, logs, None) result = container.wait() or {} - job_failed = self.stop or result.get("StatusCode", 0) > 0 + self.exit_code = result.get("StatusCode", 0) + job_failed = self.stop or self.exit_code > 0 self._mark_stopped(success=not job_failed) except Exception as err: diff --git a/moto/batch/responses.py b/moto/batch/responses.py index 01cb6d94f..1f86598d7 100644 --- a/moto/batch/responses.py +++ b/moto/batch/responses.py @@ -276,11 +276,7 @@ class BatchResponse(BaseResponse): except AWSError as err: return err.response() - result = { - "jobSummaryList": [ - {"jobId": job.job_id, "jobName": job.job_name} for job in jobs - ] - } + result = {"jobSummaryList": [job.describe_short() for job in jobs]} return json.dumps(result) # TerminateJob diff --git a/tests/test_batch/test_batch_jobs.py b/tests/test_batch/test_batch_jobs.py index 4f0c5cb5b..3ef0d3630 100644 --- a/tests/test_batch/test_batch_jobs.py +++ b/tests/test_batch/test_batch_jobs.py @@ -160,6 +160,14 @@ def test_list_jobs(): ) job_id2 = resp["jobId"] + all_jobs = batch_client.list_jobs(jobQueue=queue_arn)["jobSummaryList"] + all_jobs.should.have.length_of(2) + for job in all_jobs: + job.should.have.key("createdAt") + job.should.have.key("jobDefinition") + job.should.have.key("jobName") + job.should.have.key("status").equals("STARTING") + batch_client.list_jobs(jobQueue=queue_arn, jobStatus="SUCCEEDED")[ "jobSummaryList" ].should.have.length_of(0) @@ -168,9 +176,17 @@ def test_list_jobs(): for job_id in [job_id1, job_id2]: _wait_for_job_status(batch_client, job_id, "SUCCEEDED") - batch_client.list_jobs(jobQueue=queue_arn, jobStatus="SUCCEEDED")[ + succeeded_jobs = batch_client.list_jobs(jobQueue=queue_arn, jobStatus="SUCCEEDED")[ "jobSummaryList" - ].should.have.length_of(2) + ] + succeeded_jobs.should.have.length_of(2) + for job in succeeded_jobs: + job.should.have.key("createdAt") + job.should.have.key("jobDefinition") + job.should.have.key("jobName") + job.should.have.key("status").equals("SUCCEEDED") + job.should.have.key("stoppedAt") + job.should.have.key("container").should.have.key("exitCode").equals(0) @mock_logs