Automation and testing

Automate the workflow with Python

The standard-library client requires Python 3.9 or later. Place ireasoning_simulator.py in the same folder as your test script, or add the distribution python folder to PYTHONPATH. In a source checkout the client is in simulator/python. No pip package is needed for the client itself.

Save automation-demo/run_api.py with the following code. Run it from the installation directory while the REST server is running. The example uses the no-assertions scenario; if you add traffic assertions, run your manager at the same time.

import json
from pathlib import Path
from ireasoning_simulator import SimulatorClient

root = Path("automation-demo")
client = SimulatorClient("http://127.0.0.1:18080")
asset_id = client.upload(root / "data.xml")
definition = {
    "schemaVersion": 1, "name": "Python demonstration",
    "agents": [{
        "id": "agent-0", "name": "Router",
        "endpoint": {"address": "127.0.0.1", "port": 1161},
        "dataAssetIds": [asset_id]
    }]
}
scenario = json.loads((root / "scenario.json").read_text())
with client.lab(definition) as lab:
    report = client.run(lab["id"], scenario, timeout=120)
    run_id = report["id"]
    (root / "python-report.json").write_text(
        json.dumps(report, indent=2), encoding="utf-8")
    (root / "python-report.xml").write_text(
        client.report(lab["id"], run_id, format="junit"),
        encoding="utf-8")
    if report["state"] != "SUCCEEDED":
        raise RuntimeError("Scenario result: " + report["state"])

The lab context manager deletes the lab on exit, including on an exception, so export reports inside the with block. The client waits for asynchronous operations, but run returns the terminal report even when its assertions fail. Always check report["state"].

Use client.actions(lab_id, "start"), client.overrides(lab_id, agent_id, values), client.faults(lab_id, agent_id, rules), and client.trap(lab_id, agent_id, name) for individual operations. Provide context_id to overrides for multiple recordings. SimulatorError exposes code and an optional HTTP status without printing credentials. The client does not automatically retry mutations after a transport failure.