def test_expired_claim(self): meta = {'ttl': 0, 'grace': 60} claim_id, messages = self.controller.create(self.queue_name, meta, project=self.project) with testing.expect(storage.exceptions.DoesNotExist): self.controller.get(self.queue_name, claim_id, project=self.project) with testing.expect(storage.exceptions.DoesNotExist): self.controller.update(self.queue_name, claim_id, meta, project=self.project)
def test_message_lifecycle(self): queue_name = self.queue_name messages = [ { 'ttl': 60, 'body': { 'event': 'BackupStarted', 'backupId': 'c378813c-3f0b-11e2-ad92-7823d2b0f3ce' } }, ] # Test Message Creation created = list(self.controller.post(queue_name, messages, project=self.project, client_uuid='unused')) self.assertEqual(len(created), 1) # Test Message Get self.controller.get(queue_name, created[0], project=self.project) # Test Message Deletion self.controller.delete(queue_name, created[0], project=self.project) # Test does not exist with testing.expect(storage.exceptions.DoesNotExist): self.controller.get(queue_name, created[0], project=self.project)
def test_illformed_id(self): # any ill-formed IDs should be regarded as non-existing ones. self.controller.delete(self.queue_name, 'illformed', project=self.project) with testing.expect(exceptions.DoesNotExist): self.controller.get(self.queue_name, 'illformed', project=self.project) with testing.expect(exceptions.DoesNotExist): self.controller.update(self.queue_name, 'illformed', {'ttl': 40}, project=self.project)
def test_illformed_id(self): # any ill-formed IDs should be regarded as non-existing ones. self.queue_controller.create('unused', '480924') self.controller.delete('unused', 'illformed', '480924') with testing.expect(exceptions.DoesNotExist): self.controller.update('unused', 'illformed', {'ttl': 40}, '480924')
def test_bad_id(self): # A malformed ID should result in an error. This # doesn't hurt anything, since an attacker could just # read the source code anyway to find out how IDs are # implemented. Plus, if someone is just trying to # get a message that they don't own, they would # more likely just list the messages, not try to # guess an ID of an arbitrary message. queue = 'foo' project = '480924' self.queue_controller.create(queue, project) bad_message_id = 'xyz' with testing.expect(exceptions.MalformedID): self.controller.delete(queue, bad_message_id, project) with testing.expect(exceptions.MalformedID): self.controller.get(queue, bad_message_id, project)
def test_claim_effects(self): _insert_fixtures(self.controller, self.queue_name, project=self.project, client_uuid='my_uuid', num=12) meta = {'ttl': 70, 'grace': 60} another_cid, _ = self.claim_controller.create(self.queue_name, meta, project=self.project) cid, msgs = self.claim_controller.create(self.queue_name, meta, project=self.project) [msg1, msg2] = msgs # A wrong claim does not ensure the message deletion with testing.expect(storage.exceptions.NotPermitted): self.controller.delete(self.queue_name, msg1['id'], project=self.project, claim=another_cid) # Make sure a message can be deleted with a claim self.controller.delete(self.queue_name, msg1['id'], project=self.project, claim=cid) with testing.expect(StopIteration): result = self.controller.get(self.queue_name, msg1['id'], project=self.project) next(result) # Make sure such a deletion is idempotent self.controller.delete(self.queue_name, msg1['id'], project=self.project, claim=cid) # A non-existing claim does not ensure the message deletion self.claim_controller.delete(self.queue_name, cid, project=self.project) with testing.expect(storage.exceptions.NotPermitted): self.controller.delete(self.queue_name, msg2['id'], project=self.project, claim=cid)
def test_bad_claim_id(self): self.queue_controller.create('unused', '480924') [msgid] = self.controller.post('unused', [{'body': {}, 'ttl': 10}], project='480924', client_uuid='unused') bad_claim_id = '; DROP TABLE queues' with testing.expect(exceptions.MalformedID): self.controller.delete('unused', msgid, project='480924', claim=bad_claim_id)
def test_queue_lifecycle(self): # Test Queue Creation created = self.controller.create('test', project=self.project) self.assertTrue(created) # Test Queue retrieval metadata = self.controller.get_metadata('test', project=self.project) self.assertEqual(metadata, {}) # Test Queue Update created = self.controller.set_metadata('test', project=self.project, metadata=dict(meta='test_meta')) metadata = self.controller.get_metadata('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_metadata('test', project=self.project) self.assertEqual(metadata['meta'], 'test_meta') # Test Queue Statistic _insert_fixtures(self.message_controller, 'test', project=self.project, client_uuid='my_uuid', num=12) countof = self.controller.stats('test', project=self.project) self.assertEqual(countof['messages']['free'], 12) # Test Queue Deletion self.controller.delete('test', project=self.project) # Test DoesNotExist Exception with testing.expect(storage.exceptions.DoesNotExist): self.controller.get_metadata('test', project=self.project) with testing.expect(storage.exceptions.DoesNotExist): self.controller.set_metadata('test', '{}', project=self.project)
def test_expired_message(self): messages = [{'body': 3.14, 'ttl': 0}] [msgid] = self.controller.post(self.queue_name, messages, project=self.project, client_uuid='my_uuid') with testing.expect(storage.exceptions.DoesNotExist): self.controller.get(self.queue_name, msgid, project=self.project) countof = self.queue_controller.stats(self.queue_name, project=self.project) self.assertEquals(countof['messages']['free'], 0)
def test_bad_id(self): # NOTE(cpp-cabrera): A malformed ID should result in an empty # query. Raising an exception for validating IDs makes the # implementation more verbose instead of taking advantage of # the Maybe/Optional protocol, particularly when dealing with # bulk operations. bad_message_id = 'xyz' self.controller.delete(self.queue_name, bad_message_id, project=self.project) with testing.expect(exceptions.MessageDoesNotExist): self.controller.get(self.queue_name, bad_message_id, project=self.project)
def test_expired_message(self): messages = [{'body': 3.14, 'ttl': 0}] [msgid] = self.controller.post(self.queue_name, messages, project=self.project, client_uuid='my_uuid') with testing.expect(StopIteration): result = self.controller.get(self.queue_name, msgid, project=self.project) next(result) countof = self.queue_controller.stats(self.queue_name, project=self.project) self.assertEquals(countof['messages']['free'], 0)
def test_multi_ids(self): messages_in = [{'ttl': 120, 'body': 0}, {'ttl': 240, 'body': 1}] ids = self.controller.post(self.queue_name, messages_in, project=self.project, client_uuid='my_uuid') messages_out = self.controller.bulk_get(self.queue_name, ids, project=self.project) for idx, message in enumerate(messages_out): self.assertEquals(message['body'], idx) self.controller.bulk_delete(self.queue_name, ids, project=self.project) with testing.expect(StopIteration): result = self.controller.bulk_get(self.queue_name, ids, project=self.project) next(result)
def test_claim_effects(self): _insert_fixtures(self.controller, self.queue_name, project=self.project, client_uuid='my_uuid', num=12) def list_messages(include_claimed=None): kwargs = { 'project': self.project, 'client_uuid': 'my_uuid', 'echo': True, } # Properly test default value if include_claimed is not None: kwargs['include_claimed'] = include_claimed interaction = self.controller.list(self.queue_name, **kwargs) messages = next(interaction) return [msg['id'] for msg in messages] messages_before = list_messages(True) meta = {'ttl': 70, 'grace': 60} another_cid, _ = self.claim_controller.create(self.queue_name, meta, project=self.project) messages_after = list_messages(True) self.assertEqual(messages_before, messages_after) messages_excluding_claimed = list_messages() self.assertNotEqual(messages_before, messages_excluding_claimed) self.assertEqual(2, len(messages_excluding_claimed)) cid, msgs = self.claim_controller.create(self.queue_name, meta, project=self.project) [msg1, msg2] = msgs # A wrong claim does not ensure the message deletion with testing.expect(storage.exceptions.NotPermitted): self.controller.delete(self.queue_name, msg1['id'], project=self.project, claim=another_cid) # Make sure a message can be deleted with a claim self.controller.delete(self.queue_name, msg1['id'], project=self.project, claim=cid) with testing.expect(storage.exceptions.DoesNotExist): self.controller.get(self.queue_name, msg1['id'], project=self.project) # Make sure such a deletion is idempotent self.controller.delete(self.queue_name, msg1['id'], project=self.project, claim=cid) # A non-existing claim does not ensure the message deletion self.claim_controller.delete(self.queue_name, cid, project=self.project) with testing.expect(storage.exceptions.NotPermitted): self.controller.delete(self.queue_name, msg2['id'], project=self.project, claim=cid)
def test_queue_lifecycle(self): # Test Queue Creation created = self.controller.create('test', project=self.project) self.assertTrue(created) # Test Queue Existence self.assertTrue(self.controller.exists('test', project=self.project)) # Test Queue retrieval metadata = self.controller.get_metadata('test', project=self.project) self.assertEqual(metadata, {}) # Test Queue Update created = self.controller.set_metadata('test', project=self.project, metadata=dict(meta='test_meta')) metadata = self.controller.get_metadata('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_metadata('test', project=self.project) self.assertEqual(metadata['meta'], 'test_meta') # Test Queue Statistic _insert_fixtures(self.message_controller, 'test', project=self.project, client_uuid='my_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) _insert_fixtures(self.message_controller, 'test', project=self.project, client_uuid='my_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) # NOTE(kgriffs): Ensure "now" is different enough # for the next comparison to work. timeutils.set_time_override() timeutils.advance_time_seconds(10) 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(timeutils.utcnow())) 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)) # Test DoesNotExist Exception with testing.expect(storage.exceptions.DoesNotExist): self.controller.get_metadata('test', project=self.project) with testing.expect(storage.exceptions.DoesNotExist): self.controller.set_metadata('test', '{}', project=self.project)
def test_wrong_type(self): ns = config.namespace('local') with testing.expect(config.cfg.Error): ns.from_options(opt={})