OLD | NEW |
(Empty) | |
| 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 |
| 33 from oauth2client import client as oauth2client_client |
| 34 |
| 35 from grpc.beta import implementations |
| 36 |
| 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 |
| 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( |
| 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( |
| 54 '--use_tls', help='require a secure connection', default=False, |
| 55 type=resources.parse_bool) |
| 56 parser.add_argument( |
| 57 '--use_test_ca', help='replace platform root CAs with ca.pem', |
| 58 default=False, type=resources.parse_bool) |
| 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) |
| 66 return parser.parse_args() |
| 67 |
| 68 def _oauth_access_token(args): |
| 69 credentials = oauth2client_client.GoogleCredentials.get_application_default() |
| 70 scoped_credentials = credentials.create_scoped([args.oauth_scope]) |
| 71 return scoped_credentials.get_access_token().access_token |
| 72 |
| 73 def _stub(args): |
| 74 if args.oauth_scope: |
| 75 if args.test_case == 'oauth2_auth_token': |
| 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. |
| 81 access_token = _oauth_access_token(args) |
| 82 metadata_transformer = lambda x: [ |
| 83 ('authorization', 'Bearer %s' % access_token)] |
| 84 else: |
| 85 metadata_transformer = lambda x: [ |
| 86 ('authorization', 'Bearer %s' % _oauth_access_token(args))] |
| 87 else: |
| 88 metadata_transformer = lambda x: [] |
| 89 if args.use_tls: |
| 90 if args.use_test_ca: |
| 91 root_certificates = resources.test_root_certificates() |
| 92 else: |
| 93 root_certificates = None # will load default roots. |
| 94 |
| 95 channel = test_utilities.not_really_secure_channel( |
| 96 args.server_host, args.server_port, |
| 97 implementations.ssl_channel_credentials(root_certificates, None, None), |
| 98 args.server_host_override) |
| 99 stub = test_pb2.beta_create_TestService_stub( |
| 100 channel, metadata_transformer=metadata_transformer) |
| 101 else: |
| 102 channel = implementations.insecure_channel( |
| 103 args.server_host, args.server_port) |
| 104 stub = test_pb2.beta_create_TestService_stub(channel) |
| 105 return stub |
| 106 |
| 107 |
| 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 |
| 116 def test_interoperability(): |
| 117 args = _args() |
| 118 stub = _stub(args) |
| 119 test_case = _test_case_from_arg(args.test_case) |
| 120 test_case.test_interoperability(stub, args) |
| 121 |
| 122 |
| 123 if __name__ == '__main__': |
| 124 test_interoperability() |
OLD | NEW |