Exemple #1
0
def test_IncomingInterceptor_retrieve_span_values_from_incoming_call():
    m_handler_call_details = MagicMock()
    m_handler_call_details.invocation_metadata = [
        _Metadatum(key=B3_VALUES_KEY, value=json.dumps({'test_b3_values': 'values'})),
        _Metadatum(key='other_key', value='test_other_value')
    ]

    values = _IncomingInterceptor._retrieve_span_values_from_incoming_call(m_handler_call_details)

    assert values == {'test_b3_values': 'values'}
Exemple #2
0
    def test_allow_with_key(self):
        interceptor = KeyAuthServerInterceptor('key')

        mock_continuation = Mock()
        mock_continuation.return_value = 'aaa'
        result = interceptor.intercept_service(
            mock_continuation,
            _HandlerCallDetails(
                '/test/Foo',
                [_Metadatum('a', 'b'),
                 _Metadatum('secret-key', 'key')]))
        mock_continuation.assert_called()
        self.assertEqual('aaa', result)
Exemple #3
0
def test_IncomingInterceptor_retrieve_span_values_from_incoming_call_no_values():
    m_handler_call_details = MagicMock()
    m_handler_call_details.invocation_metadata = [
        _Metadatum(key='other_key', value='test_other_value')
    ]

    values = _IncomingInterceptor._retrieve_span_values_from_incoming_call(m_handler_call_details)

    assert values == {}
Exemple #4
0
 def _generate_metadata_with_b3_values(self, client_call_details):
     """
     Given the immutable metadata from the client call, get the existing metadata and create a new list of
     metadata with the span values metadatum appended.
     """
     subspan_values = self._tracer.generate_new_traced_subspan_values()
     metadata = list(client_call_details.metadata
                     ) if client_call_details.metadata is not None else []
     b3_metadatum = _Metadatum(key=B3_VALUES_KEY,
                               value=json.dumps(subspan_values))
     metadata.append(b3_metadatum)
     return metadata
Exemple #5
0
    def __init__(self,
                 context=None,
                 call_type=None,
                 real_method=None,
                 metadata=None):
        self._call_type = call_type
        self._context = context
        self._real_method = real_method

        if metadata:
            self._grpc_channel.context._invocation_metadata.extend(
                (_Metadatum(k, v) for k, v in metadata))
Exemple #6
0
    def test_user_and_token_set(self):
        self.service.authentication_classes = [FakeAuthentication]
        metadata = (("headers", json.dumps({"Authorization": "faketoken"})), )
        self.fake_context._invocation_metadata.extend(
            (_Metadatum(k, v) for k, v in metadata))
        self.servicer.DummyMethod(None, self.fake_context)

        self.assertEqual(service.context.META,
                         {"HTTP_AUTHORIZATION": "faketoken"})
        self.assertEqual(service.context.user,
                         {"email": "*****@*****.**"})
        self.assertEqual(service.context.auth, "faketoken")
    def test(self):
        interceptor = TracerClientInterceptor()

        mock_continuation = MagicMock()
        call_details = _ClientCallDetails('/test/Foo', None, [('a', 'b')],
                                          None)
        request = ()
        mock_call = MagicMock()  # type: Call
        mock_call.initial_metadata.return_value = [_Metadatum('c', 'd')]
        mock_continuation.return_value = mock_call
        response = interceptor.intercept_unary_unary(mock_continuation,
                                                     call_details,
                                                     request)  # type: Call
        mock_continuation.assert_called_once_with(
            _ClientCallDetails('/test/Foo', None,
                               [('a', 'b'),
                                ('tracer', 'This is from interceptor')], None),
            request)

        self.assertIn(_Metadatum('c', 'd'), response.initial_metadata())
        self.assertIn(_Metadatum('tracer', 'aaaa!'),
                      response.initial_metadata())
Exemple #8
0
        def fake_handler(request=None, metadata=None):
            nonlocal real_method
            self.context = FakeContext()

            if asyncio.iscoroutinefunction(real_method):
                real_method = async_to_sync(real_method)
                self.context = FakeAsyncContext()

            if metadata:
                self.context._invocation_metadata.extend(
                    (_Metadatum(k, v) for k, v in metadata))

            return real_method(request, self.context)
Exemple #9
0
def test_OutgoingInterceptor_generate_metadata_with_b3_values_none_already():
    m_tracer = MagicMock()
    interceptor = _OutgoingInterceptor(m_tracer)
    m_client_call_details = MagicMock()
    m_client_call_details.metadata = None
    m_tracer.generate_new_traced_subspan_values.return_value = {'test_b3_subspan_values': 'values'}

    new_metadata = interceptor._generate_metadata_with_b3_values(m_client_call_details)

    assert m_tracer.generate_new_traced_subspan_values.called
    assert new_metadata == [
        _Metadatum(key='b3-values', value='{"test_b3_subspan_values": "values"}')
    ]
 def intercept_unary_unary(self, continuation, client_call_details,
                           request):
     metadata = []
     if client_call_details.metadata is not None:
         metadata = list(client_call_details.metadata)
     metadata.append(('tracer', 'This is from interceptor'))
     new_details = _ClientCallDetails(client_call_details.method,
                                      client_call_details.timeout, metadata,
                                      client_call_details.credentials)
     response = continuation(new_details, request)
     original_initial_metadata = response.initial_metadata
     response.initial_metadata = lambda: original_initial_metadata(
     ) + [_Metadatum('tracer', 'aaaa!')]
     return response
Exemple #11
0
    def test_deny_without_key(self):
        interceptor = KeyAuthServerInterceptor('key')

        mock_continuation = Mock()
        handler = interceptor.intercept_service(
            mock_continuation,
            _HandlerCallDetails('/test/Foo', [_Metadatum('a', 'b')]))
        mock_continuation.assert_not_called()

        request = ()
        mock_context = Mock()
        handler.unary_unary(request, mock_context)
        mock_context.send_initial_metadata.assert_called_with([('initial-fail',
                                                                "a")])
        mock_context.set_trailing_metadata.assert_called_with([
            ('trailing-fail', "b")
        ])
        mock_context.abort.assert_called_once_with(StatusCode.UNAUTHENTICATED,
                                                   'Secret key is wrong!')
Exemple #12
0
 def metadata_callbak(metadata, error):
     context._invocation_metadata.extend((_Metadatum(k, v) for k, v in metadata))