def validate_controls(experiment: Experiment): """ Validate that all declared controls respect the specification. Raises :exc:`chaoslib.exceptions.InvalidControl` when they are not valid. """ controls = get_controls(experiment) references = [ c["name"] for c in get_controls(experiment) if "ref" not in c and "name" in c ] for c in controls: if "ref" in c: if c["ref"] not in references: raise InvalidControl( "Control reference '{}' declaration cannot be found") if "name" not in c: raise InvalidControl("A control must have a `name` property") name = c["name"] if "provider" not in c: raise InvalidControl( "Control '{}' must have a `provider` property".format(name)) scope = c.get("scope") if scope and scope not in ("before", "after"): raise InvalidControl( "Control '{}' scope property must be 'before' or " "'after' only".format(name)) provider_type = c.get("provider", {}).get("type") if provider_type == "python": validate_python_control(c)
def test_validate_python_control_needs_a_module(): with pytest.raises(InvalidActivity): validate_python_control({ "name": "a-python-control", "provider": { "type": "python" } })
def test_validate_python_control_must_be_loadable(): with pytest.raises(InvalidActivity): validate_python_control({ "name": "a-python-control", "provider": { "type": "python", "module": "blah.blah" } })
def test_validate_python_control_must_be_loadable(logger): validate_python_control({ "name": "a-python-control", "provider": { "type": "python", "module": "blah.blah" } }) args = logger.warning.call_args msg = "Could not find Python module 'blah.blah' in control " \ "'a-python-control'. Did you install the Python module?" assert msg in args[0][0]