I was able to mock AWS-Batch with python, moto, so I will leave it

Introduction

I mocked AWS-Batch using moto, and I was able to register the job, so I will leave it as a memorandum.

mock target resource

code

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()

Execution result

$ 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.

At the end

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

I was able to mock AWS-Batch with python, moto, so I will leave it
I was able to repeat it in Python: lambda
I made a segment tree with python, so I will introduce it
I want to do it with Python lambda Django, but I will stop
I tried to make a calculator with Tkinter so I will write it
Use Python from Java with Jython. I was also addicted to it.
I was able to recurse in Python: lambda
I customized it with Visual Studio Code (mainly for python), so I will summarize it
I was addicted to scraping with Selenium (+ Python) in 2020
A beginner tried coloring line art with chainer. I was able to do it.
I want to be able to analyze data with Python (Part 3)
[IOS] GIF animation with Pythonista3. I was addicted to it.
I was able to implement web app authentication with flask-login
I want to be able to analyze data with Python (Part 4)
I want to be able to analyze data with Python (Part 2)
PyTorch's book was difficult to understand, so I supplemented it
It's Halloween so I'll try to hide it with Python
[Qualification] I passed LinuC Level 1, so I will write about how to study and how it was.
There was a doppelganger, so I tried to distinguish it with artificial intelligence (laughs) (Part 1)
I want to debug with Python
What I was addicted to with json.dumps in Python base64 encoding
Homework was a pain, so I do management accounting with Python
I was able to print the thermal printer "PAPERANG" from Python (Windows10, Python3.6)
I want to tweet on Twitter with Python, but I'm addicted to it
When I tried to run Python, it was skipped to the Microsoft Store
I was addicted to creating a Python venv environment with VS Code
I wanted to solve ABC160 with Python
GCP's Cloud Shell Editor was clogged up with the Python development environment, so I managed to rebuild it in a vacant lot.
Every time I draw a graph with python, I check it, so I will summarize only the simplest usage
I want to analyze logs with Python
I want to play with aws with python
I researched Docker, so I will summarize it
What I was addicted to Python autorun
[Zaif] I tried to make it easy to trade virtual currencies with Python
I wanted to solve ABC172 with Python
I was addicted to trying Cython with PyCharm, so make a note
In IPython, when I tried to see the value, it was a generator, so I came up with it when I was frustrated.
When I made a treemap (area graph) with python, it was subtle, so when I used flourish, it felt pretty good.
The file edited with vim was readonly but I want to save it
Somehow the code I wrote worked and I was impressed, so I will post it
When I tried to create a virtual environment with Python, it didn't work
Three things I was addicted to when using Python and MySQL with Docker
Docker x visualization didn't work and I was addicted to it, so I summarized it!
A note I was addicted to when running Python with Visual Studio Code
A story that I was addicted to when I made SFTP communication with python
I set up TensowFlow and was addicted to it, so make a note
[Python] I introduced Word2Vec and played with it.
I want to use MATLAB feval with python
[Introduction to json] No, I was addicted to it. .. .. ♬
I want to mock datetime.datetime.now () even with pytest!
I want to make a game with Python
I wanted to install Python 3.4.3 with Homebrew + pyenv
I tried to get CloudWatch data with Python
I tried to output LLVM IR with Python
How to specify attributes with Mock of python
I want to use Temporary Directory with Python2
I tried to automate sushi making with python
#Unresolved I want to compile gobject-introspection with Python3
I want to solve APG4b with Python (Chapter 2)
[Python] I will upload the FTP to the FTP server.
I want to write to a file with Python