-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_base.py
More file actions
347 lines (290 loc) · 11.2 KB
/
test_base.py
File metadata and controls
347 lines (290 loc) · 11.2 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import contextlib
import json
import logging
import random
from collections.abc import Generator
from io import StringIO
from pathlib import Path
import pytest
from cognite.client import CogniteClient
from cognite.client.exceptions import CogniteNotFoundError
from cognite.extractorutils.metrics import CognitePusher, PrometheusPusher
from cognite.extractorutils.statestore.watermark import LocalStateStore, RawStateStore
from cognite.extractorutils.unstable.configuration.loaders import ConfigFormat, load_io
from cognite.extractorutils.unstable.configuration.models import (
ConnectionConfig,
LocalStateStoreConfig,
LogConsoleHandlerConfig,
LogFileHandlerConfig,
LogHandlerConfig,
LogLevel,
MetricsConfig,
RawStateStoreConfig,
StateStoreConfig,
TimeIntervalConfig,
_CogniteMetricsConfig,
_PromServerConfig,
_PushGatewayConfig,
)
from cognite.extractorutils.unstable.core.base import FullConfig
from cognite.extractorutils.unstable.core.checkin_worker import CheckinWorker
from cognite.extractorutils.unstable.core.tasks import TaskContext
from .conftest import TestConfig, TestExtractor, TestMetrics
def get_checkin_worker(connection_config: ConnectionConfig) -> CheckinWorker:
worker = CheckinWorker(
connection_config.get_cognite_client("testing"),
connection_config.integration.external_id,
logging.getLogger(__name__),
)
worker.active_revision = 1
return worker
@pytest.mark.parametrize(
"log_handlers, override_level, expected_logs, unexpected_logs",
[
(
[LogConsoleHandlerConfig(type="console", level=LogLevel("INFO"))],
None,
["This is an info message.", "This is a warning message."],
["This is a debug message."],
),
(
[LogConsoleHandlerConfig(type="console", level=LogLevel("INFO"))],
"DEBUG",
["This is a debug message.", "This is an info message.", "This is a warning message."],
[],
),
(
[LogConsoleHandlerConfig(type="console", level=LogLevel("INFO"))],
"WARNING",
["This is a warning message."],
["This is a debug message.", "This is an info message."],
),
(
[LogFileHandlerConfig(type="file", level=LogLevel("INFO"), path=Path("non-existing/test.log"))],
"WARNING",
["This is a warning message."],
["Falling back to console logging.", "This is a debug message.", "This is an info message."],
),
],
)
def test_log_level_override(
capsys: pytest.CaptureFixture[str],
connection_config: ConnectionConfig,
log_handlers: list[LogHandlerConfig],
override_level: str | None,
expected_logs: list[str],
unexpected_logs: list[str],
) -> None:
"""
Tests that the log level override parameter correctly overrides the log level
set in the application configuration.
"""
app_config = TestConfig(
parameter_one=1,
parameter_two="a",
log_handlers=log_handlers,
)
full_config = FullConfig(
connection_config=connection_config,
application_config=app_config,
current_config_revision=1,
log_level_override=override_level,
)
worker = get_checkin_worker(connection_config)
extractor = TestExtractor(full_config, worker)
with extractor:
startup_task = next(t for t in extractor._tasks if t.name == "log_task")
task_context = TaskContext(task=startup_task, extractor=extractor)
startup_task.target(task_context)
captured = capsys.readouterr()
console_output = captured.err
for log in expected_logs:
assert log in console_output
for log in unexpected_logs:
assert log not in console_output
def test_get_current_statestore_raises_before_start() -> None:
"""
Tests that calling get_current_statestore before the extractor's
__enter__ method is called raises a ValueError.
"""
with pytest.raises(ValueError, match="No state store singleton created. Have a state store been loaded?"):
TestExtractor.get_current_statestore()
@pytest.fixture
def local_state_file(tmp_path: Path) -> Path:
"""
Provides a path to a temporary file for a single test function run.
The file and its parent directory are automatically cleaned up by pytest.
"""
return tmp_path / "test_states.json"
def test_local_state_store_integration(local_state_file: Path, connection_config: ConnectionConfig) -> None:
"""
Tests the integration of LocalStateStore with the extractor configuration.
"""
app_config = TestConfig(
parameter_one=1,
parameter_two="a",
log_handlers=[LogConsoleHandlerConfig(type="console", level=LogLevel("INFO"))],
state_store=StateStoreConfig(local=LocalStateStoreConfig(path=local_state_file)),
)
full_config = FullConfig(
connection_config=connection_config,
application_config=app_config,
current_config_revision=1,
)
worker = get_checkin_worker(connection_config)
extractor = TestExtractor(full_config, worker)
with pytest.raises(ValueError):
TestExtractor.get_current_statestore()
with extractor:
state_store = TestExtractor.get_current_statestore()
assert isinstance(state_store, LocalStateStore)
assert state_store is extractor.state_store
assert not local_state_file.exists()
assert state_store.get_state("my-test-id") == (None, None)
state_store.set_state(external_id="my-test-id", low=1, high=5)
state_store.synchronize()
assert local_state_file.exists()
with open(local_state_file) as f:
data = json.load(f)
assert data["my-test-id"] == {"low": 1, "high": 5}
new_extractor = TestExtractor(full_config, worker)
with new_extractor:
assert new_extractor.state_store.get_state("my-test-id") == (1, 5)
@pytest.fixture(scope="function")
def raw_db_table_name() -> tuple[str, str]:
"""Provides a unique database name for a single test function run."""
test_id = random.randint(0, int(1e9))
return f"test_db_{test_id}", f"test_table_{test_id}"
@pytest.fixture
def setup_and_teardown_raw_db(
set_client: CogniteClient, raw_db_table_name: str
) -> Generator[tuple[str, str], None, None]:
"""
This fixture ensures the RAW database/table is cleaned up after the test.
"""
db_name, table_name = raw_db_table_name
yield db_name, table_name
with contextlib.suppress(CogniteNotFoundError):
set_client.raw.databases.delete(name=db_name, recursive=True)
def test_raw_state_store_integration(
connection_config: ConnectionConfig,
setup_and_teardown_raw_db: tuple[str, str],
) -> None:
"""
Tests the integration of LocalStateStore with the extractor configuration.
"""
db_name, table_name = setup_and_teardown_raw_db
app_config = TestConfig(
parameter_one=1,
parameter_two="a",
log_handlers=[LogConsoleHandlerConfig(type="console", level=LogLevel("INFO"))],
state_store=StateStoreConfig(raw=RawStateStoreConfig(database=db_name, table=table_name)),
)
full_config = FullConfig(
connection_config=connection_config,
application_config=app_config,
current_config_revision=1,
)
worker = get_checkin_worker(connection_config)
extractor = TestExtractor(full_config, worker)
with pytest.raises(ValueError):
TestExtractor.get_current_statestore()
with extractor:
state_store = TestExtractor.get_current_statestore()
assert isinstance(state_store, RawStateStore)
assert state_store is extractor.state_store
assert state_store.get_state("my-test-id") == (None, None)
state_store.set_state(external_id="my-test-id", low=1, high=5)
state_store.synchronize()
assert state_store.get_state("my-test-id") == (1, 5)
new_extractor = TestExtractor(full_config, worker)
with new_extractor:
assert new_extractor.state_store.get_state("my-test-id") == (1, 5)
@pytest.mark.parametrize("metrics_type", ["prometheus", "cognite"])
def test_extractor_with_metrics_pushers(connection_config: ConnectionConfig, metrics_type: str) -> None:
override_level = "INFO"
call_count = {"count": 0}
if metrics_type == "prometheus":
metrics_config = MetricsConfig(
server=_PromServerConfig(host="localhost", port=9090),
cognite=None,
push_gateways=[
_PushGatewayConfig(
host="localhost",
job_name="test-job",
username=None,
password=None,
clear_after=None,
push_interval=TimeIntervalConfig("30s"),
)
],
)
pusher_cls = PrometheusPusher
def counting_push(self: PrometheusPusher) -> None:
call_count["count"] += 1
return original_push(self)
original_push = pusher_cls._push_to_server
else:
metrics_config = MetricsConfig(
server=None,
cognite=_CogniteMetricsConfig(
external_id_prefix="extractor_test",
asset_name="Extractor Test Metrics",
asset_external_id="extractor_testcognite_assets",
data_set=None,
),
push_gateways=None,
)
pusher_cls = CognitePusher
def counting_push(self: CognitePusher) -> None:
call_count["count"] += 1
return None
original_push = pusher_cls._push_to_server
app_config = TestConfig(
parameter_one=1,
parameter_two="a",
metrics=metrics_config,
)
full_config = FullConfig(
connection_config=connection_config,
application_config=app_config,
current_config_revision=1,
log_level_override=override_level,
metrics_class=TestMetrics,
)
worker = get_checkin_worker(connection_config)
extractor = TestExtractor(full_config, worker)
assert isinstance(extractor.metrics, TestMetrics)
with contextlib.ExitStack() as stack:
stack.enter_context(contextlib.suppress(Exception))
pusher_cls._push_to_server = counting_push
try:
with extractor:
for pusher in extractor.metrics_push_manager.pushers:
assert pusher.thread is not None
assert pusher.thread.is_alive()
finally:
pusher_cls._push_to_server = original_push
assert call_count["count"] > 0
def test_pushgatewayconfig_none_credentials_from_yaml() -> None:
config_str = """
push-gateways:
- host: "http://localhost:9091"
job_name: "test-job"
"""
stream = StringIO(config_str)
config = load_io(stream, ConfigFormat.YAML, MetricsConfig)
metrics_config = config.push_gateways[0]
pusher = PrometheusPusher(
job_name=metrics_config.job_name,
username=metrics_config.username,
password=metrics_config.password,
url=metrics_config.host,
push_interval=30,
thread_name="TestPusher",
cancellation_token=None,
)
assert pusher.username is None
assert pusher.password is None
assert pusher.url == "http://localhost:9091"
assert pusher.job_name == "test-job"