Viewing files at commit 878fc46b2f31Commit detailsView latest

Browse files
tests/test_conformance_gate.py 286 lines · 15.2 KB · python
1
"""Conformance gate semantics and its wiring into devloop and publish_gate."""
2
import contextlib
3
import io
4
import json
5
from pathlib import Path
6
import sys
7
import tempfile
8
import unittest
9
10
ROOT = Path(__file__).resolve().parents[1]
11
sys.path.insert(0, str(ROOT / "scripts"))
12
import devloop  # noqa: E402
13
import publish_gate  # noqa: E402
14
from oakbench import conformance_gate as gate  # noqa: E402
15
from oakbench.conformance import SCENARIOS  # noqa: E402
16
from oakbench.conformance_matrix import BEHAVIOURS  # noqa: E402
17
18
ALL = list(SCENARIOS) + [b.id for b in BEHAVIOURS]
19
PROFILE = "darwin-ci-ni"
20
SHA = "b" * 64
21
OBSERVED = json.loads((ROOT / "tests/data/conformance_oak_0105_failures.json").read_text())["details"]
22
23
24
def conformance_row(behaviour, code=0, failed=None, subject="oak", sha=SHA, profile=PROFILE, **extra):
25
    """failed: {leg: detail}; legacy scenarios use {'behaviour': failure}."""
26
    row = {"evidence_class": "diagnostic_conformance", "subject": subject, "scenario": behaviour,
27
           "returncode": code, "binary_sha256": sha, "fs_profile": profile, **extra}
28
    if code == 1:
29
        failed = failed or {"clone_fs": "injected"}
30
        if behaviour in SCENARIOS:
31
            row["failure"] = failed["behaviour"]
32
        else:
33
            row["legs"] = {leg: {"status": "fail", "detail": detail} for leg, detail in failed.items()}
34
            row["failed_legs"] = sorted(failed)
35
            first = sorted(failed)[0]
36
            row["failure"] = f"{first}: {failed[first]}"
37
    if code == 77:
38
        row.setdefault("skip_reason", "filesystem capability unavailable: case_sensitive_fs")
39
    return row
40
41
42
def healthy_rows(expected, subject="oak", sha=SHA, profile=PROFILE):
43
    """Oak 0.105.0 as observed: expected failures with their real details, recorded skips."""
44
    skips = set(expected["coverage"].get(profile, {}).get("expected_skips", []))
45
    rows = []
46
    for behaviour in ALL:
47
        if behaviour in expected["entries"] and gate.entry_applies(expected["entries"][behaviour], profile):
48
            rows.append(conformance_row(behaviour, 1, OBSERVED[behaviour], subject, sha, profile))
49
        elif behaviour in skips:
50
            rows.append(conformance_row(behaviour, 77, None, subject, sha, profile))
51
        else:
52
            rows.append(conformance_row(behaviour, 0, None, subject, sha, profile))
53
    return rows
54
55
56
def replace(rows, row):
57
    return [r for r in rows if r["scenario"] != row["scenario"] or r["subject"] != row["subject"]] + [row]
58
59
60
class EvaluateTests(unittest.TestCase):
61
    def setUp(self):
62
        self.expected = gate.load_expected()
63
64
    def verdict(self, rows):
65
        return gate.evaluate(rows, self.expected, ALL)
66
67
    def test_observed_oak_0105_details_match_every_entry(self):
68
        self.assertEqual(set(OBSERVED), set(self.expected["entries"]))
69
        result = self.verdict(healthy_rows(self.expected))
70
        self.assertEqual(result["verdict"], "PASS", result)
71
        self.assertEqual(len(result["expected_failures"]) + len(result["refused"]), len(self.expected["entries"]))
72
        self.assertEqual(sorted(x.split(":")[0] for x in result["refused"]),
73
                         ["path_control_chars_v1", "path_newline_v1", "path_roundtrip_v1", "path_tab_v1"])
74
75
    def test_new_failure_is_a_regression(self):
76
        rows = replace(healthy_rows(self.expected), conformance_row("eol_crlf_v1", 1, {"clone_fs": "changed=['crlf.txt']"}))
77
        result = self.verdict(rows)
78
        self.assertEqual(result["verdict"], "REGRESSED")
79
        self.assertIn("eol_crlf_v1", result["regressions"][0])
80
81
    def test_same_legs_with_a_different_error_is_a_regression(self):
