question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Stubber not intercepting

See original GitHub issue

When using a client created outside of the test, calls are made to an actual client. Given the following code and tests:

def create_client(service):
    return boto3.client(service, config=CLIENT_CONFIG)

def get_security_group_data(filters):
    client = create_client('ec2')
    return client.describe_security_groups(Filters=filters)

def test_get_security_group_data():
    import boto3
    from botocore.stub import Stubber
    ec2_client = boto3.client('ec2', region_name='us-west-2')
    filters = [
            {
                'FilterName': 'tag:Name',
                'FilterValues': [
                    'Default'
                ]
            }
        ]

        describe_security_groups_response = {
            'SecurityGroups': [
                {
                    'GroupName': 'Default',
                    'GroupId': 'sg-1234abc',
                    'VpcId': 'vpc-12345ab',
                    'Tags': [
                        {
                            'Key': 'Name',
                            'Value': 'Default'
                        },
                    ],
                }
            ]
        }
    stubber = Stubber(ec2_client)
    stubber.add_response('describe_security_groups', describe_security_groups_response,
                         {'Filters': filters})
    stubber.activate()
    response_data = get_security_group_data(filters)

The following is raised during the test:

security_group.py:18: in get_security_group_data
    client = create_client('ec2')
functions.py:7: in create_client
    return boto3.client(service, config=CLIENT_CONFIG)
.env/lib/python3.7/site-packages/boto3/__init__.py:91: in client
    return _get_default_session().client(*args, **kwargs)
.env/lib/python3.7/site-packages/boto3/session.py:263: in client
    aws_session_token=aws_session_token, config=config)
.env/lib/python3.7/site-packages/botocore/session.py:839: in create_client
    client_config=config, api_version=api_version)
.env/lib/python3.7/site-packages/botocore/client.py:86: in create_client
    verify, credentials, scoped_config, client_config, endpoint_bridge)
.env/lib/python3.7/site-packages/botocore/client.py:328: in _get_client_args
    verify, credentials, scoped_config, client_config, endpoint_bridge)
.env/lib/python3.7/site-packages/botocore/args.py:47: in get_client_args
    endpoint_url, is_secure, scoped_config)
.env/lib/python3.7/site-packages/botocore/args.py:117: in compute_client_args
    service_name, region_name, endpoint_url, is_secure)
.env/lib/python3.7/site-packages/botocore/client.py:402: in resolve
    service_name, region_name)
.env/lib/python3.7/site-packages/botocore/regions.py:122: in construct_endpoint
    partition, service_name, region_name)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <botocore.regions.EndpointResolver object at 0x7ff86a4c9208>
partition = OrderedDict([('defaults', OrderedDict([('hostname', '{service}.{region}.{dnsSuffix}'), ('protocols', ['https']), ('sig...1', OrderedDict()), ('us-east-2', OrderedDict()), ('us-west-1', OrderedDict()), ('us-west-2', OrderedDict())]))]))]))])
service_name = 'ec2', region_name = None

    def _endpoint_for_partition(self, partition, service_name, region_name):
        # Get the service from the partition, or an empty template.
        service_data = partition['services'].get(
            service_name, DEFAULT_SERVICE_DATA)
        # Use the partition endpoint if no region is supplied.
        if region_name is None:
            if 'partitionEndpoint' in service_data:
                region_name = service_data['partitionEndpoint']
            else:
>               raise NoRegionError()
E               botocore.exceptions.NoRegionError: You must specify a region.

.env/lib/python3.7/site-packages/botocore/regions.py:135: NoRegionError
------------------------------------------------------------ Captured log call ------------------------------------------------------------
INFO     botocore.credentials:credentials.py:1093 Found credentials in shared credentials file: ~/.aws/credentials

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:15 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
tim-finnigancommented, Aug 16, 2021

Hi @mcaulifn, I can bring this up with our doc writers and see if there are better examples we could add.

I saw you were using SNS in an earlier post. This other code samples page may provide more relevant examples for your use case: https://docs.aws.amazon.com/code-samples/latest/catalog/python-sns-test-test_sns_basics.py.html

1reaction
mcaulifncommented, Aug 16, 2021

@tim-finnigan The existing documentation is not very helpful as it shows code within a test file rather than in a module or package. Can you update the docs to show a better real world type example?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot mock method from boto3 (using botocore.stub.Stubber)
It looks like the cause of this is that the client is being re-defined after the Stubber is applied, and it's trying to...
Read more >
Stubber Reference — botocore 1.29.33 documentation - AWS
This class will allow you to stub out requests so you don't have to hit an endpoint to write tests. Responses are returned...
Read more >
IntentStubber - Android Developers
Interface to intercept activity launch for a given android.content.Intent and stub ActivityResult its response. Retrieve instances of the stubber through ...
Read more >
emr_stubber.py - AWS Code Sample
... the stubber class does not set up stubs and passes all calls through to the Boto3 ... :param use_stubs: When True, use...
Read more >
How to use stubs with Cypress - DEV Community ‍ ‍
It might be available on your end, but it might not be available when you run your ... Stubbing API calls in Cypress...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found