From f916093e4fc6f052c8512fc9abe5ebe55ac7bf5c Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Fri, 16 Sep 2016 11:30:52 -0700 Subject: [PATCH] Fixing broken exception handling after GAX 0.13.0 upgrade. --- google/cloud/_helpers.py | 19 ----- google/cloud/logging/_gax.py | 39 +++++----- google/cloud/pubsub/_gax.py | 57 +++++++------- system_tests/pubsub.py | 6 +- unit_tests/_testing.py | 6 +- unit_tests/logging/test__gax.py | 90 +++++++++++----------- unit_tests/pubsub/test__gax.py | 132 ++++++++++++++++---------------- unit_tests/test__helpers.py | 31 -------- 8 files changed, 165 insertions(+), 215 deletions(-) diff --git a/google/cloud/_helpers.py b/google/cloud/_helpers.py index a7da90880bbc..3b214db77b82 100644 --- a/google/cloud/_helpers.py +++ b/google/cloud/_helpers.py @@ -31,13 +31,9 @@ except ImportError: app_identity = None try: - from google.gax.grpc import exc_to_code as beta_exc_to_code import grpc - from grpc._channel import _Rendezvous except ImportError: # pragma: NO COVER - beta_exc_to_code = None grpc = None - _Rendezvous = Exception import six from six.moves import http_client from six.moves import configparser @@ -685,21 +681,6 @@ def make_insecure_stub(stub_class, host, port=None): return stub_class(channel) -def exc_to_code(exc): - """Retrieves the status code from a gRPC exception. - - :type exc: :class:`Exception` - :param exc: An exception from gRPC beta or stable. - - :rtype: :class:`grpc.StatusCode` - :returns: The status code attached to the exception. - """ - if isinstance(exc, _Rendezvous): - return exc.code() - else: - return beta_exc_to_code(exc) - - try: from pytz import UTC # pylint: disable=unused-import,wrong-import-order except ImportError: diff --git a/google/cloud/logging/_gax.py b/google/cloud/logging/_gax.py index 8f6c76572cf1..ec4f70d42cc8 100644 --- a/google/cloud/logging/_gax.py +++ b/google/cloud/logging/_gax.py @@ -18,18 +18,17 @@ from google.gax import CallOptions from google.gax import INITIAL_PAGE -from google.gax.errors import GaxError from google.logging.type.log_severity_pb2 import LogSeverity from google.logging.v2.logging_config_pb2 import LogSink from google.logging.v2.logging_metrics_pb2 import LogMetric from google.logging.v2.log_entry_pb2 import LogEntry from google.protobuf.json_format import Parse from grpc import StatusCode +from grpc._channel import _Rendezvous # pylint: disable=ungrouped-imports from google.cloud._helpers import _datetime_to_pb_timestamp from google.cloud._helpers import _pb_timestamp_to_rfc3339 -from google.cloud._helpers import exc_to_code from google.cloud.exceptions import Conflict from google.cloud.exceptions import NotFound # pylint: enable=ungrouped-imports @@ -123,8 +122,8 @@ def logger_delete(self, project, logger_name): path = 'projects/%s/logs/%s' % (project, logger_name) try: self._gax_api.delete_log(path, options) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except _Rendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(path) raise @@ -195,8 +194,8 @@ def sink_create(self, project, sink_name, filter_, destination): destination=destination) try: self._gax_api.create_sink(parent, sink_pb, options) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.FAILED_PRECONDITION: + except _Rendezvous as exc: + if exc.code() == StatusCode.FAILED_PRECONDITION: path = 'projects/%s/sinks/%s' % (project, sink_name) raise Conflict(path) raise @@ -218,8 +217,8 @@ def sink_get(self, project, sink_name): path = 'projects/%s/sinks/%s' % (project, sink_name) try: sink_pb = self._gax_api.get_sink(path, options) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except _Rendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(path) raise return _log_sink_pb_to_mapping(sink_pb) @@ -250,8 +249,8 @@ def sink_update(self, project, sink_name, filter_, destination): sink_pb = LogSink(name=path, filter=filter_, destination=destination) try: self._gax_api.update_sink(path, sink_pb, options) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except _Rendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(path) raise return _log_sink_pb_to_mapping(sink_pb) @@ -269,8 +268,8 @@ def sink_delete(self, project, sink_name): path = 'projects/%s/sinks/%s' % (project, sink_name) try: self._gax_api.delete_sink(path, options) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except _Rendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(path) raise @@ -340,8 +339,8 @@ def metric_create(self, project, metric_name, filter_, description): description=description) try: self._gax_api.create_log_metric(parent, metric_pb, options) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.FAILED_PRECONDITION: + except _Rendezvous as exc: + if exc.code() == StatusCode.FAILED_PRECONDITION: path = 'projects/%s/metrics/%s' % (project, metric_name) raise Conflict(path) raise @@ -363,8 +362,8 @@ def metric_get(self, project, metric_name): path = 'projects/%s/metrics/%s' % (project, metric_name) try: metric_pb = self._gax_api.get_log_metric(path, options) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except _Rendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(path) raise return _log_metric_pb_to_mapping(metric_pb) @@ -395,8 +394,8 @@ def metric_update(self, project, metric_name, filter_, description): description=description) try: self._gax_api.update_log_metric(path, metric_pb, options) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except _Rendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(path) raise return _log_metric_pb_to_mapping(metric_pb) @@ -414,8 +413,8 @@ def metric_delete(self, project, metric_name): path = 'projects/%s/metrics/%s' % (project, metric_name) try: self._gax_api.delete_log_metric(path, options) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except _Rendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(path) raise diff --git a/google/cloud/pubsub/_gax.py b/google/cloud/pubsub/_gax.py index 2a0b0393aa5b..4e397bef2758 100644 --- a/google/cloud/pubsub/_gax.py +++ b/google/cloud/pubsub/_gax.py @@ -18,15 +18,14 @@ from google.cloud.gapic.pubsub.v1.subscriber_api import SubscriberApi from google.gax import CallOptions from google.gax import INITIAL_PAGE -from google.gax.errors import GaxError from google.pubsub.v1.pubsub_pb2 import PubsubMessage from google.pubsub.v1.pubsub_pb2 import PushConfig -from grpc.beta.implementations import insecure_channel +from grpc import insecure_channel from grpc import StatusCode +from grpc._channel import _Rendezvous # pylint: disable=ungrouped-imports from google.cloud._helpers import _to_bytes -from google.cloud._helpers import exc_to_code from google.cloud._helpers import _pb_timestamp_to_rfc3339 from google.cloud.exceptions import Conflict from google.cloud.exceptions import NotFound @@ -93,8 +92,8 @@ def topic_create(self, topic_path): """ try: topic_pb = self._gax_api.create_topic(topic_path) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.FAILED_PRECONDITION: + except _Rendezvous as exc: + if exc.code() == StatusCode.FAILED_PRECONDITION: raise Conflict(topic_path) raise return {'name': topic_pb.name} @@ -116,8 +115,8 @@ def topic_get(self, topic_path): """ try: topic_pb = self._gax_api.get_topic(topic_path) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except _Rendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(topic_path) raise return {'name': topic_pb.name} @@ -134,8 +133,8 @@ def topic_delete(self, topic_path): """ try: self._gax_api.delete_topic(topic_path) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except _Rendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(topic_path) raise @@ -163,8 +162,8 @@ def topic_publish(self, topic_path, messages): try: result = self._gax_api.publish(topic_path, message_pbs, options=options) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except _Rendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(topic_path) raise return result.message_ids @@ -201,8 +200,8 @@ def topic_list_subscriptions(self, topic_path, page_size=0, try: page_iter = self._gax_api.list_topic_subscriptions( topic_path, page_size=page_size, options=options) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except _Rendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(topic_path) raise subs = page_iter.next() @@ -294,8 +293,8 @@ def subscription_create(self, subscription_path, topic_path, try: sub_pb = self._gax_api.create_subscription( subscription_path, topic_path, push_config, ack_deadline) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.FAILED_PRECONDITION: + except _Rendezvous as exc: + if exc.code() == StatusCode.FAILED_PRECONDITION: raise Conflict(topic_path) raise return _subscription_pb_to_mapping(sub_pb) @@ -316,8 +315,8 @@ def subscription_get(self, subscription_path): """ try: sub_pb = self._gax_api.get_subscription(subscription_path) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except _Rendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(subscription_path) raise return _subscription_pb_to_mapping(sub_pb) @@ -335,8 +334,8 @@ def subscription_delete(self, subscription_path): """ try: self._gax_api.delete_subscription(subscription_path) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except _Rendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(subscription_path) raise @@ -360,8 +359,8 @@ def subscription_modify_push_config(self, subscription_path, push_config = PushConfig(push_endpoint=push_endpoint) try: self._gax_api.modify_push_config(subscription_path, push_config) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except _Rendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(subscription_path) raise @@ -392,8 +391,8 @@ def subscription_pull(self, subscription_path, return_immediately=False, try: response_pb = self._gax_api.pull( subscription_path, max_messages, return_immediately) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except _Rendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(subscription_path) raise return [_received_message_pb_to_mapping(rmpb) @@ -415,8 +414,8 @@ def subscription_acknowledge(self, subscription_path, ack_ids): """ try: self._gax_api.acknowledge(subscription_path, ack_ids) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except _Rendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(subscription_path) raise @@ -442,8 +441,8 @@ def subscription_modify_ack_deadline(self, subscription_path, ack_ids, try: self._gax_api.modify_ack_deadline( subscription_path, ack_ids, ack_deadline) - except GaxError as exc: - if exc_to_code(exc.cause) == StatusCode.NOT_FOUND: + except _Rendezvous as exc: + if exc.code() == StatusCode.NOT_FOUND: raise NotFound(subscription_path) raise @@ -520,7 +519,7 @@ def make_gax_publisher_api(connection): """ channel = None if connection.in_emulator: - channel = insecure_channel(connection.host, None) + channel = insecure_channel(connection.host) return PublisherApi(channel=channel) @@ -540,5 +539,5 @@ def make_gax_subscriber_api(connection): """ channel = None if connection.in_emulator: - channel = insecure_channel(connection.host, None) + channel = insecure_channel(connection.host) return SubscriberApi(channel=channel) diff --git a/system_tests/pubsub.py b/system_tests/pubsub.py index 2d512dd38002..3922085be20b 100644 --- a/system_tests/pubsub.py +++ b/system_tests/pubsub.py @@ -15,13 +15,11 @@ import os import unittest -from google.gax.errors import GaxError from grpc import StatusCode from grpc._channel import _Rendezvous import httplib2 # pylint: disable=ungrouped-imports -from google.cloud import _helpers from google.cloud.environment_vars import PUBSUB_EMULATOR from google.cloud.pubsub import client # pylint: enable=ungrouped-imports @@ -34,10 +32,10 @@ def _unavailable(exc): - return _helpers.exc_to_code(exc) == StatusCode.UNAVAILABLE + return exc.code() == StatusCode.UNAVAILABLE -retry_unavailable = RetryErrors((GaxError, _Rendezvous), _unavailable) +retry_unavailable = RetryErrors(_Rendezvous, _unavailable) class Config(object): diff --git a/unit_tests/_testing.py b/unit_tests/_testing.py index d3a1a268b5b4..5436145073fd 100644 --- a/unit_tests/_testing.py +++ b/unit_tests/_testing.py @@ -57,9 +57,13 @@ class _GAXBaseAPI(object): def __init__(self, **kw): self.__dict__.update(kw) - def _make_grpc_error(self, status_code): + def _make_grpc_error(self, status_code=None): from grpc._channel import _Rendezvous from grpc._channel import _RPCState + from grpc import StatusCode + + if status_code is None: + status_code = StatusCode.UNKNOWN details = 'Some error details.' exc_state = _RPCState((), None, None, status_code, details) diff --git a/unit_tests/logging/test__gax.py b/unit_tests/logging/test__gax.py index 13880bfed1b1..adf552c98bfa 100644 --- a/unit_tests/logging/test__gax.py +++ b/unit_tests/logging/test__gax.py @@ -423,12 +423,13 @@ def test_logger_delete_not_found(self): self.assertEqual(options, None) def test_logger_delete_error(self): - from google.gax.errors import GaxError + from grpc._channel import _Rendezvous + LOG_PATH = 'projects/%s/logs/%s' % (self.PROJECT, self.LOG_NAME) gax_api = _GAXLoggingAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(_Rendezvous): api.logger_delete(self.PROJECT, self.LOG_NAME) log_name, options = gax_api._delete_log_called_with @@ -503,11 +504,12 @@ def test_list_sinks_w_paging(self): self.assertEqual(options.page_token, TOKEN) def test_sink_create_error(self): - from google.gax.errors import GaxError + from grpc._channel import _Rendezvous + gax_api = _GAXSinksAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(_Rendezvous): api.sink_create( self.PROJECT, self.SINK_NAME, self.FILTER, self.DESTINATION_URI) @@ -548,11 +550,12 @@ def test_sink_get_error(self): api.sink_get(self.PROJECT, self.SINK_NAME) def test_sink_get_miss(self): - from google.gax.errors import GaxError + from grpc._channel import _Rendezvous + gax_api = _GAXSinksAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(_Rendezvous): api.sink_get(self.PROJECT, self.SINK_NAME) def test_sink_get_hit(self): @@ -575,11 +578,12 @@ def test_sink_get_hit(self): self.assertEqual(options, None) def test_sink_update_error(self): - from google.gax.errors import GaxError + from grpc._channel import _Rendezvous + gax_api = _GAXSinksAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(_Rendezvous): api.sink_update( self.PROJECT, self.SINK_NAME, self.FILTER, self.DESTINATION_URI) @@ -614,11 +618,12 @@ def test_sink_update_hit(self): self.assertEqual(options, None) def test_sink_delete_error(self): - from google.gax.errors import GaxError + from grpc._channel import _Rendezvous + gax_api = _GAXSinksAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(_Rendezvous): api.sink_delete(self.PROJECT, self.SINK_NAME) def test_sink_delete_miss(self): @@ -707,11 +712,12 @@ def test_list_metrics_w_paging(self): self.assertEqual(options.page_token, TOKEN) def test_metric_create_error(self): - from google.gax.errors import GaxError + from grpc._channel import _Rendezvous + gax_api = _GAXMetricsAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(_Rendezvous): api.metric_create( self.PROJECT, self.METRIC_NAME, self.FILTER, self.DESCRIPTION) @@ -752,11 +758,12 @@ def test_metric_get_error(self): api.metric_get(self.PROJECT, self.METRIC_NAME) def test_metric_get_miss(self): - from google.gax.errors import GaxError + from grpc._channel import _Rendezvous + gax_api = _GAXMetricsAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(_Rendezvous): api.metric_get(self.PROJECT, self.METRIC_NAME) def test_metric_get_hit(self): @@ -779,11 +786,12 @@ def test_metric_get_hit(self): self.assertEqual(options, None) def test_metric_update_error(self): - from google.gax.errors import GaxError + from grpc._channel import _Rendezvous + gax_api = _GAXMetricsAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(_Rendezvous): api.metric_update( self.PROJECT, self.METRIC_NAME, self.FILTER, self.DESCRIPTION) @@ -818,11 +826,12 @@ def test_metric_update_hit(self): self.assertEqual(options, None) def test_metric_delete_error(self): - from google.gax.errors import GaxError + from grpc._channel import _Rendezvous + gax_api = _GAXMetricsAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(_Rendezvous): api.metric_delete(self.PROJECT, self.METRIC_NAME) def test_metric_delete_miss(self): @@ -929,12 +938,11 @@ def write_log_entries(self, entries, log_name, resource, labels, entries, log_name, resource, labels, partial_success, options) def delete_log(self, log_name, options): - from google.gax.errors import GaxError self._delete_log_called_with = log_name, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() if self._delete_not_found: - raise GaxError('notfound', self._make_grpc_not_found()) + raise self._make_grpc_not_found() class _GAXSinksAPI(_GAXBaseAPI): @@ -947,40 +955,36 @@ def list_sinks(self, parent, page_size, options): return self._list_sinks_response def create_sink(self, parent, sink, options): - from google.gax.errors import GaxError self._create_sink_called_with = parent, sink, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() if self._create_sink_conflict: - raise GaxError('conflict', self._make_grpc_failed_precondition()) + raise self._make_grpc_failed_precondition() def get_sink(self, sink_name, options): - from google.gax.errors import GaxError self._get_sink_called_with = sink_name, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() try: return self._get_sink_response except AttributeError: - raise GaxError('notfound', self._make_grpc_not_found()) + raise self._make_grpc_not_found() def update_sink(self, sink_name, sink, options=None): - from google.gax.errors import GaxError self._update_sink_called_with = sink_name, sink, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() try: return self._update_sink_response except AttributeError: - raise GaxError('notfound', self._make_grpc_not_found()) + raise self._make_grpc_not_found() def delete_sink(self, sink_name, options=None): - from google.gax.errors import GaxError self._delete_sink_called_with = sink_name, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() if self._sink_not_found: - raise GaxError('notfound', self._make_grpc_not_found()) + raise self._make_grpc_not_found() class _GAXMetricsAPI(_GAXBaseAPI): @@ -993,40 +997,36 @@ def list_log_metrics(self, parent, page_size, options): return self._list_log_metrics_response def create_log_metric(self, parent, metric, options): - from google.gax.errors import GaxError self._create_log_metric_called_with = parent, metric, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() if self._create_log_metric_conflict: - raise GaxError('conflict', self._make_grpc_failed_precondition()) + raise self._make_grpc_failed_precondition() def get_log_metric(self, metric_name, options): - from google.gax.errors import GaxError self._get_log_metric_called_with = metric_name, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() try: return self._get_log_metric_response except AttributeError: - raise GaxError('notfound', self._make_grpc_not_found()) + raise self._make_grpc_not_found() def update_log_metric(self, metric_name, metric, options=None): - from google.gax.errors import GaxError self._update_log_metric_called_with = metric_name, metric, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() try: return self._update_log_metric_response except AttributeError: - raise GaxError('notfound', self._make_grpc_not_found()) + raise self._make_grpc_not_found() def delete_log_metric(self, metric_name, options=None): - from google.gax.errors import GaxError self._delete_log_metric_called_with = metric_name, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() if self._log_metric_not_found: - raise GaxError('notfound', self._make_grpc_not_found()) + raise self._make_grpc_not_found() class _HTTPRequestPB(object): diff --git a/unit_tests/pubsub/test__gax.py b/unit_tests/pubsub/test__gax.py index a49af1b6f46a..066c1f6c2997 100644 --- a/unit_tests/pubsub/test__gax.py +++ b/unit_tests/pubsub/test__gax.py @@ -123,11 +123,12 @@ def test_topic_create_already_exists(self): self.assertEqual(options, None) def test_topic_create_error(self): - from google.gax.errors import GaxError + from grpc._channel import _Rendezvous + gax_api = _GAXPublisherAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(_Rendezvous): api.topic_create(self.TOPIC_PATH) topic_path, options = gax_api._create_topic_called_with @@ -159,11 +160,12 @@ def test_topic_get_miss(self): self.assertEqual(options, None) def test_topic_get_error(self): - from google.gax.errors import GaxError + from grpc._channel import _Rendezvous + gax_api = _GAXPublisherAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(_Rendezvous): api.topic_get(self.TOPIC_PATH) topic_path, options = gax_api._get_topic_called_with @@ -193,11 +195,12 @@ def test_topic_delete_miss(self): self.assertEqual(options, None) def test_topic_delete_error(self): - from google.gax.errors import GaxError + from grpc._channel import _Rendezvous + gax_api = _GAXPublisherAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(_Rendezvous): api.topic_delete(self.TOPIC_PATH) topic_path, options = gax_api._delete_topic_called_with @@ -245,14 +248,15 @@ def test_topic_publish_miss_w_attrs_w_bytes_payload(self): def test_topic_publish_error(self): import base64 - from google.gax.errors import GaxError + from grpc._channel import _Rendezvous + PAYLOAD = b'This is the message text' B64 = base64.b64encode(PAYLOAD).decode('ascii') MESSAGE = {'data': B64, 'attributes': {}} gax_api = _GAXPublisherAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(_Rendezvous): api.topic_publish(self.TOPIC_PATH, [MESSAGE]) topic_path, message_pbs, options = gax_api._publish_called_with @@ -329,11 +333,12 @@ def test_topic_list_subscriptions_miss(self): def test_topic_list_subscriptions_error(self): from google.gax import INITIAL_PAGE - from google.gax.errors import GaxError + from grpc._channel import _Rendezvous + gax_api = _GAXPublisherAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(_Rendezvous): api.topic_list_subscriptions(self.TOPIC_PATH) topic_path, page_size, options = ( @@ -450,11 +455,12 @@ def test_subscription_create_already_exists(self): self.assertEqual(options, None) def test_subscription_create_error(self): - from google.gax.errors import GaxError + from grpc._channel import _Rendezvous + gax_api = _GAXSubscriberAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(_Rendezvous): api.subscription_create(self.SUB_PATH, self.TOPIC_PATH) name, topic, push_config, ack_deadline, options = ( @@ -499,11 +505,12 @@ def test_subscription_get_miss(self): self.assertEqual(options, None) def test_subscription_get_error(self): - from google.gax.errors import GaxError + from grpc._channel import _Rendezvous + gax_api = _GAXSubscriberAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(_Rendezvous): api.subscription_get(self.SUB_PATH) sub_path, options = gax_api._get_subscription_called_with @@ -533,11 +540,12 @@ def test_subscription_delete_miss(self): self.assertEqual(options, None) def test_subscription_delete_error(self): - from google.gax.errors import GaxError + from grpc._channel import _Rendezvous + gax_api = _GAXSubscriberAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(_Rendezvous): api.subscription_delete(self.TOPIC_PATH) sub_path, options = gax_api._delete_subscription_called_with @@ -570,11 +578,12 @@ def test_subscription_modify_push_config_miss(self): self.assertEqual(options, None) def test_subscription_modify_push_config_error(self): - from google.gax.errors import GaxError + from grpc._channel import _Rendezvous + gax_api = _GAXSubscriberAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(_Rendezvous): api.subscription_modify_push_config( self.SUB_PATH, self.PUSH_ENDPOINT) @@ -636,11 +645,12 @@ def test_subscription_pull_defaults_miss(self): self.assertEqual(options, None) def test_subscription_pull_defaults_error(self): - from google.gax.errors import GaxError + from grpc._channel import _Rendezvous + gax_api = _GAXSubscriberAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(_Rendezvous): api.subscription_pull(self.SUB_PATH) sub_path, max_messages, return_immediately, options = ( @@ -679,13 +689,14 @@ def test_subscription_acknowledge_miss(self): self.assertEqual(options, None) def test_subscription_acknowledge_error(self): - from google.gax.errors import GaxError + from grpc._channel import _Rendezvous + ACK_ID1 = 'DEADBEEF' ACK_ID2 = 'BEADCAFE' gax_api = _GAXSubscriberAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(_Rendezvous): api.subscription_acknowledge(self.SUB_PATH, [ACK_ID1, ACK_ID2]) sub_path, ack_ids, options = gax_api._acknowledge_called_with @@ -730,14 +741,15 @@ def test_subscription_modify_ack_deadline_miss(self): self.assertEqual(options, None) def test_subscription_modify_ack_deadline_error(self): - from google.gax.errors import GaxError + from grpc._channel import _Rendezvous + ACK_ID1 = 'DEADBEEF' ACK_ID2 = 'BEADCAFE' NEW_DEADLINE = 90 gax_api = _GAXSubscriberAPI(_random_gax_error=True) api = self._makeOne(gax_api) - with self.assertRaises(GaxError): + with self.assertRaises(_Rendezvous): api.subscription_modify_ack_deadline( self.SUB_PATH, [ACK_ID1, ACK_ID2], NEW_DEADLINE) @@ -787,8 +799,8 @@ def mock_publisher_api(channel): channels.append(channel) return mock_result - def mock_insecure_channel(host, port): - insecure_args.append((host, port)) + def mock_insecure_channel(host): + insecure_args.append(host) return mock_channel host = 'CURR_HOST:1234' @@ -799,7 +811,7 @@ def mock_insecure_channel(host, port): self.assertIs(result, mock_result) self.assertEqual(channels, [mock_channel]) - self.assertEqual(insecure_args, [(host, None)]) + self.assertEqual(insecure_args, [host]) @unittest.skipUnless(_HAVE_GAX, 'No gax-python') @@ -840,8 +852,8 @@ def mock_subscriber_api(channel): channels.append(channel) return mock_result - def mock_insecure_channel(host, port): - insecure_args.append((host, port)) + def mock_insecure_channel(host): + insecure_args.append(host) return mock_channel host = 'CURR_HOST:1234' @@ -852,7 +864,7 @@ def mock_insecure_channel(host, port): self.assertIs(result, mock_result) self.assertEqual(channels, [mock_channel]) - self.assertEqual(insecure_args, [(host, None)]) + self.assertEqual(insecure_args, [host]) class _GAXPublisherAPI(_GAXBaseAPI): @@ -864,51 +876,46 @@ def list_topics(self, name, page_size, options): return self._list_topics_response def create_topic(self, name, options=None): - from google.gax.errors import GaxError self._create_topic_called_with = name, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() if self._create_topic_conflict: - raise GaxError('conflict', self._make_grpc_failed_precondition()) + raise self._make_grpc_failed_precondition() return self._create_topic_response def get_topic(self, name, options=None): - from google.gax.errors import GaxError self._get_topic_called_with = name, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() try: return self._get_topic_response except AttributeError: - raise GaxError('miss', self._make_grpc_not_found()) + raise self._make_grpc_not_found() def delete_topic(self, name, options=None): - from google.gax.errors import GaxError self._delete_topic_called_with = name, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() if not self._delete_topic_ok: - raise GaxError('miss', self._make_grpc_not_found()) + raise self._make_grpc_not_found() def publish(self, topic, messages, options=None): - from google.gax.errors import GaxError self._publish_called_with = topic, messages, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() try: return self._publish_response except AttributeError: - raise GaxError('miss', self._make_grpc_not_found()) + raise self._make_grpc_not_found() def list_topic_subscriptions(self, topic, page_size, options=None): - from google.gax.errors import GaxError self._list_topic_subscriptions_called_with = topic, page_size, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() try: return self._list_topic_subscriptions_response except AttributeError: - raise GaxError('miss', self._make_grpc_not_found()) + raise self._make_grpc_not_found() class _GAXSubscriberAPI(_GAXBaseAPI): @@ -925,68 +932,61 @@ def list_subscriptions(self, project, page_size, options=None): def create_subscription(self, name, topic, push_config, ack_deadline_seconds, options=None): - from google.gax.errors import GaxError self._create_subscription_called_with = ( name, topic, push_config, ack_deadline_seconds, options) if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() if self._create_subscription_conflict: - raise GaxError('conflict', self._make_grpc_failed_precondition()) + raise self._make_grpc_failed_precondition() return self._create_subscription_response def get_subscription(self, name, options=None): - from google.gax.errors import GaxError self._get_subscription_called_with = name, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() try: return self._get_subscription_response except AttributeError: - raise GaxError('miss', self._make_grpc_not_found()) + raise self._make_grpc_not_found() def delete_subscription(self, name, options=None): - from google.gax.errors import GaxError self._delete_subscription_called_with = name, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() if not self._delete_subscription_ok: - raise GaxError('miss', self._make_grpc_not_found()) + raise self._make_grpc_not_found() def modify_push_config(self, name, push_config, options=None): - from google.gax.errors import GaxError self._modify_push_config_called_with = name, push_config, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() if not self._modify_push_config_ok: - raise GaxError('miss', self._make_grpc_not_found()) + raise self._make_grpc_not_found() def pull(self, name, max_messages, return_immediately, options=None): - from google.gax.errors import GaxError self._pull_called_with = ( name, max_messages, return_immediately, options) if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() try: return self._pull_response except AttributeError: - raise GaxError('miss', self._make_grpc_not_found()) + raise self._make_grpc_not_found() def acknowledge(self, name, ack_ids, options=None): - from google.gax.errors import GaxError self._acknowledge_called_with = name, ack_ids, options if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() if not self._acknowledge_ok: - raise GaxError('miss', self._make_grpc_not_found()) + raise self._make_grpc_not_found() def modify_ack_deadline(self, name, ack_ids, deadline, options=None): - from google.gax.errors import GaxError self._modify_ack_deadline_called_with = ( name, ack_ids, deadline, options) if self._random_gax_error: - raise GaxError('error') + raise self._make_grpc_error() if not self._modify_ack_deadline_ok: - raise GaxError('miss', self._make_grpc_not_found()) + raise self._make_grpc_not_found() class _TopicPB(object): diff --git a/unit_tests/test__helpers.py b/unit_tests/test__helpers.py index 908f5cd49344..ccebcc208a95 100644 --- a/unit_tests/test__helpers.py +++ b/unit_tests/test__helpers.py @@ -1025,37 +1025,6 @@ def test_without_port_argument(self): self._helper(host, host) -class Test_exc_to_code(unittest.TestCase): - - def _callFUT(self, exc): - from google.cloud._helpers import exc_to_code - return exc_to_code(exc) - - def test_with_stable(self): - from grpc._channel import _Rendezvous - from grpc._channel import _RPCState - from grpc import StatusCode - - status_code = StatusCode.FAILED_PRECONDITION - exc_state = _RPCState((), None, None, status_code, None) - exc = _Rendezvous(exc_state, None, None, None) - result = self._callFUT(exc) - self.assertEqual(result, status_code) - - def test_with_beta(self): - from grpc import StatusCode - from grpc.framework.interfaces.face.face import AbortionError - - status_code = StatusCode.UNIMPLEMENTED - exc = AbortionError(None, None, status_code, None) - result = self._callFUT(exc) - self.assertEqual(result, status_code) - - def test_with_none(self): - result = self._callFUT(None) - self.assertIsNone(result) - - class _AppIdentity(object): def __init__(self, app_id):