82
        cases = {
83
            # EEXIST turning into a path escape at the same leg.
84
            "file_to_dir_v1": {"pull_fs": "extra=['../../etc/evil']"},
85
            # A policy refusal turning into a crash at the same leg.
86
            "path_tab_v1": {"commit_push": "oak commit --no-verify exited 101: thread 'main' panicked at core/src/tree.rs"},
87
            # The legacy single-leg scenario cannot hide a different failure.
88
            "path_roundtrip_v1": {"behaviour": "state mismatch: missing=[], extra=['tab'], changed=['nested/leaf']"},
89
            # A pattern must match the whole detail, not a substring.
90
            "file_to_deep_dir_v1": {"pull_fs": "oak pull exited 1: error: IO error: Not a directory (os error 20); "
91
                                               "extra=['../outside']"},
92
        }
93
        for behaviour, failed in cases.items():
94
            with self.subTest(behaviour=behaviour):
95
                result = self.verdict(replace(healthy_rows(self.expected), conformance_row(behaviour, 1, failed)))
96
                self.assertEqual(result["verdict"], "REGRESSED", result)
97
                self.assertIn("changed", result["regressions"][0])
98
99
    def test_known_failure_at_different_legs_is_a_regression(self):
100
        failed = dict(OBSERVED["file_to_dir_v1"], clone_fs="missing=['node/child.txt']")
101
        result = self.verdict(replace(healthy_rows(self.expected), conformance_row("file_to_dir_v1", 1, failed)))
102
        self.assertEqual(result["verdict"], "REGRESSED")
103
        self.assertIn("fb-546", result["regressions"][0])
104
105
    def test_fix_flips_to_fixed_without_regression(self):
106
        result = self.verdict(replace(healthy_rows(self.expected), conformance_row("path_tab_v1", 0)))
107
        self.assertEqual(result["verdict"], "PASS")
108
        self.assertIn("fb-531", result["fixed"][0])
109
110
    def test_profile_scoped_entry_is_not_fixed_or_expected_elsewhere(self):
111
        linux = healthy_rows(self.expected, profile="linux-cs-ns")
112
        # Case-only rename is scoped to darwin-ci-ni: a pass on Linux is a plain pass...
113
        rows = replace(linux, conformance_row("path_case_only_rename_v1", 0, profile="linux-cs-ns"))
114
        result = self.verdict(rows)
115
        self.assertEqual(result["fixed"], [])
116
        self.assertIn("path_case_only_rename_v1", result["passed"])
117
        self.assertTrue(any("no recorded coverage" in n for n in result["notes"]))
118
        # ...and the darwin failure shape on Linux is a regression, not an xfail.
119
        rows = replace(linux, conformance_row("path_case_only_rename_v1", 1, OBSERVED["path_case_only_rename_v1"],
120
                                              profile="linux-cs-ns"))
121
        self.assertEqual(self.verdict(rows)["verdict"], "REGRESSED")
122
123
    def test_coverage_loss_pass_or_xfail_to_skip_is_unmeasured(self):
124
        for behaviour in ("eol_lf_v1", "file_to_dir_v1"):
125
            with self.subTest(behaviour=behaviour):
126
                rows = replace(healthy_rows(self.expected), conformance_row(behaviour, 77))
127
                result = self.verdict(rows)
128
                self.assertEqual(result["verdict"], "UNMEASURED")
129
                self.assertIn("coverage loss", result["unmeasured"][0])
130
131
    # QA round 2: skips must never add up to a PASS.
132
    def all_skipped(self, profile=PROFILE, drop_profile=False):
133
        rows = [conformance_row(b, 77, None, profile=profile) for b in ALL]
134
        if drop_profile:
135
            for row in rows:
136
                row.pop("fs_profile")
137
        return rows
138
139
    def test_all_skip_on_unrecorded_profile_is_unmeasured(self):
140
        result = self.verdict(self.all_skipped(profile="linux-cs-ns"))
141
        self.assertEqual(result["verdict"], "UNMEASURED")
142
        self.assertTrue(any("zero measured" in item for item in result["unmeasured"]))
143
        self.assertTrue(any("unrecorded profile" in item for item in result["unmeasured"]))
144
145
    def test_rows_without_fs_profile_are_unmeasured(self):
146
        result = self.verdict(self.all_skipped(drop_profile=True))
147
        self.assertEqual(result["verdict"], "UNMEASURED")
148
        self.assertTrue(any("without fs_profile" in item for item in result["unmeasured"]))
