Example #1
0
def test_validate_workflow_id():
    """
    static method validate_workflow_id
    """
    assert WorkflowHelper.validate_workflow_id(
        'aar__dvark__1924-04-12T05:34:01.222220')

    assert not WorkflowHelper.validate_workflow_id(
        'aardvark_1924-04-12T05:34:01.222220')

    assert not WorkflowHelper.validate_workflow_id(None)
Example #2
0
def test__add_id_to_run():
    """
    static method _add_id_to_run
    """
    assert WorkflowHelper._add_id_to_run({
        'dag_id':
        'aardvark',
        'execution_date':
        '1924-04-12T05:34:01.222220'
    })['workflow_id'] == 'aardvark__1924-04-12T05:34:01.222220'

    assert WorkflowHelper._add_id_to_run({})['workflow_id'] == 'None__None'
Example #3
0
def test__get_threshold_date():
    """
    static method _get_threshold_date
    """
    assert (arrow.utcnow().shift(days=-30).naive <=
            WorkflowHelper._get_threshold_date('turnip') <=
            arrow.utcnow().shift(days=-30).naive)

    assert (arrow.utcnow().shift(days=-30).naive <=
            WorkflowHelper._get_threshold_date(None) <=
            arrow.utcnow().shift(days=-30).naive)

    assert (WorkflowHelper._get_threshold_date('2017-10-09T10:00:00.000000') ==
            arrow.get('2017-10-09T10:00:00.00000').naive)
Example #4
0
def test_get_workflow_subords():
    """
    Tests the get_workflow method
    """
    helper = WorkflowHelper('')
    helper._get_dag_run_like_id_db = (
        lambda dag_id, execution_date: [DAG_RUN_SUB, DAG_RUN_1])
    helper._get_tasks_by_id_db = lambda dag_id, execution_date: TASK_LIST
    dag_detail = helper.get_workflow(
        workflow_id='dag_id__1957-03-14T12:12:12.000000')
    assert dag_detail['dag_id'] == 'did2'
    assert len(dag_detail['sub_dags']) == 1
    assert dag_detail['sub_dags'][0]['dag_id'] == 'did2.didnt'
    assert len(dag_detail['steps']) == 3
Example #5
0
 def get_workflow_detail(self, helper, workflow_id):
     """
     Retrieve a workflow by id,
     :param helper: The WorkflowHelper constructed for this invocation
     :param workflow_id: a string in {dag_id}__{execution_date} format
                         identifying a workflow
     :returns: a workflow detail dictionary including steps
     """
     if not WorkflowHelper.validate_workflow_id(workflow_id):
         raise ApiError(
             title='Invalid Workflow ID specified',
             description=(
                 'Workflow id must use {dag id}__{execution date} format',
             ),
             status=falcon.HTTP_400,
             retry=False,
         )
     workflow = helper.get_workflow(workflow_id=workflow_id)
     if workflow is None:
         raise ApiError(
             title='Workflow not found',
             description=(
                 'A Workflow with id {} was not found'.format(workflow_id),
             ),
             status=falcon.HTTP_404,
             retry=False,
         )
     return workflow
Example #6
0
def test__split_workflow_id_to_dag_run():
    """
    static method _split_workflow_id_to_dag_run
    """
    assert WorkflowHelper._split_workflow_id_to_dag_run(
        'aardvark__1924-04-12T05:34:01.222220')['dag_id'] == 'aardvark'

    assert WorkflowHelper._split_workflow_id_to_dag_run(
        'aar__dvark__1924-04-12T05:34:01.222220')['dag_id'] == 'aar__dvark'

    assert WorkflowHelper._split_workflow_id_to_dag_run(
        'aardvark__1924-04-12T05:34:01.222220'
    )['execution_date'] == '1924-04-12T05:34:01.222220'

    assert WorkflowHelper._split_workflow_id_to_dag_run(
        'aar__dvark__1924-04-12T05:34:01.222220'
    )['execution_date'] == '1924-04-12T05:34:01.222220'
Example #7
0
 def test_get_all_workflows(self):
     """
     test that get_all_workflows invokes the helper properly
     """
     wr = WorkflowResource()
     with patch.object(WorkflowHelper, 'get_workflow_list') as mock_method:
         helper = WorkflowHelper('')
         wr.get_all_workflows(helper, None)
     mock_method.assert_called_once_with(since_iso8601=None)
