Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 1 | # Copyright 2015 gRPC authors. |
Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 2 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 6 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 8 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 14 | """The Python implementation of the GRPC interoperability test client.""" |
| 15 | |
| 16 | import argparse |
Ken Payson | 937d96d | 2017-05-10 17:01:38 -0700 | [diff] [blame] | 17 | import os |
Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 18 | |
Ken Payson | 937d96d | 2017-05-10 17:01:38 -0700 | [diff] [blame] | 19 | from google import auth as google_auth |
| 20 | from google.auth import jwt as google_auth_jwt |
Nathaniel Manista | 5450f05 | 2016-08-11 02:24:46 +0000 | [diff] [blame] | 21 | import grpc |
Ken Payson | 707c9e2 | 2016-04-20 09:42:19 -0700 | [diff] [blame] | 22 | from src.proto.grpc.testing import test_pb2 |
Masood Malekghassemi | 2a0cb49 | 2015-10-22 17:43:33 -0700 | [diff] [blame] | 23 | |
Masood Malekghassemi | 7566c9a | 2015-10-21 20:29:23 -0700 | [diff] [blame] | 24 | from tests.interop import methods |
| 25 | from tests.interop import resources |
Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 26 | |
| 27 | |
| 28 | def _args(): |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 29 | parser = argparse.ArgumentParser() |
| 30 | parser.add_argument( |
| 31 | '--server_host', |
| 32 | help='the host to which to connect', |
| 33 | type=str, |
Paul Marks | 3a5bba0 | 2017-02-07 16:28:09 -0800 | [diff] [blame] | 34 | default="localhost") |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 35 | parser.add_argument( |
| 36 | '--server_port', help='the port to which to connect', type=int) |
| 37 | parser.add_argument( |
| 38 | '--test_case', |
| 39 | help='the test case to execute', |
| 40 | type=str, |
| 41 | default="large_unary") |
| 42 | parser.add_argument( |
| 43 | '--use_tls', |
| 44 | help='require a secure connection', |
| 45 | default=False, |
| 46 | type=resources.parse_bool) |
| 47 | parser.add_argument( |
| 48 | '--use_test_ca', |
| 49 | help='replace platform root CAs with ca.pem', |
| 50 | default=False, |
| 51 | type=resources.parse_bool) |
| 52 | parser.add_argument( |
| 53 | '--server_host_override', |
| 54 | default="foo.test.google.fr", |
| 55 | help='the server host to which to claim to connect', |
| 56 | type=str) |
| 57 | parser.add_argument( |
| 58 | '--oauth_scope', help='scope for OAuth tokens', type=str) |
| 59 | parser.add_argument( |
| 60 | '--default_service_account', |
| 61 | help='email address of the default service account', |
| 62 | type=str) |
| 63 | return parser.parse_args() |
Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 64 | |
| 65 | |
Nathaniel Manista | 5450f05 | 2016-08-11 02:24:46 +0000 | [diff] [blame] | 66 | def _application_default_credentials(): |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 67 | return oauth2client_client.GoogleCredentials.get_application_default() |
Nathaniel Manista | 5450f05 | 2016-08-11 02:24:46 +0000 | [diff] [blame] | 68 | |
| 69 | |
Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 70 | def _stub(args): |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 71 | target = '{}:{}'.format(args.server_host, args.server_port) |
| 72 | if args.test_case == 'oauth2_auth_token': |
Ken Payson | 937d96d | 2017-05-10 17:01:38 -0700 | [diff] [blame] | 73 | google_credentials, unused_project_id = google_auth.default( |
| 74 | scopes=[args.oauth_scope]) |
| 75 | google_credentials.refresh(google_auth.transport.requests.Request()) |
| 76 | call_credentials = grpc.access_token_call_credentials( |
| 77 | google_credentials.token) |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 78 | elif args.test_case == 'compute_engine_creds': |
Ken Payson | 937d96d | 2017-05-10 17:01:38 -0700 | [diff] [blame] | 79 | google_credentials, unused_project_id = google_auth.default( |
| 80 | scopes=[args.oauth_scope]) |
| 81 | call_credentials = grpc.metadata_call_credentials( |
| 82 | google_auth.transport.grpc.AuthMetadataPlugin( |
| 83 | credentials=google_credentials, |
| 84 | request=google_auth.transport.requests.Request())) |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 85 | elif args.test_case == 'jwt_token_creds': |
Ken Payson | 937d96d | 2017-05-10 17:01:38 -0700 | [diff] [blame] | 86 | google_credentials = google_auth_jwt.OnDemandCredentials.from_service_account_file( |
| 87 | os.environ[google_auth.environment_vars.CREDENTIALS]) |
| 88 | call_credentials = grpc.metadata_call_credentials( |
| 89 | google_auth.transport.grpc.AuthMetadataPlugin( |
| 90 | credentials=google_credentials, request=None)) |
Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 91 | else: |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 92 | call_credentials = None |
| 93 | if args.use_tls: |
| 94 | if args.use_test_ca: |
| 95 | root_certificates = resources.test_root_certificates() |
| 96 | else: |
| 97 | root_certificates = None # will load default roots. |
Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 98 | |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 99 | channel_credentials = grpc.ssl_channel_credentials(root_certificates) |
| 100 | if call_credentials is not None: |
| 101 | channel_credentials = grpc.composite_channel_credentials( |
| 102 | channel_credentials, call_credentials) |
Ken Payson | 60a83c7 | 2016-04-21 14:36:33 -0700 | [diff] [blame] | 103 | |
Ken Payson | 2fa5f2f | 2017-02-06 10:27:09 -0800 | [diff] [blame] | 104 | channel = grpc.secure_channel(target, channel_credentials, ( |
| 105 | ('grpc.ssl_target_name_override', args.server_host_override,),)) |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 106 | else: |
| 107 | channel = grpc.insecure_channel(target) |
| 108 | if args.test_case == "unimplemented_service": |
| 109 | return test_pb2.UnimplementedServiceStub(channel) |
| 110 | else: |
| 111 | return test_pb2.TestServiceStub(channel) |
Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 112 | |
| 113 | |
Nathaniel Manista | 256ccca | 2015-03-07 00:18:51 +0000 | [diff] [blame] | 114 | def _test_case_from_arg(test_case_arg): |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 115 | for test_case in methods.TestCase: |
| 116 | if test_case_arg == test_case.value: |
| 117 | return test_case |
| 118 | else: |
| 119 | raise ValueError('No test case "%s"!' % test_case_arg) |
Nathaniel Manista | 256ccca | 2015-03-07 00:18:51 +0000 | [diff] [blame] | 120 | |
| 121 | |
Masood Malekghassemi | 7566c9a | 2015-10-21 20:29:23 -0700 | [diff] [blame] | 122 | def test_interoperability(): |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 123 | args = _args() |
| 124 | stub = _stub(args) |
| 125 | test_case = _test_case_from_arg(args.test_case) |
| 126 | test_case.test_interoperability(stub, args) |
Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 127 | |
| 128 | |
| 129 | if __name__ == '__main__': |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame] | 130 | test_interoperability() |