0

The describe_reserved_instance function of mock_ec2 has not been implemented in moto lib. In this case, how can one write a unites for the following function in python?

def get_reserved_instance(ec2_client):
    ri_response = ec2_client.describe_reserved_instances(
        Filters=[
            
        ],
        OfferingClass="convertible"
    )

    ri_response = ri_response["ReservedInstances"]
    return ri_response
Jason LiLy
  • 634
  • 2
  • 9
  • 19

1 Answers1

0

You can intercept the EC2 request using a standard mock.patch:

import boto3
import botocore
from unittest.mock import patch

from moto import mock_ec2


orig = botocore.client.BaseClient._make_api_call

def mock_make_api_call(self, operation_name, kwarg):
    if operation_name == 'DescribeReservedInstances':
        # Return whatever data you want to here
        return {}
    return orig(self, operation_name, kwarg)


@mock_ec2
def test_list_findings():
    client = boto3.client("ec2")

    with patch('botocore.client.BaseClient._make_api_call', new=mock_make_api_call):
        resp = client.describe_reserved_instances()
        print(resp)

The DescribeReservedInstances call can have whatever response that you'd like - other calls will still be redirected to Moto.

Moto has this option documented here: http://docs.getmoto.org/en/latest/docs/services/patching_other_services.html

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