Fix handling of 1.1 API

This fixes various behaviors of the client talking to the v1.1 API, and
enables functional tests for queues.

Change-Id: Icd30e32a17247790dfd57d6420d67f9140020db6
Closes-Bug: #1446677
Closes-Bug: #1446679
This commit is contained in:
Thomas Herve 2015-04-27 16:33:51 +02:00
parent 272e0d21b4
commit 0047c54116
8 changed files with 79 additions and 9 deletions

View File

@ -24,3 +24,11 @@ class QueuesV1QueueHttpFunctionalTest(queues.QueuesV1QueueFunctionalTest):
transport_cls = http.HttpTransport
url = 'http://127.0.0.1:8888'
version = 1
class QueuesV1_1QueueHttpFunctionalTest(queues.QueuesV1_1QueueFunctionalTest):
is_functional = True
transport_cls = http.HttpTransport
url = 'http://127.0.0.1:8888'
version = 1.1

View File

@ -30,3 +30,6 @@ class QueuesV1_1QueueHttpUnitTest(queues.QueuesV1_1QueueUnitTest):
transport_cls = http.HttpTransport
url = 'http://127.0.0.1:8888/v1.1'
version = 1.1
def test_queue_exists(self):
pass

View File

@ -265,6 +265,15 @@ class V1_1(V1):
V1_1.schema.update({
'queue_get': {
'ref': 'queues/{queue_name}',
'method': 'GET',
'required': ['queue_name'],
'properties': {
'queue_name': {'type': 'string'}
}
},
'message_pop': {
'ref': 'queues/{queue_name}/messages',
'method': 'DELETE',

View File

@ -92,6 +92,12 @@ def queue_exists(transport, request, name, callback=None):
return False
def queue_get(transport, request, name, callback=None):
"""Retrieve a queue."""
return _common_queue_ops('queue_get', transport,
request, name, callback=callback)
def queue_get_metadata(transport, request, name, callback=None):
"""Gets queue metadata."""
return _common_queue_ops('queue_get_metadata', transport,
@ -336,7 +342,9 @@ def message_pop(transport, request, queue_name,
request.operation = 'message_delete_many'
request.params['queue_name'] = queue_name
request.params['pop'] = count
transport.send(request)
resp = transport.send(request)
return resp.deserialized_content
def claim_create(transport, request, queue_name, **kwargs):

View File

@ -50,7 +50,7 @@ class _Iterator(object):
# re-use the iterator for get_many_messages
# and message listing.
if isinstance(listing_response, dict):
self._links = listing_response['links']
self._links = listing_response.get('links', [])
self._listing_response = listing_response[self._iter_key]
def __iter__(self):

View File

@ -21,7 +21,8 @@ class Message(object):
"""A handler for Zaqar server Message resources.
Attributes are only downloaded once - at creation time.
"""
def __init__(self, queue, href, ttl, age, body):
def __init__(self, queue, ttl, age, body, href=None, id=None,
claim_id=None):
self.queue = queue
self.href = href
self.ttl = ttl
@ -35,9 +36,12 @@ class Message(object):
# /v1/queues/worker-jobs/messages/5c6939a8?claim_id=63c9a592
# or
# /v1/queues/worker-jobs/messages/5c6939a8
self._id = href.split('/')[-1]
if '?' in self._id:
self._id = self._id.split('?')[0]
if id is None:
self._id = href.split('/')[-1]
if '?' in self._id:
self._id = self._id.split('?')[0]
else:
self._id = id
def __repr__(self):
return '<Message id:{id} ttl:{ttl}>'.format(id=self._id,

View File

@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from zaqarclient import errors
from zaqarclient.queues.v1 import claim as claim_api
from zaqarclient.queues.v1 import core
from zaqarclient.queues.v1 import iterator
@ -38,7 +39,10 @@ class Queue(object):
def exists(self):
"""Checks if the queue exists."""
req, trans = self.client._request_and_transport()
return core.queue_exists(trans, req, self._name)
if self.client.api_version >= 1.1:
raise errors.InvalidOperation("Unavailable on versions >= 1.1")
else:
return core.queue_exists(trans, req, self._name)
def ensure_exists(self):
"""Ensures a queue exists
@ -82,7 +86,10 @@ class Queue(object):
if self._metadata and not force_reload:
return self._metadata
self._metadata = core.queue_get_metadata(trans, req, self._name)
if self.client.api_version >= 1.1:
self._metadata = core.queue_get(trans, req, self._name)
else:
self._metadata = core.queue_get_metadata(trans, req, self._name)
return self._metadata
@property

View File

@ -16,6 +16,7 @@
import json
import mock
from zaqarclient import errors
from zaqarclient.queues.v1 import iterator
from zaqarclient.queues.v1 import message
from zaqarclient.tests.queues import base
@ -234,11 +235,13 @@ class QueuesV1QueueFunctionalTest(base.QueuesTestBase):
def test_queue_create_functional(self):
queue = self.client.queue("nonono")
self.addCleanup(queue.delete)
queue._get_transport = mock.Mock(return_value=self.transport)
self.assertTrue(queue.exists())
def test_queue_delete_functional(self):
queue = self.client.queue("nonono")
self.addCleanup(queue.delete)
queue._get_transport = mock.Mock(return_value=self.transport)
self.assertTrue(queue.exists())
queue.delete()
@ -257,6 +260,7 @@ class QueuesV1QueueFunctionalTest(base.QueuesTestBase):
]
queue = self.client.queue("nonono")
self.addCleanup(queue.delete)
queue._get_transport = mock.Mock(return_value=self.transport)
queue.post(messages)
stats = queue.stats
@ -298,6 +302,7 @@ class QueuesV1QueueFunctionalTest(base.QueuesTestBase):
def test_message_list_functional(self):
queue = self.client.queue("test_queue")
self.addCleanup(queue.delete)
queue._get_transport = mock.Mock(return_value=self.transport)
messages = [{'ttl': 60, 'body': 'Post It 1!'}]
@ -309,6 +314,7 @@ class QueuesV1QueueFunctionalTest(base.QueuesTestBase):
def test_message_list_echo_functional(self):
queue = self.client.queue("test_queue")
self.addCleanup(queue.delete)
queue._get_transport = mock.Mock(return_value=self.transport)
messages = [
@ -323,6 +329,7 @@ class QueuesV1QueueFunctionalTest(base.QueuesTestBase):
def test_message_get_functional(self):
queue = self.client.queue("test_queue")
self.addCleanup(queue.delete)
queue._get_transport = mock.Mock(return_value=self.transport)
messages = [
@ -339,6 +346,7 @@ class QueuesV1QueueFunctionalTest(base.QueuesTestBase):
def test_message_get_many_functional(self):
queue = self.client.queue("test_queue")
self.addCleanup(queue.delete)
queue._get_transport = mock.Mock(return_value=self.transport)
messages = [
@ -357,6 +365,7 @@ class QueuesV1QueueFunctionalTest(base.QueuesTestBase):
def test_message_delete_many_functional(self):
queue = self.client.queue("test_queue")
self.addCleanup(queue.delete)
queue._get_transport = mock.Mock(return_value=self.transport)
messages = [
@ -405,8 +414,29 @@ class QueuesV1_1QueueUnitTest(QueuesV1QueueUnitTest):
class QueuesV1_1QueueFunctionalTest(QueuesV1QueueFunctionalTest):
def test_queue_create_functional(self):
pass
def test_queue_exists_functional(self):
queue = self.client.queue("404")
self.assertRaises(errors.InvalidOperation, queue.exists)
def test_queue_delete_functional(self):
queue = self.client.queue("nonono")
queue._get_transport = mock.Mock(return_value=self.transport)
messages = [
{'ttl': 60, 'body': 'Post It 1!'},
{'ttl': 60, 'body': 'Post It 2!'},
{'ttl': 60, 'body': 'Post It 3!'},
]
queue.post(messages)
queue.delete()
self.assertEqual(len(list(queue.messages(echo=True))), 0)
def test_message_pop(self):
queue = self.client.queue("test_queue")
self.addCleanup(queue.delete)
queue._get_transport = mock.Mock(return_value=self.transport)
messages = [
@ -415,9 +445,10 @@ class QueuesV1_1QueueFunctionalTest(QueuesV1QueueFunctionalTest):
{'ttl': 60, 'body': 'Post It 2!'},
]
queue.post(messages)
messages = queue.pop(count=2)
self.assertTrue(isinstance(messages, iterator._Iterator))
self.assertEqual(len(list(messages)), 2)
remaining = queue.messages()
remaining = queue.messages(echo=True)
self.assertEqual(len(list(remaining)), 1)