def test_responder_unserializable_result(mock_publish): message = Mock() message.properties = {'reply_to': ''} config = {AMQP_URI_CONFIG_KEY: ''} responder = Responder(config, message) # unserialisable result worker_result = object() result, exc_info = responder.send_response(worker_result, None) # responder will return the TypeError from json.dumps assert result is None assert exc_info == (TypeError, ANY, ANY) assert str(exc_info[1]) == "{} is not JSON serializable".format( worker_result) # and publish a dictionary-serialized UnserializableValueError # on worker_result expected_msg = { 'result': None, 'error': { 'exc_path': 'nameko.exceptions.UnserializableValueError', 'value': 'Unserializable value: `{}`'.format(worker_result), 'exc_type': 'UnserializableValueError', 'exc_args': [], } } (msg,), _ = mock_publish.call_args assert msg == expected_msg
def test_responder_worker_exc(mock_publish): message = Mock() message.properties = {"reply_to": ""} container = Mock() container.config = {AMQP_URI_CONFIG_KEY: ""} responder = Responder(message) # serialisable exception worker_exc = Exception("error") result, exc_info = responder.send_response(container, None, (Exception, worker_exc, "tb")) assert result is None assert exc_info == (Exception, worker_exc, "tb") expected_msg = { "result": None, "error": { "exc_path": "exceptions.Exception", "value": "error", "exc_type": "Exception", "exc_args": ("error",), }, } (msg,), _ = mock_publish.call_args assert msg == expected_msg
def test_responder_unserializable_exc(mock_publish): message = Mock() message.properties = {"reply_to": ""} container = Mock() container.config = {AMQP_URI_CONFIG_KEY: ""} responder = Responder(message) # unserialisable exception worker_exc = Exception(object()) result, exc_info = responder.send_response(container, True, (Exception, worker_exc, "tb")) # responder will return the TypeError from json.dumps assert result is None assert exc_info == (TypeError, ANY, ANY) assert exc_info[1].message == ("{} is not JSON " "serializable".format(worker_exc.args[0])) # and publish a dictionary-serialized UnserializableValueError # (where the unserialisable value is a dictionary-serialized worker_exc) serialized_exc = serialize(worker_exc) expected_msg = { "result": None, "error": { "exc_path": "nameko.exceptions.UnserializableValueError", "value": "Unserializable value: `{}`".format(serialized_exc), "exc_type": "UnserializableValueError", "exc_args": (), }, } (msg,), _ = mock_publish.call_args assert msg == expected_msg
def test_responder_unserializable_result( mock_publish, unserializable, serializer, exception_info_string): message = Mock() message.properties = {'reply_to': ''} config = {AMQP_URI_CONFIG_KEY: '', SERIALIZER_CONFIG_KEY: serializer} responder = Responder(config, message) # unserialisable result worker_result = unserializable result, exc_info = responder.send_response(worker_result, None) # responder will return the error from the serializer assert result is None # Different kombu versions return different exceptions, so # testing for the concrete exception is not feasible assert exc_info == (ANY, ANY, ANY) assert exception_info_string in str(exc_info[1]) # and publish a dictionary-serialized UnserializableValueError # on worker_result expected_msg = { 'result': None, 'error': { 'exc_path': 'nameko.exceptions.UnserializableValueError', 'value': 'Unserializable value: `{}`'.format(worker_result), 'exc_type': 'UnserializableValueError', 'exc_args': [], } } (msg,), _ = mock_publish.call_args assert msg == expected_msg
def test_responder_worker_exc(mock_publish): message = Mock() message.properties = {'reply_to': ''} config = {AMQP_URI_CONFIG_KEY: ''} responder = Responder(config, message) # serialisable exception worker_exc = Exception('error') result, exc_info = responder.send_response( None, (Exception, worker_exc, "tb")) assert result is None assert exc_info == (Exception, worker_exc, "tb") expected_msg = { 'result': None, 'error': { 'exc_path': '{}.Exception'.format(EXCEPTION_MODULE), 'value': 'error', 'exc_type': 'Exception', 'exc_args': ['error'] } } (msg,), _ = mock_publish.call_args assert msg == expected_msg
def test_RestClientInstanceAdapter_not_empty(self): _client = Mock() _client.node_instances = Mock() _client.nodes = Mock() _instance = Mock() _node = Mock() _client.node_instances.list = Mock(return_value=[_instance]) _client.nodes.get = Mock(return_value=_node) _instance.id = 'id' _instance.deployment_id = 'deployment_id' _instance.node_id = 'node_id' _node.type_hierarchy = ['cloudify.nodes.Root', 'a'] _node.properties = { 'system_name': 'system', 'resource_name': 'resource', 'scope': 'scope', 'runtime_property_name': "c" } _instance.runtime_properties = {'resource': 'b', 'c': 'd'} inst = instance.RestClientInstanceAdapter.get_instances( _client, 'deployment_id') # calls _client.nodes.get.assert_called_with(deployment_id='deployment_id', node_id='node_id') _client.node_instances.list.assert_called_with( deployment_id='deployment_id') # results self.assertEqual(len(inst), 1) self._check_instance(inst[0])
def _gen_ctx(self): _ctx = Mock() _graph_mock = Mock() _graph_mock.execute = Mock() _sequence = Mock() _sequence.add = Mock() _graph_mock._sequence = _sequence _graph_mock.sequence = Mock(return_value=_sequence) _node = Mock() _node.operations = { 'operation1': {}, 'operation2': {}, } _instance = Mock() _instance.id = "instance_id" _instance.send_event = Mock(return_value='event') _instance.execute_operation = Mock(return_value='execute_operation') _node.properties = {} _node.id = 'node_id' _node.instances = [_instance] _workflow_ctx = Mock() _workflow_ctx.nodes = [_node] _workflow_ctx.graph_mode = Mock(return_value=_graph_mock) _workflow_ctx.get_ctx = Mock(return_value=_ctx) return _workflow_ctx, _graph_mock, _instance
def test_responder_worker_exc(mock_producer): message = Mock() message.properties = {'reply_to': ''} exchange = Mock() responder = Responder('amqp://localhost', exchange, 'json', message) # serialisable exception worker_exc = Exception('error') result, exc_info = responder.send_response(None, (Exception, worker_exc, "tb")) assert result is None assert exc_info == (Exception, worker_exc, "tb") expected_msg = { 'result': None, 'error': { 'exc_path': '{}.Exception'.format(EXCEPTION_MODULE), 'value': 'error', 'exc_type': 'Exception', 'exc_args': ['error'] } } (msg, ), _ = mock_producer.publish.call_args assert msg == expected_msg
def test_responder_worker_exc(mock_publish): message = Mock() message.properties = {'reply_to': ''} config = {AMQP_URI_CONFIG_KEY: ''} responder = Responder(config, message) # serialisable exception worker_exc = Exception('error') result, exc_info = responder.send_response(None, (Exception, worker_exc, "tb")) assert result is None assert exc_info == (Exception, worker_exc, "tb") expected_msg = { 'result': None, 'error': { 'exc_path': '{}.Exception'.format(EXCEPTION_MODULE), 'value': 'error', 'exc_type': 'Exception', 'exc_args': ['error'] } } (msg, ), _ = mock_publish.call_args assert msg == expected_msg
def test_responder_unserializable_result(mock_publish): message = Mock() message.properties = {'reply_to': ''} config = {AMQP_URI_CONFIG_KEY: ''} responder = Responder(config, message) # unserialisable result worker_result = object() result, exc_info = responder.send_response(worker_result, None) # responder will return the TypeError from json.dumps assert result is None assert exc_info == (TypeError, ANY, ANY) assert str( exc_info[1]) == "{} is not JSON serializable".format(worker_result) # and publish a dictionary-serialized UnserializableValueError # on worker_result expected_msg = { 'result': None, 'error': { 'exc_path': 'nameko.exceptions.UnserializableValueError', 'value': 'Unserializable value: `{}`'.format(worker_result), 'exc_type': 'UnserializableValueError', 'exc_args': [], } } (msg, ), _ = mock_publish.call_args assert msg == expected_msg
def test_responder_unserializable_exc(mock_publish): message = Mock() message.properties = {'reply_to': ''} container = Mock() container.config = {AMQP_URI_CONFIG_KEY: ''} responder = Responder(message) # unserialisable exception worker_exc = Exception(object()) result, exc_info = responder.send_response(container, True, (Exception, worker_exc, "tb")) # responder will return the TypeError from json.dumps assert result is None assert exc_info == (TypeError, ANY, ANY) assert exc_info[1].message == ("{} is not JSON " "serializable".format(worker_exc.args[0])) # and publish a dictionary-serialized UnserializableValueError # (where the unserialisable value is a dictionary-serialized worker_exc) serialized_exc = serialize(worker_exc) expected_msg = { 'result': None, 'error': { 'exc_path': 'nameko.exceptions.UnserializableValueError', 'value': 'Unserializable value: `{}`'.format(serialized_exc), 'exc_type': 'UnserializableValueError', 'exc_args': () } } (msg, ), _ = mock_publish.call_args assert msg == expected_msg
def test_ResourceManagementContext_resolve_project(self): inst, _client, _ctx, _instances_ctx = self._gen_resource_instance() # no deployment self.assertFalse(inst.resolve_project()) # have some deployment but without project inst.instance.properties[context.PROPERTY_DEPLOYMENT_ID] = 'depl' self.assertFalse(inst.resolve_project()) # we have some project inst.instance.properties[context.PROPERTY_PROJECT_NAME] = 'proj' _cfy_instance = Mock() _cfy_node = Mock() _cfy_instance.id = 'id' _cfy_instance.deployment_id = 'deployment_id' _cfy_instance.node_id = 'node_id' _cfy_node.type_hierarchy = ['cloudify.nodes.Root', 'a'] _cfy_node.properties = { 'system_name': 'system', 'resource_name': 'resource', 'scope': 'scope', 'runtime_property_name': "c" } _cfy_instance.runtime_properties = {'resource': 'b', 'c': 'd'} _client.node_instances.list = Mock(return_value=[_cfy_instance]) _client.nodes.get = Mock(return_value=_cfy_node) self.assertTrue(inst.resolve_project()) next_instance = inst.next_instance() self.assertEqual(next_instance['project'], 'proj') self.assertTrue(next_instance['visited'])
def test_populate_database_non_existent_model(self): # given item = Mock() item.type = 'random' item.properties = [''] items = [item] # then with self.assertRaises(NonExistentModelException): populate_database(items)
def test_populate_database_add_status(self): # given item = Mock() item.type = 'status' item.properties = ['status_txt'] items = [item] # when populate_database(items) # then self.assert_status_with_name_exists_in_database(item.properties[0])
def test_populate_database_add_post(self): # given item = Mock() item.type = 'post' item.properties = ['title', 'filename', datetime.now(), 'category', 'author'] items = [item] # when populate_database(items) # then self.assert_post_with_title_exists_in_database(item.properties[0])
def test_populate_database_add_technology(self): # given item = Mock() item.type = 'technology' item.properties = ['techno_txt'] items = [item] # when populate_database(items) # then self.assert_technology_with_name_exists_in_database(item.properties[0])
def test_populate_database_add_user(self): # given item = Mock() item.type = 'user' item.properties = ['bob', '*****@*****.**', 'bobpw'] items = [item] # when populate_database(items) # then self.assert_user_with_username_exists_in_database(item.properties[0])
def test_populate_database_add_project(self): # given item = Mock() item.type = 'project' item.properties = ['title', 'filename', 'techno', 'url', 1] items = [item] # when populate_database(items) # then self.assert_project_with_title_exists_in_database(item.properties[0])
def test_get_properties(self, find): service = Mock() service.name = 'web' service.properties = properties.Properties(defaults={"prop1": 1}) find.return_value = service r = web.ServiceResource() request = DummyRequest(['web', 'properties']) d = _render(r, request) def rendered(ignored): self.assertEquals(request.responseCode, None) # Gets treated as 200 OK self.assertEquals(json.loads("".join(request.written)), {'prop1': 1}) d.addCallback(rendered) return d
def test_populate_database_add_post(self): # given item = Mock() item.type = 'post' item.properties = [ 'title', 'filename', datetime.now(), 'category', 'author' ] items = [item] # when populate_database(items) # then self.assert_post_with_title_exists_in_database(item.properties[0])
def test_responder(mock_publish): message = Mock() message.properties = {'reply_to': ''} config = {AMQP_URI_CONFIG_KEY: ''} responder = Responder(config, message) # serialisable result result, exc_info = responder.send_response(True, None) assert result is True assert exc_info is None expected_msg = {'result': True, 'error': None} (msg, ), _ = mock_publish.call_args assert msg == expected_msg
def test_responder_cannot_unicode_exc(mock_publish): message = Mock() message.properties = {'reply_to': ''} config = {AMQP_URI_CONFIG_KEY: ''} responder = Responder(config, message) class CannotUnicode(object): def __str__(self): raise Exception('error') # un-unicode-able exception worker_exc = Exception(CannotUnicode()) # send_response should not throw responder.send_response(True, (Exception, worker_exc, "tb"))
def test_responder(mock_producer): message = Mock() message.properties = {'reply_to': ''} exchange = Mock() responder = Responder('amqp://localhost', exchange, 'json', message) # serialisable result result, exc_info = responder.send_response(True, None) assert result is True assert exc_info is None expected_msg = {'result': True, 'error': None} (msg, ), _ = mock_producer.publish.call_args assert msg == expected_msg
def test_responder_cannot_repr_exc(mock_publish): message = Mock() message.properties = {'reply_to': ''} config = {AMQP_URI_CONFIG_KEY: ''} responder = Responder(config, message) class CannotRepr(object): def __repr__(self): raise Exception('error') # un-repr-able exception worker_exc = Exception(CannotRepr()) # send_response should not throw responder.send_response(True, (Exception, worker_exc, "tb"))
def test_responder_cannot_repr_exc(mock_producer): message = Mock() message.properties = {'reply_to': ''} exchange = Mock() responder = Responder('amqp://localhost', exchange, 'json', message) class CannotRepr(object): def __repr__(self): raise Exception('error') # un-repr-able exception worker_exc = Exception(CannotRepr()) # send_response should not throw responder.send_response(True, (Exception, worker_exc, "tb"))
def test_responder(mock_publish): message = Mock() message.properties = {"reply_to": ""} container = Mock() container.config = {AMQP_URI_CONFIG_KEY: ""} responder = Responder(message) # serialisable result result, exc_info = responder.send_response(container, True, None) assert result is True assert exc_info is None expected_msg = {"result": True, "error": None} (msg,), _ = mock_publish.call_args assert msg == expected_msg
def test_responder_cannot_unicode_exc(mock_publish): message = Mock() message.properties = {"reply_to": ""} container = Mock() container.config = {AMQP_URI_CONFIG_KEY: ""} responder = Responder(message) class CannotUnicode(object): def __str__(self): raise Exception("error") # un-unicode-able exception worker_exc = Exception(CannotUnicode()) # send_response should not throw responder.send_response(container, True, (Exception, worker_exc, "tb"))
def test_responder(mock_publish): message = Mock() message.properties = {'reply_to': ''} config = {AMQP_URI_CONFIG_KEY: ''} responder = Responder(config, message) # serialisable result result, exc_info = responder.send_response(True, None) assert result is True assert exc_info is None expected_msg = { 'result': True, 'error': None } (msg,), _ = mock_publish.call_args assert msg == expected_msg
def test_update(self): node = Node(ElementTree.fromstring(NODE_XML)) resp = Mock() resp.headers.get = Mock( return_value="https://www.canfar.phys.uvic.ca/vospace") conn = Mock(spec=vos.Connection) conn.session.post = Mock(return_value=resp) client = Client(conn=conn) client.get_node_url = Mock( return_value='https://www.canfar.phys.uvic.ca/vospace') client.get_transfer_error = Mock() client.protocol = 'https' data = str(node) property_url = 'https://www.canfar.phys.uvic.ca/vospace/nodeprops' endpoints_mock = Mock() endpoints_mock.properties = property_url client.get_endpoints = Mock(return_value=endpoints_mock) result = client.update(node, False) self.assertEqual(result, 0) client.conn.session.post.assert_called_with( 'https://www.canfar.phys.uvic.ca/vospace', data=data, allow_redirects=False) call1 = call(property_url, allow_redirects=False, data=data, headers={'Content-type': 'text/xml'}) call2 = call( 'https://www.canfar.phys.uvic.ca/vospace/phase', allow_redirects=False, data="PHASE=RUN", headers={'Content-type': 'application/x-www-form-urlencoded'}) calls = [call1, call2] client.conn = Mock(spec=vos.Connection) client.conn.session.post = Mock(return_value=resp) result = client.update(node, True) self.assertEqual(result, 0) client.conn.session.post.assert_has_calls(calls)
def test_update(self): node = Node(ElementTree.fromstring(NODE_XML)) resp = Mock() resp.headers.get = Mock( return_value="https://www.canfar.phys.uvic.ca/vospace") conn = Mock(spec=vos.Connection) conn.session.post = Mock(return_value=resp) client = Client(conn=conn) client.get_node_url = Mock( return_value='https://www.canfar.phys.uvic.ca/vospace') client.get_transfer_error = Mock() client.protocol = 'https' data = str(node) property_url = 'https://www.canfar.phys.uvic.ca/vospace/nodeprops' endpoints_mock = Mock() endpoints_mock.properties = property_url client.get_endpoints = Mock(return_value=endpoints_mock) result = client.update(node, False) self.assertEqual(result, 0) client.conn.session.post.assert_called_with( 'https://www.canfar.phys.uvic.ca/vospace', data=data, allow_redirects=False) call1 = call(property_url, allow_redirects=False, data=data, headers={'Content-type': 'text/xml'}) call2 = call('https://www.canfar.phys.uvic.ca/vospace/phase', allow_redirects=False, data="PHASE=RUN", headers={'Content-type': "text/text"}) calls = [call1, call2] client.conn = Mock(spec=vos.Connection) client.conn.session.post = Mock(return_value=resp) result = client.update(node, True) self.assertEqual(result, 0) client.conn.session.post.assert_has_calls(calls)
def test_dump_message(self): m = Mock() m.body = 'the quick brown fox' m.properties = {'a': 1} m.delivery_info = {'exchange': 'bar'} self.assertTrue(dump_message(m))
def test_dump_message(self): m = Mock() m.body = "the quick brown fox" m.properties = {"a": 1} m.delivery_info = {"exchange": "bar"} self.assertTrue(dump_message(m))
def message(): message = Mock() message.properties = {'reply_to': '', 'content_type': 'application/json'} return message