def test_message_counter(self): queue_name = self.queue_name iterations = 10 seed_marker1 = self.queue_controller._get_counter(queue_name, self.project) self.assertEqual(seed_marker1, 1, 'First marker is 1') for i in range(iterations): self.controller.post(queue_name, [{'ttl': 60}], 'uuid', project=self.project) marker1 = self.queue_controller._get_counter(queue_name, self.project) marker2 = self.queue_controller._get_counter(queue_name, self.project) marker3 = self.queue_controller._get_counter(queue_name, self.project) self.assertEqual(marker1, marker2) self.assertEqual(marker2, marker3) self.assertEqual(marker1, i + 2) new_value = self.queue_controller._inc_counter(queue_name, self.project) self.assertIsNotNone(new_value) value_before = self.queue_controller._get_counter(queue_name, project=self.project) new_value = self.queue_controller._inc_counter(queue_name, project=self.project) self.assertIsNotNone(new_value) value_after = self.queue_controller._get_counter(queue_name, project=self.project) self.assertEqual(value_after, value_before + 1) value_before = value_after new_value = self.queue_controller._inc_counter(queue_name, project=self.project, amount=7) value_after = self.queue_controller._get_counter(queue_name, project=self.project) self.assertEqual(value_after, value_before + 7) self.assertEqual(value_after, new_value) reference_value = value_after unchanged = self.queue_controller._inc_counter(queue_name, project=self.project, window=10) self.assertIsNone(unchanged) now = timeutils.utcnow() + datetime.timedelta(seconds=10) timeutils_utcnow = 'zaqar.openstack.common.timeutils.utcnow' with mock.patch(timeutils_utcnow) as mock_utcnow: mock_utcnow.return_value = now changed = self.queue_controller._inc_counter(queue_name, project=self.project, window=5) self.assertEqual(changed, reference_value + 1)
def get(self, queue, claim_id, project=None): if project is None: project = '' cid = utils.cid_decode(claim_id) if cid is None: raise errors.ClaimDoesNotExist(claim_id, queue, project) with self.driver.trans() as trans: sel = sa.sql.select([tables.Claims.c.id, tables.Claims.c.ttl, tables.Claims.c.created], sa.and_(tables.Claims.c.ttl > utils.get_age(tables.Claims.c.created), tables.Claims.c.id == cid, tables.Queues.c.project == project, tables.Queues.c.name == queue), from_obj=[tables.Queues.join(tables.Claims)]) res = trans.execute(sel).fetchone() if res is None: raise errors.ClaimDoesNotExist(claim_id, queue, project) cid, ttl, created = res return ( {'id': claim_id, 'ttl': ttl, 'age': (timeutils.utcnow() - created).seconds}, list(self.__get(cid, trans)) )
def get(self, queue, claim_id, project=None): if project is None: project = '' cid = utils.cid_decode(claim_id) if cid is None: raise errors.ClaimDoesNotExist(claim_id, queue, project) with self.driver.trans() as trans: sel = sa.sql.select([ tables.Claims.c.id, tables.Claims.c.ttl, tables.Claims.c.created ], sa.and_( tables.Claims.c.ttl > utils.get_age( tables.Claims.c.created), tables.Claims.c.id == cid, tables.Queues.c.project == project, tables.Queues.c.name == queue), from_obj=[tables.Queues.join(tables.Claims)]) res = trans.execute(sel).fetchone() if res is None: raise errors.ClaimDoesNotExist(claim_id, queue, project) cid, ttl, created = res return ({ 'id': claim_id, 'ttl': ttl, 'age': (timeutils.utcnow() - created).seconds }, list(self.__get(cid, trans)))
def verify_message_stats(self, message): """Verifies the oldest & newest message stats :param message: oldest (or) newest message returned by queue_name/stats. """ expected_keys = ['age', 'created', 'href'] response_keys = message.keys() response_keys = sorted(response_keys) self.assertEqual(response_keys, expected_keys) # Verify that age has valid values age = message['age'] self.assertTrue(0 <= age <= self.limits.max_message_ttl, msg='Invalid Age {0}'.format(age)) # Verify that GET on href returns 200 path = message['href'] result = self.client.get(path) self.assertEqual(result.status_code, 200) # Verify that created time falls within the last 10 minutes # NOTE(malini): The messages are created during the test. created_time = message['created'] created_time = timeutils.normalize_time( timeutils.parse_isotime(created_time)) now = timeutils.utcnow() delta = timeutils.delta_seconds(before=created_time, after=now) # NOTE(malini): The 'int()' below is a work around for the small time # difference between julianday & UTC. # (needed to pass this test on sqlite driver) delta = int(delta) msg = ('Invalid Time Delta {0}, Created time {1}, Now {2}' .format(delta, created_time, now)) self.assertTrue(0 <= delta <= 6000, msg)
def verify_message_stats(self, message): """Verifies the oldest & newest message stats :param message: oldest (or) newest message returned by queue_name/stats. """ expected_keys = ['age', 'created', 'href'] response_keys = message.keys() response_keys = sorted(response_keys) self.assertEqual(response_keys, expected_keys) # Verify that age has valid values age = message['age'] self.assertTrue(0 <= age <= self.limits.max_message_ttl, msg='Invalid Age {0}'.format(age)) # Verify that GET on href returns 200 path = message['href'] result = self.client.get(path) self.assertEqual(result.status_code, 200) # Verify that created time falls within the last 10 minutes # NOTE(malini): The messages are created during the test. created_time = message['created'] created_time = timeutils.normalize_time( timeutils.parse_isotime(created_time)) now = timeutils.utcnow() delta = timeutils.delta_seconds(before=created_time, after=now) # NOTE(malini): The 'int()' below is a work around for the small time # difference between julianday & UTC. # (needed to pass this test on sqlite driver) delta = int(delta) msg = ('Invalid Time Delta {0}, Created time {1}, Now {2}'.format( delta, created_time, now)) self.assertTrue(0 <= delta <= 6000, msg)
def __get(self, cid, trans): # NOTE(flaper87): This probably needs to # join on `Claim` to check the claim ttl. sel = sa.sql.select([tables.Messages.c.id, tables.Messages.c.body, tables.Messages.c.ttl, tables.Messages.c.created], sa.and_( tables.Messages.c.ttl > utils.get_age(tables.Messages.c.created), # tables.Messages.c.ttl > # utils.get_age(tables.Claims.c.created), tables.Messages.c.cid == cid)) records = trans.execute(sel) for id, body, ttl, created in records: yield { 'id': utils.msgid_encode(int(id)), 'ttl': ttl, 'age': (timeutils.utcnow() - created).seconds, 'body': utils.json_decode(body), }
def first(self, queue, project=None, sort=1): if project is None: project = '' qid = utils.get_qid(self.driver, queue, project) sel = sa.sql.select([tables.Messages.c.id, tables.Messages.c.body, tables.Messages.c.ttl, tables.Messages.c.created], sa.and_( tables.Messages.c.ttl > sfunc.now() - tables.Messages.c.created, tables.Messages.c.qid == qid)) if sort not in (1, -1): raise ValueError(u'sort must be either 1 (ascending) ' u'or -1 (descending)') order = sa.asc if sort == -1: order = sa.desc sel = sel.order_by(order(tables.Messages.c.id)) try: id, body, ttl, created = self.driver.get(sel) except utils.NoResult: raise errors.QueueIsEmpty(queue, project) created_iso = timeutils.isotime(created) return { 'id': utils.msgid_encode(int(id)), 'ttl': ttl, 'created': created_iso, 'age': int((timeutils.utcnow() - created).seconds), 'body': body, }
def __get(self, cid, trans): # NOTE(flaper87): This probably needs to # join on `Claim` to check the claim ttl. sel = sa.sql.select( [ tables.Messages.c.id, tables.Messages.c.body, tables.Messages.c.ttl, tables.Messages.c.created ], sa.and_( tables.Messages.c.ttl > utils.get_age( tables.Messages.c.created), # tables.Messages.c.ttl > # utils.get_age(tables.Claims.c.created), tables.Messages.c.cid == cid)) records = trans.execute(sel) for id, body, ttl, created in records: yield { 'id': utils.msgid_encode(int(id)), 'ttl': ttl, 'age': (timeutils.utcnow() - created).seconds, 'body': utils.json_decode(body), }
def test_lifecycle(self): doc = '{"ttl": 100, "grace": 60}' # First, claim some messages body = self.simulate_post(self.claims_path, self.project_id, body=doc) self.assertEqual(self.srmock.status, falcon.HTTP_201) claimed = jsonutils.loads(body[0]) claim_href = self.srmock.headers_dict['Location'] message_href, params = claimed[0]['href'].split('?') # No more messages to claim self.simulate_post(self.claims_path, self.project_id, body=doc, query_string='limit=3') self.assertEqual(self.srmock.status, falcon.HTTP_204) headers = { 'Client-ID': str(uuid.uuid4()), } # Listing messages, by default, won't include claimed body = self.simulate_get(self.messages_path, self.project_id, headers=headers) self.assertEqual(self.srmock.status, falcon.HTTP_204) # Include claimed messages this time body = self.simulate_get(self.messages_path, self.project_id, query_string='include_claimed=true', headers=headers) listed = jsonutils.loads(body[0]) self.assertEqual(self.srmock.status, falcon.HTTP_200) self.assertEqual(len(listed['messages']), len(claimed)) now = timeutils.utcnow() + datetime.timedelta(seconds=10) timeutils_utcnow = 'zaqar.openstack.common.timeutils.utcnow' with mock.patch(timeutils_utcnow) as mock_utcnow: mock_utcnow.return_value = now body = self.simulate_get(claim_href, self.project_id) claim = jsonutils.loads(body[0]) self.assertEqual(self.srmock.status, falcon.HTTP_200) self.assertEqual(self.srmock.headers_dict['Content-Location'], claim_href) self.assertEqual(claim['ttl'], 100) # NOTE(cpp-cabrera): verify that claim age is non-negative self.assertThat(claim['age'], matchers.GreaterThan(-1)) # Try to delete the message without submitting a claim_id self.simulate_delete(message_href, self.project_id) self.assertEqual(self.srmock.status, falcon.HTTP_403) # Delete the message and its associated claim self.simulate_delete(message_href, self.project_id, query_string=params) self.assertEqual(self.srmock.status, falcon.HTTP_204) # Try to get it from the wrong project self.simulate_get(message_href, 'bogus_project', query_string=params) self.assertEqual(self.srmock.status, falcon.HTTP_404) # Get the message self.simulate_get(message_href, self.project_id, query_string=params) self.assertEqual(self.srmock.status, falcon.HTTP_404) # Update the claim new_claim_ttl = '{"ttl": 60}' creation = timeutils.utcnow() self.simulate_patch(claim_href, self.project_id, body=new_claim_ttl) self.assertEqual(self.srmock.status, falcon.HTTP_204) # Get the claimed messages (again) body = self.simulate_get(claim_href, self.project_id) query = timeutils.utcnow() claim = jsonutils.loads(body[0]) message_href, params = claim['messages'][0]['href'].split('?') self.assertEqual(claim['ttl'], 60) estimated_age = timeutils.delta_seconds(creation, query) self.assertTrue(estimated_age > claim['age']) # Delete the claim self.simulate_delete(claim['href'], 'bad_id') self.assertEqual(self.srmock.status, falcon.HTTP_204) self.simulate_delete(claim['href'], self.project_id) self.assertEqual(self.srmock.status, falcon.HTTP_204) # Try to delete a message with an invalid claim ID self.simulate_delete(message_href, self.project_id, query_string=params) self.assertEqual(self.srmock.status, falcon.HTTP_403) # Make sure it wasn't deleted! self.simulate_get(message_href, self.project_id, query_string=params) self.assertEqual(self.srmock.status, falcon.HTTP_200) # Try to get a claim that doesn't exist self.simulate_get(claim['href']) self.assertEqual(self.srmock.status, falcon.HTTP_404) # Try to update a claim that doesn't exist self.simulate_patch(claim['href'], body=doc) self.assertEqual(self.srmock.status, falcon.HTTP_404)
def test_message_counter(self): queue_name = self.queue_name iterations = 10 seed_marker1 = self.queue_controller._get_counter( queue_name, self.project) self.assertEqual(seed_marker1, 1, 'First marker is 1') for i in range(iterations): self.controller.post(queue_name, [{ 'ttl': 60 }], 'uuid', project=self.project) marker1 = self.queue_controller._get_counter( queue_name, self.project) marker2 = self.queue_controller._get_counter( queue_name, self.project) marker3 = self.queue_controller._get_counter( queue_name, self.project) self.assertEqual(marker1, marker2) self.assertEqual(marker2, marker3) self.assertEqual(marker1, i + 2) new_value = self.queue_controller._inc_counter(queue_name, self.project) self.assertIsNotNone(new_value) value_before = self.queue_controller._get_counter(queue_name, project=self.project) new_value = self.queue_controller._inc_counter(queue_name, project=self.project) self.assertIsNotNone(new_value) value_after = self.queue_controller._get_counter(queue_name, project=self.project) self.assertEqual(value_after, value_before + 1) value_before = value_after new_value = self.queue_controller._inc_counter(queue_name, project=self.project, amount=7) value_after = self.queue_controller._get_counter(queue_name, project=self.project) self.assertEqual(value_after, value_before + 7) self.assertEqual(value_after, new_value) reference_value = value_after unchanged = self.queue_controller._inc_counter(queue_name, project=self.project, window=10) self.assertIsNone(unchanged) now = timeutils.utcnow() + datetime.timedelta(seconds=10) timeutils_utcnow = 'zaqar.openstack.common.timeutils.utcnow' with mock.patch(timeutils_utcnow) as mock_utcnow: mock_utcnow.return_value = now changed = self.queue_controller._inc_counter(queue_name, project=self.project, window=5) self.assertEqual(changed, reference_value + 1)
def test_lifecycle(self): doc = '{"ttl": 100, "grace": 60}' # First, claim some messages body = self.simulate_post(self.claims_path, body=doc, headers=self.headers) self.assertEqual(self.srmock.status, falcon.HTTP_201) claimed = jsonutils.loads(body[0]) claim_href = self.srmock.headers_dict['Location'] message_href, params = claimed[0]['href'].split('?') # No more messages to claim self.simulate_post(self.claims_path, body=doc, query_string='limit=3', headers=self.headers) self.assertEqual(self.srmock.status, falcon.HTTP_204) # Listing messages, by default, won't include claimed, will echo body = self.simulate_get(self.messages_path, headers=self.headers, query_string="echo=true") self.assertEqual(self.srmock.status, falcon.HTTP_200) self._empty_message_list(body) # Listing messages, by default, won't include claimed, won't echo body = self.simulate_get(self.messages_path, headers=self.headers, query_string="echo=false") self.assertEqual(self.srmock.status, falcon.HTTP_200) self._empty_message_list(body) # List messages, include_claimed, but don't echo body = self.simulate_get(self.messages_path, query_string='include_claimed=true' '&echo=false', headers=self.headers) self.assertEqual(self.srmock.status, falcon.HTTP_200) self._empty_message_list(body) # List messages with a different client-id and echo=false. # Should return some messages headers = self.headers.copy() headers["Client-ID"] = str(uuid.uuid4()) body = self.simulate_get(self.messages_path, query_string='include_claimed=true' '&echo=false', headers=headers) self.assertEqual(self.srmock.status, falcon.HTTP_200) # Include claimed messages this time, and echo body = self.simulate_get(self.messages_path, query_string='include_claimed=true' '&echo=true', headers=self.headers) listed = jsonutils.loads(body[0]) self.assertEqual(self.srmock.status, falcon.HTTP_200) self.assertEqual(len(listed['messages']), len(claimed)) now = timeutils.utcnow() + datetime.timedelta(seconds=10) timeutils_utcnow = 'zaqar.openstack.common.timeutils.utcnow' with mock.patch(timeutils_utcnow) as mock_utcnow: mock_utcnow.return_value = now body = self.simulate_get(claim_href, headers=self.headers) claim = jsonutils.loads(body[0]) self.assertEqual(self.srmock.status, falcon.HTTP_200) self.assertEqual(self.srmock.headers_dict['Content-Location'], claim_href) self.assertEqual(claim['ttl'], 100) # NOTE(cpp-cabrera): verify that claim age is non-negative self.assertThat(claim['age'], matchers.GreaterThan(-1)) # Try to delete the message without submitting a claim_id self.simulate_delete(message_href, headers=self.headers) self.assertEqual(self.srmock.status, falcon.HTTP_403) # Delete the message and its associated claim self.simulate_delete(message_href, query_string=params, headers=self.headers) self.assertEqual(self.srmock.status, falcon.HTTP_204) # Try to get it from the wrong project headers = { 'Client-ID': str(uuid.uuid4()), 'X-Project-ID': 'bogusproject' } self.simulate_get(message_href, query_string=params, headers=headers) self.assertEqual(self.srmock.status, falcon.HTTP_404) # Get the message self.simulate_get(message_href, query_string=params, headers=self.headers) self.assertEqual(self.srmock.status, falcon.HTTP_404) # Update the claim new_claim_ttl = '{"ttl": 60}' creation = timeutils.utcnow() self.simulate_patch(claim_href, body=new_claim_ttl, headers=self.headers) self.assertEqual(self.srmock.status, falcon.HTTP_204) # Get the claimed messages (again) body = self.simulate_get(claim_href, headers=self.headers) query = timeutils.utcnow() claim = jsonutils.loads(body[0]) message_href, params = claim['messages'][0]['href'].split('?') self.assertEqual(claim['ttl'], 60) estimated_age = timeutils.delta_seconds(creation, query) self.assertTrue(estimated_age > claim['age']) # Delete the claim self.simulate_delete(claim['href'], headers=self.headers) self.assertEqual(self.srmock.status, falcon.HTTP_204) # Try to delete a message with an invalid claim ID self.simulate_delete(message_href, query_string=params, headers=self.headers) self.assertEqual(self.srmock.status, falcon.HTTP_403) # Make sure it wasn't deleted! self.simulate_get(message_href, query_string=params, headers=self.headers) self.assertEqual(self.srmock.status, falcon.HTTP_200) # Try to get a claim that doesn't exist self.simulate_get(claim['href'], headers=self.headers) self.assertEqual(self.srmock.status, falcon.HTTP_404) # Try to update a claim that doesn't exist self.simulate_patch(claim['href'], body=doc, headers=self.headers) self.assertEqual(self.srmock.status, falcon.HTTP_404)
def test_queue_lifecycle(self): # Test queue creation created = self.controller.create('test', metadata=dict(meta='test_meta'), project=self.project) self.assertTrue(created) # Test queue existence self.assertTrue(self.controller.exists('test', project=self.project)) # Test queue retrieval interaction = self.controller.list(project=self.project) queue = list(next(interaction))[0] self.assertEqual(queue['name'], 'test') # Test queue metadata retrieval metadata = self.controller.get('test', project=self.project) self.assertEqual(metadata['meta'], 'test_meta') # Touching an existing queue does not affect metadata created = self.controller.create('test', project=self.project) self.assertFalse(created) metadata = self.controller.get('test', project=self.project) self.assertEqual(metadata['meta'], 'test_meta') client_uuid = uuid.uuid4() # Test queue statistic _insert_fixtures(self.message_controller, 'test', project=self.project, client_uuid=client_uuid, num=6) # NOTE(kgriffs): We can't get around doing this, because # we don't know how the storage drive may be calculating # message timestamps (and may not be monkey-patchable). time.sleep(1.2) _insert_fixtures(self.message_controller, 'test', project=self.project, client_uuid=client_uuid, num=6) stats = self.controller.stats('test', project=self.project) message_stats = stats['messages'] self.assertEqual(message_stats['free'], 12) self.assertEqual(message_stats['claimed'], 0) self.assertEqual(message_stats['total'], 12) oldest = message_stats['oldest'] newest = message_stats['newest'] self.assertNotEqual(oldest, newest) age = oldest['age'] self.assertThat(age, matchers.GreaterThan(0)) # NOTE(kgriffs): Ensure is different enough # for the next comparison to work. soon = timeutils.utcnow() + datetime.timedelta(seconds=60) for message_stat in (oldest, newest): created_iso = message_stat['created'] created = timeutils.parse_isotime(created_iso) self.assertThat(timeutils.normalize_time(created), matchers.LessThan(soon)) self.assertIn('id', message_stat) self.assertThat(oldest['created'], matchers.LessThan(newest['created'])) # Test queue deletion self.controller.delete('test', project=self.project) # Test queue existence self.assertFalse(self.controller.exists('test', project=self.project))
def _test_post(self, sample_messages): sample_doc = jsonutils.dumps({'messages': sample_messages}) result = self.simulate_post(self.messages_path, 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']) # NOTE(kgriffs): As of v1.1, "partial" is no longer given # in the response document. self.assertNotIn('partial', result_doc) 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 = 'zaqar.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 headers = self.headers.copy() headers['X-Project-ID'] = '777777' # Wrong project ID self.simulate_get(message_uri, headers=headers) self.assertEqual(self.srmock.status, falcon.HTTP_404) # Correct project ID result = self.simulate_get(message_uri, headers=self.headers) 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, query_string=query_string, headers=self.headers) 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 _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']) # NOTE(kgriffs): As of the Icehouse release, drivers are # required to either completely succeed, or completely fail # to enqueue the entire batch of messages. 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 = 'zaqar.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 _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']) # NOTE(kgriffs): As of the Icehouse release, drivers are # required to either completely succeed, or completely fail # to enqueue the entire batch of messages. 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 = 'zaqar.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 _test_post(self, sample_messages): sample_doc = jsonutils.dumps({'messages': sample_messages}) result = self.simulate_post(self.messages_path, 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']) # NOTE(kgriffs): As of v1.1, "partial" is no longer given # in the response document. self.assertNotIn('partial', result_doc) 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 = 'zaqar.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 headers = self.headers.copy() headers['X-Project-ID'] = '777777' # Wrong project ID self.simulate_get(message_uri, headers=headers) self.assertEqual(self.srmock.status, falcon.HTTP_404) # Correct project ID result = self.simulate_get(message_uri, headers=self.headers) 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, query_string=query_string, headers=self.headers) 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)