|
| 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.httplib import trace |
| 20 | + |
| 21 | + |
| 22 | +class Test_httplib_trace(unittest.TestCase): |
| 23 | + |
| 24 | + def test_trace_integration(self): |
| 25 | + mock_wrap_request = mock.Mock() |
| 26 | + mock_wrap_response = mock.Mock() |
| 27 | + mock_httplib = mock.Mock() |
| 28 | + |
| 29 | + wrap_request_result = 'wrap request result' |
| 30 | + wrap_response_result = 'wrap response result' |
| 31 | + mock_wrap_request.return_value = wrap_request_result |
| 32 | + mock_wrap_response.return_value = wrap_response_result |
| 33 | + |
| 34 | + mock_request_func = mock.Mock() |
| 35 | + mock_response_func = mock.Mock() |
| 36 | + mock_request_func.__name__ = 'request' |
| 37 | + mock_response_func.__name__ = 'getresponse' |
| 38 | + setattr(mock_httplib.HTTPConnection, 'request', mock_request_func) |
| 39 | + setattr(mock_httplib.HTTPConnection, 'getresponse', mock_response_func) |
| 40 | + |
| 41 | + patch_wrap_request = mock.patch( |
| 42 | + 'opencensus.trace.ext.httplib.trace.wrap_httplib_request', |
| 43 | + mock_wrap_request) |
| 44 | + patch_wrap_response = mock.patch( |
| 45 | + 'opencensus.trace.ext.httplib.trace.wrap_httplib_response', |
| 46 | + mock_wrap_response) |
| 47 | + patch_httplib = mock.patch( |
| 48 | + 'opencensus.trace.ext.httplib.trace.httplib', mock_httplib) |
| 49 | + |
| 50 | + with patch_wrap_request, patch_wrap_response, patch_httplib: |
| 51 | + trace.trace_integration() |
| 52 | + |
| 53 | + self.assertEqual(getattr(mock_httplib.HTTPConnection, 'request'), |
| 54 | + wrap_request_result) |
| 55 | + self.assertEqual(getattr(mock_httplib.HTTPConnection, 'getresponse'), |
| 56 | + wrap_response_result) |
| 57 | + |
| 58 | + def test_wrap_httplib_request(self): |
| 59 | + mock_tracer = MockTracer() |
| 60 | + mock_request_func = mock.Mock() |
| 61 | + mock_request_func.__name__ = 'request' |
| 62 | + |
| 63 | + patch = mock.patch( |
| 64 | + 'opencensus.trace.ext.requests.trace.execution_context.' |
| 65 | + 'get_opencensus_tracer', |
| 66 | + return_value=mock_tracer) |
| 67 | + |
| 68 | + wrapped = trace.wrap_httplib_request(mock_request_func) |
| 69 | + |
| 70 | + url = 'http://localhost:8080' |
| 71 | + method = 'GET' |
| 72 | + |
| 73 | + with patch: |
| 74 | + wrapped(mock.Mock(), method, url) |
| 75 | + |
| 76 | + expected_attributes = { |
| 77 | + '/http/url': url, |
| 78 | + '/http/method': method} |
| 79 | + expected_name = '[httplib]request' |
| 80 | + |
| 81 | + self.assertEqual(expected_attributes, |
| 82 | + mock_tracer.span.attributes) |
| 83 | + self.assertEqual(expected_name, mock_tracer.span.name) |
| 84 | + |
| 85 | + def test_wrap_httplib_response(self): |
| 86 | + mock_span = mock.Mock() |
| 87 | + span_id = '1234' |
| 88 | + mock_span.span_id = span_id |
| 89 | + mock_span.attributes = {} |
| 90 | + mock_tracer = MockTracer(mock_span) |
| 91 | + mock_response_func = mock.Mock() |
| 92 | + mock_result = mock.Mock() |
| 93 | + mock_result.status = '200' |
| 94 | + mock_response_func.return_value = mock_result |
| 95 | + |
| 96 | + patch_tracer = mock.patch( |
| 97 | + 'opencensus.trace.ext.requests.trace.execution_context.' |
| 98 | + 'get_opencensus_tracer', |
| 99 | + return_value=mock_tracer) |
| 100 | + patch_attr = mock.patch('opencensus.trace.ext.httplib.trace.' |
| 101 | + 'execution_context.get_opencensus_attr', |
| 102 | + return_value=span_id) |
| 103 | + |
| 104 | + wrapped = trace.wrap_httplib_response(mock_response_func) |
| 105 | + |
| 106 | + with patch_tracer, patch_attr: |
| 107 | + wrapped(mock.Mock()) |
| 108 | + |
| 109 | + expected_attributes = { |
| 110 | + '/http/status_code': '200'} |
| 111 | + |
| 112 | + self.assertEqual(expected_attributes, |
| 113 | + mock_tracer.span.attributes) |
| 114 | + |
| 115 | + def test_wrap_httplib_response_no_open_span(self): |
| 116 | + mock_span = mock.Mock() |
| 117 | + span_id = '1234' |
| 118 | + mock_span.span_id = span_id |
| 119 | + mock_span.attributes = {} |
| 120 | + mock_tracer = MockTracer(mock_span) |
| 121 | + mock_response_func = mock.Mock() |
| 122 | + mock_result = mock.Mock() |
| 123 | + mock_result.status = '200' |
| 124 | + mock_response_func.return_value = mock_result |
| 125 | + |
| 126 | + patch_tracer = mock.patch( |
| 127 | + 'opencensus.trace.ext.requests.trace.execution_context.' |
| 128 | + 'get_opencensus_tracer', |
| 129 | + return_value=mock_tracer) |
| 130 | + patch_attr = mock.patch('opencensus.trace.ext.httplib.trace.' |
| 131 | + 'execution_context.get_opencensus_attr', |
| 132 | + return_value='1111') |
| 133 | + |
| 134 | + wrapped = trace.wrap_httplib_response(mock_response_func) |
| 135 | + |
| 136 | + with patch_tracer, patch_attr: |
| 137 | + wrapped(mock.Mock()) |
| 138 | + |
| 139 | + # Attribute should be empty as there is no matching span |
| 140 | + expected_attributes = {} |
| 141 | + |
| 142 | + self.assertEqual(expected_attributes, |
| 143 | + mock_tracer.span.attributes) |
| 144 | + |
| 145 | + |
| 146 | +class MockTracer(object): |
| 147 | + def __init__(self, span=None): |
| 148 | + self.span = span |
| 149 | + |
| 150 | + def current_span(self): |
| 151 | + return self.span |
| 152 | + |
| 153 | + def start_span(self): |
| 154 | + span = mock.Mock() |
| 155 | + span.attributes = {} |
| 156 | + self.span = span |
| 157 | + return span |
| 158 | + |
| 159 | + def end_span(self): |
| 160 | + pass |
| 161 | + |
| 162 | + def add_attribute_to_current_span(self, key, value): |
| 163 | + self.span.attributes[key] = value |
0 commit comments