Masood Malekghassemi | aff6936 | 2016-09-21 15:10:36 -0700 | [diff] [blame] | 1 | # Copyright 2016, Google Inc. |
| 2 | # All rights reserved. |
| 3 | # |
| 4 | # Redistribution and use in source and binary forms, with or without |
| 5 | # modification, are permitted provided that the following conditions are |
| 6 | # met: |
| 7 | # |
| 8 | # * Redistributions of source code must retain the above copyright |
| 9 | # notice, this list of conditions and the following disclaimer. |
| 10 | # * Redistributions in binary form must reproduce the above |
| 11 | # copyright notice, this list of conditions and the following disclaimer |
| 12 | # in the documentation and/or other materials provided with the |
| 13 | # distribution. |
| 14 | # * Neither the name of Google Inc. nor the names of its |
| 15 | # contributors may be used to endorse or promote products derived from |
| 16 | # this software without specific prior written permission. |
| 17 | # |
| 18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Masood Malekghassemi | fb261bf | 2016-12-12 13:19:31 -0800 | [diff] [blame] | 29 | """Tests of grpc_reflection.v1alpha.reflection.""" |
Masood Malekghassemi | aff6936 | 2016-09-21 15:10:36 -0700 | [diff] [blame] | 30 | |
| 31 | import unittest |
| 32 | |
| 33 | import grpc |
| 34 | from grpc.framework.foundation import logging_pool |
Masood Malekghassemi | fb261bf | 2016-12-12 13:19:31 -0800 | [diff] [blame] | 35 | from grpc_reflection.v1alpha import reflection |
| 36 | from grpc_reflection.v1alpha import reflection_pb2 |
Nathaniel Manista | bb7484c | 2017-03-17 23:42:39 +0000 | [diff] [blame] | 37 | from grpc_reflection.v1alpha import reflection_pb2_grpc |
Masood Malekghassemi | aff6936 | 2016-09-21 15:10:36 -0700 | [diff] [blame] | 38 | |
| 39 | from google.protobuf import descriptor_pool |
| 40 | from google.protobuf import descriptor_pb2 |
| 41 | |
Masood Malekghassemi | aff6936 | 2016-09-21 15:10:36 -0700 | [diff] [blame] | 42 | from src.proto.grpc.testing import empty_pb2 |
Mehrdad Afshari | a0cbe8c | 2017-03-17 17:15:05 +0000 | [diff] [blame] | 43 | |
| 44 | from src.proto.grpc.testing.proto2 import empty2_pb2 |
| 45 | from src.proto.grpc.testing.proto2 import empty2_extensions_pb2 |
| 46 | |
Masood Malekghassemi | aff6936 | 2016-09-21 15:10:36 -0700 | [diff] [blame] | 47 | from tests.unit.framework.common import test_constants |
| 48 | |
| 49 | _EMPTY_PROTO_FILE_NAME = 'src/proto/grpc/testing/empty.proto' |
| 50 | _EMPTY_PROTO_SYMBOL_NAME = 'grpc.testing.Empty' |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 51 | _SERVICE_NAMES = ('Angstrom', 'Bohr', 'Curie', 'Dyson', 'Einstein', 'Feynman', |
| 52 | 'Galilei') |
Mehrdad Afshari | a0cbe8c | 2017-03-17 17:15:05 +0000 | [diff] [blame] | 53 | _EMPTY_EXTENSIONS_SYMBOL_NAME = 'grpc.testing.proto2.EmptyWithExtensions' |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 54 | |
Masood Malekghassemi | aff6936 | 2016-09-21 15:10:36 -0700 | [diff] [blame] | 55 | |
| 56 | def _file_descriptor_to_proto(descriptor): |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 57 | proto = descriptor_pb2.FileDescriptorProto() |
| 58 | descriptor.CopyToProto(proto) |
| 59 | return proto.SerializeToString() |
| 60 | |
Masood Malekghassemi | aff6936 | 2016-09-21 15:10:36 -0700 | [diff] [blame] | 61 | |
| 62 | class ReflectionServicerTest(unittest.TestCase): |
| 63 | |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 64 | def setUp(self): |
| 65 | servicer = reflection.ReflectionServicer(service_names=_SERVICE_NAMES) |
| 66 | server_pool = logging_pool.pool(test_constants.THREAD_CONCURRENCY) |
| 67 | self._server = grpc.server(server_pool) |
| 68 | port = self._server.add_insecure_port('[::]:0') |
Nathaniel Manista | bb7484c | 2017-03-17 23:42:39 +0000 | [diff] [blame] | 69 | reflection_pb2_grpc.add_ServerReflectionServicer_to_server(servicer, |
| 70 | self._server) |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 71 | self._server.start() |
Masood Malekghassemi | aff6936 | 2016-09-21 15:10:36 -0700 | [diff] [blame] | 72 | |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 73 | channel = grpc.insecure_channel('localhost:%d' % port) |
Nathaniel Manista | bb7484c | 2017-03-17 23:42:39 +0000 | [diff] [blame] | 74 | self._stub = reflection_pb2_grpc.ServerReflectionStub(channel) |
Masood Malekghassemi | aff6936 | 2016-09-21 15:10:36 -0700 | [diff] [blame] | 75 | |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 76 | def testFileByName(self): |
Ken Payson | 2fa5f2f | 2017-02-06 10:27:09 -0800 | [diff] [blame] | 77 | requests = (reflection_pb2.ServerReflectionRequest( |
| 78 | file_by_filename=_EMPTY_PROTO_FILE_NAME), |
| 79 | reflection_pb2.ServerReflectionRequest( |
| 80 | file_by_filename='i-donut-exist'),) |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 81 | responses = tuple(self._stub.ServerReflectionInfo(iter(requests))) |
| 82 | expected_responses = ( |
| 83 | reflection_pb2.ServerReflectionResponse( |
| 84 | valid_host='', |
| 85 | file_descriptor_response=reflection_pb2.FileDescriptorResponse( |
| 86 | file_descriptor_proto=( |
| 87 | _file_descriptor_to_proto(empty_pb2.DESCRIPTOR),))), |
| 88 | reflection_pb2.ServerReflectionResponse( |
| 89 | valid_host='', |
| 90 | error_response=reflection_pb2.ErrorResponse( |
| 91 | error_code=grpc.StatusCode.NOT_FOUND.value[0], |
| 92 | error_message=grpc.StatusCode.NOT_FOUND.value[1].encode(), |
| 93 | )),) |
| 94 | self.assertSequenceEqual(expected_responses, responses) |
Masood Malekghassemi | aff6936 | 2016-09-21 15:10:36 -0700 | [diff] [blame] | 95 | |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 96 | def testFileBySymbol(self): |
Ken Payson | 2fa5f2f | 2017-02-06 10:27:09 -0800 | [diff] [blame] | 97 | requests = (reflection_pb2.ServerReflectionRequest( |
| 98 | file_containing_symbol=_EMPTY_PROTO_SYMBOL_NAME |
| 99 | ), reflection_pb2.ServerReflectionRequest( |
| 100 | file_containing_symbol='i.donut.exist.co.uk.org.net.me.name.foo'),) |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 101 | responses = tuple(self._stub.ServerReflectionInfo(iter(requests))) |
| 102 | expected_responses = ( |
| 103 | reflection_pb2.ServerReflectionResponse( |
| 104 | valid_host='', |
| 105 | file_descriptor_response=reflection_pb2.FileDescriptorResponse( |
| 106 | file_descriptor_proto=( |
| 107 | _file_descriptor_to_proto(empty_pb2.DESCRIPTOR),))), |
| 108 | reflection_pb2.ServerReflectionResponse( |
| 109 | valid_host='', |
| 110 | error_response=reflection_pb2.ErrorResponse( |
| 111 | error_code=grpc.StatusCode.NOT_FOUND.value[0], |
| 112 | error_message=grpc.StatusCode.NOT_FOUND.value[1].encode(), |
| 113 | )),) |
| 114 | self.assertSequenceEqual(expected_responses, responses) |
Masood Malekghassemi | aff6936 | 2016-09-21 15:10:36 -0700 | [diff] [blame] | 115 | |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 116 | def testFileContainingExtension(self): |
Ken Payson | 2fa5f2f | 2017-02-06 10:27:09 -0800 | [diff] [blame] | 117 | requests = (reflection_pb2.ServerReflectionRequest( |
| 118 | file_containing_extension=reflection_pb2.ExtensionRequest( |
Mehrdad Afshari | a0cbe8c | 2017-03-17 17:15:05 +0000 | [diff] [blame] | 119 | containing_type=_EMPTY_EXTENSIONS_SYMBOL_NAME, |
Ken Payson | 2fa5f2f | 2017-02-06 10:27:09 -0800 | [diff] [blame] | 120 | extension_number=125,), |
| 121 | ), reflection_pb2.ServerReflectionRequest( |
| 122 | file_containing_extension=reflection_pb2.ExtensionRequest( |
| 123 | containing_type='i.donut.exist.co.uk.org.net.me.name.foo', |
| 124 | extension_number=55,),),) |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 125 | responses = tuple(self._stub.ServerReflectionInfo(iter(requests))) |
| 126 | expected_responses = ( |
| 127 | reflection_pb2.ServerReflectionResponse( |
| 128 | valid_host='', |
| 129 | file_descriptor_response=reflection_pb2.FileDescriptorResponse( |
| 130 | file_descriptor_proto=(_file_descriptor_to_proto( |
Mehrdad Afshari | a0cbe8c | 2017-03-17 17:15:05 +0000 | [diff] [blame] | 131 | empty2_extensions_pb2.DESCRIPTOR),))), |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 132 | reflection_pb2.ServerReflectionResponse( |
| 133 | valid_host='', |
| 134 | error_response=reflection_pb2.ErrorResponse( |
| 135 | error_code=grpc.StatusCode.NOT_FOUND.value[0], |
| 136 | error_message=grpc.StatusCode.NOT_FOUND.value[1].encode(), |
| 137 | )),) |
| 138 | self.assertSequenceEqual(expected_responses, responses) |
Masood Malekghassemi | aff6936 | 2016-09-21 15:10:36 -0700 | [diff] [blame] | 139 | |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 140 | def testListServices(self): |
Ken Payson | 2fa5f2f | 2017-02-06 10:27:09 -0800 | [diff] [blame] | 141 | requests = (reflection_pb2.ServerReflectionRequest( |
| 142 | list_services='',),) |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 143 | responses = tuple(self._stub.ServerReflectionInfo(iter(requests))) |
| 144 | expected_responses = (reflection_pb2.ServerReflectionResponse( |
| 145 | valid_host='', |
| 146 | list_services_response=reflection_pb2.ListServiceResponse( |
| 147 | service=tuple( |
| 148 | reflection_pb2.ServiceResponse(name=name) |
| 149 | for name in _SERVICE_NAMES))),) |
| 150 | self.assertSequenceEqual(expected_responses, responses) |
| 151 | |
Masood Malekghassemi | aff6936 | 2016-09-21 15:10:36 -0700 | [diff] [blame] | 152 | |
| 153 | if __name__ == '__main__': |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 154 | unittest.main(verbosity=2) |