blob: e520c08290301e9c532659e9eae013b579be8377 [file] [log] [blame]
Jan Tattermusch7897ae92017-06-07 22:57:36 +02001# Copyright 2015 gRPC authors.
Nathaniel Manistac2b402002015-02-26 16:23:38 +00002#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003# 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 Manistac2b402002015-02-26 16:23:38 +00006#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02007# http://www.apache.org/licenses/LICENSE-2.0
Nathaniel Manistac2b402002015-02-26 16:23:38 +00008#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009# 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 Manistac2b402002015-02-26 16:23:38 +000014"""The Python implementation of the GRPC interoperability test client."""
15
16import argparse
Ken Payson937d96d2017-05-10 17:01:38 -070017import os
Nathaniel Manistac2b402002015-02-26 16:23:38 +000018
Ken Payson937d96d2017-05-10 17:01:38 -070019from google import auth as google_auth
20from google.auth import jwt as google_auth_jwt
Nathaniel Manista5450f052016-08-11 02:24:46 +000021import grpc
Nathaniel Manistae232f1b2017-09-21 05:52:25 +000022from src.proto.grpc.testing import test_pb2_grpc
Masood Malekghassemi2a0cb492015-10-22 17:43:33 -070023
Masood Malekghassemi7566c9a2015-10-21 20:29:23 -070024from tests.interop import methods
25from tests.interop import resources
Nathaniel Manistac2b402002015-02-26 16:23:38 +000026
27
28def _args():
Masood Malekghassemicc793702017-01-13 19:20:10 -080029 parser = argparse.ArgumentParser()
30 parser.add_argument(
31 '--server_host',
32 help='the host to which to connect',
33 type=str,
Paul Marks3a5bba02017-02-07 16:28:09 -080034 default="localhost")
Masood Malekghassemicc793702017-01-13 19:20:10 -080035 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 Manistac2b402002015-02-26 16:23:38 +000064
65
Nathaniel Manista5450f052016-08-11 02:24:46 +000066def _application_default_credentials():
Masood Malekghassemicc793702017-01-13 19:20:10 -080067 return oauth2client_client.GoogleCredentials.get_application_default()
Nathaniel Manista5450f052016-08-11 02:24:46 +000068
69
Nathaniel Manistac2b402002015-02-26 16:23:38 +000070def _stub(args):
Masood Malekghassemicc793702017-01-13 19:20:10 -080071 target = '{}:{}'.format(args.server_host, args.server_port)
72 if args.test_case == 'oauth2_auth_token':
Ken Payson937d96d2017-05-10 17:01:38 -070073 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 Malekghassemicc793702017-01-13 19:20:10 -080078 elif args.test_case == 'compute_engine_creds':
Ken Payson937d96d2017-05-10 17:01:38 -070079 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 Malekghassemicc793702017-01-13 19:20:10 -080085 elif args.test_case == 'jwt_token_creds':
Ken Payson937d96d2017-05-10 17:01:38 -070086 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 Manistac2b402002015-02-26 16:23:38 +000091 else:
Masood Malekghassemicc793702017-01-13 19:20:10 -080092 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 Manistac2b402002015-02-26 16:23:38 +000098
Masood Malekghassemicc793702017-01-13 19:20:10 -080099 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 Payson60a83c72016-04-21 14:36:33 -0700103
Ken Payson2fa5f2f2017-02-06 10:27:09 -0800104 channel = grpc.secure_channel(target, channel_credentials, (
105 ('grpc.ssl_target_name_override', args.server_host_override,),))
Masood Malekghassemicc793702017-01-13 19:20:10 -0800106 else:
107 channel = grpc.insecure_channel(target)
108 if args.test_case == "unimplemented_service":
Nathaniel Manistae232f1b2017-09-21 05:52:25 +0000109 return test_pb2_grpc.UnimplementedServiceStub(channel)
Masood Malekghassemicc793702017-01-13 19:20:10 -0800110 else:
Nathaniel Manistae232f1b2017-09-21 05:52:25 +0000111 return test_pb2_grpc.TestServiceStub(channel)
Nathaniel Manistac2b402002015-02-26 16:23:38 +0000112
113
Nathaniel Manista256ccca2015-03-07 00:18:51 +0000114def _test_case_from_arg(test_case_arg):
Masood Malekghassemicc793702017-01-13 19:20:10 -0800115 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 Manista256ccca2015-03-07 00:18:51 +0000120
121
Masood Malekghassemi7566c9a2015-10-21 20:29:23 -0700122def test_interoperability():
Masood Malekghassemicc793702017-01-13 19:20:10 -0800123 args = _args()
124 stub = _stub(args)
125 test_case = _test_case_from_arg(args.test_case)
126 test_case.test_interoperability(stub, args)
Nathaniel Manistac2b402002015-02-26 16:23:38 +0000127
128
129if __name__ == '__main__':
Masood Malekghassemicc793702017-01-13 19:20:10 -0800130 test_interoperability()