4

I'm using moto to mock out aws services to write my test cases and supported use cases is fine:

@mock_sts
def test_check_aws_profile(self):
    session = boto3.Session(profile_name='foo')
    client = session.client('sts')
    client.get_caller_identity().get('Account')

But there are a few services like es that isn't supported at all. How do I begin to mock this?

I've tried this alternative mocking approach for testing but not sure if there's a better approach

def test_with_some_domains(self, mocker):
    mocked_boto = mocker.patch('mymodule.boto3')
    mocked_session = mocked_boto.Session()
    mocked_client.list_domain_names.return_value = ['foo-bar-baz']
    mymodule.function_call()

mymodule.py 

def function_call():
    session = boto3.Session(profile_name=my_profile)
    client = session.client('es', some_region)
    domain_names = client.list_domain_names()
Stupid.Fat.Cat
  • 10,755
  • 23
  • 83
  • 144
  • 2
    Maybe check out [`botocore.stubber`](https://botocore.amazonaws.com/v1/documentation/api/latest/reference/stubber.html) – wim Jan 15 '20 at 22:47

1 Answers1

0

The ElasticSearch has now been implemented as of Moto 2.2.20

Other unimplemented services/features can be mocked like so:

import boto3
import botocore
from unittest.mock import patch

orig = botocore.client.BaseClient._make_api_call

def mock_make_api_call(self, operation_name, kwarg):
    if operation_name == 'UnsupportedOperation':
        # Make whatever changes you expect to happen during this operation
        return {
            "expected": "response",
            "for this": "operation"
        }
    # If we don't want to patch the API call, call the original API
    return orig(self, operation_name, kwarg)


# add supported services as appropriate 
@mock_s3
def test_unsupported_feature()

    with patch('botocore.client.BaseClient._make_api_call', new=mock_make_api_call):
        function_under_test()

This allows you to completely customize the boto3 behaviour, on a per-method basis.

Taken from the Moto documentation here: http://docs.getmoto.org/en/latest/docs/services/patching_other_services.html

Bert Blommers
  • 1,788
  • 2
  • 13
  • 19