From 9b5ba657f4246ed2695c97bf41cf9691a4f6c9b9 Mon Sep 17 00:00:00 2001 From: Jon Wayne Parrott Date: Mon, 31 Oct 2016 16:27:34 -0700 Subject: [PATCH] Add system tests; address review comments; skip tests when grpc isn't available. --- google/auth/transport/grpc.py | 14 ++++++------ pylintrc | 6 ------ pylintrc.tests | 2 +- system_tests/nox.py | 6 ++++++ system_tests/test_grpc.py | 40 +++++++++++++++++++++++++++++++++++ tests/transport/test_grpc.py | 10 ++++++++- tox.ini | 4 +++- 7 files changed, 65 insertions(+), 17 deletions(-) create mode 100644 system_tests/test_grpc.py diff --git a/google/auth/transport/grpc.py b/google/auth/transport/grpc.py index 7a2ec254f..dfb288304 100644 --- a/google/auth/transport/grpc.py +++ b/google/auth/transport/grpc.py @@ -14,6 +14,8 @@ """Authorization support for gRPC.""" +from __future__ import absolute_import + import grpc @@ -101,20 +103,16 @@ def secure_authorized_channel( grpc.Channel: The created gRPC channel. """ # Create the metadata plugin for inserting the authorization header. - google_auth_credentials_metadata_plugin = AuthMetadataPlugin( - credentials, request) + metadata_plugin = AuthMetadataPlugin(credentials, request) # Create a set of grpc.CallCredentials using the metadata plugin. - google_auth_credentials = grpc.metadata_call_credentials( - google_auth_credentials_metadata_plugin) + google_auth_credentials = grpc.metadata_call_credentials(metadata_plugin) - if not ssl_credentials: + if ssl_credentials is None: ssl_credentials = grpc.ssl_channel_credentials() # Combine the ssl credentials and the authorization credentials. composite_credentials = grpc.composite_channel_credentials( ssl_credentials, google_auth_credentials) - channel = grpc.secure_channel(target, composite_credentials) - - return channel + return grpc.secure_channel(target, composite_credentials) diff --git a/pylintrc b/pylintrc index 7fb95e3f6..1f01cfa1d 100644 --- a/pylintrc +++ b/pylintrc @@ -137,12 +137,6 @@ method-rgx=[a-z_][a-z0-9_]{2,40}$ # especially those that implemented wordy RFCs. function-rgx=[a-z_][a-z0-9_]{2,40}$ -# Regular expression matching correct variable names -# DEFAULT: variable-rgx=[a-z_][a-z0-9_]{2,30}$ -# RATIONALE: Prefer longer, more specific variable names to avoid ambiguity. -# especially for methods that implement wordy rfcs or use wordy libraries. -variable-rgx=[a-z_][a-z0-9_]{2,40}$ - [TYPECHECK] # List of module names for which member attributes should not be checked # (useful for modules/projects where namespaces are manipulated during runtime diff --git a/pylintrc.tests b/pylintrc.tests index 09772c5f5..fd08046f4 100644 --- a/pylintrc.tests +++ b/pylintrc.tests @@ -126,7 +126,7 @@ reports=no # DEFAULT: good-names=i,j,k,ex,Run,_ # RATIONALE: 'fh' is a well-known file handle variable name. good-names = i, j, k, ex, Run, _, - fh + fh, pytestmark # Regular expression matching correct method names # DEFAULT: method-rgx=[a-z_][a-z0-9_]{2,30}$ diff --git a/system_tests/nox.py b/system_tests/nox.py index 6429f7b66..5df72c81b 100644 --- a/system_tests/nox.py +++ b/system_tests/nox.py @@ -248,3 +248,9 @@ def session_app_engine(session): session.env['TEST_APP_URL'] = application_url session.chdir(HERE) session.run('pytest', 'test_app_engine.py') + + +def session_grpc(session): + session.virtualenv = False + session.env[EXPLICIT_CREDENTIALS_ENV] = SERVICE_ACCOUNT_FILE + session.run('pytest', 'test_grpc.py') diff --git a/system_tests/test_grpc.py b/system_tests/test_grpc.py new file mode 100644 index 000000000..0db9f1dd5 --- /dev/null +++ b/system_tests/test_grpc.py @@ -0,0 +1,40 @@ +# Copyright 2016 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import google.auth +import google.auth.credentials +import google.auth.transport.grpc +from google.cloud.gapic.pubsub.v1 import publisher_api + + +def test_grpc_request(http_request): + credentials, project_id = google.auth.default() + credentials = google.auth.credentials.with_scopes_if_required( + credentials, ['https://www.googleapis.com/auth/pubsub']) + + target = '{}:{}'.format( + publisher_api.PublisherApi.SERVICE_ADDRESS, + publisher_api.PublisherApi.DEFAULT_SERVICE_PORT) + + channel = google.auth.transport.grpc.secure_authorized_channel( + credentials, target, http_request) + + # Create a pub/sub client. + client = publisher_api.PublisherApi(channel=channel) + + # list the topics and drain the iterator to test that an authorized API + # call works. + list_topics_iter = client.list_topics( + project='projects/{}'.format(project_id)) + list(list_topics_iter) diff --git a/tests/transport/test_grpc.py b/tests/transport/test_grpc.py index 7d1ec117b..2b214a725 100644 --- a/tests/transport/test_grpc.py +++ b/tests/transport/test_grpc.py @@ -14,8 +14,16 @@ import mock +import pytest -import google.auth.transport.grpc +try: + import google.auth.transport.grpc + HAS_GRPC = True +except ImportError: # pragma: NO COVER + HAS_GRPC = False + + +pytestmark = pytest.mark.skipif(not HAS_GRPC, reason='gRPC is unavailable.') class MockCredentials(object): diff --git a/tox.ini b/tox.ini index 835a09f20..e5e0c5803 100644 --- a/tox.ini +++ b/tox.ini @@ -11,7 +11,7 @@ deps = urllib3 certifi requests - grpcio + grpcio; platform_python_implementation != 'PyPy' commands = py.test --cov=google.auth --cov=google.oauth2 --cov=tests {posargs:tests} @@ -31,6 +31,7 @@ commands = deps = {[testenv]deps} nox-automation + gapic-google-pubsub-v1==0.11.1 passenv = SKIP_APP_ENGINE_SYSTEM_TEST CLOUD_SDK_ROOT @@ -43,6 +44,7 @@ commands = deps = {[testenv]deps} nox-automation + gapic-google-pubsub-v1==0.11.1 passenv = SKIP_APP_ENGINE_SYSTEM_TEST CLOUD_SDK_ROOT