From ad5314ad0686e76353eed6415267ae355bd4d795 Mon Sep 17 00:00:00 2001 From: mzgierski Date: Wed, 15 May 2019 17:04:31 +0200 Subject: [PATCH 1/4] Enable the test that AWS-Batch describe_jobs fails at. --- tests/test_batch/test_batch.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_batch/test_batch.py b/tests/test_batch/test_batch.py index 141d6b343..de4b349e0 100644 --- a/tests/test_batch/test_batch.py +++ b/tests/test_batch/test_batch.py @@ -692,7 +692,8 @@ def test_submit_job_by_name(): # SLOW TESTS -@expected_failure + +# @expected_failure @mock_logs @mock_ec2 @mock_ecs From bfeaf73109c8f5c1712badb8f785022e357bf95b Mon Sep 17 00:00:00 2001 From: mzgierski Date: Wed, 15 May 2019 17:05:45 +0200 Subject: [PATCH 2/4] Fix the AWS-Batch describe_jobs problem with not-yet-started jobs. --- moto/batch/models.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/moto/batch/models.py b/moto/batch/models.py index fc35f2997..a5986b7a4 100644 --- a/moto/batch/models.py +++ b/moto/batch/models.py @@ -338,10 +338,11 @@ class Job(threading.Thread, BaseModel): "jobId": self.job_id, "jobName": self.job_name, "jobQueue": self.job_queue.arn, - "startedAt": datetime2int(self.job_started_at), "status": self.job_state, "dependsOn": [], } + if result['status'] not in ['SUBMITTED', 'PENDING', 'RUNNABLE', 'STARTING']: + result['startedAt'] = datetime2int(self.job_started_at) if self.job_stopped: result["stoppedAt"] = datetime2int(self.job_stopped_at) result["container"] = {} From bb5a54ca4b42a77f98c276009552087a3926a031 Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Thu, 12 Mar 2020 13:37:46 +0000 Subject: [PATCH 3/4] Batch - Fix tests --- moto/batch/models.py | 6 ++++-- tests/test_batch/test_batch.py | 37 ++++++++-------------------------- 2 files changed, 12 insertions(+), 31 deletions(-) diff --git a/moto/batch/models.py b/moto/batch/models.py index a5986b7a4..08f4cbdb2 100644 --- a/moto/batch/models.py +++ b/moto/batch/models.py @@ -301,7 +301,7 @@ class Job(threading.Thread, BaseModel): self.job_name = name self.job_id = str(uuid.uuid4()) self.job_definition = job_def - self.container_overrides = container_overrides + self.container_overrides = container_overrides or {} self.job_queue = job_queue self.job_state = "SUBMITTED" # One of SUBMITTED | PENDING | RUNNABLE | STARTING | RUNNING | SUCCEEDED | FAILED self.job_queue.jobs.append(self) @@ -317,6 +317,7 @@ class Job(threading.Thread, BaseModel): self.docker_client = docker.from_env() self._log_backend = log_backend + self.log_stream_name = None # Unfortunately mocking replaces this method w/o fallback enabled, so we # need to replace it if we detect it's been mocked @@ -504,7 +505,8 @@ class Job(threading.Thread, BaseModel): for line in logs_stdout + logs_stderr: date, line = line.split(" ", 1) date = dateutil.parser.parse(date) - date = int(date.timestamp()) + # TODO: Replace with int(date.timestamp()) once we yeet Python2 out of the window + date = int((time.mktime(date.timetuple()) + date.microsecond / 1000000.0)) logs.append({"timestamp": date, "message": line.strip()}) # Send to cloudwatch diff --git a/tests/test_batch/test_batch.py b/tests/test_batch/test_batch.py index de4b349e0..6eedf452c 100644 --- a/tests/test_batch/test_batch.py +++ b/tests/test_batch/test_batch.py @@ -10,17 +10,6 @@ import functools import nose -def expected_failure(test): - @functools.wraps(test) - def inner(*args, **kwargs): - try: - test(*args, **kwargs) - except Exception as err: - raise nose.SkipTest - - return inner - - DEFAULT_REGION = "eu-central-1" @@ -693,7 +682,6 @@ def test_submit_job_by_name(): # SLOW TESTS -# @expected_failure @mock_logs @mock_ec2 @mock_ecs @@ -721,13 +709,13 @@ def test_submit_job(): queue_arn = resp["jobQueueArn"] resp = batch_client.register_job_definition( - jobDefinitionName="sleep10", + jobDefinitionName="sayhellotomylittlefriend", type="container", containerProperties={ - "image": "busybox", + "image": "busybox:latest", "vcpus": 1, "memory": 128, - "command": ["sleep", "10"], + "command": ["echo", "hello"], }, ) job_def_arn = resp["jobDefinitionArn"] @@ -741,13 +729,6 @@ def test_submit_job(): while datetime.datetime.now() < future: resp = batch_client.describe_jobs(jobs=[job_id]) - print( - "{0}:{1} {2}".format( - resp["jobs"][0]["jobName"], - resp["jobs"][0]["jobId"], - resp["jobs"][0]["status"], - ) - ) if resp["jobs"][0]["status"] == "FAILED": raise RuntimeError("Batch job failed") @@ -764,10 +745,9 @@ def test_submit_job(): resp = logs_client.get_log_events( logGroupName="/aws/batch/job", logStreamName=ls_name ) - len(resp["events"]).should.be.greater_than(5) + [event['message'] for event in resp["events"]].should.equal(['hello']) -@expected_failure @mock_logs @mock_ec2 @mock_ecs @@ -795,13 +775,13 @@ def test_list_jobs(): queue_arn = resp["jobQueueArn"] resp = batch_client.register_job_definition( - jobDefinitionName="sleep10", + jobDefinitionName="sleep5", type="container", containerProperties={ - "image": "busybox", + "image": "busybox:latest", "vcpus": 1, "memory": 128, - "command": ["sleep", "10"], + "command": ["sleep", "5"], }, ) job_def_arn = resp["jobDefinitionArn"] @@ -844,7 +824,6 @@ def test_list_jobs(): len(resp_finished_jobs2["jobSummaryList"]).should.equal(2) -@expected_failure @mock_logs @mock_ec2 @mock_ecs @@ -875,7 +854,7 @@ def test_terminate_job(): jobDefinitionName="sleep10", type="container", containerProperties={ - "image": "busybox", + "image": "busybox:latest", "vcpus": 1, "memory": 128, "command": ["sleep", "10"], From 1b031aeeb0a2816bc153d64d73d47251ec642465 Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Thu, 12 Mar 2020 14:07:34 +0000 Subject: [PATCH 4/4] Linting --- moto/batch/models.py | 8 +++++--- tests/test_batch/test_batch.py | 3 ++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/moto/batch/models.py b/moto/batch/models.py index 08f4cbdb2..95ad64789 100644 --- a/moto/batch/models.py +++ b/moto/batch/models.py @@ -342,8 +342,8 @@ class Job(threading.Thread, BaseModel): "status": self.job_state, "dependsOn": [], } - if result['status'] not in ['SUBMITTED', 'PENDING', 'RUNNABLE', 'STARTING']: - result['startedAt'] = datetime2int(self.job_started_at) + if result["status"] not in ["SUBMITTED", "PENDING", "RUNNABLE", "STARTING"]: + result["startedAt"] = datetime2int(self.job_started_at) if self.job_stopped: result["stoppedAt"] = datetime2int(self.job_stopped_at) result["container"] = {} @@ -506,7 +506,9 @@ class Job(threading.Thread, BaseModel): date, line = line.split(" ", 1) date = dateutil.parser.parse(date) # TODO: Replace with int(date.timestamp()) once we yeet Python2 out of the window - date = int((time.mktime(date.timetuple()) + date.microsecond / 1000000.0)) + date = int( + (time.mktime(date.timetuple()) + date.microsecond / 1000000.0) + ) logs.append({"timestamp": date, "message": line.strip()}) # Send to cloudwatch diff --git a/tests/test_batch/test_batch.py b/tests/test_batch/test_batch.py index 6eedf452c..4b75fb857 100644 --- a/tests/test_batch/test_batch.py +++ b/tests/test_batch/test_batch.py @@ -682,6 +682,7 @@ def test_submit_job_by_name(): # SLOW TESTS + @mock_logs @mock_ec2 @mock_ecs @@ -745,7 +746,7 @@ def test_submit_job(): resp = logs_client.get_log_events( logGroupName="/aws/batch/job", logStreamName=ls_name ) - [event['message'] for event in resp["events"]].should.equal(['hello']) + [event["message"] for event in resp["events"]].should.equal(["hello"]) @mock_logs