forked from ChelseaKR/nearmiss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ingestion.py
More file actions
1218 lines (1045 loc) · 42.8 KB
/
Copy pathtest_ingestion.py
File metadata and controls
1218 lines (1045 loc) · 42.8 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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import datetime as dt
import hashlib
import json
import stat
import traceback
from pathlib import Path
from typing import cast
import pytest
from jsonschema import Draft202012Validator, FormatChecker
import nearmiss.ingestion as ingestion
from nearmiss.ingestion import (
ConcurrentIngestionError,
IngestionError,
IngestionRunError,
run_ingestion,
)
NOW = dt.datetime(2026, 7, 12, 18, 30, tzinfo=dt.UTC)
SCHEMA_PATH = Path(__file__).parents[1] / "schema" / "ingestion-receipt.schema.json"
SCHEMA = json.loads(SCHEMA_PATH.read_text(encoding="utf-8"))
VALIDATOR = Draft202012Validator(SCHEMA, format_checker=FormatChecker())
def _clock() -> dt.datetime:
return NOW
def _receipt(path: Path) -> dict[str, object]:
value = json.loads(path.read_text(encoding="utf-8"))
assert isinstance(value, dict)
VALIDATOR.validate(value)
return value
def test_receipt_schema_is_valid_and_matches_embedded_runtime_contract() -> None:
Draft202012Validator.check_schema(SCHEMA)
assert SCHEMA == ingestion.RECEIPT_SCHEMA
def test_success_writes_content_addressed_snapshot_and_activates_atomically(
tmp_path: Path,
) -> None:
raw = b'{"features": [1]}\n'
normalized = b'{"reports": [1]}\n'
result = run_ingestion(
root=tmp_path,
source_id="bikemaps-sacramento",
fetch=lambda: raw,
normalize=lambda payload: normalized if payload == raw else b"",
clock=_clock,
attempt_id="success-1",
)
raw_hash = hashlib.sha256(raw).hexdigest()
assert result.raw_snapshot == (
tmp_path / "bikemaps-sacramento" / "raw" / "sha256" / f"{raw_hash}.bin"
)
assert result.raw_snapshot.read_bytes() == raw
assert result.normalized_path.read_bytes() == normalized
assert result.current_path == (tmp_path / "bikemaps-sacramento" / "normalized" / "current.json")
assert result.raw_snapshot.stat().st_mode & stat.S_IWUSR == 0
assert result.normalized_path.stat().st_mode & stat.S_IWUSR == 0
receipt = _receipt(result.receipt_path)
assert receipt["schema_version"] == "1.0.0"
assert receipt["status"] == "success"
assert receipt["activated"] is True
assert receipt["raw_sha256"] == raw_hash
assert receipt["normalized_sha256"] == hashlib.sha256(normalized).hexdigest()
assert receipt["previous_normalized_sha256"] is None
assert receipt["error"] is None
assert result.current_path.read_bytes() == result.receipt_path.read_bytes()
assert result.receipt_path.stat().st_mode & stat.S_IWUSR == 0
def test_repeated_payload_reuses_immutable_snapshot_and_creates_new_receipt(tmp_path: Path) -> None:
raw = b"same upstream payload"
normalize = lambda _payload: b"same normalized payload" # noqa: E731
first = run_ingestion(
root=tmp_path,
source_id="source",
fetch=lambda: raw,
normalize=normalize,
clock=_clock,
attempt_id="attempt-1",
)
second = run_ingestion(
root=tmp_path,
source_id="source",
fetch=lambda: raw,
normalize=normalize,
clock=_clock,
attempt_id="attempt-2",
)
assert first.raw_snapshot == second.raw_snapshot
assert first.raw_snapshot.read_bytes() == raw
assert first.receipt_path != second.receipt_path
assert len(list((tmp_path / "source" / "raw" / "sha256").glob("*.bin"))) == 1
assert len(list((tmp_path / "source" / "normalized" / "sha256").glob("*.bin"))) == 1
second_receipt = _receipt(second.receipt_path)
assert second_receipt["previous_normalized_sha256"] == second.normalized_sha256
def test_receipt_bytes_are_deterministic_for_fixed_attempt_and_clock(tmp_path: Path) -> None:
raw = b"raw"
normalized = b"normalized"
result = run_ingestion(
root=tmp_path,
source_id="source",
fetch=lambda: raw,
normalize=lambda _raw: normalized,
clock=_clock,
attempt_id="deterministic",
)
raw_hash = hashlib.sha256(raw).hexdigest()
expected = {
"schema_version": "1.0.0",
"attempt_id": "deterministic",
"source_id": "source",
"status": "success",
"started_at": "2026-07-12T18:30:00Z",
"completed_at": "2026-07-12T18:30:00Z",
"raw_sha256": raw_hash,
"raw_snapshot": f"raw/sha256/{raw_hash}.bin",
"normalized_sha256": hashlib.sha256(normalized).hexdigest(),
"normalized_path": (f"normalized/sha256/{hashlib.sha256(normalized).hexdigest()}.bin"),
"previous_normalized_sha256": None,
"activated": True,
"error": None,
}
assert (
result.receipt_path.read_bytes()
== (json.dumps(expected, indent=2, sort_keys=True) + "\n").encode()
)
def test_invalid_internal_failure_receipt_is_not_installed_or_allowed_to_mask_cause(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
class OriginalError(RuntimeError):
pass
original = OriginalError("private upstream detail")
def fail_fetch() -> bytes:
raise original
monkeypatch.setattr(ingestion, "_error_type", lambda _exc, _rollback: "invalid type!")
with pytest.raises(IngestionError, match="failure receipt could not be written") as raised:
run_ingestion(
root=tmp_path,
source_id="source",
fetch=fail_fetch,
normalize=lambda raw: raw,
clock=_clock,
attempt_id="invalid-internal",
)
rendered = "".join(traceback.format_exception(raised.value))
assert raised.value.__cause__ is None
assert str(original) not in rendered
assert not (tmp_path / "source" / "receipts" / "invalid-internal.json").exists()
def test_receipt_validation_rejects_digest_path_mismatch() -> None:
digest = "a" * 64
receipt: dict[str, object] = {
"schema_version": "1.0.0",
"attempt_id": "attempt",
"source_id": "source",
"status": "success",
"started_at": "2026-07-12T18:30:00Z",
"completed_at": "2026-07-12T18:30:00Z",
"raw_sha256": digest,
"raw_snapshot": f"raw/sha256/{'b' * 64}.bin",
"normalized_sha256": digest,
"normalized_path": f"normalized/sha256/{digest}.bin",
"previous_normalized_sha256": None,
"activated": True,
"error": None,
}
with pytest.raises(IngestionError, match="does not match its digest"):
ingestion._receipt_bytes(cast(ingestion.IngestionReceipt, receipt))
@pytest.mark.parametrize("class_name", ["unsafe error!", "Token_supersecret"])
def test_uncontrolled_exception_type_is_sanitized(tmp_path: Path, class_name: str) -> None:
unsafe_error = type(class_name, (RuntimeError,), {})
def fail_fetch() -> bytes:
raise unsafe_error("private")
with pytest.raises(IngestionRunError) as raised:
run_ingestion(
root=tmp_path,
source_id="source",
fetch=fail_fetch,
normalize=lambda raw: raw,
clock=_clock,
attempt_id="sanitized-type",
)
assert _receipt(raised.value.receipt_path)["error"] == {
"message": "fetch failed",
"type": "Exception",
}
@pytest.mark.parametrize("failure_stage", ["fetch", "normalize"])
def test_failure_receipt_preserves_last_known_good(tmp_path: Path, failure_stage: str) -> None:
initial = run_ingestion(
root=tmp_path,
source_id="source",
fetch=lambda: b"raw-v1",
normalize=lambda _raw: b"normalized-v1",
clock=_clock,
attempt_id="initial",
)
before = initial.normalized_path.read_bytes()
def fail_fetch() -> bytes:
if failure_stage == "fetch":
raise TimeoutError("upstream timed out")
return b"raw-v2"
def fail_normalize(_raw: bytes) -> bytes:
if failure_stage == "normalize":
raise ValueError("schema validation failed")
return b"normalized-v2"
with pytest.raises(IngestionRunError) as raised:
run_ingestion(
root=tmp_path,
source_id="source",
fetch=fail_fetch,
normalize=fail_normalize,
clock=_clock,
attempt_id=f"failed-{failure_stage}",
)
assert initial.normalized_path.read_bytes() == before
receipt = _receipt(raised.value.receipt_path)
assert receipt["status"] == "failure"
assert receipt["activated"] is False
assert receipt["previous_normalized_sha256"] == initial.normalized_sha256
assert isinstance(receipt["error"], dict)
if failure_stage == "fetch":
assert receipt["raw_snapshot"] is None
else:
assert receipt["raw_snapshot"] is not None
def test_failure_receipt_and_public_error_do_not_expose_exception_text(
tmp_path: Path,
) -> None:
secret = "https://token:super-secret@example.test/private"
def fail_fetch() -> bytes:
raise RuntimeError(secret)
with pytest.raises(IngestionRunError) as raised:
run_ingestion(
root=tmp_path,
source_id="source",
fetch=fail_fetch,
normalize=lambda raw: raw,
clock=_clock,
attempt_id="redacted",
)
receipt_text = raised.value.receipt_path.read_text(encoding="utf-8")
assert secret not in receipt_text
assert secret not in str(raised.value)
assert secret not in "".join(traceback.format_exception(raised.value))
assert _receipt(raised.value.receipt_path)["error"] == {
"message": "fetch failed",
"type": "Exception",
}
def test_failure_receipt_uses_null_when_completion_time_cannot_be_observed(
tmp_path: Path,
) -> None:
clock_calls = 0
def fail_after_start() -> dt.datetime:
nonlocal clock_calls
clock_calls += 1
if clock_calls == 1:
return NOW
raise RuntimeError("clock unavailable")
def fail_fetch() -> bytes:
raise RuntimeError("fetch failed")
with pytest.raises(IngestionRunError) as raised:
run_ingestion(
root=tmp_path,
source_id="source",
fetch=fail_fetch,
normalize=lambda raw: raw,
clock=fail_after_start,
attempt_id="missing-completion-time",
)
receipt = _receipt(raised.value.receipt_path)
assert receipt["started_at"] == "2026-07-12T18:30:00Z"
assert receipt["completed_at"] is None
def test_lock_rejects_concurrent_writer_without_fetching(tmp_path: Path) -> None:
lock = tmp_path / "source" / ".ingestion.lock"
lock.mkdir(parents=True)
called = False
def fetch() -> bytes:
nonlocal called
called = True
return b"raw"
with pytest.raises(ConcurrentIngestionError):
run_ingestion(
root=tmp_path,
source_id="source",
fetch=fetch,
normalize=lambda raw: raw,
clock=_clock,
attempt_id="concurrent",
)
assert called is False
assert lock.is_dir()
def test_lock_cleanup_never_recursively_deletes_or_masks_original_error(
tmp_path: Path,
) -> None:
lock = tmp_path / "source" / ".ingestion.lock"
def fail_normalize(_raw: bytes) -> bytes:
(lock / "unexpected-owner-data").write_text("preserve me", encoding="utf-8")
raise ValueError("normalization failed")
with pytest.raises(IngestionRunError, match="ingestion failed"):
run_ingestion(
root=tmp_path,
source_id="source",
fetch=lambda: b"raw",
normalize=fail_normalize,
clock=_clock,
attempt_id="dirty-lock",
)
assert (lock / "unexpected-owner-data").read_text(encoding="utf-8") == "preserve me"
def test_preflight_errors_release_owned_lock_without_fetching(tmp_path: Path) -> None:
run_ingestion(
root=tmp_path,
source_id="source",
fetch=lambda: b"raw",
normalize=lambda raw: raw,
clock=_clock,
attempt_id="duplicate",
)
called = False
def fetch() -> bytes:
nonlocal called
called = True
return b"replacement"
with pytest.raises(IngestionError, match="attempt receipt already exists"):
run_ingestion(
root=tmp_path,
source_id="source",
fetch=fetch,
normalize=lambda raw: raw,
clock=_clock,
attempt_id="duplicate",
)
assert called is False
assert not (tmp_path / "source" / ".ingestion.lock").exists()
def test_invalid_source_and_attempt_ids_cannot_escape_root(tmp_path: Path) -> None:
for source_id, attempt_id in (("../escape", "safe"), ("source", "../escape")):
with pytest.raises(IngestionError):
run_ingestion(
root=tmp_path,
source_id=source_id,
fetch=lambda: b"raw",
normalize=lambda raw: raw,
clock=_clock,
attempt_id=attempt_id,
)
assert not (tmp_path.parent / "escape").exists()
def test_symlinked_source_and_artifacts_fail_closed(tmp_path: Path) -> None:
outside = tmp_path / "outside"
outside.mkdir()
root = tmp_path / "root"
root.mkdir()
(root / "source").symlink_to(outside, target_is_directory=True)
with pytest.raises(IngestionError, match="real directory"):
run_ingestion(
root=root,
source_id="source",
fetch=lambda: b"raw",
normalize=lambda raw: raw,
clock=_clock,
attempt_id="source-link",
)
assert list(outside.iterdir()) == []
(root / "source").unlink()
snapshot_target = outside / "snapshot"
snapshot_target.write_bytes(b"raw")
raw_hash = hashlib.sha256(b"raw").hexdigest()
raw_dir = root / "source" / "raw" / "sha256"
raw_dir.mkdir(parents=True)
(raw_dir / f"{raw_hash}.bin").symlink_to(snapshot_target)
with pytest.raises(IngestionRunError) as raised:
run_ingestion(
root=root,
source_id="source",
fetch=lambda: b"raw",
normalize=lambda raw: raw,
clock=_clock,
attempt_id="artifact-link",
)
assert snapshot_target.read_bytes() == b"raw"
assert not (root / "source" / "normalized" / "current.json").exists()
assert _receipt(raised.value.receipt_path)["error"] == {
"message": "raw snapshot preservation failed",
"type": "IngestionError",
}
assert _receipt(raised.value.receipt_path)["raw_snapshot"] is None
def test_symlinked_ingestion_root_is_rejected(tmp_path: Path) -> None:
actual = tmp_path / "actual"
actual.mkdir()
linked = tmp_path / "linked"
linked.symlink_to(actual, target_is_directory=True)
with pytest.raises(IngestionError, match="not a symlink"):
run_ingestion(
root=linked,
source_id="source",
fetch=lambda: b"raw",
normalize=lambda raw: raw,
clock=_clock,
attempt_id="root-link",
)
assert list(actual.iterdir()) == []
def test_non_bytes_and_empty_payloads_fail_closed(tmp_path: Path) -> None:
fetchers: list[object] = [lambda: b"", lambda: "not bytes"]
for index, fetch in enumerate(fetchers):
with pytest.raises(IngestionRunError):
run_ingestion(
root=tmp_path,
source_id="source",
fetch=fetch, # type: ignore[arg-type]
normalize=lambda raw: raw,
clock=_clock,
attempt_id=f"bad-{index}",
)
assert not (tmp_path / "source" / "normalized" / "current.json").exists()
def test_limits_and_source_aware_validator_reject_suspicious_payloads(
tmp_path: Path,
) -> None:
initial = run_ingestion(
root=tmp_path,
source_id="source",
fetch=lambda: b"raw-v1",
normalize=lambda _raw: b"many-records",
clock=_clock,
attempt_id="initial",
)
def reject_regression(candidate: bytes, previous: bytes | None) -> None:
assert previous == b"many-records"
if len(candidate) < len(previous) // 2:
raise ValueError("customer@example.test dataset regressed")
with pytest.raises(IngestionRunError) as regression:
run_ingestion(
root=tmp_path,
source_id="source",
fetch=lambda: b"raw-v2",
normalize=lambda _raw: b"tiny",
validate_normalized=reject_regression,
clock=_clock,
attempt_id="regressed",
)
assert initial.normalized_path.read_bytes() == b"many-records"
regression_receipt = _receipt(regression.value.receipt_path)
assert regression_receipt["error"] == {
"message": "normalized artifact validation failed",
"type": "ValueError",
}
assert "customer@example.test" not in regression.value.receipt_path.read_text()
with pytest.raises(IngestionRunError) as oversized:
run_ingestion(
root=tmp_path,
source_id="source",
fetch=lambda: b"too-large",
normalize=lambda raw: raw,
max_raw_bytes=3,
clock=_clock,
attempt_id="oversized",
)
assert _receipt(oversized.value.receipt_path)["error"] == {
"message": "active artifact preflight failed",
"type": "IngestionError",
}
def test_history_validator_runs_under_lock_after_preservation_before_commit(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
source_root = tmp_path / "source"
candidate = b"normalized-with-history"
candidate_digest = hashlib.sha256(candidate).hexdigest()
candidate_path = source_root / "normalized" / "sha256" / f"{candidate_digest}.bin"
events: list[str] = []
original_require_unchanged = ingestion._require_active_marker_unchanged
original_replace = ingestion._atomic_replace
def track_require_unchanged(
actual_source_root: Path, current_path: Path, expected: bytes | None
) -> None:
events.append("marker-reread")
original_require_unchanged(actual_source_root, current_path, expected)
def track_replace(root: Path, destination: Path, payload: bytes) -> None:
events.append("replace")
original_replace(root, destination, payload)
def validate_history(
actual_source_root: Path, actual_candidate: bytes, actual_started_at: str
) -> None:
assert actual_source_root == source_root
assert actual_candidate == candidate
assert actual_started_at == "2026-07-12T18:30:00Z"
assert candidate_path.read_bytes() == candidate
assert (source_root / ".ingestion.lock").is_dir()
events.append("history-validation")
def validate_candidate(actual_candidate: bytes, previous: bytes | None) -> None:
assert actual_candidate == candidate
assert previous is None
assert not candidate_path.exists()
assert (source_root / ".ingestion.lock").is_dir()
events.append("candidate-validation")
monkeypatch.setattr(ingestion, "_require_active_marker_unchanged", track_require_unchanged)
monkeypatch.setattr(ingestion, "_atomic_replace", track_replace)
result = run_ingestion(
root=tmp_path,
source_id="source",
fetch=lambda: b"raw-with-history",
normalize=lambda _raw: candidate,
validate_normalized=validate_candidate,
validate_history=validate_history,
clock=_clock,
attempt_id="history-success",
)
assert events == [
"candidate-validation",
"history-validation",
"marker-reread",
"replace",
]
assert result.normalized_path == candidate_path
assert not (source_root / ".ingestion.lock").exists()
def test_history_validator_failure_preserves_current_and_prevents_replace(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
initial = run_ingestion(
root=tmp_path,
source_id="source",
fetch=lambda: b"raw-initial",
normalize=lambda _raw: b"normalized-initial",
clock=_clock,
attempt_id="history-initial",
)
marker_before = initial.current_path.read_bytes()
candidate = b"private-candidate-payload"
candidate_digest = hashlib.sha256(candidate).hexdigest()
candidate_path = tmp_path / "source" / "normalized" / "sha256" / f"{candidate_digest}.bin"
replace_calls = 0
secret = f"{tmp_path}/private-candidate-payload"
original_replace = ingestion._atomic_replace
def track_replace(root: Path, destination: Path, payload: bytes) -> None:
nonlocal replace_calls
replace_calls += 1
original_replace(root, destination, payload)
def reject_history(source_root: Path, actual_candidate: bytes, actual_started_at: str) -> None:
assert source_root == tmp_path / "source"
assert actual_candidate == candidate
assert actual_started_at == "2026-07-12T18:30:00Z"
assert candidate_path.read_bytes() == candidate
assert (source_root / ".ingestion.lock").is_dir()
raise ValueError(secret)
monkeypatch.setattr(ingestion, "_atomic_replace", track_replace)
with pytest.raises(IngestionRunError) as raised:
run_ingestion(
root=tmp_path,
source_id="source",
fetch=lambda: b"raw-candidate",
normalize=lambda _raw: candidate,
validate_history=reject_history,
clock=_clock,
attempt_id="history-rejected",
)
assert replace_calls == 0
assert initial.current_path.read_bytes() == marker_before
assert candidate_path.read_bytes() == candidate
receipt_text = raised.value.receipt_path.read_text(encoding="utf-8")
assert _receipt(raised.value.receipt_path)["error"] == {
"message": "normalized artifact validation failed",
"type": "ValueError",
}
assert secret not in receipt_text
assert secret not in str(raised.value)
assert secret not in "".join(traceback.format_exception(raised.value))
assert not (tmp_path / "source" / ".ingestion.lock").exists()
@pytest.mark.parametrize("target_kind", ["raw", "normalized"])
def test_history_validator_cannot_mutate_preserved_candidate_before_commit(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, target_kind: str
) -> None:
initial = run_ingestion(
root=tmp_path,
source_id="source",
fetch=lambda: b"raw-initial",
normalize=lambda _raw: b"normalized-initial",
clock=_clock,
attempt_id=f"candidate-{target_kind}-initial",
)
marker_before = initial.current_path.read_bytes()
raw = b"private-candidate-raw"
normalized = b"private-candidate-normalized"
target_payload = raw if target_kind == "raw" else normalized
target_digest = hashlib.sha256(target_payload).hexdigest()
target_directory = "raw" if target_kind == "raw" else "normalized"
replace_calls = 0
original_replace = ingestion._atomic_replace
def track_replace(root: Path, destination: Path, payload: bytes) -> None:
nonlocal replace_calls
replace_calls += 1
original_replace(root, destination, payload)
def mutate_candidate(
source_root: Path, actual_candidate: bytes, actual_started_at: str
) -> None:
assert actual_candidate == normalized
assert actual_started_at == "2026-07-12T18:30:00Z"
target = source_root / target_directory / "sha256" / f"{target_digest}.bin"
target.chmod(0o600)
target.write_bytes(b"x" * len(target_payload))
target.chmod(0o400)
monkeypatch.setattr(ingestion, "_atomic_replace", track_replace)
with pytest.raises(IngestionRunError) as raised:
run_ingestion(
root=tmp_path,
source_id="source",
fetch=lambda: raw,
normalize=lambda _raw: normalized,
validate_history=mutate_candidate,
clock=_clock,
attempt_id=f"candidate-{target_kind}-mutated",
)
assert replace_calls == 0
assert initial.current_path.read_bytes() == marker_before
failure = _receipt(raised.value.receipt_path)
assert failure["error"] == {
"message": "active marker commit failed",
"type": "IngestionError",
}
assert failure["raw_sha256"] is None
assert failure["raw_snapshot"] is None
assert failure["normalized_sha256"] is None
assert failure["normalized_path"] is None
assert "private-candidate" not in raised.value.receipt_path.read_text(encoding="utf-8")
assert not (tmp_path / "source" / ".ingestion.lock").exists()
@pytest.mark.parametrize("target_kind", ["raw", "normalized", "current"])
def test_history_validator_cannot_mutate_prior_active_state_before_commit(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, target_kind: str
) -> None:
initial = run_ingestion(
root=tmp_path,
source_id="source",
fetch=lambda: b"raw-initial",
normalize=lambda _raw: b"normalized-initial",
clock=_clock,
attempt_id=f"prior-{target_kind}-initial",
)
marker_before = initial.current_path.read_bytes()
target = {
"raw": initial.raw_snapshot,
"normalized": initial.normalized_path,
"current": initial.current_path,
}[target_kind]
original_payload = target.read_bytes()
changed_payload = b"x" * len(original_payload)
replace_calls = 0
original_replace = ingestion._atomic_replace
def track_replace(root: Path, destination: Path, payload: bytes) -> None:
nonlocal replace_calls
replace_calls += 1
original_replace(root, destination, payload)
def mutate_prior(_source_root: Path, _candidate: bytes, actual_started_at: str) -> None:
assert actual_started_at == "2026-07-12T18:30:00Z"
target.chmod(0o600)
target.write_bytes(changed_payload)
target.chmod(0o400)
monkeypatch.setattr(ingestion, "_atomic_replace", track_replace)
with pytest.raises(IngestionRunError) as raised:
run_ingestion(
root=tmp_path,
source_id="source",
fetch=lambda: b"raw-candidate",
normalize=lambda _raw: b"normalized-candidate",
validate_history=mutate_prior,
clock=_clock,
attempt_id=f"prior-{target_kind}-mutated",
)
assert replace_calls == 0
expected_marker = changed_payload if target_kind == "current" else marker_before
assert initial.current_path.read_bytes() == expected_marker
assert _receipt(raised.value.receipt_path)["error"] == {
"message": "active marker commit failed",
"type": "IngestionError",
}
assert not (tmp_path / "source" / ".ingestion.lock").exists()
def test_history_validator_is_optional_for_backward_compatible_calls(
tmp_path: Path,
) -> None:
result = run_ingestion(
root=tmp_path,
source_id="source",
fetch=lambda: b"raw-without-history-validator",
normalize=lambda raw: raw,
clock=_clock,
attempt_id="history-optional",
)
assert result.current_path.read_bytes() == result.receipt_path.read_bytes()
def test_activated_validator_runs_under_lock_after_replace_before_success_receipt(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
source_root = tmp_path / "source"
candidate = b"normalized-activated-candidate"
expected_receipt = source_root / "receipts" / "activated-order.json"
events: list[str] = []
original_replace = ingestion._atomic_replace
original_install = ingestion._install_immutable
def track_replace(root: Path, destination: Path, payload: bytes) -> None:
original_replace(root, destination, payload)
events.append("replace")
def track_install(root: Path, destination: Path, payload: bytes) -> None:
if destination == expected_receipt:
events.append("receipt")
original_install(root, destination, payload)
def validate_activated(
actual_source_root: Path, actual_candidate: bytes, success_marker: bytes
) -> None:
assert actual_source_root == source_root
assert actual_candidate == candidate
assert (source_root / ".ingestion.lock").is_dir()
assert (source_root / "normalized" / "current.json").read_bytes() == success_marker
assert not expected_receipt.exists()
marker = json.loads(success_marker)
assert marker["status"] == "success"
assert marker["attempt_id"] == "activated-order"
events.append("activated-validation")
monkeypatch.setattr(ingestion, "_atomic_replace", track_replace)
monkeypatch.setattr(ingestion, "_install_immutable", track_install)
result = run_ingestion(
root=tmp_path,
source_id="source",
fetch=lambda: b"raw-activated-candidate",
normalize=lambda _raw: candidate,
validate_activated=validate_activated,
clock=_clock,
attempt_id="activated-order",
)
assert events == ["replace", "activated-validation", "receipt"]
assert result.current_path.read_bytes() == result.receipt_path.read_bytes()
assert not (source_root / ".ingestion.lock").exists()
def test_first_activated_validator_failure_removes_current_and_is_redacted(
tmp_path: Path,
) -> None:
secret = f"{tmp_path}/private-activated-validator-secret"
def reject_activated(source_root: Path, candidate: bytes, success_marker: bytes) -> None:
assert candidate == b"normalized-candidate"
assert (source_root / "normalized" / "current.json").read_bytes() == success_marker
assert (source_root / ".ingestion.lock").is_dir()
raise ValueError(secret)
with pytest.raises(IngestionRunError) as raised:
run_ingestion(
root=tmp_path,
source_id="source",
fetch=lambda: b"raw-candidate",
normalize=lambda _raw: b"normalized-candidate",
validate_activated=reject_activated,
clock=_clock,
attempt_id="activated-first-failure",
)
source_root = tmp_path / "source"
assert not (source_root / "normalized" / "current.json").exists()
failure = _receipt(raised.value.receipt_path)
assert failure["status"] == "failure"
assert failure["activated"] is False
assert failure["error"] == {
"message": "activated artifact validation failed",
"type": "ValueError",
}
assert raised.value.receipt_path.name == "activated-first-failure.json"
assert secret not in raised.value.receipt_path.read_text(encoding="utf-8")
assert secret not in str(raised.value)
assert secret not in "".join(traceback.format_exception(raised.value))
assert not (source_root / ".ingestion.lock").exists()
def test_later_activated_validator_failure_restores_exact_prior_marker(
tmp_path: Path,
) -> None:
initial = run_ingestion(
root=tmp_path,
source_id="source",
fetch=lambda: b"raw-initial",
normalize=lambda _raw: b"normalized-initial",
clock=_clock,
attempt_id="activated-prior",
)
marker_before = initial.current_path.read_bytes()
def reject_activated(_source_root: Path, _candidate: bytes, _marker: bytes) -> None:
raise RuntimeError("private post-activation detail")
with pytest.raises(IngestionRunError) as raised:
run_ingestion(
root=tmp_path,
source_id="source",
fetch=lambda: b"raw-replacement",
normalize=lambda _raw: b"normalized-replacement",
validate_activated=reject_activated,
clock=_clock,
attempt_id="activated-later-failure",
)
assert initial.current_path.read_bytes() == marker_before
failure = _receipt(raised.value.receipt_path)
assert failure["status"] == "failure"
assert failure["activated"] is False
assert failure["error"] == {
"message": "activated artifact validation failed",
"type": "Exception",
}
assert not (tmp_path / "source" / "receipts" / "activated-later-failure.failure.json").exists()
assert not (tmp_path / "source" / ".ingestion.lock").exists()
def test_activated_validator_mutation_is_detected_and_candidate_refs_are_cleared(
tmp_path: Path,
) -> None:
initial = run_ingestion(
root=tmp_path,
source_id="source",
fetch=lambda: b"raw-initial",
normalize=lambda _raw: b"normalized-initial",
clock=_clock,
attempt_id="activated-mutation-prior",
)
marker_before = initial.current_path.read_bytes()
def mutate_candidate(source_root: Path, candidate: bytes, _success_marker: bytes) -> None:
digest = hashlib.sha256(candidate).hexdigest()
target = source_root / "normalized" / "sha256" / f"{digest}.bin"
target.chmod(0o600)
target.write_bytes(b"x" * len(candidate))
target.chmod(0o400)
with pytest.raises(IngestionRunError) as raised:
run_ingestion(
root=tmp_path,
source_id="source",
fetch=lambda: b"raw-candidate",
normalize=lambda _raw: b"normalized-candidate",
validate_activated=mutate_candidate,
clock=_clock,
attempt_id="activated-mutation",
)
assert initial.current_path.read_bytes() == marker_before
failure = _receipt(raised.value.receipt_path)
assert failure["error"] == {
"message": "activated artifact validation failed",
"type": "IngestionError",
}
assert failure["raw_sha256"] is None
assert failure["raw_snapshot"] is None
assert failure["normalized_sha256"] is None
assert failure["normalized_path"] is None
assert not (tmp_path / "source" / ".ingestion.lock").exists()
def test_activated_validator_is_optional_for_backward_compatible_calls(
tmp_path: Path,
) -> None:
result = run_ingestion(
root=tmp_path,
source_id="source",
fetch=lambda: b"raw-without-activated-validator",
normalize=lambda raw: raw,
clock=_clock,
attempt_id="activated-optional",
)
assert result.current_path.read_bytes() == result.receipt_path.read_bytes()
def test_invalid_payload_limits_fail_before_lock_or_fetch(tmp_path: Path) -> None:
called = False
def fetch() -> bytes:
nonlocal called
called = True
return b"raw"
with pytest.raises(IngestionError, match="must be positive"):
run_ingestion(
root=tmp_path,
source_id="source",
fetch=fetch,
normalize=lambda raw: raw,
max_raw_bytes=0,
clock=_clock,
)
assert called is False
assert not (tmp_path / "source" / ".ingestion.lock").exists()
def test_rollback_failure_is_truthful_redacted_and_retains_lock(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
secret = "postgres://operator:secret@example.test/private"
original_install = ingestion._install_immutable
receipt_writes = 0