From 1689a2808f95a1b6bdff107f6cb683bee2cc06ed Mon Sep 17 00:00:00 2001 From: Steve Pulec Date: Thu, 31 May 2018 23:05:50 -0400 Subject: [PATCH] Fix creating SQS queue with same attributes. Closes #1663. --- moto/awslambda/models.py | 1 + moto/sqs/exceptions.py | 9 +++++++++ moto/sqs/models.py | 8 +++++++- tests/test_sqs/test_sqs.py | 23 +++++++++++++++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/moto/awslambda/models.py b/moto/awslambda/models.py index 1404e0936..b11bde042 100644 --- a/moto/awslambda/models.py +++ b/moto/awslambda/models.py @@ -47,6 +47,7 @@ _stderr_regex = re.compile(r'START|END|REPORT RequestId: .*') _orig_adapter_send = requests.adapters.HTTPAdapter.send docker_3 = docker.__version__.startswith("3") + def zip2tar(zip_bytes): with TemporaryDirectory() as td: tarname = os.path.join(td, 'data.tar') diff --git a/moto/sqs/exceptions.py b/moto/sqs/exceptions.py index baf721b53..5f1cc46b2 100644 --- a/moto/sqs/exceptions.py +++ b/moto/sqs/exceptions.py @@ -1,4 +1,5 @@ from __future__ import unicode_literals +from moto.core.exceptions import RESTError class MessageNotInflight(Exception): @@ -21,3 +22,11 @@ class MessageAttributesInvalid(Exception): class QueueDoesNotExist(Exception): status_code = 404 description = "The specified queue does not exist for this wsdl version." + + +class QueueAlreadyExists(RESTError): + code = 400 + + def __init__(self, message): + super(QueueAlreadyExists, self).__init__( + "QueueAlreadyExists", message) diff --git a/moto/sqs/models.py b/moto/sqs/models.py index 797568781..b8db356e9 100644 --- a/moto/sqs/models.py +++ b/moto/sqs/models.py @@ -18,6 +18,7 @@ from .exceptions import ( MessageAttributesInvalid, MessageNotInflight, QueueDoesNotExist, + QueueAlreadyExists, ReceiptHandleIsInvalid, ) @@ -383,7 +384,12 @@ class SQSBackend(BaseBackend): def create_queue(self, name, **kwargs): queue = self.queues.get(name) - if queue is None: + if queue: + # Queue already exist. If attributes don't match, throw error + for key, value in kwargs.items(): + if getattr(queue, camelcase_to_underscores(key)) != value: + raise QueueAlreadyExists("The specified queue already exists.") + else: try: kwargs.pop('region') except KeyError: diff --git a/tests/test_sqs/test_sqs.py b/tests/test_sqs/test_sqs.py index cfe481bea..d3e4ca917 100644 --- a/tests/test_sqs/test_sqs.py +++ b/tests/test_sqs/test_sqs.py @@ -40,6 +40,29 @@ def test_create_fifo_queue_fail(): raise RuntimeError('Should of raised InvalidParameterValue Exception') +@mock_sqs +def test_create_queue_with_different_attributes_fail(): + sqs = boto3.client('sqs', region_name='us-east-1') + + sqs.create_queue( + QueueName='test-queue', + Attributes={ + 'VisibilityTimeout': '10', + } + ) + try: + sqs.create_queue( + QueueName='test-queue', + Attributes={ + 'VisibilityTimeout': '60', + } + ) + except botocore.exceptions.ClientError as err: + err.response['Error']['Code'].should.equal('QueueAlreadyExists') + else: + raise RuntimeError('Should of raised QueueAlreadyExists Exception') + + @mock_sqs def test_create_fifo_queue(): sqs = boto3.client('sqs', region_name='us-east-1')