"""The critic's own coverage count for a live run (ADR 0017), against stand-in listings (no runtime).""" import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT)) from app import coverage # noqa: E402 RG = "/subscriptions/s/resourceGroups/g/providers/" ROWS = [{"id": RG + "Microsoft.KeyVault/vaults/kv-a", "type": "microsoft.keyvault/vaults", "location": "eastus"}, {"id": RG + "Microsoft.Storage/storageAccounts/sa1", "type": "microsoft.storage/storageaccounts", "location": "eastus"}, {"id": RG + "Microsoft.Network/networkInterfaces/nic1", "type": "microsoft.network/networkinterfaces", "location": "eastus"}, {"id": RG + "Microsoft.EventGrid/systemTopics/t1", "type": "microsoft.eventgrid/systemtopics", "location": "eastus"}] def call_with(rows, fail=False): def call(tool, args): assert tool == "azure_list_resources" if fail: return {"success": False, "error": {"message": "403"}} return {"success": True, "data": {"items": rows, "next_page_token": None, "evidence_ref": "sha256:x"}} return call def test_an_unscanned_resource_is_a_coverage_gap(): payload = {"provider": "azure", "findings": [{"scope_entry_id": RG + "Microsoft.Storage/storageAccounts/sa1"}], "coverage_gaps": [{"scope_entry_id": RG + "Microsoft.EventGrid/systemTopics/t1"}]} live = coverage.live_scope(payload, call_with(ROWS)) assert (live["listed"], live["scope_entries"], live["folded"], live["covered"]) == (4, 3, 1, 2) assert live["coverage_gaps"] == [RG + "Microsoft.KeyVault/vaults/kv-a"] and live["coverage_gap_count"] == 1 def test_azure_ids_match_whatever_their_case(): payload = {"provider": "azure", "findings": [{"scope_entry_id": r["id"].upper()} for r in ROWS]} assert coverage.live_scope(payload, call_with(ROWS))["coverage_gap_count"] == 0 def test_a_failed_listing_is_reported_not_passed(): live = coverage.live_scope({"provider": "azure", "findings": []}, call_with(ROWS, fail=True)) assert live["listing_issues"] and live["scope_entries"] == 0 def test_every_recorded_dependency_is_checked_against_the_graph(): rg = "/subscriptions/s/resourceGroups/g/providers/" pe, pg = rg + "Microsoft.Network/privateEndpoints/pe", rg + "Microsoft.DBforPostgreSQL/flexibleServers/pg" subnet = "/subscriptions/s/resourceGroups/net/providers/Microsoft.Network/virtualNetworks/v/subnets/s" nic, ip = rg + "Microsoft.Network/networkInterfaces/pe.nic", "/subscriptions/s/resourceGroups/n/providers/Microsoft.Network/publicIPAddresses/ip" payload = {"provider": "azure", "records": [{"canonical_id": "CI-001", "scope_entry_id": pe}, {"canonical_id": "CI-002", "scope_entry_id": pg}], "findings": [{"scope_entry_id": pe, "dependencies": [pg, subnet, nic, pe]}, {"scope_entry_id": pg, "dependencies": [ip]}], "edges": [{"source": "CI-001", "target": "CI-002"}, {"source": "CI-001", "target": subnet.upper()}], "nodes": [{"id": subnet, "kind": "external"}], "folded_dependencies": [{"source": "CI-001", "target": nic}]} result = coverage.live_dependencies(payload) assert (result["recorded"], result["edges"], result["to_external_nodes"], result["folded"]) == (4, 2, 1, 1) assert result["missing"] == [f"{pg} -> {ip}"] and result["missing_count"] == 1