
This patch adds several guidelines: * Global constants should be ALL_CAPS (cfg => CFG) * Prefer single-quotes over double-quotes ("foo" => 'foo') * Place a space before TODO in comments ("#TODO" => "# TODO") Change-Id: Ib5b5c5916744856eca2ecaa37e949a3cdc4b3bd7
271 lines
9.5 KiB
Python
271 lines
9.5 KiB
Python
# Copyright (c) 2013 Rackspace, Inc.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
# implied.
|
|
#
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
import json
|
|
import os
|
|
|
|
import falcon
|
|
from falcon import testing
|
|
import pymongo
|
|
|
|
from marconi.common import config
|
|
from marconi.tests.transport.wsgi import base
|
|
from marconi import transport
|
|
|
|
|
|
class QueueLifecycleBaseTest(base.TestBase):
|
|
|
|
config_filename = None
|
|
|
|
def test_simple(self):
|
|
doc = '{"messages": {"ttl": 600}}'
|
|
|
|
# Create
|
|
env = testing.create_environ('/v1/480924/queues/gumshoe',
|
|
method='PUT', body=doc)
|
|
|
|
self.app(env, self.srmock)
|
|
self.assertEquals(self.srmock.status, falcon.HTTP_201)
|
|
|
|
location = ('Location', '/v1/480924/queues/gumshoe')
|
|
self.assertIn(location, self.srmock.headers)
|
|
|
|
env = testing.create_environ('/v1/480924/queues/gumshoe')
|
|
result = self.app(env, self.srmock)
|
|
result_doc = json.loads(result[0])
|
|
self.assertEquals(self.srmock.status, falcon.HTTP_200)
|
|
self.assertEquals(result_doc, json.loads(doc))
|
|
|
|
# Delete
|
|
env = testing.create_environ('/v1/480924/queues/gumshoe',
|
|
method='DELETE')
|
|
|
|
self.app(env, self.srmock)
|
|
self.assertEquals(self.srmock.status, falcon.HTTP_204)
|
|
|
|
# Get non-existing
|
|
env = testing.create_environ('/v1/480924/queues/gumshoe')
|
|
|
|
self.app(env, self.srmock)
|
|
self.assertEquals(self.srmock.status, falcon.HTTP_404)
|
|
|
|
def test_no_metadata(self):
|
|
env = testing.create_environ('/v1/480924/queues/fizbat', method='PUT')
|
|
|
|
self.app(env, self.srmock)
|
|
self.assertEquals(self.srmock.status, falcon.HTTP_400)
|
|
|
|
def test_bad_metadata(self):
|
|
env = testing.create_environ('/v1/480924/queues/fizbat',
|
|
body='{',
|
|
method='PUT')
|
|
|
|
self.app(env, self.srmock)
|
|
self.assertEquals(self.srmock.status, falcon.HTTP_400)
|
|
|
|
env = testing.create_environ('/v1/480924/queues/fizbat',
|
|
body='[]',
|
|
method='PUT')
|
|
|
|
self.app(env, self.srmock)
|
|
self.assertEquals(self.srmock.status, falcon.HTTP_400)
|
|
|
|
def test_too_much_metadata(self):
|
|
doc = '{"messages": {"ttl": 600}, "padding": "%s"}'
|
|
padding_len = transport.MAX_QUEUE_METADATA_SIZE - (len(doc) - 2) + 1
|
|
doc = doc % ('x' * padding_len)
|
|
env = testing.create_environ('/v1/480924/queues/fizbat',
|
|
method='PUT', body=doc)
|
|
|
|
self.app(env, self.srmock)
|
|
self.assertEquals(self.srmock.status, falcon.HTTP_400)
|
|
|
|
def test_way_too_much_metadata(self):
|
|
doc = '{"messages": {"ttl": 600}, "padding": "%s"}'
|
|
padding_len = transport.MAX_QUEUE_METADATA_SIZE * 100
|
|
doc = doc % ('x' * padding_len)
|
|
env = testing.create_environ('/v1/480924/queues/gumshoe',
|
|
method='PUT', body=doc)
|
|
|
|
self.app(env, self.srmock)
|
|
self.assertEquals(self.srmock.status, falcon.HTTP_400)
|
|
|
|
def test_custom_metadata(self):
|
|
# Set
|
|
doc = '{"messages": {"ttl": 600}, "padding": "%s"}'
|
|
padding_len = transport.MAX_QUEUE_METADATA_SIZE - (len(doc) - 2)
|
|
doc = doc % ('x' * padding_len)
|
|
env = testing.create_environ('/v1/480924/queues/gumshoe',
|
|
method='PUT', body=doc)
|
|
|
|
self.app(env, self.srmock)
|
|
self.assertEquals(self.srmock.status, falcon.HTTP_201)
|
|
|
|
# Get
|
|
env = testing.create_environ('/v1/480924/queues/gumshoe')
|
|
result = self.app(env, self.srmock)
|
|
result_doc = json.loads(result[0])
|
|
self.assertEquals(result_doc, json.loads(doc))
|
|
|
|
def test_update_metadata(self):
|
|
# Create
|
|
doc1 = '{"messages": {"ttl": 600}}'
|
|
env = testing.create_environ('/v1/480924/queues/xyz',
|
|
method='PUT', body=doc1)
|
|
|
|
self.app(env, self.srmock)
|
|
self.assertEquals(self.srmock.status, falcon.HTTP_201)
|
|
|
|
# Update
|
|
doc2 = '{"messages": {"ttl": 100}}'
|
|
env = testing.create_environ('/v1/480924/queues/xyz',
|
|
method='PUT', body=doc2)
|
|
|
|
self.app(env, self.srmock)
|
|
self.assertEquals(self.srmock.status, falcon.HTTP_204)
|
|
|
|
# Get
|
|
env = testing.create_environ('/v1/480924/queues/xyz')
|
|
result = self.app(env, self.srmock)
|
|
result_doc = json.loads(result[0])
|
|
|
|
self.assertEquals(result_doc, json.loads(doc2))
|
|
self.assertEquals(self.srmock.headers_dict['Content-Location'],
|
|
env['PATH_INFO'])
|
|
|
|
def test_list(self):
|
|
# List empty
|
|
env = testing.create_environ('/v1/480924/queues')
|
|
|
|
self.app(env, self.srmock)
|
|
self.assertEquals(self.srmock.status, falcon.HTTP_204)
|
|
|
|
# Create some
|
|
env = testing.create_environ('/v1/480924/queues/q1',
|
|
method='PUT',
|
|
body='{"_ttl": 30 }')
|
|
self.app(env, self.srmock)
|
|
|
|
env = testing.create_environ('/v1/480924/queues/q2',
|
|
method='PUT',
|
|
body='{}')
|
|
self.app(env, self.srmock)
|
|
|
|
env = testing.create_environ('/v1/480924/queues/q3',
|
|
method='PUT',
|
|
body='{"_ttl": 30 }')
|
|
self.app(env, self.srmock)
|
|
|
|
# List
|
|
env = testing.create_environ('/v1/480924/queues',
|
|
query_string='limit=2')
|
|
|
|
result = self.app(env, self.srmock)
|
|
result_doc = json.loads(result[0])
|
|
[target, params] = result_doc['links'][0]['href'].split('?')
|
|
|
|
self.assertEquals(self.srmock.status, falcon.HTTP_200)
|
|
self.assertEquals(self.srmock.headers_dict['Content-Location'],
|
|
env['PATH_INFO'] + '?' + env['QUERY_STRING'])
|
|
|
|
for queue in result_doc['queues']:
|
|
env = testing.create_environ(queue['href'])
|
|
self.app(env, self.srmock)
|
|
self.assertEquals(self.srmock.status, falcon.HTTP_200)
|
|
self.assertNotIn('metadata', queue)
|
|
|
|
# List with metadata
|
|
env = testing.create_environ(target,
|
|
query_string=params + '&detailed=true')
|
|
|
|
result = self.app(env, self.srmock)
|
|
result_doc = json.loads(result[0])
|
|
[target, params] = result_doc['links'][0]['href'].split('?')
|
|
|
|
[queue] = result_doc['queues']
|
|
env = testing.create_environ(queue['href'])
|
|
result = self.app(env, self.srmock)
|
|
result_doc = json.loads(result[0])
|
|
self.assertEquals(result_doc, queue['metadata'])
|
|
|
|
# List tail
|
|
env = testing.create_environ(target, query_string=params)
|
|
|
|
self.app(env, self.srmock)
|
|
self.assertEquals(self.srmock.status, falcon.HTTP_204)
|
|
|
|
|
|
class QueueLifecycleMongoDBTests(QueueLifecycleBaseTest):
|
|
|
|
config_filename = 'wsgi_mongodb.conf'
|
|
|
|
def setUp(self):
|
|
if not os.environ.get('MONGODB_TEST_LIVE'):
|
|
self.skipTest('No MongoDB instance running')
|
|
super(QueueLifecycleMongoDBTests, self).setUp()
|
|
|
|
self.cfg = config.namespace('drivers:storage:mongodb').from_options()
|
|
|
|
def tearDown(self):
|
|
conn = pymongo.MongoClient(self.cfg.uri)
|
|
conn.drop_database(self.cfg.database)
|
|
super(QueueLifecycleMongoDBTests, self).tearDown()
|
|
|
|
|
|
class QueueLifecycleSQLiteTests(QueueLifecycleBaseTest):
|
|
|
|
config_filename = 'wsgi_sqlite.conf'
|
|
|
|
|
|
class QueueFaultyDriverTests(base.TestBaseFaulty):
|
|
|
|
config_filename = 'wsgi_faulty.conf'
|
|
|
|
def test_simple(self):
|
|
doc = '{"messages": {"ttl": 600}}'
|
|
env = testing.create_environ('/v1/480924/queues/gumshoe',
|
|
method='PUT', body=doc)
|
|
|
|
self.app(env, self.srmock)
|
|
self.assertEquals(self.srmock.status, falcon.HTTP_503)
|
|
|
|
location = ('Location', '/v1/480924/queues/gumshoe')
|
|
self.assertNotIn(location, self.srmock.headers)
|
|
|
|
env = testing.create_environ('/v1/480924/queues/gumshoe')
|
|
result = self.app(env, self.srmock)
|
|
result_doc = json.loads(result[0])
|
|
self.assertEquals(self.srmock.status, falcon.HTTP_503)
|
|
self.assertNotEquals(result_doc, json.loads(doc))
|
|
|
|
env = testing.create_environ('/v1/480924/queues/gumshoe/stats')
|
|
self.app(env, self.srmock)
|
|
self.assertEquals(self.srmock.status, falcon.HTTP_503)
|
|
|
|
env = testing.create_environ('/v1/480924/queues')
|
|
self.app(env, self.srmock)
|
|
self.assertEquals(self.srmock.status, falcon.HTTP_503)
|
|
|
|
env = testing.create_environ('/v1/480924/queues/gumshoe',
|
|
method='DELETE')
|
|
self.app(env, self.srmock)
|
|
self.assertEquals(self.srmock.status, falcon.HTTP_503)
|
|
|
|
def test_bad_document(self):
|
|
env = testing.create_environ('/v1/480924/queues/bad-doc')
|
|
self.app(env, self.srmock)
|
|
self.assertEquals(self.srmock.status, falcon.HTTP_503)
|