Exemple #1
0
def run(path: str,
        journal_path: str = "./journal.json",
        dry: bool = False,
        no_validation: bool = False) -> Journal:
    """Run the experiment given at PATH."""
    experiment = load_experiment(click.format_filename(path))
    settings = load_settings()

    notify(settings, RunFlowEvent.RunStarted, experiment)

    if not no_validation:
        try:
            ensure_experiment_is_valid(experiment)
        except ChaosException as x:
            logger.error(str(x))
            logger.debug(x)
            sys.exit(1)

    experiment["dry"] = dry

    journal = run_experiment(experiment)

    with io.open(journal_path, "w") as r:
        json.dump(journal, r, indent=2, ensure_ascii=False, default=encoder)

    if journal["status"] == "completed":
        notify(settings, RunFlowEvent.RunCompleted, journal)
    else:
        notify(settings, RunFlowEvent.RunFailed, journal)

    return journal
Exemple #2
0
def test_load_json():
    with tempfile.NamedTemporaryFile(suffix=".json") as f:
        f.write(json.dumps({"a": 12}).encode('utf-8'))
        f.seek(0)
        doc = load_experiment(f.name)
        assert "a" in doc
        assert doc["a"] == 12
Exemple #3
0
def test_load_yml():
    with tempfile.NamedTemporaryFile(suffix=".yml") as f:
        f.write(b"""---
a: 12
""")
        f.seek(0)
        doc = load_experiment(f.name)
        assert "a" in doc
        assert doc["a"] == 12
Exemple #4
0
def validate(path: str):
    """Validate the experiment at PATH."""
    experiment = load_experiment(click.format_filename(path))
    try:
        ensure_experiment_is_valid(experiment)
        logger.info("experiment syntax and semantic look valid")
    except ChaosException as x:
        logger.error(str(x))
        sys.exit(1)
Exemple #5
0
def validate(path: str) -> Experiment:
    """Validate the experiment at PATH."""
    settings = load_settings()
    experiment = load_experiment(click.format_filename(path))
    try:
        notify(settings, ValidateFlowEvent.ValidateStarted, experiment)
        ensure_experiment_is_valid(experiment)
        notify(settings, ValidateFlowEvent.ValidateCompleted, experiment)
        logger.info("experiment syntax and semantic look valid")
    except ChaosException as x:
        notify(settings, ValidateFlowEvent.ValidateFailed, experiment, x)
        logger.error(str(x))
        logger.debug(x)
        sys.exit(1)

    return experiment
Exemple #6
0
def run(path: str,
        report_path: str = "./chaos-report.json",
        dry: bool = False,
        no_validation: bool = False):
    """Run the plan given at PATH."""
    experiment = load_experiment(click.format_filename(path))
    if not no_validation:
        try:
            ensure_experiment_is_valid(experiment)
        except ChaosException as x:
            logger.error(str(x))
            logger.debug(x)
            sys.exit(1)

    journal = run_experiment(experiment)

    with io.open(report_path, "w") as r:
        json.dump(journal, r, indent=2, ensure_ascii=False)
Exemple #7
0
def run(path: str,
        journal_path: str = "./journal.json",
        dry: bool = False,
        no_validation: bool = False) -> Journal:
    """Run the experiment given at PATH."""
    experiment = load_experiment(click.format_filename(path))
    if not no_validation:
        try:
            ensure_experiment_is_valid(experiment)
        except ChaosException as x:
            logger.error(str(x))
            logger.debug(x)
            sys.exit(1)

    experiment["dry"] = dry
    journal = run_experiment(experiment)

    with io.open(journal_path, "w") as r:
        json.dump(journal, r, indent=2, ensure_ascii=False)

    return journal
Exemple #8
0
def test_unknown_extension():
    with tempfile.NamedTemporaryFile(suffix=".txt") as f:
        with pytest.raises(InvalidExperiment) as x:
            load_experiment(f.name)
        assert "json, yaml or yml extensions are supported" in str(x.value)
Exemple #9
0
def test_valid_experiment_from_yaml():
    with tempfile.NamedTemporaryFile(suffix=".yaml") as f:
        f.write(yaml.dump(experiments.Experiment).encode('utf-8'))
        f.seek(0)
        doc = load_experiment(f.name)
        assert ensure_experiment_is_valid(doc) is None
def test_valid_experiment_from_json():
    with tempfile.NamedTemporaryFile(suffix=".json") as f:
        f.write(json.dumps(experiments.Experiment).encode("utf-8"))
        f.seek(0)
        doc = load_experiment(f.name)
        assert ensure_experiment_is_valid(doc) is None