def test_put_raises_if_missing_fields(self): path = self.url_prefix + '/pools/' + str(uuid.uuid1()) self.simulate_put(path, body=jsonutils.dumps({'weight': 100})) self.assertEqual(self.srmock.status, falcon.HTTP_400) self.simulate_put(path, body=jsonutils.dumps( {'uri': 'sqlite://:memory:'})) self.assertEqual(self.srmock.status, falcon.HTTP_400)
def _prepare_messages(self, count): doc = jsonutils.dumps([{'body': 239, 'ttl': 300}] * count) self.simulate_post(self.messages_path, body=doc, headers={'Client-ID': str(uuid.uuid4())}) self.assertEqual(self.srmock.status, falcon.HTTP_201)
def format(self, record): message = {'message': record.getMessage(), 'asctime': self.formatTime(record, self.datefmt), 'name': record.name, 'msg': record.msg, 'args': record.args, 'levelname': record.levelname, 'levelno': record.levelno, 'pathname': record.pathname, 'filename': record.filename, 'module': record.module, 'lineno': record.lineno, 'funcname': record.funcName, 'created': record.created, 'msecs': record.msecs, 'relative_created': record.relativeCreated, 'thread': record.thread, 'thread_name': record.threadName, 'process_name': record.processName, 'process': record.process, 'traceback': None} if hasattr(record, 'extra'): message['extra'] = record.extra if record.exc_info: message['traceback'] = self.formatException(record.exc_info) return jsonutils.dumps(message)
def test_unacceptable_new_ttl(self, ttl): href = self._get_a_claim() self.simulate_patch(href, self.project_id, body=jsonutils.dumps({'ttl': ttl})) self.assertEqual(self.srmock.status, falcon.HTTP_400)
def setUp(self): super(MessagesBaseTest, self).setUp() if self.conf.pooling: for i in range(4): uri = self.conf['drivers:storage:mongodb'].uri doc = {'weight': 100, 'uri': uri} self.simulate_put(self.url_prefix + '/pools/' + str(i), body=jsonutils.dumps(doc)) self.assertEqual(self.srmock.status, falcon.HTTP_201) self.project_id = '7e55e1a7e' # TODO(kgriffs): Add support in self.simulate_* for a "base path" # so that we don't have to concatenate against self.url_prefix # all over the place. self.queue_path = self.url_prefix + '/queues/fizbit' self.messages_path = self.queue_path + '/messages' doc = '{"_ttl": 60}' self.simulate_put(self.queue_path, self.project_id, body=doc) self.headers = { 'Client-ID': str(uuid.uuid4()), }
def _test_post(self, sample_messages): sample_doc = jsonutils.dumps(sample_messages) result = self.simulate_post(self.messages_path, self.project_id, body=sample_doc, headers=self.headers) self.assertEqual(self.srmock.status, falcon.HTTP_201) result_doc = jsonutils.loads(result[0]) msg_ids = self._get_msg_ids(self.srmock.headers_dict) self.assertEqual(len(msg_ids), len(sample_messages)) expected_resources = [six.text_type(self.messages_path + '/' + id) for id in msg_ids] self.assertEqual(expected_resources, result_doc['resources']) self.assertFalse(result_doc['partial']) self.assertEqual(len(msg_ids), len(sample_messages)) lookup = dict([(m['ttl'], m['body']) for m in sample_messages]) # Test GET on the message resource directly # NOTE(cpp-cabrera): force the passing of time to age a message timeutils_utcnow = 'marconi.openstack.common.timeutils.utcnow' now = timeutils.utcnow() + datetime.timedelta(seconds=10) with mock.patch(timeutils_utcnow) as mock_utcnow: mock_utcnow.return_value = now for msg_id in msg_ids: message_uri = self.messages_path + '/' + msg_id # Wrong project ID self.simulate_get(message_uri, '777777') self.assertEqual(self.srmock.status, falcon.HTTP_404) # Correct project ID result = self.simulate_get(message_uri, self.project_id) self.assertEqual(self.srmock.status, falcon.HTTP_200) self.assertEqual(self.srmock.headers_dict['Content-Location'], message_uri) # Check message properties message = jsonutils.loads(result[0]) self.assertEqual(message['href'], message_uri) self.assertEqual(message['body'], lookup[message['ttl']]) # no negative age # NOTE(cpp-cabrera): testtools lacks GreaterThanEqual on py26 self.assertThat(message['age'], matchers.GreaterThan(-1)) # Test bulk GET query_string = 'ids=' + ','.join(msg_ids) result = self.simulate_get(self.messages_path, self.project_id, query_string=query_string) self.assertEqual(self.srmock.status, falcon.HTTP_200) result_doc = jsonutils.loads(result[0]) expected_ttls = set(m['ttl'] for m in sample_messages) actual_ttls = set(m['ttl'] for m in result_doc) self.assertFalse(expected_ttls - actual_ttls)
def format(self, record): message = { 'message': record.getMessage(), 'asctime': self.formatTime(record, self.datefmt), 'name': record.name, 'msg': record.msg, 'args': record.args, 'levelname': record.levelname, 'levelno': record.levelno, 'pathname': record.pathname, 'filename': record.filename, 'module': record.module, 'lineno': record.lineno, 'funcname': record.funcName, 'created': record.created, 'msecs': record.msecs, 'relative_created': record.relativeCreated, 'thread': record.thread, 'thread_name': record.threadName, 'process_name': record.processName, 'process': record.process, 'traceback': None } if hasattr(record, 'extra'): message['extra'] = record.extra if record.exc_info: message['traceback'] = self.formatException(record.exc_info) return jsonutils.dumps(message)
def test_unacceptable_ttl(self, ttl): self.simulate_post(self.queue_path + '/messages', body=jsonutils.dumps([{ 'ttl': ttl, 'body': None }]), headers=self.headers) self.assertEqual(self.srmock.status, falcon.HTTP_400)
def test_unacceptable_ttl_or_grace(self, ttl_grace): ttl, grace = ttl_grace self.simulate_post(self.claims_path, self.project_id, body=jsonutils.dumps({ 'ttl': ttl, 'grace': grace })) self.assertEqual(self.srmock.status, falcon.HTTP_400)
def _patch_test(self, doc): self.simulate_patch(self.pool, body=jsonutils.dumps(doc)) self.assertEqual(self.srmock.status, falcon.HTTP_200) result = self.simulate_get(self.pool, query_string='?detailed=True') self.assertEqual(self.srmock.status, falcon.HTTP_200) pool = jsonutils.loads(result[0]) self._pool_expect(pool, self.pool, doc['weight'], doc['uri']) self.assertEqual(pool['options'], doc['options'])
def test_put_existing_overwrites(self): # NOTE(cabrera): setUp creates default pool expect = self.doc self.simulate_put(self.pool, body=jsonutils.dumps(expect)) self.assertEqual(self.srmock.status, falcon.HTTP_201) result = self.simulate_get(self.pool) self.assertEqual(self.srmock.status, falcon.HTTP_200) doc = jsonutils.loads(result[0]) self.assertEqual(doc['weight'], expect['weight']) self.assertEqual(doc['uri'], expect['uri'])
def test_exceeded_message_posting(self): # Total (raw request) size doc = jsonutils.dumps([{'body': "some body", 'ttl': 100}] * 20, indent=4) max_len = self.transport_cfg.max_message_size long_doc = doc + (' ' * (max_len - len(doc) + 1)) self.simulate_post(self.queue_path + '/messages', body=long_doc, headers=self.headers) self.assertEqual(self.srmock.status, falcon.HTTP_400)
def setUp(self): super(ClaimsBaseTest, self).setUp() self.project_id = '480924' self.queue_path = self.url_prefix + '/queues/fizbit' self.claims_path = self.queue_path + '/claims' self.messages_path = self.queue_path + '/messages' doc = '{"_ttl": 60}' self.simulate_put(self.queue_path, self.project_id, body=doc) self.assertEqual(self.srmock.status, falcon.HTTP_201) doc = jsonutils.dumps([{'body': 239, 'ttl': 300}] * 10) self.simulate_post(self.queue_path + '/messages', self.project_id, body=doc, headers={'Client-ID': str(uuid.uuid4())}) self.assertEqual(self.srmock.status, falcon.HTTP_201)
def pools(test, count, uri): """A context manager for constructing pools for use in testing. Deletes the pools after exiting the context. :param test: Must expose simulate_* methods :param count: Number of pools to create :type count: int :returns: (paths, weights, uris, options) :rtype: ([six.text_type], [int], [six.text_type], [dict]) """ base = test.url_prefix + '/pools/' args = [(base + str(i), i, {str(i): i}) for i in range(count)] for path, weight, option in args: doc = {'weight': weight, 'uri': uri, 'options': option} test.simulate_put(path, body=jsonutils.dumps(doc)) try: yield args finally: for path, _, _ in args: test.simulate_delete(path)
def pool(test, name, weight, uri, options={}): """A context manager for constructing a pool for use in testing. Deletes the pool after exiting the context. :param test: Must expose simulate_* methods :param name: Name for this pool :type name: six.text_type :type weight: int :type uri: six.text_type :type options: dict :returns: (name, weight, uri, options) :rtype: see above """ doc = {'weight': weight, 'uri': uri, 'options': options} path = test.url_prefix + '/pools/' + name test.simulate_put(path, body=jsonutils.dumps(doc)) try: yield name, weight, uri, options finally: test.simulate_delete(path)
def test_patch_nonexistent_claim_404s(self): patch_data = jsonutils.dumps({'ttl': 100}) self.simulate_patch(self.claims_path + '/a', body=patch_data) self.assertEqual(self.srmock.status, falcon.HTTP_404)
def setUp(self): super(PoolsBaseTest, self).setUp() self.doc = {'weight': 100, 'uri': 'sqlite://:memory:'} self.pool = self.url_prefix + '/pools/' + str(uuid.uuid1()) self.simulate_put(self.pool, body=jsonutils.dumps(self.doc)) self.assertEqual(self.srmock.status, falcon.HTTP_201)
def test_patch_raises_404_if_pool_not_found(self): self.simulate_patch(self.url_prefix + '/pools/notexists', body=jsonutils.dumps({'weight': 1})) self.assertEqual(self.srmock.status, falcon.HTTP_404)
def test_patch_raises_400_on_invalid_options(self, options): self.simulate_patch(self.pool, body=jsonutils.dumps({'options': options})) self.assertEqual(self.srmock.status, falcon.HTTP_400)
def test_unacceptable_ttl_or_grace(self, ttl_grace): ttl, grace = ttl_grace self.simulate_post(self.claims_path, self.project_id, body=jsonutils.dumps({'ttl': ttl, 'grace': grace})) self.assertEqual(self.srmock.status, falcon.HTTP_400)
def test_patch_raises_if_missing_fields(self): self.simulate_patch(self.pool, body=jsonutils.dumps({'location': 1})) self.assertEqual(self.srmock.status, falcon.HTTP_400)
def test_put_raises_if_invalid_options(self, options): path = self.url_prefix + '/pools/' + str(uuid.uuid1()) doc = {'weight': 1, 'uri': 'a', 'options': options} self.simulate_put(path, body=jsonutils.dumps(doc)) self.assertEqual(self.srmock.status, falcon.HTTP_400)
def test_put_raises_if_invalid_uri(self, uri): path = self.url_prefix + '/pools/' + str(uuid.uuid1()) self.simulate_put(path, body=jsonutils.dumps({'weight': 1, 'uri': uri})) self.assertEqual(self.srmock.status, falcon.HTTP_400)
def test_unacceptable_ttl(self, ttl): self.simulate_post(self.queue_path + '/messages', body=jsonutils.dumps([{'ttl': ttl, 'body': None}]), headers=self.headers) self.assertEqual(self.srmock.status, falcon.HTTP_400)
def _serialize(self, message, **kwargs): return jsonutils.dumps(message, kwargs)
def json_encode(obj): return strutils.safe_encode(jsonutils.dumps(obj), 'utf-8')
def _post_messages(self, target, repeat=1): doc = jsonutils.dumps([{'body': 239, 'ttl': 300}] * repeat) return self.simulate_post(target, self.project_id, body=doc, headers=self.headers)
def test_patch_raises_400_on_invalid_weight(self, weight): self.simulate_patch(self.pool, body=jsonutils.dumps({'weight': weight})) self.assertEqual(self.srmock.status, falcon.HTTP_400)
def test_patch_raises_400_on_invalid_uri(self, uri): self.simulate_patch(self.pool, body=jsonutils.dumps({'uri': uri})) self.assertEqual(self.srmock.status, falcon.HTTP_400)