struc-stratum / tests /test_faults.py
tailot's picture
publish struc-stratum bundle
13a6b66 verified
Raw History Blame Contribute Delete
5.26 kB
"""The faults this bundle can switch on, and what each of them is required to leave behind.
The catalogue is the counterpart of the one the structures topology runs: here the misbehaviour is in
the reading and in the reporting, never in the mechanics. A fault is worth having only if the stratum
reports it, so every experiment below ends by asking the same question -- did the assurance fall, and
did the reason name the node that fell short?
"""
import pytest
from lgstratum import agents, canonical, corpus, faults, pipeline
ITEMS = {item["id"]: item for item in corpus.load()}
ONE = "it-01"
# A reading with numbers in it: the frame of a verification carries the member, the abscissa, the
# section and the limit, so every fault of a reader has something to bite on.
NUMBERED = "it-07"
FRAME = ("frame nodes [ 1 0 0 ; 2 4 0 ] members [ 1 1 2 ] props [ 1 210000000 1/5 2/1875 ] "
"supports [ 1 pin ; 2 roller 0 1 ] loads [ member 1 udl 0 -15 ]")
VERIFY = {"V": "bending", "M": "2", "P": "1", "S": "rect 1/5 2/5", "L": "150000000", "F": FRAME}
def _ask(item_id, fault_spec=(), active=agents.ORDER):
item = ITEMS[item_id]
return pipeline.ask(str(item["question"]), str(item["lang"]), active=active, fault_spec=fault_spec)
def test_the_catalogue_names_nodes_of_this_topology_only():
assert set(faults.CATALOG) <= set(agents.ORDER)
# The invoker is not in the catalogue: it reads nothing and proves nothing, it only asks the
# structures topology a question, so there is no shortcut it could take.
assert "invoker" not in faults.CATALOG
faults.configure(())
for node, kinds in faults.CATALOG.items():
assert kinds, node
assert len(set(kinds)) == len(kinds), node
for kind in kinds:
assert not faults.has(node, kind), (node, kind)
def test_a_fault_configured_is_a_fault_fired_once():
faults.configure(["reader_a:merge_slots", "reader_a:merge_slots", "coverage:rubber_stamp"])
assert faults.has("reader_a", "merge_slots")
faults.fire("reader_a", "merge_slots")
faults.fire("reader_a", "merge_slots")
assert faults.fired("reader_a") == ["reader_a:merge_slots"]
# Configured is not the same as fired: only a fault that really changed something is recorded, so
# the ablation can tell an activated fault from a dormant one.
assert faults.has("coverage", "rubber_stamp")
assert "coverage:rubber_stamp" not in faults.FIRED
assert faults.FIRED == ["reader_a:merge_slots"]
faults.configure(())
assert not faults.has("reader_a", "merge_slots")
def test_the_reader_faults_damage_a_reading_that_carries_them():
# A reading of a verification carries six slots: one holds a word of a closed set, the rest hold a
# number or a block of them. The member and the abscissa carry different values on purpose, since
# a fault that copies one slot over another damages nothing when the two already agree. Each fault
# leaves a sentence the structures topology would happily solve, which is what makes the experiment
# measure the readers and not the parser.
kind = "verify"
order = canonical.slots_for(kind)
base = dict(VERIFY)
for fault in faults.CATALOG["reader_a"]:
faults.configure(["reader_a:" + fault])
changed, was_changed = faults.mutate_reading("reader_a", kind, base, order)
assert was_changed is True, fault
assert changed != base, fault
assert faults.fired("reader_a") == ["reader_a:" + fault], fault
assert canonical.balanced(canonical.print_sentence(kind, changed)) is True, fault
faults.configure(())
def test_a_reader_that_shifts_a_number_is_found_out_by_the_other_reader():
honest = _ask(NUMBERED)
shifted = _ask(NUMBERED, ["reader_a:shift_number"])
assert honest["final"]["assurance"] > 0
assert shifted["final"]["state"] == "DISPUTED", shifted["final"]
assert shifted["final"]["assurance"] == 0
assert shifted["final"]["answer"] == ""
assert "disagree" in shifted["final"]["reasons"] or "number_count" in shifted["final"]["reasons"]
def test_a_layer_that_answers_another_sentence_is_reported_as_tampering():
tampered = _ask(ONE, ["boundary:tamper_struc"])
assert tampered["final"]["state"] == "TAMPERED", tampered["final"]
assert tampered["final"]["assurance"] == 0
assert tampered["final"]["answer"] == ""
assert "boundary" in tampered["final"]["reasons"]
def test_an_arbiter_that_lifts_the_assurance_is_overruled_by_the_tools():
honest = _ask(ONE)
forced = _ask(ONE, ["arbiter:force_assurance"])
assert any(record["claimed"] != record["referee"] for record in forced["records"]), forced["records"]
assert forced["final"]["assurance"] <= honest["final"]["assurance"], forced["final"]
assert forced["final"]["assurance"] <= 2
assert "arbiter" in (forced["final"].get("detected") or [])
@pytest.mark.parametrize("node", sorted(faults.CATALOG))
def test_no_single_fault_lifts_the_assurance_above_the_honest_one(node):
honest = _ask(ONE)
for kind in faults.CATALOG[node]:
outcome = _ask(ONE, ["%s:%s" % (node, kind)])
shown = outcome["final"]
assert shown["assurance"] <= min(2, honest["final"]["assurance"]), (node, kind, shown)