Craig Tiller | 084aa62 | 2016-04-05 08:36:49 -0700 | [diff] [blame] | 1 | # Copyright 2015, Google Inc. |
Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 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. |
Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 29 | """The Python implementation of the GRPC interoperability test client.""" |
| 30 | |
| 31 | import argparse |
Masood Malekghassemi | e6b0038 | 2015-04-09 12:25:39 -0700 | [diff] [blame] | 32 | from oauth2client import client as oauth2client_client |
Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 33 | |
Nathaniel Manista | 5450f05 | 2016-08-11 02:24:46 +0000 | [diff] [blame] | 34 | import grpc |
Masood Malekghassemi | 2a0cb49 | 2015-10-22 17:43:33 -0700 | [diff] [blame] | 35 | from grpc.beta import implementations |
Ken Payson | 707c9e2 | 2016-04-20 09:42:19 -0700 | [diff] [blame] | 36 | from src.proto.grpc.testing import test_pb2 |
Masood Malekghassemi | 2a0cb49 | 2015-10-22 17:43:33 -0700 | [diff] [blame] | 37 | |
Masood Malekghassemi | 7566c9a | 2015-10-21 20:29:23 -0700 | [diff] [blame] | 38 | from tests.interop import methods |
| 39 | from tests.interop import resources |
Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 40 | |
| 41 | |
| 42 | def _args(): |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame^] | 43 | parser = argparse.ArgumentParser() |
| 44 | parser.add_argument( |
| 45 | '--server_host', |
| 46 | help='the host to which to connect', |
| 47 | type=str, |
| 48 | default="127.0.0.1") |
| 49 | parser.add_argument( |
| 50 | '--server_port', help='the port to which to connect', type=int) |
| 51 | parser.add_argument( |
| 52 | '--test_case', |
| 53 | help='the test case to execute', |
| 54 | type=str, |
| 55 | default="large_unary") |
| 56 | parser.add_argument( |
| 57 | '--use_tls', |
| 58 | help='require a secure connection', |
| 59 | default=False, |
| 60 | type=resources.parse_bool) |
| 61 | parser.add_argument( |
| 62 | '--use_test_ca', |
| 63 | help='replace platform root CAs with ca.pem', |
| 64 | default=False, |
| 65 | type=resources.parse_bool) |
| 66 | parser.add_argument( |
| 67 | '--server_host_override', |
| 68 | default="foo.test.google.fr", |
| 69 | help='the server host to which to claim to connect', |
| 70 | type=str) |
| 71 | parser.add_argument( |
| 72 | '--oauth_scope', help='scope for OAuth tokens', type=str) |
| 73 | parser.add_argument( |
| 74 | '--default_service_account', |
| 75 | help='email address of the default service account', |
| 76 | type=str) |
| 77 | return parser.parse_args() |
Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 78 | |
| 79 | |
Nathaniel Manista | 5450f05 | 2016-08-11 02:24:46 +0000 | [diff] [blame] | 80 | def _application_default_credentials(): |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame^] | 81 | return oauth2client_client.GoogleCredentials.get_application_default() |
Nathaniel Manista | 5450f05 | 2016-08-11 02:24:46 +0000 | [diff] [blame] | 82 | |
| 83 | |
Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 84 | def _stub(args): |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame^] | 85 | target = '{}:{}'.format(args.server_host, args.server_port) |
| 86 | if args.test_case == 'oauth2_auth_token': |
| 87 | google_credentials = _application_default_credentials() |
| 88 | scoped_credentials = google_credentials.create_scoped( |
| 89 | [args.oauth_scope]) |
| 90 | access_token = scoped_credentials.get_access_token().access_token |
| 91 | call_credentials = grpc.access_token_call_credentials(access_token) |
| 92 | elif args.test_case == 'compute_engine_creds': |
| 93 | google_credentials = _application_default_credentials() |
| 94 | scoped_credentials = google_credentials.create_scoped( |
| 95 | [args.oauth_scope]) |
| 96 | # TODO(https://github.com/grpc/grpc/issues/6799): Eliminate this last |
| 97 | # remaining use of the Beta API. |
| 98 | call_credentials = implementations.google_call_credentials( |
| 99 | scoped_credentials) |
| 100 | elif args.test_case == 'jwt_token_creds': |
| 101 | google_credentials = _application_default_credentials() |
| 102 | # TODO(https://github.com/grpc/grpc/issues/6799): Eliminate this last |
| 103 | # remaining use of the Beta API. |
| 104 | call_credentials = implementations.google_call_credentials( |
| 105 | google_credentials) |
Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 106 | else: |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame^] | 107 | call_credentials = None |
| 108 | if args.use_tls: |
| 109 | if args.use_test_ca: |
| 110 | root_certificates = resources.test_root_certificates() |
| 111 | else: |
| 112 | root_certificates = None # will load default roots. |
Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 113 | |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame^] | 114 | channel_credentials = grpc.ssl_channel_credentials(root_certificates) |
| 115 | if call_credentials is not None: |
| 116 | channel_credentials = grpc.composite_channel_credentials( |
| 117 | channel_credentials, call_credentials) |
Ken Payson | 60a83c7 | 2016-04-21 14:36:33 -0700 | [diff] [blame] | 118 | |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame^] | 119 | channel = grpc.secure_channel(target, channel_credentials, (( |
| 120 | 'grpc.ssl_target_name_override', |
| 121 | args.server_host_override,),)) |
| 122 | else: |
| 123 | channel = grpc.insecure_channel(target) |
| 124 | if args.test_case == "unimplemented_service": |
| 125 | return test_pb2.UnimplementedServiceStub(channel) |
| 126 | else: |
| 127 | return test_pb2.TestServiceStub(channel) |
Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 128 | |
| 129 | |
Nathaniel Manista | 256ccca | 2015-03-07 00:18:51 +0000 | [diff] [blame] | 130 | def _test_case_from_arg(test_case_arg): |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame^] | 131 | for test_case in methods.TestCase: |
| 132 | if test_case_arg == test_case.value: |
| 133 | return test_case |
| 134 | else: |
| 135 | raise ValueError('No test case "%s"!' % test_case_arg) |
Nathaniel Manista | 256ccca | 2015-03-07 00:18:51 +0000 | [diff] [blame] | 136 | |
| 137 | |
Masood Malekghassemi | 7566c9a | 2015-10-21 20:29:23 -0700 | [diff] [blame] | 138 | def test_interoperability(): |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame^] | 139 | args = _args() |
| 140 | stub = _stub(args) |
| 141 | test_case = _test_case_from_arg(args.test_case) |
| 142 | test_case.test_interoperability(stub, args) |
Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 143 | |
| 144 | |
| 145 | if __name__ == '__main__': |
Masood Malekghassemi | cc79370 | 2017-01-13 19:20:10 -0800 | [diff] [blame^] | 146 | test_interoperability() |