Esempio n. 1
0
 def test_logs_include_flow_run_name_and_level(self, MockFlowRunView,
                                               mock_watch_flow_run, caplog):
     MockFlowRunView.from_flow_run_id.return_value.name = "fake-run-name"
     run_logs = [
         FlowRunLog(timestamp=pendulum.now(),
                    level=logging.INFO,
                    message="Log message"),
         FlowRunLog(timestamp=pendulum.now(),
                    level=logging.ERROR,
                    message="Another log"),
     ]
     mock_watch_flow_run.return_value = run_logs
     wait_for_flow_run.run("flow-run-id")
     for record, run_log in zip(caplog.records, run_logs):
         assert record.levelno == run_log.level
         assert record.msg == f"Flow 'fake-run-name': {run_log.message}"
Esempio n. 2
0
def test_watch_flow_run(monkeypatch):
    flow_run = FlowRunView._from_flow_run_data(FLOW_RUN_DATA_1)
    flow_run.state = Scheduled()  # Not running
    flow_run.states = []
    flow_run.get_latest = MagicMock(return_value=flow_run)
    flow_run.get_logs = MagicMock()

    MockView = MagicMock()
    MockView.from_flow_run_id.return_value = flow_run

    monkeypatch.setattr("prefect.backend.flow_run.FlowRunView", MockView)
    monkeypatch.setattr(
        "prefect.backend.flow_run.check_for_compatible_agents",
        MagicMock(return_value="Helpful agent message."),
    )

    # Mock sleep so that we do not have a slow test
    monkeypatch.setattr("prefect.backend.flow_run.time.sleep", MagicMock())

    for i, log in enumerate(watch_flow_run("id")):
        # Assert that we get the agent warning a couple times then update the state
        if i == 0:
            assert log.message == (
                "It has been 15 seconds and your flow run has not been submitted by an agent. "
                "Helpful agent message.")
            assert log.level == logging.WARNING

        elif i == 1:
            assert log.message == (
                "It has been 50 seconds and your flow run has not been submitted by an agent. "
                "Helpful agent message.")

            # Mark the flow run as finished and give it a few past states to log
            # If this test times out, we did not reach this log
            flow_run.state = Success()
            scheduled = Scheduled("My message")
            scheduled.timestamp = pendulum.now()
            running = Running("Another message")
            running.timestamp = pendulum.now().add(seconds=10)

            # Given intentionally out of order states to prove sorting
            flow_run.states = [running, scheduled]

            # Add a log between the states and a log at the end
            flow_run.get_logs = MagicMock(return_value=[
                FlowRunLog(
                    timestamp=pendulum.now().add(seconds=5),
                    message="Foo",
                    level=logging.DEBUG,
                ),
                FlowRunLog(
                    timestamp=pendulum.now().add(seconds=15),
                    message="Bar",
                    level=logging.ERROR,
                ),
            ])

        elif i == 2:
            assert log.message == "Entered state <Scheduled>: My message"
            assert log.level == logging.INFO
        elif i == 3:
            assert log.message == "Foo"
            assert log.level == logging.DEBUG
        elif i == 4:
            assert log.message == "Entered state <Running>: Another message"
            assert log.level == logging.INFO
        elif i == 5:
            assert log.message == "Bar"
            assert log.level == logging.ERROR

    assert i == 5  # Assert we saw all of the expected logs