def test_wrap_httplib_response(self): mock_span = mock.Mock() span_id = '1234' mock_span.span_id = span_id mock_span.attributes = {} mock_tracer = MockTracer(mock_span) mock_response_func = mock.Mock() mock_result = mock.Mock() mock_result.status = '200' mock_response_func.return_value = mock_result patch_tracer = mock.patch( 'opencensus.trace.ext.requests.trace.execution_context.' 'get_opencensus_tracer', return_value=mock_tracer) patch_attr = mock.patch('opencensus.trace.ext.httplib.trace.' 'execution_context.get_opencensus_attr', return_value=span_id) wrapped = trace.wrap_httplib_response(mock_response_func) with patch_tracer, patch_attr: wrapped(mock.Mock()) expected_attributes = { '/http/status_code': '200'} self.assertEqual(expected_attributes, mock_tracer.span.attributes)
def test_wrap_httplib_response_no_open_span(self): mock_span = mock.Mock() span_id = '1234' mock_span.span_id = span_id mock_span.attributes = {} mock_tracer = MockTracer(mock_span) mock_response_func = mock.Mock() mock_result = mock.Mock() mock_result.status = '200' mock_response_func.return_value = mock_result patch_tracer = mock.patch( 'opencensus.trace.ext.requests.trace.execution_context.' 'get_opencensus_tracer', return_value=mock_tracer) patch_attr = mock.patch('opencensus.trace.ext.httplib.trace.' 'execution_context.get_opencensus_attr', return_value='1111') wrapped = trace.wrap_httplib_response(mock_response_func) with patch_tracer, patch_attr: wrapped(mock.Mock()) # Attribute should be empty as there is no matching span expected_attributes = {} self.assertEqual(expected_attributes, mock_tracer.span.attributes)