File size: 3,243 Bytes
1eea488
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e818181
 
 
1eea488
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import hashlib
import json
from pathlib import Path
import stat

import pytest

from scripts import ctx_ab_failure_evidence as evidence


def _canonical(value: object) -> bytes:
    return json.dumps(value, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode()


def test_publishes_chained_private_failure_and_authenticated_staging(
    tmp_path: Path,
) -> None:
    staging = tmp_path / ".staging"
    staging.mkdir(mode=0o700)
    raw = staging / "raw"
    raw.mkdir(mode=0o755)
    (raw / "detail.txt").write_text("private task detail", encoding="utf-8")
    (raw / "detail.txt").chmod(0o644)
    destination = tmp_path / "failure"

    try:
        try:
            raise ValueError("private cause")
        except ValueError as cause:
            raise RuntimeError("private outer") from cause
    except RuntimeError as exc:
        manifest = evidence.publish_failure(
            destination=destination,
            operation="test-operation",
            exc=exc,
            repository_root=tmp_path / "unrelated-repository",
            staging=staging,
        )

    assert not staging.exists()
    assert evidence.already_preserved(destination)
    failure = json.loads((destination / "failure.json").read_text(encoding="utf-8"))
    assert failure["exception_chain"] == [
        {"message": "private outer", "type": "RuntimeError"},
        {"message": "private cause", "type": "ValueError"},
    ]
    assert failure["operation"] == "test-operation"
    assert "private cause" in failure["traceback"]
    assert (
        manifest["manifest_sha256"] == hashlib.sha256(_canonical(manifest["entries"])).hexdigest()
    )
    assert json.loads((destination / evidence.FAILURE_MANIFEST).read_bytes()) == manifest
    assert (destination / "raw" / "detail.txt").read_text(encoding="utf-8") == (
        "private task detail"
    )
    assert stat.S_IMODE(destination.stat().st_mode) == 0o700
    assert stat.S_IMODE((destination / "raw").stat().st_mode) == 0o700
    assert stat.S_IMODE((destination / "raw" / "detail.txt").stat().st_mode) == 0o600


def test_refuses_overwrite_and_nonprivate_repository_destination(tmp_path: Path) -> None:
    destination = tmp_path / "failure"
    destination.mkdir()
    with pytest.raises(evidence.FailureEvidenceError, match="already exists"):
        evidence.publish_failure(
            destination=destination,
            operation="test",
            exc=RuntimeError("detail"),
            repository_root=tmp_path / "repository",
        )

    repository = tmp_path / "repository"
    repository.mkdir()
    with pytest.raises(evidence.FailureEvidenceError, match="private root"):
        evidence.publish_failure(
            destination=repository / "public-failure",
            operation="test",
            exc=RuntimeError("detail"),
            repository_root=repository,
        )


def test_requires_absolute_destination(tmp_path: Path) -> None:
    with pytest.raises(evidence.FailureEvidenceError, match="absolute"):
        evidence.publish_failure(
            destination=Path("relative-failure"),
            operation="test",
            exc=RuntimeError("detail"),
            repository_root=tmp_path,
        )