forked from elastic/apm-agent-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_tests.py
112 lines (79 loc) · 3.35 KB
/
base_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# -*- coding: utf-8 -*-
import types
import mock
import pytest
import elasticapm
from elasticapm.instrumentation.packages.base import AbstractInstrumentedModule
from elasticapm.utils import compat, wrapt
class Dummy(object):
def dummy(self, call_args=None):
return call_args
class _TestInstrumentNonExistingFunctionOnModule(AbstractInstrumentedModule):
name = "test_non_existing_function_instrumentation"
instrument_list = [
("os.path", "non_existing_function"),
]
class _TestInstrumentNonExistingMethod(AbstractInstrumentedModule):
name = "test_non_existing_method_instrumentation"
instrument_list = [
("dict", "non_existing_method"),
]
class _TestDummyInstrumentation(AbstractInstrumentedModule):
name = "test_dummy_instrument"
instrument_list = [
("tests.instrumentation.base_tests", "Dummy.dummy"),
]
def call(self, module, method, wrapped, instance, args, kwargs):
kwargs = kwargs or {}
kwargs['call_args'] = (module, method)
return wrapped(*args, **kwargs)
def test_instrument_nonexisting_method_on_module():
_TestInstrumentNonExistingFunctionOnModule().instrument()
def test_instrument_nonexisting_method():
_TestInstrumentNonExistingMethod().instrument()
@pytest.mark.skipif(compat.PY3, reason="different object model")
def test_uninstrument_py2():
assert isinstance(Dummy.dummy, types.MethodType)
assert not isinstance(Dummy.dummy, wrapt.BoundFunctionWrapper)
instrumentation = _TestDummyInstrumentation()
instrumentation.instrument()
assert isinstance(Dummy.dummy, wrapt.BoundFunctionWrapper)
instrumentation.uninstrument()
assert isinstance(Dummy.dummy, types.MethodType)
assert not isinstance(Dummy.dummy, wrapt.BoundFunctionWrapper)
@pytest.mark.skipif(compat.PY2, reason="different object model")
def test_uninstrument_py3():
original = Dummy.dummy
assert not isinstance(Dummy.dummy, wrapt.BoundFunctionWrapper)
instrumentation = _TestDummyInstrumentation()
instrumentation.instrument()
assert Dummy.dummy is not original
assert isinstance(Dummy.dummy, wrapt.BoundFunctionWrapper)
instrumentation.uninstrument()
assert Dummy.dummy is original
assert not isinstance(Dummy.dummy, wrapt.BoundFunctionWrapper)
def test_module_method_args(elasticapm_client):
"""
Test that the module/method arguments are correctly passed to
the _TestDummyInstrumentation.call method
"""
instrumentation = _TestDummyInstrumentation()
instrumentation.instrument()
elasticapm_client.begin_transaction('test')
dummy = Dummy()
call_args = dummy.dummy()
elasticapm_client.end_transaction('test', 'test')
instrumentation.uninstrument()
assert call_args == ('tests.instrumentation.base_tests', 'Dummy.dummy')
def test_skip_instrument_env_var():
instrumentation = _TestDummyInstrumentation()
with mock.patch.dict('os.environ', {'SKIP_INSTRUMENT_TEST_DUMMY_INSTRUMENT': 'foo'}):
instrumentation.instrument()
assert not instrumentation.instrumented
def test_skip_ignored_frames(elasticapm_client):
elasticapm_client.begin_transaction('test')
with elasticapm.capture_span('test'):
pass
transaction = elasticapm_client.end_transaction('test', 'test')
for frame in transaction.spans[0].frames:
assert not frame['module'].startswith('elasticapm')