149
        # Even one otherwise-healthy row missing the field is unmeasured.
150
        rows = healthy_rows(self.expected)
151
        rows[0].pop("fs_profile")
152
        self.assertEqual(self.verdict(rows)["verdict"], "UNMEASURED")
153
154
    def test_zero_measured_is_unmeasured_even_on_recorded_profile(self):
155
        result = self.verdict(self.all_skipped())
156
        self.assertEqual(result["verdict"], "UNMEASURED")
157
        self.assertEqual(result["measured"], 0)
158
159
    def test_unrecorded_profile_allows_only_known_filesystem_dependent_skips(self):
160
        linux = "linux-cs-ns"
161
        rows = healthy_rows(self.expected, profile=linux)
162
        # Legitimate on Linux: the case-insensitive-only behaviour skips.
163
        ok = replace(rows, conformance_row("path_case_only_rename_v1", 77, profile=linux,
164
                                           skip_reason="filesystem capability unavailable: case_insensitive_fs"))
165
        ok = replace(ok, conformance_row("huge_file_sparse_v1", 77, profile=linux, skip_reason="gated: off"))
166
        self.assertEqual(self.verdict(ok)["verdict"], "PASS")
167
        # A broken probe skipping symlink behaviours is not accepted.
168
        bad = replace(ok, conformance_row("symlink_relative_v1", 77, profile=linux,
169
                                          skip_reason="filesystem capability unavailable: symlink"))
170
        result = self.verdict(bad)
171
        self.assertEqual(result["verdict"], "UNMEASURED")
172
        self.assertIn("symlink_relative_v1", result["unmeasured"][0])
173
174
    def test_mixed_profiles_are_unmeasured(self):
175
        rows = replace(healthy_rows(self.expected), conformance_row("eol_lf_v1", 0, profile="linux-cs-ns"))
176
        self.assertEqual(self.verdict(rows)["verdict"], "UNMEASURED")
177
178
    def test_missing_rows_harness_errors_and_reader_skips_are_unmeasured(self):
179
        rows = [r for r in healthy_rows(self.expected) if r["scenario"] != "eol_lf_v1"]
180
        self.assertEqual(self.verdict(rows)["verdict"], "UNMEASURED")
181
        self.assertEqual(self.verdict(rows + [conformance_row("eol_lf_v1", 2, failure="harness")])["verdict"], "UNMEASURED")
182
        reader = conformance_row("eol_lf_v1", 77, skip_reason="leg unmeasured: reader bound")
183
        self.assertEqual(self.verdict(rows + [reader])["verdict"], "UNMEASURED")
184
        self.assertEqual(gate.evaluate([], self.expected)["verdict"], "UNMEASURED")
185
186
    def test_git_rows_never_gate(self):
187
        rows = healthy_rows(self.expected) + [conformance_row("eol_lf_v1", 1, {"clone_fs": "x"}, subject="git")]
188
        result = self.verdict(rows)
189
        self.assertEqual(result["verdict"], "PASS")
190
        self.assertEqual(len(result["git_reference_failures"]), 1)
191
192
    def test_list_validation(self):
193
        good = {"behaviour": "a", "ticket": "fb-1", "failed_legs": {"clone_fs": "x"}}
194
        bad = [dict(good, ticket="proposed:x"), dict(good, ticket="TODO"), dict(good, failed_legs=["clone_fs"]),
195
               dict(good, failed_legs={"clone_fs": ""}), dict(good, outcome="flaky"), dict(good, fs_profiles=[])]
196
        with tempfile.TemporaryDirectory() as tmp:
197
            path = Path(tmp) / "x.json"
198
            for entry in bad:
199
                path.write_text(json.dumps({"schema_version": 2, "entries": [entry]}))
200
                with self.assertRaises(ValueError):
201
                    gate.load_expected(path)
202
            path.write_text(json.dumps({"schema_version": 1, "entries": [good]}))
203
            with self.assertRaises(ValueError):
204
                gate.load_expected(path)
205
206
207
class WiringTests(unittest.TestCase):
208
    def setUp(self):
209
        self.expected = gate.load_expected()
210
211
    def test_devloop_lane_is_default_and_maps_verdicts(self):
212
        self.assertIn("conformance", devloop.LANES)
213
        regressions, notes = devloop.conformance_checks(healthy_rows(self.expected))
214
        self.assertEqual(regressions, [])
215
        self.assertFalse(any(n.startswith("UNMEASURED") for n in notes))
