|
| 1 | +# Copyright 2017, OpenCensus Authors |
| 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 unittest |
| 16 | + |
| 17 | +import mock |
| 18 | + |
| 19 | +from opencensus.trace.ext.google_cloud_clientlibs import trace |
| 20 | + |
| 21 | + |
| 22 | +class Test_google_cloud_clientlibs_trace(unittest.TestCase): |
| 23 | + |
| 24 | + def test_trace_integration(self): |
| 25 | + mock_trace_grpc = mock.Mock() |
| 26 | + mock_trace_http = mock.Mock() |
| 27 | + |
| 28 | + patch_trace_grpc = mock.patch( |
| 29 | + 'opencensus.trace.ext.google_cloud_clientlibs.trace.trace_grpc', |
| 30 | + mock_trace_grpc) |
| 31 | + patch_trace_http = mock.patch( |
| 32 | + 'opencensus.trace.ext.google_cloud_clientlibs.trace.trace_http', |
| 33 | + mock_trace_http) |
| 34 | + |
| 35 | + with patch_trace_grpc, patch_trace_http: |
| 36 | + trace.trace_integration() |
| 37 | + |
| 38 | + self.assertTrue(mock_trace_grpc.called) |
| 39 | + self.assertTrue(mock_trace_http.called) |
| 40 | + |
| 41 | + def test_trace_grpc(self): |
| 42 | + mock_wrap = mock.Mock() |
| 43 | + mock__helpers = mock.Mock() |
| 44 | + |
| 45 | + wrap_result = 'wrap result' |
| 46 | + mock_wrap.return_value = wrap_result |
| 47 | + |
| 48 | + mock_make_secure_channel_func = mock.Mock() |
| 49 | + mock_make_secure_channel_func.__name__ = 'make_secure_channel' |
| 50 | + setattr( |
| 51 | + mock__helpers, |
| 52 | + 'make_secure_channel', |
| 53 | + mock_make_secure_channel_func) |
| 54 | + |
| 55 | + patch_wrap = mock.patch( |
| 56 | + 'opencensus.trace.ext.google_cloud_clientlibs.trace.wrap_make_secure_channel', mock_wrap) |
| 57 | + patch__helpers = mock.patch( |
| 58 | + 'opencensus.trace.ext.google_cloud_clientlibs.trace._helpers', mock__helpers) |
| 59 | + |
| 60 | + with patch_wrap, patch__helpers: |
| 61 | + trace.trace_integration() |
| 62 | + |
| 63 | + self.assertEqual(getattr(mock__helpers, 'make_secure_channel'), wrap_result) |
| 64 | + |
| 65 | + def test_trace_http(self): |
| 66 | + mock_trace_requests = mock.Mock() |
| 67 | + patch = mock.patch( |
| 68 | + 'opencensus.trace.ext.google_cloud_clientlibs.trace.trace_requests', |
| 69 | + mock_trace_requests) |
| 70 | + |
| 71 | + with patch: |
| 72 | + trace.trace_http() |
| 73 | + |
| 74 | + self.assertTrue(mock_trace_requests.called) |
| 75 | + |
| 76 | + def test_wrap_make_secure_channel(self): |
| 77 | + mock_tracer = mock.Mock() |
| 78 | + mock_interceptor = mock.Mock() |
| 79 | + mock_func = mock.Mock() |
| 80 | + |
| 81 | + patch_tracer = mock.patch( |
| 82 | + 'opencensus.trace.ext.google_cloud_clientlibs.trace.execution_context.' |
| 83 | + 'get_opencensus_tracer', |
| 84 | + return_value=mock_tracer) |
| 85 | + patch_interceptor = mock.patch( |
| 86 | + 'opencensus.trace.ext.google_cloud_clientlibs.trace.OpenCensusClientInterceptor', |
| 87 | + mock_interceptor) |
| 88 | + |
| 89 | + wrapped = trace.wrap_make_secure_channel(mock_func) |
| 90 | + |
| 91 | + with patch_tracer, patch_interceptor: |
| 92 | + wrapped() |
| 93 | + |
| 94 | + self.assertTrue(mock_interceptor.called) |
| 95 | + |
| 96 | + def test_wrap_insecure_channel(self): |
| 97 | + mock_tracer = mock.Mock() |
| 98 | + mock_interceptor = mock.Mock() |
| 99 | + mock_func = mock.Mock() |
| 100 | + |
| 101 | + patch_tracer = mock.patch( |
| 102 | + 'opencensus.trace.ext.google_cloud_clientlibs.trace.execution_context.' |
| 103 | + 'get_opencensus_tracer', |
| 104 | + return_value=mock_tracer) |
| 105 | + patch_interceptor = mock.patch( |
| 106 | + 'opencensus.trace.ext.google_cloud_clientlibs.trace.OpenCensusClientInterceptor', |
| 107 | + mock_interceptor) |
| 108 | + |
| 109 | + wrapped = trace.wrap_insecure_channel(mock_func) |
| 110 | + |
| 111 | + with patch_tracer, patch_interceptor: |
| 112 | + wrapped() |
| 113 | + |
| 114 | + self.assertTrue(mock_interceptor.called) |
0 commit comments