Debian Codegraph @

tests/mocktracer/test_propagation.py

raw symbols SCIP
1 # Copyright (c) The OpenTracing Authors.
2 #
3 # Permission is hereby granted, free of charge, to any person obtaining a copy
4 # of this software and associated documentation files (the "Software"), to deal
5 # in the Software without restriction, including without limitation the rights
6 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 # copies of the Software, and to permit persons to whom the Software is
8 # furnished to do so, subject to the following conditions:
9 #
10 # The above copyright notice and this permission notice shall be included in
11 # all copies or substantial portions of the Software.
12 #
13 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 # THE SOFTWARE.
20
21 import pytest
22 from opentracing import Format, SpanContextCorruptedException, \
23 UnsupportedFormatException
24 from opentracing.mocktracer import MockTracer
25
26
27 def test_propagation():
28 tracer = MockTracer()
29 sp = tracer.start_span(operation_name='test')
30 sp.set_baggage_item('foo', 'bar')
31
32 # Test invalid types
33 with pytest.raises(UnsupportedFormatException):
34 tracer.inject(sp.context, 'invalid', {})
35 with pytest.raises(UnsupportedFormatException):
36 tracer.extract('invalid', {})
37
38 tests = [(Format.BINARY, bytearray()),
39 (Format.TEXT_MAP, {})]
40 for format, carrier in tests:
41 tracer.inject(sp.context, format, carrier)
42 extracted_ctx = tracer.extract(format, carrier)
43
44 assert extracted_ctx.trace_id == sp.context.trace_id
45 assert extracted_ctx.span_id == sp.context.span_id
46 assert extracted_ctx.baggage == sp.context.baggage
47
48
49 def test_propagation_extract_corrupted_data():
50 tracer = MockTracer()
51
52 tests = [(Format.BINARY, bytearray()),
53 (Format.TEXT_MAP, {})]
54 for format, carrier in tests:
55 with pytest.raises(SpanContextCorruptedException):
56 tracer.extract(format, carrier)
57
58
59 def test_start_span():
60 """ Test in process child span creation."""
61 tracer = MockTracer()
62 sp = tracer.start_span(operation_name='test')
63 sp.set_baggage_item('foo', 'bar')
64
65 child = tracer.start_span(
66 operation_name='child', child_of=sp.context)
67 assert child.context.trace_id == sp.context.trace_id
68 assert child.context.baggage == sp.context.baggage
69 assert child.parent_id == sp.context.span_id