summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Villas <villasv@outlook.com>2018-11-20 08:15:32 -0200
committerAsif Saif Uddin <auvipy@gmail.com>2018-11-20 16:15:31 +0600
commit4a06fc6bb81a1160a68ef59e672fe423a0934b3b (patch)
tree2c1edcc8d525aa1fcbd2a71c7e59cc5b57891395
parent71c0d0a34bc5f1b58a74b294182aceb64abb6ad2 (diff)
downloadkombu-4a06fc6bb81a1160a68ef59e672fe423a0934b3b.tar.gz
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
-rw-r--r--kombu/transport/SQS.py4
-rw-r--r--t/unit/transport/test_SQS.py32
2 files changed, 35 insertions, 1 deletions
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)