216
        rows = replace(healthy_rows(self.expected), conformance_row("eol_lf_v1", 1, {"pull_fs": "x"}))
217
        regressions, notes = devloop.conformance_checks(rows)
218
        self.assertTrue(regressions and regressions[0].startswith("[conformance] CONFORMANCE eol_lf_v1"))
219
        rows = [r for r in healthy_rows(self.expected) if r["scenario"] != "eol_lf_v1"]
220
        regressions, notes = devloop.conformance_checks(rows)
221
        self.assertTrue(any(n.startswith("UNMEASURED conformance") for n in notes))
222
223
    def gate_output(self, rows):
224
        with tempfile.TemporaryDirectory() as tmp:
225
            path = Path(tmp) / "rows.jsonl"
226
            path.write_text("".join(json.dumps(r) + "\n" for r in rows))
227
            with contextlib.redirect_stdout(io.StringIO()) as out:
228
                code = publish_gate.main([str(path)])
229
            return code, out.getvalue()
230
231
    def oak_row(self, sha=SHA, **extra):
232
        return {"subject": "oak_installed", "binary_sha256": sha, "returncode": 0,
233
                "source": {"oak_hash": "abc", "oak_status": ""}, **extra}
234
235
    def test_requires_conformance_for_oak_rows(self):
236
        code, out = self.gate_output([self.oak_row()])
237
        self.assertIn("FAIL   conformance: Oak rows published without", out)
238
        code, out = self.gate_output([self.oak_row(), *healthy_rows(self.expected)])
239
        self.assertIn("PASS   [bbbbbbbbbbbb] conformance (oak, darwin-ci-ni): PASS", out)
240
        self.assertNotIn("FAIL   conformance", out)
241
242
    def test_oak_rows_without_binary_identity_fail(self):
243
        anonymous = self.oak_row()
244
        anonymous.pop("binary_sha256")
245
        code, out = self.gate_output([anonymous, *healthy_rows(self.expected)])
246
        self.assertIn("Oak rows lack binary identity", out)
247
        via_details = self.oak_row(subject_details={"oak_installed": {"binary_sha256": SHA}})
248
        via_details.pop("binary_sha256")
249
        code, out = self.gate_output([via_details, *healthy_rows(self.expected)])
250
        self.assertNotIn("lack binary identity", out)
251
252
    def test_rows_of_another_binary_never_satisfy_or_mix(self):
253
        other = "c" * 64
254
        code, out = self.gate_output([self.oak_row(sha=other), *healthy_rows(self.expected)])
255
        self.assertIn(f"conformance[{other[:12]}]: UNMEASURED", out)
256
        # A good binary's rows cannot mask a regressed binary's row, and vice versa.
257
        mixed = healthy_rows(self.expected) + replace(healthy_rows(self.expected, sha=other),
258
                                                      conformance_row("eol_lf_v1", 1, {"pull_fs": "x"}, sha=other))
259
        code, out = self.gate_output([self.oak_row(), self.oak_row(sha=other), *mixed])
260
        self.assertIn(f"conformance[{other[:12]}]: REGRESSED", out)
261
        self.assertNotIn(f"conformance[{SHA[:12]}]: REGRESSED", out)
262
263
    def test_publish_requires_a_recorded_profile(self):
264
        linux = [dict(row, fs_profile="linux-cs-ns") for row in healthy_rows(self.expected, profile="linux-cs-ns")]
265
        code, out = self.gate_output([self.oak_row(), *linux])
266
        self.assertEqual(code, 1)
267
        self.assertIn("has no recorded coverage", out)
268
        skipped = [conformance_row(b, 77, None, profile="linux-cs-ns") for b in ALL]
269
        code, out = self.gate_output([self.oak_row(), *skipped])
270
        self.assertIn("UNMEASURED", out)
271
        self.assertNotIn("PASS   [", out)
272
        missing = healthy_rows(self.expected)
273
        for row in missing:
274
            row.pop("fs_profile")
275
        code, out = self.gate_output([self.oak_row(), *missing])
276
        self.assertIn("FAIL   conformance[", out)
277
        self.assertNotIn("PASS   [", out)
278
279
    def test_stale_list_fails(self):
280
        rows = replace(healthy_rows(self.expected), conformance_row("path_tab_v1", 0))
281
        code, out = self.gate_output([self.oak_row(), *rows])
282
        self.assertIn("expected-failures list is stale", out)
283
284
285
if __name__ == "__main__":
286
    unittest.main()