-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_runtime.py
More file actions
580 lines (454 loc) · 20.3 KB
/
test_runtime.py
File metadata and controls
580 lines (454 loc) · 20.3 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
import os
import sys
import time
from argparse import Namespace
from collections.abc import Generator
from multiprocessing import Process
from pathlib import Path
from random import randint
from threading import Thread
from unittest.mock import MagicMock, patch
import pytest
import yaml
from _pytest.monkeypatch import MonkeyPatch
from typing_extensions import Self
from cognite.examples.unstable.extractors.simple_extractor.main import SimpleExtractor
from cognite.extractorutils.metrics import BaseMetrics
from cognite.extractorutils.unstable.configuration.exceptions import InvalidArgumentError
from cognite.extractorutils.unstable.configuration.models import ConnectionConfig
from cognite.extractorutils.unstable.core.base import ConfigRevision, FullConfig
from cognite.extractorutils.unstable.core.checkin_worker import CheckinWorker
from cognite.extractorutils.unstable.core.runtime import Runtime
from cognite.extractorutils.unstable.core.tasks import StartupTask, TaskContext
from test_unstable.conftest import TestConfig, TestExtractor, TestMetrics
class MetricsTestExtractor(SimpleExtractor):
"""Custom extractor for testing metrics in multiprocessing context."""
def __init_tasks__(self) -> None:
super().__init_tasks__()
def test_metrics_task(context: TaskContext) -> None:
# Increment counter twice
self.metrics.a_counter.inc()
self.metrics.a_counter.inc()
# Log the counter value so we can verify it in output
counter_value = self.metrics.a_counter._value.get()
context.info(f"METRICS_TEST: Counter value is {counter_value}")
# Add startup task to test metrics
self.add_task(
StartupTask(
name="test-metrics",
description="Test metrics increment",
target=test_metrics_task,
)
)
@pytest.fixture
def local_config_file() -> Generator[Path, None, None]:
file = Path(__file__).parent.parent.parent / f"test-{randint(0, 1000000)}.yaml"
with open(file, "w") as f:
f.write("parameter_one: 123\nparameter_two: abc\n")
yield file
file.unlink(missing_ok=True)
def test_load_local_config(connection_config: ConnectionConfig, local_config_file: Path) -> None:
runtime = Runtime(TestExtractor)
runtime._cognite_client = connection_config.get_cognite_client(
f"{TestExtractor.EXTERNAL_ID}-{TestExtractor.VERSION}"
)
config: TestConfig
config, revision = runtime._try_get_application_config(
args=Namespace(force_local_config=[local_config_file]),
connection_config=connection_config,
)
assert revision == "local"
assert config.parameter_one == 123
assert config.parameter_two == "abc"
def test_load_cdf_config(connection_config: ConnectionConfig) -> None:
cognite_client = connection_config.get_cognite_client(f"{TestExtractor.EXTERNAL_ID}-{TestExtractor.VERSION}")
cognite_client.post(
url=f"/api/v1/projects/{cognite_client.config.project}/odin/config",
json={
"externalId": connection_config.integration.external_id,
"config": "parameter-one: 123\nparameter-two: abc\n",
},
headers={"cdf-version": "alpha"},
)
runtime = Runtime(TestExtractor)
runtime._cognite_client = cognite_client
config: TestConfig
config, revision = runtime._try_get_application_config(
args=Namespace(force_local_config=None),
connection_config=connection_config,
)
assert revision == 1
assert config.parameter_one == 123
assert config.parameter_two == "abc"
def test_load_cdf_config_initial_empty(connection_config: ConnectionConfig) -> None:
"""
Test that the runtime can handle an initial empty config, and that it's picked up when it's set
"""
cognite_client = connection_config.get_cognite_client(f"{TestExtractor.EXTERNAL_ID}-{TestExtractor.VERSION}")
runtime = Runtime(TestExtractor)
runtime._cognite_client = cognite_client
runtime.RETRY_CONFIG_INTERVAL = 1
def set_config_after_delay() -> None:
time.sleep(3)
cognite_client.post(
url=f"/api/v1/projects/{cognite_client.config.project}/odin/config",
json={
"externalId": connection_config.integration.external_id,
"config": "parameter-one: 123\nparameter-two: abc\n",
},
headers={"cdf-version": "alpha"},
)
def cancel_after_delay() -> None:
time.sleep(10)
runtime._cancellation_token.cancel()
Thread(target=set_config_after_delay, daemon=True).start()
Thread(target=cancel_after_delay, daemon=True).start()
start_time = time.time()
result: tuple[TestConfig, ConfigRevision] | None = runtime._safe_get_application_config(
args=Namespace(force_local_config=None),
connection_config=connection_config,
)
duration = time.time() - start_time
assert result is not None
# Duration should not be much higher than sleep before set (3) + retry interval (1)
assert duration < 5
config, revision = result
assert revision == 1
assert config.parameter_one == 123
assert config.parameter_two == "abc"
errors = cognite_client.get(
url=f"/api/v1/projects/{cognite_client.config.project}/integrations/errors",
params={"integration": connection_config.integration.external_id},
headers={"cdf-version": "alpha"},
).json()
assert len(errors["items"]) == 1
assert "No configuration found for the given integration" in errors["items"][0]["description"]
def test_verify_connection_config(connection_config: ConnectionConfig) -> None:
runtime = Runtime(TestExtractor)
assert runtime._verify_connection_config(connection_config)
def test_changing_cwd() -> None:
runtime = Runtime(TestExtractor)
original_cwd = os.getcwd()
runtime._try_set_cwd(args=Namespace(cwd=(Path(__file__).parent.as_posix(),)))
assert os.getcwd() == str(Path(__file__).parent)
assert os.getcwd() != original_cwd
def test_change_cwd_to_nonexistent() -> None:
runtime = Runtime(TestExtractor)
with pytest.raises(InvalidArgumentError, match="No such file or directory"):
runtime._try_set_cwd(args=Namespace(cwd=(Path("nonexistent_directory").as_posix(),)))
def _write_conn_from_fixture(base_yaml_path: Path, out_path: Path, cfg: ConnectionConfig) -> None:
"""Start from the repo YAML and overwrite with plain strings from the fixture."""
data = yaml.safe_load(base_yaml_path.read_text())
data["project"] = cfg.project
data["base_url"] = cfg.base_url
integ = data.setdefault("integration", {})
integ["external_id"] = cfg.integration.external_id
auth = cfg.authentication
scopes_value = getattr(auth.scopes, "value", None) or str(auth.scopes)
data["authentication"] = {
"type": "client-credentials",
"client_id": auth.client_id,
"client_secret": auth.client_secret,
"token_url": auth.token_url,
"scopes": scopes_value,
}
out_path.write_text(yaml.safe_dump(data, sort_keys=False), encoding="utf-8")
def test_runtime_cancellation_propagates_to_extractor(
connection_config: ConnectionConfig, tmp_path: Path, monkeypatch: MonkeyPatch, capfd: pytest.CaptureFixture[str]
) -> None:
"""
Start the runtime, then cancel its token. Verify that:
1) The child's watcher logs "Cancellation signal received from runtime. Shutting down gracefully."
2) The child process is not alive after shutdown.
3) The runtime main loop returns (thread finished).
This test runs close to how you run the CLI:
uv run simple-extractor --cwd cognite/examples/unstable/extractors/simple_extractor/config \
-c connection_config.yaml -f config.yaml --skip-init-checks
"""
cfg_dir = Path("cognite/examples/unstable/extractors/simple_extractor/config")
base_conn = cfg_dir / "connection_config.yaml"
base_app = cfg_dir / "config.yaml"
conn_file = tmp_path / f"test-{randint(0, 1000000)}-connection_config.yaml"
_write_conn_from_fixture(base_conn, conn_file, connection_config)
app_file = tmp_path / f"test-{randint(0, 1000000)}-config.yaml"
app_file.write_text(base_app.read_text(encoding="utf-8"))
argv = [
"simple-extractor",
"--cwd",
str(tmp_path),
"-c",
conn_file.name,
"-f",
app_file.name,
"--skip-init-checks",
"-l",
"info",
]
monkeypatch.setattr(sys, "argv", argv)
runtime = Runtime(SimpleExtractor)
child_holder = {}
original_spawn = Runtime._spawn_extractor
def spy_spawn(self: Self, config: FullConfig, checkin_worker: CheckinWorker) -> Process:
p = original_spawn(
self,
config,
checkin_worker,
)
child_holder["proc"] = p
return p
monkeypatch.setattr(Runtime, "_spawn_extractor", spy_spawn, raising=True)
t = Thread(target=runtime.run, name="RuntimeMain")
t.start()
start = time.time()
while "proc" not in child_holder and time.time() - start < 10:
time.sleep(0.05)
assert "proc" in child_holder, "Extractor process was not spawned in time."
proc = child_holder["proc"]
time.sleep(0.5)
runtime._cancellation_token.cancel()
t.join(timeout=30)
assert not t.is_alive(), "Runtime did not shut down within timeout after cancellation."
proc.join(timeout=0)
assert not proc.is_alive(), "Extractor process is still alive"
out, err = capfd.readouterr()
combined = (out or "") + (err or "")
assert "Cancellation signal received from runtime. Shutting down gracefully." in combined, (
f"Expected cancellation log line not found in output.\nCaptured output:\n{combined}"
)
def test_service_flag_non_windows(monkeypatch: MonkeyPatch) -> None:
runtime = Runtime(TestExtractor)
monkeypatch.setattr(sys, "platform", "linux")
with patch("argparse.ArgumentParser.parse_args") as mock_args:
mock_args.return_value = MagicMock(service=True, log_level="info")
with pytest.raises(SystemExit) as excinfo:
runtime.run()
assert excinfo.value.code == 1
def test_service_flag_windows_import_error(monkeypatch: MonkeyPatch) -> None:
runtime = Runtime(TestExtractor)
monkeypatch.setattr(sys, "platform", "win32")
with patch("argparse.ArgumentParser.parse_args") as mock_args:
mock_args.return_value = MagicMock(service=True, log_level="info")
with patch.dict("sys.modules", {"simple_winservice": None}):
with pytest.raises(SystemExit) as excinfo:
runtime.run()
assert excinfo.value.code == 1
def test_service_flag_windows_success(monkeypatch: MonkeyPatch) -> None:
runtime = Runtime(TestExtractor)
monkeypatch.setattr(sys, "platform", "win32")
with patch("argparse.ArgumentParser.parse_args") as mock_args:
mock_args.return_value = MagicMock(service=True, log_level="info")
mock_register = MagicMock()
mock_run = MagicMock()
mock_handle = MagicMock()
sys.modules["simple_winservice"] = MagicMock(
register_service=mock_register,
run_service=mock_run,
ServiceHandle=mock_handle,
)
with (
patch("simple_winservice.register_service", mock_register),
patch("simple_winservice.run_service", mock_run),
):
runtime.run()
mock_register.assert_called()
mock_run.assert_called()
def test_service_main_entrypoint(monkeypatch: MonkeyPatch, connection_config: ConnectionConfig) -> None:
runtime = Runtime(TestExtractor)
monkeypatch.setattr(sys, "platform", "win32")
args = MagicMock(service=True, log_level="info")
handle = MagicMock()
# Simulate cancellation after a short delay
def cancel() -> None:
time.sleep(0.5)
runtime._cancellation_token.cancel()
cancel_thread = Thread(target=cancel)
from simple_winservice import ServiceHandle
# Simulate service_main logic
def service_main(handle: ServiceHandle, service_args: list[str]) -> None:
handle.event_log_info("Extractor Windows service is starting.")
handle.set_service_running()
runtime._main_runtime(args)
handle.event_log_info("Extractor Windows service is stopping.")
cancel_thread.start()
with (
patch("cognite.extractorutils.unstable.core.runtime.load_file", return_value=connection_config),
patch("logging.Logger.info") as mock_logger_info,
):
service_main(handle, [])
cancel_thread.join()
handle.event_log_info.assert_any_call("Extractor Windows service is starting.")
handle.set_service_running.assert_called_once()
handle.event_log_info.assert_any_call("Extractor Windows service is stopping.")
# Assert that 'Shutting down runtime' was logged, confirming _main_runtime ran
mock_logger_info.assert_any_call("Shutting down runtime")
assert runtime._cancellation_token.is_cancelled
@patch("sys.platform", "win32")
@patch("cognite.extractorutils.unstable.core.runtime.Queue")
@patch("cognite.extractorutils.unstable.core.runtime.WindowsEventHandler")
@patch("logging.getLogger")
def test_logging_on_windows(mock_get_logger: MagicMock, mock_windows_handler: MagicMock, mock_queue: MagicMock) -> None:
"""
Tests that the logger correctly initializes a console handler
and a WindowsEventHandler when running on Windows.
"""
mock_root_logger = MagicMock()
mock_get_logger.return_value = mock_root_logger
mock_handler_instance = MagicMock()
mock_windows_handler.return_value = mock_handler_instance
runtime = Runtime(TestExtractor)
mock_windows_handler.assert_called_once_with(TestExtractor.NAME)
assert mock_root_logger.addHandler.call_count == 2
mock_root_logger.addHandler.assert_any_call(mock_handler_instance)
@patch("sys.platform", "linux")
@patch("cognite.extractorutils.unstable.core.runtime.WindowsEventHandler")
@patch("logging.getLogger")
def test_logging_on_non_windows(mock_get_logger: MagicMock, mock_windows_handler: MagicMock) -> None:
"""
Tests that the logger only initializes a console handler
and skips the WindowsEventHandler when not on Windows.
"""
mock_root_logger = MagicMock()
mock_get_logger.return_value = mock_root_logger
runtime = Runtime(TestExtractor)
mock_windows_handler.assert_not_called()
assert mock_root_logger.addHandler.call_count == 1
@patch("sys.platform", "win32")
@patch("cognite.extractorutils.unstable.core.runtime.Queue")
@patch("cognite.extractorutils.unstable.core.runtime.WindowsEventHandler", side_effect=ImportError)
@patch("logging.getLogger")
def test_logging_on_windows_with_import_error(
mock_get_logger: MagicMock, mock_windows_handler: MagicMock, mock_queue: MagicMock
) -> None:
"""
Tests that the bootstrap logger handles an ImportError gracefully if pywin32
is not installed on a Windows system.
"""
mock_root_logger = MagicMock()
mock_get_logger.return_value = mock_root_logger
runtime = Runtime(TestExtractor)
runtime.logger.warning.assert_called_with(
"Failed to import the 'pywin32' package. This should install automatically on windows. "
"Please try reinstalling to resolve this issue."
)
assert mock_root_logger.addHandler.call_count == 1
def test_extractor_with_metrics(
connection_config: ConnectionConfig, tmp_path: Path, monkeypatch: MonkeyPatch, capfd: pytest.CaptureFixture[str]
) -> None:
"""
Test metrics_class is properly passed through Runtime to child process.
This test verifies multiprocessing integration with metrics and counter increments.
"""
cfg_dir = Path("cognite/examples/unstable/extractors/simple_extractor/config")
base_conn = cfg_dir / "connection_config.yaml"
base_app = cfg_dir / "config.yaml"
conn_file = tmp_path / f"test-{randint(0, 1000000)}-connection_config.yaml"
_write_conn_from_fixture(base_conn, conn_file, connection_config)
app_file = tmp_path / f"test-{randint(0, 1000000)}-config.yaml"
app_file.write_text(base_app.read_text(encoding="utf-8"))
argv = [
"simple-extractor",
"--cwd",
str(tmp_path),
"-c",
conn_file.name,
"-f",
app_file.name,
"--skip-init-checks",
"-l",
"info",
]
monkeypatch.setattr(sys, "argv", argv)
runtime = Runtime(MetricsTestExtractor, metrics=TestMetrics)
# Verify runtime stores metrics class
assert runtime._metrics_class is TestMetrics, "Runtime should store TestMetrics class"
child_holder = {}
original_spawn = Runtime._spawn_extractor
def spy_spawn(self: Self, config: FullConfig, checkin_worker: CheckinWorker) -> Process:
assert config.metrics_class is TestMetrics, "FullConfig should carry TestMetrics class"
p = original_spawn(
self,
config,
checkin_worker,
)
child_holder["proc"] = p
return p
monkeypatch.setattr(Runtime, "_spawn_extractor", spy_spawn, raising=True)
t = Thread(target=runtime.run, name="RuntimeMain")
t.start()
start = time.time()
while "proc" not in child_holder and time.time() - start < 10:
time.sleep(0.05)
assert "proc" in child_holder, "Extractor process was not spawned in time."
proc = child_holder["proc"]
time.sleep(1.5) # Give more time for the startup task to run
runtime._cancellation_token.cancel()
t.join(timeout=30)
assert not t.is_alive(), "Runtime did not shut down within timeout after cancellation."
proc.join(timeout=0)
assert not proc.is_alive(), "Extractor process is still alive"
out, err = capfd.readouterr()
combined = (out or "") + (err or "")
# Verify metrics counter was incremented
assert "METRICS_TEST: Counter value is 2" in combined, (
f"Expected metrics counter to be 2 in child process.\nCaptured output:\n{combined}"
)
class InvalidMetrics:
"""A dummy metrics class that does not inherit from BaseMetrics."""
pass
@pytest.mark.parametrize(
"metrics_input, should_raise",
[
(TestMetrics, False),
(TestMetrics(), True),
(InvalidMetrics, True),
(None, False),
],
)
def test_metrics_class_validation_parametrized(
caplog: pytest.LogCaptureFixture, metrics_input: type[BaseMetrics] | None, should_raise: bool
) -> None:
"""
Combined parameterized test for metrics class validation behavior.
For cases that should not raise, we only assert the runtime stored the value.
For invalid cases we assert _main_runtime exits with SystemExit(1).
"""
runtime = Runtime(TestExtractor, metrics=metrics_input)
if should_raise:
mock_connection_config = MagicMock()
args = MagicMock(
connection_config=[Path("dummy.yaml")],
force_local_config=None,
cwd=None,
skip_init_checks=True,
log_level="info",
)
with (
patch("cognite.extractorutils.unstable.core.runtime.load_file", return_value=mock_connection_config),
pytest.raises(SystemExit) as excinfo,
):
runtime._main_runtime(args)
assert excinfo.value.code == 1
assert any(
"The provided metrics class does not inherit from BaseMetrics" in record.message
for record in caplog.records
), f"Expected critical log not found. Captured logs: {[r.message for r in caplog.records]}"
else:
assert runtime._metrics_class is metrics_input
def test_type_checker_would_catch_invalid_metrics() -> None:
"""
This test validates that the type signature itself is correct and would
provide IDE/linter feedback to developers.
"""
from typing import get_type_hints
hints = get_type_hints(Runtime.__init__)
assert "metrics" in hints
metrics_type_str = str(hints["metrics"])
# Verify it expects a type/class, not an instance
assert "type[" in metrics_type_str or "Type[" in metrics_type_str, (
f"Expected metrics parameter to be type[...], got: {metrics_type_str}"
)
# Verify None is allowed (Optional)
# Python 3.10 uses typing.Optional[X], 3.10+ can use X | None
assert any(pattern in metrics_type_str for pattern in ["None", "| None", "Optional"]), (
f"Expected metrics parameter to be Optional, got: {metrics_type_str}"
)