blob: f177896e8e6aaf1bdbc48d2d8d3e985a2200e86c [file] [log] [blame]
Craig Tiller084aa622016-04-05 08:36:49 -07001# Copyright 2015, Google Inc.
Nathaniel Manistac2b402002015-02-26 16:23:38 +00002# 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 Manistac2b402002015-02-26 16:23:38 +000029"""The Python implementation of the GRPC interoperability test client."""
30
31import argparse
Masood Malekghassemie6b00382015-04-09 12:25:39 -070032from oauth2client import client as oauth2client_client
Nathaniel Manistac2b402002015-02-26 16:23:38 +000033
Nathaniel Manista5450f052016-08-11 02:24:46 +000034import grpc
Masood Malekghassemi2a0cb492015-10-22 17:43:33 -070035from grpc.beta import implementations
Ken Payson707c9e22016-04-20 09:42:19 -070036from src.proto.grpc.testing import test_pb2
Masood Malekghassemi2a0cb492015-10-22 17:43:33 -070037
Masood Malekghassemi7566c9a2015-10-21 20:29:23 -070038from tests.interop import methods
39from tests.interop import resources
Nathaniel Manistac2b402002015-02-26 16:23:38 +000040
41
42def _args():
Masood Malekghassemicc793702017-01-13 19:20:10 -080043 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 Manistac2b402002015-02-26 16:23:38 +000078
79
Nathaniel Manista5450f052016-08-11 02:24:46 +000080def _application_default_credentials():
Masood Malekghassemicc793702017-01-13 19:20:10 -080081 return oauth2client_client.GoogleCredentials.get_application_default()
Nathaniel Manista5450f052016-08-11 02:24:46 +000082
83
Nathaniel Manistac2b402002015-02-26 16:23:38 +000084def _stub(args):
Masood Malekghassemicc793702017-01-13 19:20:10 -080085 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 Manistac2b402002015-02-26 16:23:38 +0000106 else:
Masood Malekghassemicc793702017-01-13 19:20:10 -0800107 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 Manistac2b402002015-02-26 16:23:38 +0000113
Masood Malekghassemicc793702017-01-13 19:20:10 -0800114 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 Payson60a83c72016-04-21 14:36:33 -0700118
Masood Malekghassemicc793702017-01-13 19:20:10 -0800119 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 Manistac2b402002015-02-26 16:23:38 +0000128
129
Nathaniel Manista256ccca2015-03-07 00:18:51 +0000130def _test_case_from_arg(test_case_arg):
Masood Malekghassemicc793702017-01-13 19:20:10 -0800131 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 Manista256ccca2015-03-07 00:18:51 +0000136
137
Masood Malekghassemi7566c9a2015-10-21 20:29:23 -0700138def test_interoperability():
Masood Malekghassemicc793702017-01-13 19:20:10 -0800139 args = _args()
140 stub = _stub(args)
141 test_case = _test_case_from_arg(args.test_case)
142 test_case.test_interoperability(stub, args)
Nathaniel Manistac2b402002015-02-26 16:23:38 +0000143
144
145if __name__ == '__main__':
Masood Malekghassemicc793702017-01-13 19:20:10 -0800146 test_interoperability()