diff --git a/moto/sqs/models.py b/moto/sqs/models.py index f82dc3208..13aabd424 100644 --- a/moto/sqs/models.py +++ b/moto/sqs/models.py @@ -1,5 +1,6 @@ import hashlib import time +import re from moto.core import BaseBackend from moto.core.utils import camelcase_to_underscores, get_random_message_id @@ -73,8 +74,16 @@ class SQSBackend(BaseBackend): self.queues[name] = queue return queue - def list_queues(self): - return self.queues.values() + def list_queues(self, queue_name_prefix): + re_str = '.*' + if queue_name_prefix: + re_str = '^{0}.*'.format(queue_name_prefix) + prefix_re = re.compile(re_str) + qs = [] + for name, q in self.queues.items(): + if prefix_re.search(name): + qs.append(q) + return qs def get_queue(self, queue_name): return self.queues.get(queue_name, None) diff --git a/moto/sqs/responses.py b/moto/sqs/responses.py index b49796545..fc69e7f05 100644 --- a/moto/sqs/responses.py +++ b/moto/sqs/responses.py @@ -27,7 +27,8 @@ class QueuesResponse(BaseResponse): return "", dict(status=404) def list_queues(self): - queues = sqs_backend.list_queues() + queue_name_prefix = self.querystring.get("QueueNamePrefix", [None])[0] + queues = sqs_backend.list_queues(queue_name_prefix) template = Template(LIST_QUEUES_RESPONSE) return template.render(queues=queues) diff --git a/tests/test_sqs/test_server.py b/tests/test_sqs/test_server.py index 4e5102edf..59b81bea7 100644 --- a/tests/test_sqs/test_server.py +++ b/tests/test_sqs/test_server.py @@ -16,6 +16,10 @@ def test_sqs_list_identities(): res.data.should.contain("ListQueuesResponse") res = test_client.put('/?Action=CreateQueue&QueueName=testqueue') + res = test_client.put('/?Action=CreateQueue&QueueName=otherqueue') + + res = test_client.get('/?Action=ListQueues&QueueNamePrefix=other') + res.data.should_not.contain('testqueue') res = test_client.put( '/123/testqueue?MessageBody=test-message&Action=SendMessage') diff --git a/tests/test_sqs/test_sqs.py b/tests/test_sqs/test_sqs.py index bfd1c2759..7c84a04db 100644 --- a/tests/test_sqs/test_sqs.py +++ b/tests/test_sqs/test_sqs.py @@ -31,6 +31,20 @@ def test_get_queue(): nonexisting_queue.should.be.none +@mock_sqs +def test_get_queue_with_prefix(): + conn = boto.connect_sqs('the_key', 'the_secret') + conn.create_queue("prefixa-queue") + conn.create_queue("prefixb-queue") + conn.create_queue("test-queue") + + conn.get_all_queues().should.have.length_of(3) + + queue = conn.get_all_queues("test-") + queue.should.have.length_of(1) + queue[0].name.should.equal("test-queue") + + @mock_sqs def test_delete_queue(): conn = boto.connect_sqs('the_key', 'the_secret')