def test_queue_metadata_update(self): test_metadata = {'type': 'Bank Accounts', 'name': 'test1'} with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, json.dumps(test_metadata)) send_method.return_value = resp # add 'test_metadata' metadata = self.queue.metadata(new_meta=test_metadata) self.assertEqual(test_metadata, metadata) new_metadata_replace = {'type': 'test', 'name': 'test1'} with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, json.dumps(new_metadata_replace)) send_method.return_value = resp # repalce 'type' metadata = self.queue.metadata(new_meta=new_metadata_replace) expect_metadata = {'type': 'test', "name": 'test1'} self.assertEqual(expect_metadata, metadata) remove_metadata = {'name': 'test1'} with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, json.dumps(remove_metadata)) send_method.return_value = resp # remove 'type' metadata = self.queue.metadata(new_meta=remove_metadata) expect_metadata = {"name": 'test1'} self.assertEqual(expect_metadata, metadata)
def test_subscription_delete(self): subscription_data = {'subscriber': 'http://trigger.me', 'ttl': 3600} with mock.patch.object(self.transport, 'send', autospec=True) as send_method: create_resp = response.Response(None, '{"subscription_id": "fake_id"}') get_content = ('{"subscriber": "http://trigger.me","ttl": 3600, ' '"id": "fake_id"}') get_resp = response.Response(None, get_content) send_method.side_effect = iter( [create_resp, get_resp, None, errors.ResourceNotFound]) # NOTE(flwang): This will call # ensure exists in the client instance # since auto_create's default is True subscription = self.client.subscription('beijing', **subscription_data) self.assertEqual('http://trigger.me', subscription.subscriber) self.assertEqual(3600, subscription.ttl) self.assertEqual('fake_id', subscription.id) subscription.delete() self.assertRaises(errors.ResourceNotFound, self.client.subscription, 'beijing', **{'id': 'fake_id'})
def test_flavor_delete(self): flavor_data = {'pool': 'stomach'} with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, None) resp_data = response.Response(None, json.dumps(flavor_data)) send_method.side_effect = iter([resp_data, resp]) # NOTE(flaper87): This will call # ensure exists in the client instance # since auto_create's default is True flavor = self.client.flavor('tasty', **flavor_data) flavor.delete()
def test_pool_delete(self): pool_data = {'weight': 10, 'uri': 'sqlite://', 'options': {}} with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, None) resp_data = response.Response(None, json.dumps(pool_data)) send_method.side_effect = iter([resp_data, resp]) # NOTE(flaper87): This will call # ensure exists in the client instance # since auto_create's default is True pool = self.client.pool('test', **pool_data) pool.delete()
def test_iterator_respect_paging(self): messages = { 'links': [], 'messages': [{ 'href': '/v1/queues/mine/messages/123123423', 'ttl': 800, 'age': 790, 'body': { 'event': 'ActivateAccount', 'mode': 'active' } }] } with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, json.dumps(messages)) send_method.return_value = resp link = { 'rel': 'next', 'href': "/v1/queues/mine/messages?marker=6244-244224-783" } messages['links'].append(link) iterator = iterate._Iterator(self.queue.client, messages, 'messages', message.create_object(self.queue)) iterated = [msg for msg in iterator] self.assertEqual(1, len(iterated))
def send(self, request): url, method, request = self._prepare(request) # NOTE(flape87): Do not modify # request's headers directly. headers = request.headers.copy() headers['content-type'] = 'application/json' resp = self.client.request(method, url=url, params=request.params, headers=headers, data=request.content) if resp.status_code in self.http_to_zaqar: try: msg = json.loads(resp.text)['description'] except Exception: # TODO(flaper87): Log this exception # but don't stop raising the corresponding # exception msg = '' raise self.http_to_zaqar[resp.status_code](msg) # NOTE(flaper87): This reads the whole content # and will consume any attempt of streaming. return response.Response(request, resp.text, headers=resp.headers)
def test_queue_subscriptions(self): result = { "subscriptions": [{ "source": 'test', "id": "1", "subscriber": 'http://trigger.me', "ttl": 3600, "options": {} }, { "source": 'test', "id": "2", "subscriber": 'http://trigger.you', "ttl": 7200, "options": {} }] } with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, json.dumps(result)) send_method.return_value = resp subscriptions = self.queue.subscriptions() subscriber_list = [s.subscriber for s in list(subscriptions)] self.assertIn('http://trigger.me', subscriber_list) self.assertIn('http://trigger.you', subscriber_list)
def test_claim(self): result = [{ 'href': '/v1/queues/fizbit/messages/50b68a50d6f5b8c8a7c62b01', 'ttl': 800, 'age': 790, 'body': { 'event': 'ActivateAccount', 'mode': 'active' } }, { 'href': '/v1/queues/fizbit/messages/50b68a50d6f5b8c8a7c62b02', 'ttl': 800, 'age': 790, 'body': { 'event': 'ActivateAccount', 'mode': 'active' } }] with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, json.dumps(result)) send_method.return_value = resp claimed = self.queue.claim(ttl=60, grace=60) # messages doesn't support len() num_tested = 0 for num, msg in enumerate(claimed): num_tested += 1 self.assertEqual(result[num]['href'], msg.href) self.assertEqual(len(result), num_tested)
def test_message_list(self): returned = { 'links': [{ 'rel': 'next', 'href': '/v1/queues/fizbit/messages?marker=6244-244224-783' }], 'messages': [{ 'href': '/v1/queues/fizbit/messages/50b68a50d6f5b8c8a7c62b01', 'ttl': 800, 'age': 790, 'body': { 'event': 'ActivateAccount', 'mode': 'active' } }] } with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, json.dumps(returned)) send_method.return_value = resp msgs = self.queue.messages(limit=1) self.assertIsInstance(msgs, iterator._Iterator)
def test_message_pop(self): returned = [{ 'href': '/v1/queues/fizbit/messages/50b68a50d6f5b8c8a7c62b01', 'ttl': 800, 'age': 790, 'body': { 'event': 'ActivateAccount', 'mode': 'active' } }, { 'href': '/v1/queues/fizbit/messages/50b68a50d6f5b8c8a7c62b02', 'ttl': 800, 'age': 790, 'body': { 'event': 'ActivateAccount', 'mode': 'active' } }] with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, json.dumps(returned)) send_method.return_value = resp msg = self.queue.pop(count=2) self.assertIsInstance(msg, iterator._Iterator)
def test_stream(self): messages = { 'links': [], 'messages': [{ 'href': '/v1/queues/mine/messages/123123423', 'ttl': 800, 'age': 790, 'body': { 'event': 'ActivateAccount', 'mode': 'active' } }] } with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, json.dumps(messages)) send_method.return_value = resp # NOTE(flaper87): The first iteration will return 1 message # and then call `_next_page` which will use the rel-next link # to get a new set of messages. link = { 'rel': 'next', 'href': "/v1/queues/mine/messages?marker=6244-244224-783" } messages['links'].append(link) iterator = iterate._Iterator(self.queue.client, messages, 'messages', message.create_object(self.queue)) iterated = [msg for msg in iterator.stream()] self.assertEqual(2, len(iterated))
def test_queue_stats(self): result = { "messages": { "free": 146929, "claimed": 2409, "total": 149338, "oldest": { "href": "/v1/queues/qq/messages/50b68a50d6f5b8c8a7c62b01", "age": 63, "created": "2013-08-12T20:44:55Z" }, "newest": { "href": "/v1/queues/qq/messages/50b68a50d6f5b8c8a7c62b01", "age": 12, "created": "2013-08-12T20:45:46Z" } } } with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, json.dumps(result)) send_method.return_value = resp stats = self.queue.stats self.assertEqual(result, stats)
def test_claim_get_by_id(self): result = { 'href': '/v1/queues/fizbit/messages/50b68a50d6cb01?claim_id=4524', 'age': 790, 'ttl': 800, 'messages': [{ 'href': '/v1/queues/fizbit/messages/50b68a50d6f5b8c8a7c62b01', 'ttl': 800, 'age': 790, 'body': { 'event': 'ActivateAccount', 'mode': 'active' } }] } with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, json.dumps(result)) send_method.return_value = resp cl = self.queue.claim(id='5245432') # messages doesn't support len() num_tested = 0 for num, msg in enumerate(cl): num_tested += 1 self.assertEqual(result['messages'][num]['href'], msg.href) self.assertEqual(len(result['messages']), num_tested)
def test_queue_exists(self): with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, None) send_method.return_value = resp self.queue.exists()
def test_claim_update(self): with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, None) send_method.return_value = resp self.queue.claim(id='5245432').update(ttl=444, grace=987)
def test_get_queue_metadata(self): with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, '{}') send_method.return_value = resp req = request.Request() core.queue_get_metadata(self.transport, req, 'test')
def test_health(self): with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, None) send_method.return_value = resp req = request.Request() core.health(self.transport, req)
def test_queue_delete(self): with mock.patch.object(self.transport, 'send', autospec=True) as send_method: send_method.return_value = response.Response(None, None) req = request.Request() core.queue_delete(self.transport, req, 'test') self.assertIn('queue_name', req.params)
def test_claim_delete(self): with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, None) send_method.return_value = resp self.queue.claim(id='4225').delete()
def test_queue_get_stats(self): with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, '{}') send_method.return_value = resp req = request.Request() result = core.queue_get_stats(self.transport, req, 'test') self.assertEqual({}, result)
def test_set_queue_metadata(self): update_data = {'some': 'data'} with mock.patch.object(self.transport, 'send', autospec=True) as send_method: send_method.return_value = response.Response(None, None) req = request.Request() core.queue_exists(self.transport, req, update_data, 'test') self.assertIn('queue_name', req.params)
def test_message_delete(self): with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, None) send_method.return_value = resp req = request.Request() core.message_delete(self.transport, req, 'test', 'message_id')
def test_queue_metadata(self): test_metadata = {'type': 'Bank Accounts'} with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, json.dumps(test_metadata)) send_method.return_value = resp self.assertRaises(RuntimeError, self.queue.metadata, test_metadata)
def send(self, request): self._test.assertEqual('ws://127.0.0.1:9000/', request.endpoint) if request.operation == 'message_list': body = json.loads(request.content) self._test.assertEqual('4f3f46d3-09f1-42a7-8c13-f91a5457192c', body['queue_name']) return response.Response(request, content=json.dumps(self._messages), status_code=200)
def test_queue_purge_messages(self): with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, None) send_method.return_value = resp self.queue.purge(resource_types=['messages']) self.assertEqual({"resource_types": ["messages"]}, json.loads(send_method.call_args[0][0].content))
def test_message_delete_many(self): with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, None) send_method.return_value = resp rst = self.queue.delete_messages('50b68a50d6f5b8c8a7c62b01', '50b68a50d6f5b8c8a7c62b02') self.assertIsNone(rst)
def test_pool_create(self): with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, None) send_method.return_value = resp req = request.Request() core.pool_create(self.transport, req, 'test_pool', {'uri': 'sqlite://', 'weight': 0})
def test_message_list(self): with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, '{}') send_method.return_value = resp req = request.Request() core.message_list(self.transport, req, 'test') self.assertIn('queue_name', req.params)
def test_flavor_update(self): flavor_data = {'pool': 'stomach'} updated_data = {'pool': 'belly'} with mock.patch.object(self.transport, 'send', autospec=True) as send_method: resp = response.Response(None, json.dumps(updated_data)) send_method.return_value = resp flavor = self.client.flavor('tasty', **flavor_data) flavor.update({'pool': 'belly'}) self.assertEqual('belly', flavor.pool)
def send(self, request): url, method, request = self._prepare(request) # NOTE(flape87): Do not modify # request's headers directly. headers = request.headers.copy() if (request.operation == 'queue_update' and (version.LooseVersion( request.api.label) >= version.LooseVersion('v2'))): headers['content-type'] = \ 'application/openstack-messaging-v2.0-json-patch' else: headers['content-type'] = 'application/json' if osprofiler_web: headers.update(osprofiler_web.get_trace_id_headers()) if request.verify: if request.cert: verify = request.cert else: verify = True else: verify = False resp = self.client.request(method, url=url, params=request.params, headers=headers, data=request.content, verify=verify) if resp.status_code in self.http_to_zaqar: kwargs = {} try: error_body = json.loads(resp.text) kwargs['title'] = error_body['title'] kwargs['description'] = error_body['description'] except Exception: # TODO(flaper87): Log this exception # but don't stop raising the corresponding # exception # Note(Eva-i): most of the error responses from Zaqar have # dict with title and description in their bodies. If it's not # the case, let's just show body text. kwargs['text'] = resp.text raise self.http_to_zaqar[resp.status_code](**kwargs) # NOTE(flaper87): This reads the whole content # and will consume any attempt of streaming. return response.Response(request, resp.text, headers=resp.headers, status_code=resp.status_code)