Files
tom_analysis_agent/tests/test_digest.py

238 lines
8.6 KiB
Python

"""Digesting a solution architecture's files into an operating spec, in code."""
import json
from pathlib import Path
import pytest
from execution_engine.scope import ESSENTIAL, merged, read_scope
from app.operating_model import digest, spec
from app.operating_model.workflow import ROOT
from tests.support import BUNDLE, CATALOGUE, WORKFLOW
FIXTURE = Path(__file__).parent / "fixtures" / "architecture" / "case-08-landing-zone"
SOURCES = digest.read_folder(FIXTURE)
KINDS = digest.item_kinds(ROOT, "gcp")
DIGESTED = digest.architecture(SOURCES, KINDS)
GRAPH = json.loads(SOURCES[digest.GRAPH])
def test_every_file_the_digest_reads_came_with_the_fixture():
assert set(SOURCES) == set(digest.FILES)
assert set(DIGESTED.used) == set(digest.FILES)
def test_every_component_becomes_one_item_and_every_element_is_accounted_for():
items = DIGESTED.spec["solution"]["items"]
components = json.loads(SOURCES[digest.COMPONENTS])
assert [item["source_ref"] for item in items] == [c["id"] for c in components]
assert [item["id"] for item in items] == [f"S-{n}" for n in range(1, 10)]
placed = [element for item in items for element in item["elements"]]
assert sorted(placed) == sorted(e["id"] for e in GRAPH["elements"])
assert len(placed) == 60
def test_an_item_takes_its_product_and_its_kind_from_the_providers_table():
by_ref = {item["source_ref"]: item for item in DIGESTED.spec["solution"]["items"]}
assert (by_ref["C-2"]["product"], by_ref["C-2"]["kind"]) == (
"Cloud Interconnect",
"network",
)
assert by_ref["C-4"]["kind"] == "storage"
assert by_ref["C-7"]["kind"] == "keys"
# A product the table does not know is a service; nothing is guessed.
assert by_ref["C-3"]["kind"] == "service"
def test_what_the_architecture_leaves_to_operation_becomes_the_conditions():
conditions = DIGESTED.spec["solution"]["conditions"]
left_to_operation = [
(entry["id"], statement)
for name in (digest.REQUIREMENTS, digest.RECOMMENDATIONS)
for entry in json.loads(SOURCES[name])
for statement in entry.get("outside_code") or []
]
assert [(c["source_ref"], c["condition"]) for c in conditions] == [
(identifier, " ".join(statement.split()))
for identifier, statement in left_to_operation
]
recovery = next(
c for c in conditions if "recovery time objective" in c["condition"]
)
assert recovery["source_ref"] == "NFR-11"
items = {item["id"] for item in DIGESTED.spec["solution"]["items"]}
assert all(c["on"] and set(c["on"]) <= items for c in conditions)
def test_the_settings_checked_at_build_are_not_conditions():
# 104 Checks on infrastructure settings were verified in validation. Keeping
# them true is configuration management, not a Condition each.
assert len(GRAPH["checks"]) == 104
assert len(DIGESTED.spec["solution"]["conditions"]) == 12
def test_only_what_the_customer_stated_is_carried_across_as_stated():
carried = DIGESTED.spec
assert carried["operations"]["support_hours"] == "24x7" # stated: always
assert carried["compliance"]["frameworks"] == ["DORA", "ISO27001", "PCI_DSS_v4"]
assert carried["commercial"]["currency"] == "GBP"
assert carried["solution"]["cloud_provider"] == "gcp"
assert carried["engagement"]["kind"] == "platform_foundation"
# The architecture agent assumed these for the customer; they are asked again.
assert "change_process" not in carried["operations"]
assert "service_levels" not in carried
def test_the_build_and_what_the_customer_owns_of_it_are_carried_across():
build = DIGESTED.spec["solution"]["build"]
assert build["duration_days"] == 36
assert build["customer_owned"] == [
"Order and provision the Interconnect carrier circuits"
]
def test_the_spec_says_which_execution_it_was_digested_from():
assert DIGESTED.spec["metadata"] == {
"customer_case": "case-08-landing-zone",
"source": "architecture:case-08-landing-zone-185426",
}
assert "payment platform" in DIGESTED.spec["intent"]["statement"]
def test_the_digested_spec_is_one_the_catalogue_accepts():
scope = read_scope(
DIGESTED.spec, BUNDLE.scope_catalogue(), provider_values=BUNDLE.scope_values
)
# What only the customer can say is still essential: what the business wants
# from the solution, and who operates it.
assert [gap["field"] for gap in scope.gaps(ESSENTIAL)] == [
"intent.business_outcomes",
"sourcing.model",
]
def test_every_digested_value_is_marked_and_what_the_caller_adds_is_theirs():
scope = merged(
DIGESTED.spec,
"sourcing:\n model: shared\ncommercial:\n currency: EUR\n"
"intent:\n business_outcomes: decide\n",
BUNDLE.scope_catalogue(),
provider_values=BUNDLE.scope_values,
notes=DIGESTED.notes,
)
assert scope.source("solution.items") == "digested"
assert scope.notes["solution.items"].startswith("9 components")
assert scope.source("sourcing.model") == "supplied"
assert scope.values["commercial.currency"] == "EUR"
assert scope.source("commercial.currency") == "supplied"
assert scope.gaps(ESSENTIAL) == []
def test_the_capabilities_proposed_follow_from_what_was_digested():
scope = read_scope(
DIGESTED.spec, BUNDLE.scope_catalogue(), provider_values=BUNDLE.scope_values
).for_context()
proposed = CATALOGUE.applicable(scope)
assert "backup-and-recovery" in proposed # the audit log store is storage
assert "regulatory-evidence" in proposed # DORA, ISO 27001, PCI DSS
assert len(spec.items(scope)) == 9
def test_a_check_that_did_not_pass_is_an_inherited_risk():
sources = dict(SOURCES)
verdicts = json.loads(sources[digest.VERDICTS])
verdicts["checks"][0].update(verdict="fail", evidence="display_name is empty")
sources[digest.VERDICTS] = json.dumps(verdicts)
risks = digest.architecture(sources, KINDS).spec["solution"]["inherited_risks"]
assert risks == [
{
"on": "S-1",
"risk": "Check K-1 failed in validation: `display_name` is set "
"(display_name is empty)",
"source_ref": "K-1",
}
]
assert "inherited_risks" not in DIGESTED.spec["solution"] # every Check passed
def test_files_passed_without_their_manifest_are_marked_unverified():
sources = {k: v for k, v in SOURCES.items() if k != digest.MANIFEST}
digested = digest.architecture(sources, KINDS)
assert digested.spec["metadata"] == {"source": "architecture:unverified"}
assert (
"not known to be one completed Execution" in digested.notes["metadata.source"]
)
assert len(digested.spec["solution"]["items"]) == 9
def test_the_graph_alone_is_enough_to_say_what_is_operated():
digested = digest.architecture({digest.GRAPH: SOURCES[digest.GRAPH]}, KINDS)
items = digested.spec["solution"]["items"]
# With no components file, they are taken from the Elements, in the order met.
assert sorted(item["source_ref"] for item in items) == [
f"C-{n}" for n in range(1, 10)
]
assert "conditions" not in digested.spec["solution"]
@pytest.mark.parametrize(
("change", "problem"),
[
(
lambda s: s.update({digest.GRAPH: "{"}),
"target-architecture.json: not valid JSON",
),
(
lambda s: s.update({digest.GRAPH: "[]"}),
"must be an object with a list of elements",
),
(
lambda s: s.update(
{digest.MANIFEST: json.dumps({"status": "completed_with_warnings"})}
),
"the Execution did not complete",
),
(
lambda s: s.update(
{digest.COMPONENTS: json.dumps([{"id": "C-1", "name": "Only one"}])}
),
"these Elements name a component that components.json does not have",
),
],
)
def test_an_architecture_that_cannot_be_used_is_refused_and_says_why(change, problem):
sources = dict(SOURCES)
change(sources)
with pytest.raises(ValueError, match=problem):
digest.architecture(sources, KINDS)
def test_sources_that_are_not_an_architecture_are_left_to_be_read():
assert digest.architecture({"design.md": "# Our platform\n"}, KINDS).spec == {}
assert WORKFLOW.digest is not None
assert WORKFLOW.digest({"design.md": "# Our platform\n"}).used == ()
def test_the_command_prints_the_spec_and_what_it_was_read_from(capsys):
assert digest.main([str(FIXTURE)]) == 0
printed = capsys.readouterr()
assert "source: architecture:case-08-landing-zone-185426" in printed.out
assert "# solution.items: 9 components" in printed.err
assert digest.main([str(FIXTURE.parent)]) == 1