Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 1 | # 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 | |
| 32 | import argparse |
Masood Malekghassemi | e6b0038 | 2015-04-09 12:25:39 -0700 | [diff] [blame] | 33 | from oauth2client import client as oauth2client_client |
Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 34 | |
Masood Malekghassemi | 2a0cb49 | 2015-10-22 17:43:33 -0700 | [diff] [blame] | 35 | from grpc.beta import implementations |
| 36 | |
Masood Malekghassemi | 7566c9a | 2015-10-21 20:29:23 -0700 | [diff] [blame^] | 37 | from tests.interop import methods |
| 38 | from tests.interop import resources |
| 39 | from tests.interop import test_pb2 |
| 40 | from tests.unit.beta import test_utilities |
Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 41 | |
| 42 | _ONE_DAY_IN_SECONDS = 60 * 60 * 24 |
| 43 | |
| 44 | |
| 45 | def _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 Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 50 | '--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 Tattermusch | 785efd4 | 2015-10-15 17:20:22 -0700 | [diff] [blame] | 54 | '--use_tls', help='require a secure connection', default=False, |
| 55 | type=resources.parse_bool) |
Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 56 | parser.add_argument( |
| 57 | '--use_test_ca', help='replace platform root CAs with ca.pem', |
Jan Tattermusch | 785efd4 | 2015-10-15 17:20:22 -0700 | [diff] [blame] | 58 | default=False, type=resources.parse_bool) |
Masood Malekghassemi | e6b0038 | 2015-04-09 12:25:39 -0700 | [diff] [blame] | 59 | 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 Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 66 | return parser.parse_args() |
| 67 | |
Masood Malekghassemi | e6b0038 | 2015-04-09 12:25:39 -0700 | [diff] [blame] | 68 | def _oauth_access_token(args): |
Masood Malekghassemi | 41251e4 | 2015-04-13 15:56:05 -0700 | [diff] [blame] | 69 | credentials = oauth2client_client.GoogleCredentials.get_application_default() |
Masood Malekghassemi | e6b0038 | 2015-04-09 12:25:39 -0700 | [diff] [blame] | 70 | scoped_credentials = credentials.create_scoped([args.oauth_scope]) |
| 71 | return scoped_credentials.get_access_token().access_token |
Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 72 | |
| 73 | def _stub(args): |
Masood Malekghassemi | e6b0038 | 2015-04-09 12:25:39 -0700 | [diff] [blame] | 74 | if args.oauth_scope: |
Masood Malekghassemi | b59f134 | 2015-08-27 11:09:20 -0700 | [diff] [blame] | 75 | if args.test_case == 'oauth2_auth_token': |
Jan Tattermusch | 3b6fef1 | 2015-10-19 19:33:24 -0700 | [diff] [blame] | 76 | # 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 Malekghassemi | b59f134 | 2015-08-27 11:09:20 -0700 | [diff] [blame] | 81 | access_token = _oauth_access_token(args) |
| 82 | metadata_transformer = lambda x: [ |
Jan Tattermusch | 3b6fef1 | 2015-10-19 19:33:24 -0700 | [diff] [blame] | 83 | ('authorization', 'Bearer %s' % access_token)] |
Masood Malekghassemi | b59f134 | 2015-08-27 11:09:20 -0700 | [diff] [blame] | 84 | else: |
| 85 | metadata_transformer = lambda x: [ |
Jan Tattermusch | 3b6fef1 | 2015-10-19 19:33:24 -0700 | [diff] [blame] | 86 | ('authorization', 'Bearer %s' % _oauth_access_token(args))] |
Masood Malekghassemi | e6b0038 | 2015-04-09 12:25:39 -0700 | [diff] [blame] | 87 | else: |
| 88 | metadata_transformer = lambda x: [] |
Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 89 | 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 Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 94 | |
Masood Malekghassemi | 2a0cb49 | 2015-10-22 17:43:33 -0700 | [diff] [blame] | 95 | 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 Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 101 | else: |
Masood Malekghassemi | 2a0cb49 | 2015-10-22 17:43:33 -0700 | [diff] [blame] | 102 | channel = implementations.insecure_channel( |
| 103 | args.server_host, args.server_port) |
| 104 | stub = test_pb2.beta_create_TestService_stub(channel) |
Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 105 | return stub |
| 106 | |
| 107 | |
Nathaniel Manista | 256ccca | 2015-03-07 00:18:51 +0000 | [diff] [blame] | 108 | def _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 Malekghassemi | 7566c9a | 2015-10-21 20:29:23 -0700 | [diff] [blame^] | 116 | def test_interoperability(): |
Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 117 | args = _args() |
| 118 | stub = _stub(args) |
Nathaniel Manista | 256ccca | 2015-03-07 00:18:51 +0000 | [diff] [blame] | 119 | test_case = _test_case_from_arg(args.test_case) |
Masood Malekghassemi | e6b0038 | 2015-04-09 12:25:39 -0700 | [diff] [blame] | 120 | test_case.test_interoperability(stub, args) |
Nathaniel Manista | c2b40200 | 2015-02-26 16:23:38 +0000 | [diff] [blame] | 121 | |
| 122 | |
| 123 | if __name__ == '__main__': |
Masood Malekghassemi | 7566c9a | 2015-10-21 20:29:23 -0700 | [diff] [blame^] | 124 | test_interoperability() |