forked from instana/python-sensor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
288 lines (224 loc) · 8.54 KB
/
conftest.py
File metadata and controls
288 lines (224 loc) · 8.54 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# (c) Copyright IBM Corp. 2021
# (c) Copyright Instana Inc. 2020
import importlib.util
import os
import sys
from typing import Any, Dict
import pytest
from opentelemetry.context.context import Context
from opentelemetry.trace import set_span_in_context
from opentelemetry.trace.span import format_span_id
if importlib.util.find_spec("celery"):
pytest_plugins = ("celery.contrib.pytest",)
from instana.agent.host import HostAgent
from instana.collector.base import BaseCollector
from instana.fsm import TheMachine
from instana.recorder import StanRecorder
from instana.span.base_span import BaseSpan
from instana.span.span import InstanaSpan
from instana.span_context import SpanContext
from instana.tracer import InstanaTracerProvider
from instana.util.runtime import is_ppc64, is_s390x
collect_ignore_glob = [
"*collector/test_gcr*",
"*agent/test_google*",
]
# ppc64le and s390x have limitations with some supported libraries.
if is_ppc64() or is_s390x():
collect_ignore_glob.extend(
[
"*test_google-cloud*",
"*test_pymongo*",
]
)
if is_ppc64():
collect_ignore_glob.append("*test_grpcio*")
# # Cassandra and gevent tests are run in dedicated jobs on CircleCI and will
# # be run explicitly. (So always exclude them here)
if not os.environ.get("CASSANDRA_TEST"):
collect_ignore_glob.append("*test_cassandra*")
if not os.environ.get("COUCHBASE_TEST"):
collect_ignore_glob.append("*test_couchbase*")
if not os.environ.get("GEVENT_TEST"):
collect_ignore_glob.extend(
[
"*test_gevent*",
]
)
if not os.environ.get("KAFKA_TEST"):
collect_ignore_glob.append("*kafka/test*")
# Currently asyncio and tornado_server depends on aiohttp and
# since aiohttp versions < 3.12.14 have vulnerability we skip the tests below
if sys.version_info < (3, 9):
collect_ignore_glob.extend(
[
"*test_aiohttp*",
"*test_asyncio*",
"*test_tornado_server*",
]
)
if sys.version_info >= (3, 12):
# Currently Spyne does not support python > 3.12
collect_ignore_glob.append("*test_spyne*")
if sys.version_info >= (3, 15):
collect_ignore_glob.extend(
[
# Currently not installable dependencies because of 3.15 incompatibilities
"*test_fastapi*",
# Development version of Python always break logging tests, so we skip then
"*test_logging*",
# Removing the following tests due to "Too long with no output"
# errors from CircleCI pipeline
"*test_aio_pika*",
"*test_pyramid*",
"*test_sanic*",
"*test_starlette*",
]
)
@pytest.fixture(scope="session")
def celery_config():
return {
"broker_connection_retry_on_startup": True,
"broker_url": "redis://localhost:6379",
"result_backend": "redis://localhost:6379",
}
@pytest.fixture(scope="session")
def celery_enable_logging():
return True
@pytest.fixture(scope="session")
def celery_includes():
return {"tests.frameworks.test_celery"}
@pytest.fixture
def trace_id() -> int:
return 1812338823475918251
@pytest.fixture
def span_id() -> int:
return 6895521157646639861
@pytest.fixture
def hex_trace_id(trace_id: int) -> str:
# Using format_span_id() to return a 16-byte hexadecimal string, instead of
# the 32-byte hexadecimal string from format_trace_id().
return format_span_id(trace_id)
@pytest.fixture
def hex_span_id(span_id: int) -> str:
return format_span_id(span_id)
@pytest.fixture
def span_processor() -> StanRecorder:
rec = StanRecorder(HostAgent())
rec.THREAD_NAME = "InstanaSpan Recorder Test"
return rec
@pytest.fixture
def tracer_provider(span_processor: StanRecorder) -> InstanaTracerProvider:
return InstanaTracerProvider(span_processor=span_processor, exporter=HostAgent())
@pytest.fixture
def span_context(trace_id: int, span_id: int) -> SpanContext:
return SpanContext(
trace_id=trace_id,
span_id=span_id,
is_remote=False,
)
@pytest.fixture
def span(span_context: SpanContext, span_processor: StanRecorder) -> InstanaSpan:
span_name = "test-span"
return InstanaSpan(span_name, span_context, span_processor)
@pytest.fixture
def base_span(span: InstanaSpan) -> BaseSpan:
return BaseSpan(span, None)
@pytest.fixture
def context(span: InstanaSpan) -> Context:
return set_span_in_context(span)
def always_true(_: object, *args: object, **kwargs: object) -> bool:
return True
# Mocking HostAgent.can_send()
@pytest.fixture(autouse=True)
def can_send(monkeypatch, request) -> None:
"""Return always True for HostAgent.can_send()"""
if "original" in request.keywords:
# If using the `@pytest.mark.original` marker before the test function,
# uses the original HostAgent.can_send()
monkeypatch.setattr(HostAgent, "can_send", HostAgent.can_send)
else:
monkeypatch.setattr(HostAgent, "can_send", always_true)
# Mocking HostAgent.get_from_structure()
@pytest.fixture(autouse=True)
def get_from_structure(monkeypatch, request) -> None:
"""
Retrieves the From data that is reported alongside monitoring data.
@return: dict()
"""
def _get_from_structure(_: object) -> Dict[str, Any]:
return {"e": os.getpid(), "h": "fake"}
if "original" in request.keywords:
# If using the `@pytest.mark.original` marker before the test function,
# uses the original HostAgent.get_from_structure()
monkeypatch.setattr(
HostAgent, "get_from_structure", HostAgent.get_from_structure
)
else:
monkeypatch.setattr(HostAgent, "get_from_structure", _get_from_structure)
# Mocking BaseCollector.prepare_and_report_data()
@pytest.fixture(autouse=True)
def prepare_and_report_data(monkeypatch, request):
"""Return always True for BaseCollector.prepare_and_report_data()"""
if "original" in request.keywords:
# If using the `@pytest.mark.original` marker before the test function,
# uses the original BaseCollector.prepare_and_report_data()
monkeypatch.setattr(
BaseCollector,
"prepare_and_report_data",
BaseCollector.prepare_and_report_data,
)
else:
monkeypatch.setattr(BaseCollector, "prepare_and_report_data", always_true)
# Mocking HostAgent.is_agent_listening()
@pytest.fixture(autouse=True)
def is_agent_listening(monkeypatch, request) -> None:
"""Always return `True` for `HostAgent.is_agent_listening()`"""
if "original" in request.keywords:
# If using the `@pytest.mark.original` marker before the test function,
# uses the original HostAgent.is_agent_listening()
monkeypatch.setattr(
HostAgent, "is_agent_listening", HostAgent.is_agent_listening
)
else:
monkeypatch.setattr(HostAgent, "is_agent_listening", always_true)
@pytest.fixture(autouse=True)
def lookup_agent_host(monkeypatch, request) -> None:
"""Always return `True` for `TheMachine.lookup_agent_host()`"""
if "original" in request.keywords:
# If using the `@pytest.mark.original` marker before the test function,
# uses the original TheMachine.lookup_agent_host()
monkeypatch.setattr(
TheMachine, "lookup_agent_host", TheMachine.lookup_agent_host
)
else:
monkeypatch.setattr(TheMachine, "lookup_agent_host", always_true)
@pytest.fixture(autouse=True)
def announce_sensor(monkeypatch, request) -> None:
"""Always return `True` for `TheMachine.announce_sensor()`"""
if "original" in request.keywords:
# If using the `@pytest.mark.original` marker before the test function,
# uses the original TheMachine.announce_sensor()
monkeypatch.setattr(TheMachine, "announce_sensor", TheMachine.announce_sensor)
else:
monkeypatch.setattr(TheMachine, "announce_sensor", always_true)
@pytest.fixture(autouse=True)
def announce(monkeypatch, request) -> None:
"""Always return `True` for `Host.announce()`"""
if "original" in request.keywords:
# If using the `@pytest.mark.original` marker before the test function,
# uses the original HostAgent.announce()
monkeypatch.setattr(HostAgent, "announce", HostAgent.announce)
else:
monkeypatch.setattr(HostAgent, "announce", always_true)
# Mocking the import of uwsgi
def _uwsgi_masterpid() -> int:
return 12345
module = type(sys)("uwsgi")
module.opt = {
"master": True,
"lazy-apps": True,
"enable-threads": True,
}
module.masterpid = _uwsgi_masterpid
sys.modules["uwsgi"] = module