blob: 9449ff5429fd99a7f6decdd06eca1a2d9a876209 [file] [log] [blame]
Nathaniel Manistac2b402002015-02-26 16:23:38 +00001# Copyright 2015, Google Inc.
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.
29
30"""The Python implementation of the GRPC interoperability test client."""
31
32import argparse
Masood Malekghassemie6b00382015-04-09 12:25:39 -070033from oauth2client import client as oauth2client_client
Nathaniel Manistac2b402002015-02-26 16:23:38 +000034
Masood Malekghassemi2a0cb492015-10-22 17:43:33 -070035from grpc.beta import implementations
36
Masood Malekghassemi7566c9a2015-10-21 20:29:23 -070037from tests.interop import methods
38from tests.interop import resources
39from tests.interop import test_pb2
40from tests.unit.beta import test_utilities
Nathaniel Manistac2b402002015-02-26 16:23:38 +000041
42_ONE_DAY_IN_SECONDS = 60 * 60 * 24
43
44
45def _args():
46 parser = argparse.ArgumentParser()
47 parser.add_argument(
48 '--server_host', help='the host to which to connect', type=str)
49 parser.add_argument(
Nathaniel Manistac2b402002015-02-26 16:23:38 +000050 '--server_port', help='the port to which to connect', type=int)
51 parser.add_argument(
52 '--test_case', help='the test case to execute', type=str)
53 parser.add_argument(
Jan Tattermusch785efd42015-10-15 17:20:22 -070054 '--use_tls', help='require a secure connection', default=False,
55 type=resources.parse_bool)
Nathaniel Manistac2b402002015-02-26 16:23:38 +000056 parser.add_argument(
57 '--use_test_ca', help='replace platform root CAs with ca.pem',
Jan Tattermusch785efd42015-10-15 17:20:22 -070058 default=False, type=resources.parse_bool)
Masood Malekghassemie6b00382015-04-09 12:25:39 -070059 parser.add_argument(
60 '--server_host_override',
61 help='the server host to which to claim to connect', type=str)
62 parser.add_argument('--oauth_scope', help='scope for OAuth tokens', type=str)
63 parser.add_argument(
64 '--default_service_account',
65 help='email address of the default service account', type=str)
Nathaniel Manistac2b402002015-02-26 16:23:38 +000066 return parser.parse_args()
67
Masood Malekghassemie6b00382015-04-09 12:25:39 -070068def _oauth_access_token(args):
Masood Malekghassemi41251e42015-04-13 15:56:05 -070069 credentials = oauth2client_client.GoogleCredentials.get_application_default()
Masood Malekghassemie6b00382015-04-09 12:25:39 -070070 scoped_credentials = credentials.create_scoped([args.oauth_scope])
71 return scoped_credentials.get_access_token().access_token
Nathaniel Manistac2b402002015-02-26 16:23:38 +000072
73def _stub(args):
Masood Malekghassemie6b00382015-04-09 12:25:39 -070074 if args.oauth_scope:
Masood Malekghassemib59f1342015-08-27 11:09:20 -070075 if args.test_case == 'oauth2_auth_token':
Jan Tattermusch3b6fef12015-10-19 19:33:24 -070076 # TODO(jtattermusch): This testcase sets the auth metadata key-value
77 # manually, which also means that the user would need to do the same
78 # thing every time he/she would like to use and out of band oauth token.
79 # The transformer function that produces the metadata key-value from
80 # the access token should be provided by gRPC auth library.
Masood Malekghassemib59f1342015-08-27 11:09:20 -070081 access_token = _oauth_access_token(args)
82 metadata_transformer = lambda x: [
Jan Tattermusch3b6fef12015-10-19 19:33:24 -070083 ('authorization', 'Bearer %s' % access_token)]
Masood Malekghassemib59f1342015-08-27 11:09:20 -070084 else:
85 metadata_transformer = lambda x: [
Jan Tattermusch3b6fef12015-10-19 19:33:24 -070086 ('authorization', 'Bearer %s' % _oauth_access_token(args))]
Masood Malekghassemie6b00382015-04-09 12:25:39 -070087 else:
88 metadata_transformer = lambda x: []
Nathaniel Manistac2b402002015-02-26 16:23:38 +000089 if args.use_tls:
90 if args.use_test_ca:
91 root_certificates = resources.test_root_certificates()
92 else:
93 root_certificates = resources.prod_root_certificates()
Nathaniel Manistac2b402002015-02-26 16:23:38 +000094
Masood Malekghassemi2a0cb492015-10-22 17:43:33 -070095 channel = test_utilities.not_really_secure_channel(
96 args.server_host, args.server_port,
97 implementations.ssl_client_credentials(root_certificates, None, None),
98 args.server_host_override)
99 stub = test_pb2.beta_create_TestService_stub(
100 channel, metadata_transformer=metadata_transformer)
Nathaniel Manistac2b402002015-02-26 16:23:38 +0000101 else:
Masood Malekghassemi2a0cb492015-10-22 17:43:33 -0700102 channel = implementations.insecure_channel(
103 args.server_host, args.server_port)
104 stub = test_pb2.beta_create_TestService_stub(channel)
Nathaniel Manistac2b402002015-02-26 16:23:38 +0000105 return stub
106
107
Nathaniel Manista256ccca2015-03-07 00:18:51 +0000108def _test_case_from_arg(test_case_arg):
109 for test_case in methods.TestCase:
110 if test_case_arg == test_case.value:
111 return test_case
112 else:
113 raise ValueError('No test case "%s"!' % test_case_arg)
114
115
Masood Malekghassemi7566c9a2015-10-21 20:29:23 -0700116def test_interoperability():
Nathaniel Manistac2b402002015-02-26 16:23:38 +0000117 args = _args()
118 stub = _stub(args)
Nathaniel Manista256ccca2015-03-07 00:18:51 +0000119 test_case = _test_case_from_arg(args.test_case)
Masood Malekghassemie6b00382015-04-09 12:25:39 -0700120 test_case.test_interoperability(stub, args)
Nathaniel Manistac2b402002015-02-26 16:23:38 +0000121
122
123if __name__ == '__main__':
Masood Malekghassemi7566c9a2015-10-21 20:29:23 -0700124 test_interoperability()