def setUp(self): self.wrapped = mock.Mock(Resource) self.resource = TracingWrapperResource(self.wrapped) self.request = mock.Mock(Request) self.request.method = 'GET' self.request.requestHeaders = mock.Mock(wraps=Headers({})) self.request.getHost.return_value.host = '127.0.0.1' self.request.getHost.return_value.port = 8080
def test_sets_endpoint_with_service_name(self, mock_trace): resource = TracingWrapperResource( self.wrapped, service_name='test-http') resource.getChildWithDefault('foo', self.request) set_endpoint = mock_trace.return_value.set_endpoint self.assertEqual(set_endpoint.call_count, 1) endpoint = set_endpoint.mock_calls[0][1][0] self.assertEqual(endpoint.ipv4, '127.0.0.1') self.assertEqual(endpoint.port, 8080) self.assertEqual(endpoint.service_name, 'test-http')
class TracingWrapperResourceTests(TestCase): def setUp(self): self.wrapped = mock.Mock(Resource) self.resource = TracingWrapperResource(self.wrapped) self.request = mock.Mock(Request) self.request.method = 'GET' self.request.requestHeaders = mock.Mock(wraps=Headers({})) self.request.getHost.return_value.host = '127.0.0.1' self.request.getHost.return_value.port = 8080 def test_verifyObject(self): verifyObject(IResource, self.resource) def test_putChildRaises(self): self.assertRaises( NotImplementedError, self.resource.putChild, 'foo', mock.Mock()) def test_renderRaises(self): self.assertRaises( NotImplementedError, self.resource.render, mock.Mock()) def test_getChildWithDefault_calls_wrapped(self): self.assertEqual( self.resource.getChildWithDefault('foo', self.request), self.wrapped.getChildWithDefault.return_value) self.wrapped.getChildWithDefault.assert_called_with( 'foo', self.request) @mock.patch('tryfer.http.Trace') def test_constructsTrace(self, mock_trace): self.resource.getChildWithDefault('foo', self.request) mock_trace.assert_called_with('GET', None, None, None) @mock.patch('tryfer.http.Annotation') @mock.patch('tryfer.http.Trace') def test_server_recv_annotation(self, mock_trace, mock_annotation): self.resource.getChildWithDefault('foo', self.request) mock_annotation.server_recv.assert_called_with() mock_trace.return_value.record( mock_annotation.server_recv.return_value) @mock.patch('tryfer.http.Annotation') @mock.patch('tryfer.http.Trace') def test_server_send_annotation(self, mock_trace, mock_annotation): self.request.notifyFinish.return_value = succeed(None) self.resource.getChildWithDefault('foo', self.request) mock_annotation.server_send.assert_called_with() @mock.patch('tryfer.http.Trace') def test_uses_trace_headers(self, mock_trace): self.request.requestHeaders.setRawHeaders('X-B3-TraceId', ['a']) self.request.requestHeaders.setRawHeaders('X-B3-SpanId', ['b']) self.request.requestHeaders.setRawHeaders('X-B3-ParentSpanId', ['c']) self.resource.getChildWithDefault('foo', self.request) mock_trace.assert_called_with('GET', 10, 11, 12) @mock.patch('tryfer.http.Trace') def test_uses_trace_headers_no_parent(self, mock_trace): self.request.requestHeaders.setRawHeaders('X-B3-TraceId', ['a']) self.request.requestHeaders.setRawHeaders('X-B3-SpanId', ['b']) self.resource.getChildWithDefault('foo', self.request) mock_trace.assert_called_with('GET', 10, 11, None) @mock.patch('tryfer.http.Trace') def test_sets_endpoint(self, mock_trace): self.resource.getChildWithDefault('foo', self.request) set_endpoint = mock_trace.return_value.set_endpoint self.assertEqual(set_endpoint.call_count, 1) endpoint = set_endpoint.mock_calls[0][1][0] self.assertEqual(endpoint.ipv4, '127.0.0.1') self.assertEqual(endpoint.port, 8080) self.assertEqual(endpoint.service_name, 'http') @mock.patch('tryfer.http.Trace') def test_sets_endpoint_with_service_name(self, mock_trace): resource = TracingWrapperResource( self.wrapped, service_name='test-http') resource.getChildWithDefault('foo', self.request) set_endpoint = mock_trace.return_value.set_endpoint self.assertEqual(set_endpoint.call_count, 1) endpoint = set_endpoint.mock_calls[0][1][0] self.assertEqual(endpoint.ipv4, '127.0.0.1') self.assertEqual(endpoint.port, 8080) self.assertEqual(endpoint.service_name, 'test-http')