def setUp(self, get_client_type_mock): self.job_id = "8ba9d676-4108-4474-9dca-8bbac1da9b19" self.region_name = AWS_REGION self.batch_waiters = AwsBatchWaitersHook(region_name=self.region_name) assert self.batch_waiters.aws_conn_id == 'aws_default' assert self.batch_waiters.region_name == self.region_name # init the mock client self.client_mock = self.batch_waiters.client get_client_type_mock.assert_called_once_with("batch", region_name=self.region_name) # don't pause in these unit tests self.mock_delay = mock.Mock(return_value=None) self.batch_waiters.delay = self.mock_delay self.mock_exponential_delay = mock.Mock(return_value=0) self.batch_waiters.exponential_delay = self.mock_exponential_delay
def test_aws_batch_job_waiting(aws_clients, aws_region, job_queue_name, job_definition_name): """ Submit batch jobs and wait for various job status indicators or errors. These batch job waiter tests can be slow and might need to be marked for conditional skips if they take too long, although it seems to run in about 30 sec to a minute. .. note:: These tests have no control over how moto transitions the batch job status. .. seealso:: - https://github.com/boto/botocore/blob/develop/botocore/waiter.py - https://github.com/spulec/moto/blob/master/moto/batch/models.py#L360 - https://github.com/spulec/moto/blob/master/tests/test_batch/test_batch.py """ aws_resources = batch_infrastructure(aws_clients, aws_region, job_queue_name, job_definition_name) batch_waiters = AwsBatchWaitersHook(region_name=aws_resources.aws_region) job_exists_waiter = batch_waiters.get_waiter("JobExists") assert job_exists_waiter assert isinstance(job_exists_waiter, botocore.waiter.Waiter) assert job_exists_waiter.__class__.__name__ == "Batch.Waiter.JobExists" job_running_waiter = batch_waiters.get_waiter("JobRunning") assert job_running_waiter assert isinstance(job_running_waiter, botocore.waiter.Waiter) assert job_running_waiter.__class__.__name__ == "Batch.Waiter.JobRunning" job_complete_waiter = batch_waiters.get_waiter("JobComplete") assert job_complete_waiter assert isinstance(job_complete_waiter, botocore.waiter.Waiter) assert job_complete_waiter.__class__.__name__ == "Batch.Waiter.JobComplete" # test waiting on a jobId that does not exist (this throws immediately) with pytest.raises(botocore.exceptions.WaiterError) as ctx: job_exists_waiter.config.delay = 0.2 job_exists_waiter.config.max_attempts = 2 job_exists_waiter.wait(jobs=["missing-job"]) assert isinstance(ctx.value, botocore.exceptions.WaiterError) assert "Waiter JobExists failed" in str(ctx.value) # Submit a job and wait for various job status indicators; # moto transitions the batch job status automatically. job_name = "test-job" job_cmd = [ '/bin/sh -c "for a in `seq 1 2`; do echo Hello World; sleep 0.25; done"' ] job_response = aws_clients.batch.submit_job( jobName=job_name, jobQueue=aws_resources.job_queue_arn, jobDefinition=aws_resources.job_definition_arn, containerOverrides={"command": job_cmd}, ) job_id = job_response["jobId"] job_description = aws_clients.batch.describe_jobs(jobs=[job_id]) job_status = [ job for job in job_description["jobs"] if job["jobId"] == job_id ][0]["status"] assert job_status == "PENDING" # this should not raise a WaiterError and note there is no 'state' maintained in # the waiter that can be checked after calling wait() and it has no return value; # see https://github.com/boto/botocore/blob/develop/botocore/waiter.py#L287 job_exists_waiter.config.delay = 0.2 job_exists_waiter.config.max_attempts = 20 job_exists_waiter.wait(jobs=[job_id]) # test waiting for job completion with too few attempts (possibly before job is running) job_complete_waiter.config.delay = 0.1 job_complete_waiter.config.max_attempts = 1 with pytest.raises(botocore.exceptions.WaiterError) as ctx: job_complete_waiter.wait(jobs=[job_id]) assert isinstance(ctx.value, botocore.exceptions.WaiterError) assert "Waiter JobComplete failed: Max attempts exceeded" in str(ctx.value) # wait for job to be running (or complete) job_running_waiter.config.delay = 0.25 # sec delays between status checks job_running_waiter.config.max_attempts = 50 job_running_waiter.wait(jobs=[job_id]) # wait for job completion job_complete_waiter.config.delay = 0.25 job_complete_waiter.config.max_attempts = 50 job_complete_waiter.wait(jobs=[job_id]) job_description = aws_clients.batch.describe_jobs(jobs=[job_id]) job_status = [ job for job in job_description["jobs"] if job["jobId"] == job_id ][0]["status"] assert job_status == "SUCCEEDED"
def test_aws_batch_waiters(aws_region): assert inspect.isclass(AwsBatchWaitersHook) batch_waiters = AwsBatchWaitersHook(region_name=aws_region) assert isinstance(batch_waiters, AwsBatchWaitersHook)