From 4a06fc6bb81a1160a68ef59e672fe423a0934b3b Mon Sep 17 00:00:00 2001 From: Victor Villas Date: Tue, 20 Nov 2018 08:15:32 -0200 Subject: Add boto3 default region before hardcoded default (#951) * Add boto3 default region before hardcoded default * Add unit tests for SQS region default behavior * Import boto3 only inside the test The library may not be available --- kombu/transport/SQS.py | 4 +++- t/unit/transport/test_SQS.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/kombu/transport/SQS.py b/kombu/transport/SQS.py index b4d5093b..f74ab740 100644 --- a/kombu/transport/SQS.py +++ b/kombu/transport/SQS.py @@ -467,7 +467,9 @@ class Channel(virtual.Channel): @cached_property def region(self): - return self.transport_options.get('region') or self.default_region + return (self.transport_options.get('region') or + boto3.Session().region_name or + self.default_region) @cached_property def regioninfo(self): diff --git a/t/unit/transport/test_SQS.py b/t/unit/transport/test_SQS.py index f647f47d..85f60f2b 100644 --- a/t/unit/transport/test_SQS.py +++ b/t/unit/transport/test_SQS.py @@ -7,6 +7,7 @@ slightly. from __future__ import absolute_import, unicode_literals +import os import pytest import random import string @@ -173,6 +174,37 @@ class test_Channel: """kombu.SQS.Channel instantiates correctly with mocked queues""" assert self.queue_name in self.channel._queue_cache + def test_region(self): + import boto3 + _environ = dict(os.environ) + + # when the region is unspecified + connection = Connection(transport=SQS.Transport) + channel = connection.channel() + assert channel.transport_options.get('region') is None + # the default region is us-east-1 + assert channel.region == 'us-east-1' + + # when boto3 picks a region + os.environ['AWS_DEFAULT_REGION'] = 'us-east-2' + assert boto3.Session().region_name == 'us-east-2' + # the default region should match + connection = Connection(transport=SQS.Transport) + channel = connection.channel() + assert channel.region == 'us-east-2' + + # when transport_options are provided + connection = Connection(transport=SQS.Transport, transport_options={ + 'region': 'us-west-2' + }) + channel = connection.channel() + assert channel.transport_options.get('region') == 'us-west-2' + # the specified region should be used + assert connection.channel().region == 'us-west-2' + + os.environ.clear() + os.environ.update(_environ) + def test_endpoint_url(self): url = 'sqs://@localhost:5493' self.connection = Connection(hostname=url, transport=SQS.Transport) -- cgit v1.2.1