Debian Codegraph @

src/python/grpcio_tests/tests/unit/_local_credentials_test.py

raw
1 # Copyright 2019 The gRPC Authors
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 """Test of RPCs made using local credentials."""
15
16 from concurrent.futures import ThreadPoolExecutor
17 import os
18 import unittest
19
20 import grpc
21
22
23 class _GenericHandler(grpc.GenericRpcHandler):
24
25 def service(self, handler_call_details):
26 return grpc.unary_unary_rpc_method_handler(
27 lambda request, unused_context: request)
28
29
30 class LocalCredentialsTest(unittest.TestCase):
31
32 def _create_server(self):
33 server = grpc.server(ThreadPoolExecutor())
34 server.add_generic_rpc_handlers((_GenericHandler(),))
35 return server
36
37 @unittest.skipIf(os.name == 'nt',
38 'TODO(https://github.com/grpc/grpc/issues/20078)')
39 def test_local_tcp(self):
40 server_addr = 'localhost:{}'
41 channel_creds = grpc.local_channel_credentials(
42 grpc.LocalConnectionType.LOCAL_TCP)
43 server_creds = grpc.local_server_credentials(
44 grpc.LocalConnectionType.LOCAL_TCP)
45
46 server = self._create_server()
47 port = server.add_secure_port(server_addr.format(0), server_creds)
48 server.start()
49 with grpc.secure_channel(server_addr.format(port),
50 channel_creds) as channel:
51 self.assertEqual(
52 b'abc',
53 channel.unary_unary('/test/method')(b'abc',
54 wait_for_ready=True))
55 server.stop(None)
56
57 @unittest.skipIf(os.name == 'nt',
58 'Unix Domain Socket is not supported on Windows')
59 def test_uds(self):
60 server_addr = 'unix:/tmp/grpc_fullstack_test'
61 channel_creds = grpc.local_channel_credentials(
62 grpc.LocalConnectionType.UDS)
63 server_creds = grpc.local_server_credentials(
64 grpc.LocalConnectionType.UDS)
65
66 server = self._create_server()
67 server.add_secure_port(server_addr, server_creds)
68 server.start()
69 with grpc.secure_channel(server_addr, channel_creds) as channel:
70 self.assertEqual(
71 b'abc',
72 channel.unary_unary('/test/method')(b'abc',
73 wait_for_ready=True))
74 server.stop(None)
75
76
77 if __name__ == '__main__':
78 unittest.main()