|
| 1 | +# Copyright 2024 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import time |
| 16 | +from unittest import mock |
| 17 | +import grpc |
| 18 | +import pytest |
| 19 | +import google.auth.transport.grpc as transport_grpc |
| 20 | +def test_interceptor_uses_factory_if_callable(mock_replayable): |
| 21 | + import google.auth.transport.grpc as transport_grpc |
| 22 | + interceptor = transport_grpc.CertRotationInterceptor() |
| 23 | + call_no_factory = transport_grpc._RetryableStreamResponseIterator( |
| 24 | + continuation=mock.Mock(), |
| 25 | + client_call_details=mock.Mock(), |
| 26 | + request_or_iterator=[b"1", b"2"], |
| 27 | + interceptor=interceptor, |
| 28 | + is_client_stream=True, |
| 29 | + ) |
| 30 | + assert call_no_factory._uses_factory is False |
| 31 | + assert call_no_factory._payload is not None |
| 32 | + def generator_factory(): |
| 33 | + return (x for x in [b"1", b"2"]) |
| 34 | + call_factory = transport_grpc._RetryableStreamResponseIterator( |
| 35 | + continuation=mock.Mock(), |
| 36 | + client_call_details=mock.Mock(), |
| 37 | + request_or_iterator=generator_factory, |
| 38 | + interceptor=interceptor, |
| 39 | + is_client_stream=True, |
| 40 | + ) |
| 41 | + assert call_factory._uses_factory is True |
| 42 | + assert call_factory._payload is None |
| 43 | +@mock.patch("google.auth.transport.grpc.CertRotationInterceptor._should_retry") |
| 44 | +def test_factory_infinite_replay_on_error(mock_should_retry): |
| 45 | + import google.auth.transport.grpc as transport_grpc |
| 46 | + interceptor = transport_grpc.CertRotationInterceptor() |
| 47 | + interceptor._wrapper = mock.Mock() |
| 48 | + interceptor._wrapper._cached_cert = "cert" |
| 49 | + mock_should_retry.side_effect = [ |
| 50 | + (True, b"cert", b"key", None), |
| 51 | + (False, None, None, None), |
| 52 | + ] |
| 53 | + mock_inner_call1 = mock.Mock() |
| 54 | + mock_err = transport_grpc.grpc.RpcError() |
| 55 | + mock_err.code = lambda: transport_grpc.grpc.StatusCode.UNAUTHENTICATED |
| 56 | + mock_inner_call1.__next__ = mock.Mock(side_effect=mock_err) |
| 57 | + mock_inner_call2 = mock.Mock() |
| 58 | + mock_inner_call2.__next__ = mock.Mock(side_effect=[b"SUCCESS", StopIteration]) |
| 59 | + continuation = mock.Mock(side_effect=[mock_inner_call1, mock_inner_call2]) |
0 commit comments