Example #8
0
def test_get_workflow_list():
    """
    Tests the get_workflow_list method
    """
    helper = WorkflowHelper('')
    helper._get_all_dag_runs_db = lambda: [DAG_RUN_1, DAG_RUN_1, DAG_RUN_1]

    # Time includes items
    dag_list = helper.get_workflow_list(
        since_iso8601='2017-09-13T11:12:00.000000')
    assert DAG_RUN_1 in dag_list
    assert len(dag_list) == 3

    # Time excludes items
    dag_list = helper.get_workflow_list(
        since_iso8601='2017-10-01T11:12:00.000000')
    assert DAG_RUN_1 not in dag_list
    assert len(dag_list) == 0
Example #9
0
 def on_get(self, req, resp, workflow_id):
     """
     Retrieve the step details of workflows invoked in Airflow.
     :returns: a json object of a workflow entity
     """
     helper = WorkflowHelper(req.context.external_marker)
     resp.body = self.to_json(
         self.get_workflow_detail(helper=helper, workflow_id=workflow_id)
     )
     resp.status = falcon.HTTP_200
Example #10
0
 def on_get(self, req, resp):
     """
     Return actions that have been invoked through shipyard.
     :returns: a json array of workflow entities
     """
     since_date = req.params.get('since')
     helper = WorkflowHelper(req.context.external_marker)
     resp.body = self.to_json(
         self.get_all_workflows(helper=helper, since_date=since_date)
     )
     resp.status = falcon.HTTP_200
Example #11
0
 def test_get_workflow_detail_success(self):
     """
     test that get_workflow_detail properly invokes the helper
     """
     wir = WorkflowIdResource()
     with patch.object(WorkflowHelper, 'get_workflow') as mock_method:
         helper = WorkflowHelper('')
         wir.get_workflow_detail(helper,
                                 'deploy_site__1972-04-03T10:00:00.20123')
     mock_method.assert_called_once_with(
         workflow_id='deploy_site__1972-04-03T10:00:00.20123')
Example #12
0
 def test_get_workflow_detail_not_found(self):
     """
     Assert ApiError 'Workflow not found' is raised when get_workflow
     returns None.
     """
     wir = WorkflowIdResource()
     with patch.object(WorkflowHelper, 'get_workflow') as mock_method:
         helper = WorkflowHelper('')
         mock_method.return_value = None
         with pytest.raises(ApiError) as expected_exc:
             wir.get_workflow_detail(helper, 'dag_id__execution_date')
         assert "Workflow not found" in str(expected_exc)
Example #13
0
def test_get_workflow():
    """
    Tests the get_workflow method
    """
    helper = WorkflowHelper('')
    helper._get_dag_run_like_id_db = lambda dag_id, execution_date: [DAG_RUN_1]
    helper._get_tasks_by_id_db = lambda dag_id, execution_date: TASK_LIST
    dag_detail = helper.get_workflow(
        workflow_id='dag_id__1957-03-14T12:12:12.000000')
    assert dag_detail['dag_id'] == 'did2'
    assert len(dag_detail['steps']) == 3

    dag_detail = helper.get_workflow(workflow_id='NOTHING')
    assert dag_detail == {}
Example #14
0
 def test_get_workflow_detail_invalid_format(self):
     """
     Assert ApiError 'Invalid Workflow ID specified' is raised when
     workflow ID is not a valid format, otherwise the error is not raised.
     """
     wir = WorkflowIdResource()
     with patch.object(WorkflowHelper, 'get_workflow') as mock_method:
         helper = WorkflowHelper('')
         with pytest.raises(ApiError) as expected_exc:
             wir.get_workflow_detail(helper, None)
         assert "Invalid Workflow ID specified" in str(expected_exc)
         with pytest.raises(ApiError) as expected_exc:
             wir.get_workflow_detail(helper, 'this is a bad id')
         assert "Invalid Workflow ID specified" in str(expected_exc)
         with pytest.raises(ApiError) as expected_exc:
             wir.get_workflow_detail(helper, 'dag_idTexecution_date')
         assert "Invalid Workflow ID specified" in str(expected_exc)
         with pytest.raises(ApiError) as expected_exc:
             wir.get_workflow_detail(helper, 'dag_id_execution_date')
         assert "Invalid Workflow ID specified" in str(expected_exc)
         wir.get_workflow_detail(helper, 'dag_id__execution_date')
     mock_method.assert_called_once_with(
         workflow_id='dag_id__execution_date')