예제 #1
0
def test_script_execution_service_worker_verify_prepare_method_called(caplog):
    """
    SES.prepare should be called when 'request.procedure.create' message is received
    """
    cmd = application.PrepareProcessCommand(
        script_uri="test:///hi", init_args=domain.ProcedureInput()
    )
    with mock.patch(
        "oet.procedure.application.main.ScriptExecutionService.prepare"
    ) as mock_method:
        assert_command_request_and_response(
            caplog,
            mock_method,
            topics.request.procedure.create,
            topics.procedure.lifecycle.created,
            cmd,
        )
예제 #2
0
def test_script_execution_service_worker_verify_start_method_called(caplog):
    """
    SES.start should be called when 'request.procedure.start' message is received
    """
    cmd = application.StartProcessCommand(
        process_uid=123, run_args=domain.ProcedureInput()
    )
    with mock.patch(
        "oet.procedure.application.main.ScriptExecutionService.start"
    ) as mock_method:
        assert_command_request_and_response(
            caplog,
            mock_method,
            topics.request.procedure.start,
            topics.procedure.lifecycle.started,
            cmd,
        )
예제 #3
0
def create_procedure():
    """
    Create a new Procedure.

    This method requests creation of a new Procedure as specified in the JSON
    payload POSTed to this function.

    :return: JSON summary of created Procedure
    """
    if not flask.request.json or "script_uri" not in flask.request.json:
        description = {
            "type": "Malformed Request",
            "Message": "script_uri missing"
        }
        flask.abort(400, description=description)
    script_uri = flask.request.json["script_uri"]

    if "script_args" in flask.request.json and not isinstance(
            flask.request.json["script_args"], dict):
        description = {
            "type": "Malformed Request",
            "Message": "Malformed script_uri in request",
        }
        flask.abort(400, description=description)
    script_args = flask.request.json.get("script_args", {})

    init_dict = script_args.get("init", {})
    init_args = init_dict.get("args", [])
    init_kwargs = init_dict.get("kwargs", {})

    procedure_input = domain.ProcedureInput(*init_args, **init_kwargs)
    prepare_cmd = application.PrepareProcessCommand(script_uri=script_uri,
                                                    init_args=procedure_input)

    summary = call_and_respond(
        topics.request.procedure.create,
        topics.procedure.lifecycle.created,
        cmd=prepare_cmd,
    )

    return flask.jsonify({"procedure": make_public_summary(summary)}), 201
예제 #4
0
from oet.procedure.domain import ProcedureInput, ProcedureSummary

# Endpoint for the REST API
ENDPOINT = "api/v1.0/procedures"

# Valid JSON struct for creating a new procedure
CREATE_JSON = dict(
    script_uri="test:///test.py",
    script_args={"init": dict(args=(1, 2, 3), kwargs=dict(kw1="a", kw2="b"))},
)

# object expected to be returned when creating the Procedure defined above
CREATE_SUMMARY = ProcedureSummary(
    id=1,
    script_uri="test:///test.py",
    script_args={"init": domain.ProcedureInput(1, 2, 3, kw1="a", kw2="b")},
    history=domain.ProcedureHistory(
        process_states=OrderedDict([(domain.ProcedureState.CREATED,
                                     1601295086.129294)]),
        stacktrace=None,
    ),
    state=domain.ProcedureState.CREATED,
)

ABORT_JSON = dict(state="STOPPED", abort=True)

# Valid JSON struct for starting a prepared procedure
RUN_JSON = dict(
    script_uri="test:///test.py",
    script_args={"run": dict(args=(4, 5, 6), kwargs=dict(kw3="c", kw4="d"))},
    state="RUNNING",
예제 #5
0
def update_procedure(procedure_id: int):
    """
    Update a Procedure resource using the desired Procedure state described in
    the PUT JSON payload.

    :param procedure_id: ID of Procedure to modify
    :return: ProcedureSummary reflecting the final state of the Procedure
    """
    summary = _get_summary_or_404(procedure_id)

    if not flask.request.json:
        description = {
            "type": "Empty Response",
            "Message": "No JSON available in response",
        }
        flask.abort(400, description=description)

    if "script_args" in flask.request.json and not isinstance(
            flask.request.json["script_args"], dict):
        description = {
            "type": "Malformed Response",
            "Message": "Malformed script_args in response",
        }
        flask.abort(400, description=description)
    script_args = flask.request.json.get("script_args", {})

    old_state = summary.state
    new_state = domain.ProcedureState[flask.request.json.get(
        "state", summary.state.name)]

    if new_state is domain.ProcedureState.STOPPED:
        if old_state is domain.ProcedureState.RUNNING:
            run_abort = flask.request.json.get("abort")
            cmd = application.StopProcessCommand(procedure_id,
                                                 run_abort=run_abort)
            result = call_and_respond(
                topics.request.procedure.stop,
                topics.procedure.lifecycle.stopped,
                cmd=cmd,
            )
            # result is list of process summaries started in response to abort
            # If script was stopped and no post-termination abort script was run,
            # the result list will be empty.
            msg = f"Successfully stopped script with ID {procedure_id}"
            if result:
                msg += " and aborted subarray activity"
            return flask.jsonify({"abort_message": msg})

        else:
            msg = f"Cannot stop script with ID {procedure_id}: Script is not running"
            return flask.jsonify({"abort_message": msg})

    elif (old_state is domain.ProcedureState.CREATED
          and new_state is domain.ProcedureState.RUNNING):
        run_dict = script_args.get("run", {})
        run_args = run_dict.get("args", [])
        run_kwargs = run_dict.get("kwargs", {})
        procedure_input = domain.ProcedureInput(*run_args, **run_kwargs)
        cmd = application.StartProcessCommand(procedure_id,
                                              run_args=procedure_input)

        summary = call_and_respond(topics.request.procedure.start,
                                   topics.procedure.lifecycle.started,
                                   cmd=cmd)

    return flask.jsonify({"procedure": make_public_summary(summary)})