I mocked AWS-Batch using moto, and I was able to register the job, so I will leave it as a memorandum.
test_aws_batch.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
import boto3
from moto import mock_batch, mock_iam
class MyTestCase(unittest.TestCase):
@mock_iam
@mock_batch
def test_aws_batch(self):
client = boto3.client('batch')
iam = boto3.client('iam')
#mock iam role
iams = iam.create_role(
RoleName='test_matsu_iam',
AssumeRolePolicyDocument='string',
)
iam_arn = iams.get('Role').get('Arn')
print("iamRoleArn: " + iam_arn)
# aws-batch computing environment mock
batch = client.create_compute_environment(
computeEnvironmentName='test_matsu_batch',
type='UNMANAGED',
serviceRole=iam_arn
)
compute_environment_arn = batch.get('computeEnvironmentArn')
print("computeEnvironmentArn: " + compute_environment_arn)
# aws-batch job queue mock
job_qs = client.create_job_queue(
jobQueueName='test_matsu_job_q',
state='ENABLED',
priority=1,
computeEnvironmentOrder=[
{
'order': 1,
'computeEnvironment': compute_environment_arn
},
]
)
job_q_arn = job_qs.get('jobQueueArn')
print("jobQueueArn: " + job_q_arn)
# aws-batch job definition mock
job_definition = client.register_job_definition(
jobDefinitionName='test_matsu_job_definition',
type='container',
containerProperties={
'image': 'string',
'vcpus': 123,
'memory': 123
},
)
job_definition_arn = job_definition.get('jobDefinitionArn')
print("jobDefinitionArn: " + job_definition_arn)
#Add job
client.submit_job(
jobName='string',
jobQueue=job_q_arn,
jobDefinition=job_definition_arn
)
#Get job list
jobs = client.list_jobs(
jobQueue=job_q_arn
)
#Output job list
print("jobSummaryList: " + str(jobs.get('jobSummaryList')))
if __name__ == '__main__':
unittest.main()
$ python -m unittest test.test_aws_batch -v
test_aws_batch (test.test_aws_batch.MyTestCase) ... iamRoleArn: arn:aws:iam::123456789012:role/test_matsu_iam
computeEnvironmentArn: arn:aws:batch:ap-northeast-1:123456789012:compute-environment/test_matsu_batch
jobQueueArn: arn:aws:batch:ap-northeast-1:123456789012:job-queue/test_matsu_job_q
jobDefinitionArn: arn:aws:batch:ap-northeast-1:123456789012:job-definition/test_matsu_job_definition:1
jobSummaryList: [{'jobId': 'ee3a3206-fdfe-404f-a1c1-9c444b41b546', 'jobName': 'string'}]
ok
----------------------------------------------------------------------
Ran 1 test in 0.167s
OK
Various resources were mocked and the job was successfully submitted.
With this mock, it is possible to test submitting jobs to AWS-Batch without using actual AWS resources.
Unfortunately, the content of the submitted job is only jobId
and jobName
.
It is unlikely to be available for tests related to job creation time or status.
The conclusion is that it seems better to use mock
of ʻunit test` for tests that use the submitted jobs.
Recommended Posts