def test_invoke_airflow_dag_errors(mock_info):
    act_resource = ActionsResource()
    dag_id = 'test_dag_id'
    action = {'id': '123', 'user': '******'}
    web_server_url = CONF.base.web_server
    conf_value = {'action': action}
    responses.add(method='POST',
                  url='{}api/experimental/dags/{}/dag_runs'.format(
                      web_server_url, dag_id),
                  body=json.dumps({"error": "not found"}),
                  status=404,
                  content_type='application/json')

    with pytest.raises(ApiError) as expected_exc:
        act_resource.invoke_airflow_dag(dag_id, action, context)
    mock_info.assert_called_with('Response code from Airflow trigger_dag: %s',
                                 404)
    assert 'Unable to complete request to Airflow' in str(expected_exc)
    assert 'Airflow could not be contacted properly by Shipyard' in str(
        expected_exc)

    with mock.patch.object(actions_api, 'CONF') as mock_conf:
        mock_conf.base.web_server = 'Error'
        with pytest.raises(ApiError) as expected_exc:
            act_resource.invoke_airflow_dag(dag_id, action, context)
        assert 'Unable to invoke workflow' in str(expected_exc)
        assert ('Airflow URL not found by Shipyard. Shipyard configuration is '
                'missing web_server value') in str(expected_exc)
Example #2
0
def test_create_action_validator_error():
    action_resource = ActionsResource()
    action_resource.get_all_actions_db = actions_db
    action_resource.get_all_dag_runs_db = dag_runs_db
    action_resource.get_all_tasks_db = tasks_db
    action_resource.invoke_airflow_dag = airflow_stub
    action_resource.insert_action = insert_action_stub
    action_resource.audit_control_command_db = audit_control_command_db
    action_resource.get_committed_design_version = lambda: DESIGN_VERSION
    action_resource.check_intermediate_commit_revision = (
        CHECK_INTERMEDIATE_COMMIT)

    # with valid input and some parameters
    with mock.patch('shipyard_airflow.control.action.action_validators'
                    '.validate_site_action',
                    side_effect=ApiError(title='bad')):
        with pytest.raises(ApiError) as apie:
            action = action_resource.create_action(
                action={'name': 'deploy_site',
                        'parameters': {
                            'a': 'aaa'
                        }},
                context=context,
                allow_intermediate_commits=False)
            assert action['timestamp']
            assert action['id']
            assert len(action['id']) == 26
            assert action['dag_execution_date'] == '2017-09-06 14:10:08.528402'
            assert action['dag_status'] == 'SCHEDULED'
            assert action['committed_rev_id'] == 1
        assert apie.value.title == 'bad'
def _gen_action_resource_stubbed():
    # TODO(bryan-strassner): mabye subclass this instead?
    action_resource = ActionsResource()
    action_resource.get_all_actions_db = actions_db
    action_resource.get_all_dag_runs_db = dag_runs_db
    action_resource.get_all_tasks_db = tasks_db
    action_resource.invoke_airflow_dag = airflow_stub
    action_resource.insert_action = insert_action_stub
    action_resource.audit_control_command_db = audit_control_command_db
    action_resource.get_committed_design_version = lambda: DESIGN_VERSION
    return action_resource
def test_invoke_airflow_dag_success(mock_info, mock_exhume_date):
    act_resource = ActionsResource()
    dag_id = 'test_dag_id'
    action = {'id': '123', 'user': '******'}
    CONF = cfg.CONF
    web_server_url = CONF.base.web_server
    conf_value = {'action': action}
    log_string = 'Created <DagRun deploy_site @ 2017-09-22 22:16:14: man'
    responses.add(method='POST',
                  url='{}api/experimental/dags/{}/dag_runs'.format(
                      web_server_url, dag_id),
                  body=json.dumps({'message': log_string}),
                  status=200,
                  content_type='application/json')

    result = act_resource.invoke_airflow_dag(dag_id, action, context)
    mock_exhume_date.assert_called_with(dag_id, log_string)
    assert result == '2017-09-22T22:16:14'
Example #5
0
def test_invoke_airflow_dag_success(mock_info, mock_exhume_date):
    act_resource = ActionsResource()
    dag_id = 'test_dag_id'
    action = 'test_action'
    CONF = cfg.CONF
    web_server_url = CONF.base.web_server
    conf_value = {'action': action}
    log_string = 'Created <DagRun deploy_site @ 2017-09-22 22:16:14: man'
    responses.add(
        method='GET',
        url='{}admin/rest_api/api?api=trigger_dag&dag_id={}&conf={}'.format(
            web_server_url, dag_id, act_resource.to_json(conf_value)),
        body=json.dumps({'output': {
            'stdout': log_string
        }}),
        status=200,
        content_type='application/json')

    result = act_resource.invoke_airflow_dag(dag_id, action, context)
    mock_exhume_date.assert_called_with(dag_id, log_string)
    assert result == '2017-09-22T22:16:14'
Example #6
0
def test_create_action():
    action_resource = ActionsResource()
    action_resource.get_all_actions_db = actions_db
    action_resource.get_all_dag_runs_db = dag_runs_db
    action_resource.get_all_tasks_db = tasks_db
    action_resource.invoke_airflow_dag = airflow_stub
    action_resource.insert_action = insert_action_stub
    action_resource.audit_control_command_db = audit_control_command_db
    action_resource.get_committed_design_version = lambda: DESIGN_VERSION
    action_resource.check_intermediate_commit_revision = (
        CHECK_INTERMEDIATE_COMMIT)

    # with invalid input. fail.
    with mock.patch('shipyard_airflow.control.action.action_validators'
                    '.validate_site_action') as validator:
        try:
            action = action_resource.create_action(
                action={'name': 'broken',
                        'parameters': {
                            'a': 'aaa'
                        }},
                context=context,
                allow_intermediate_commits=False)
            assert False, 'Should throw an ApiError'
        except ApiError:
            # expected
            pass
    assert not validator.called

    # with valid input and some parameters
    with mock.patch('shipyard_airflow.control.action.action_validators'
                    '.validate_site_action') as validator:
        try:
            action = action_resource.create_action(
                action={'name': 'deploy_site',
                        'parameters': {
                            'a': 'aaa'
                        }},
                context=context,
                allow_intermediate_commits=False)
            assert action['timestamp']
            assert action['id']
            assert len(action['id']) == 26
            assert action['dag_execution_date'] == '2017-09-06 14:10:08.528402'
            assert action['dag_status'] == 'SCHEDULED'
            assert action['committed_rev_id'] == 1
        except ApiError:
            assert False, 'Should not raise an ApiError'
    validator.assert_called_once_with(action)

    # with valid input and no parameters
    with mock.patch('shipyard_airflow.control.action.action_validators'
                    '.validate_site_action') as validator:
        try:
            action = action_resource.create_action(
                action={'name': 'deploy_site'},
                context=context,
                allow_intermediate_commits=False)
            assert action['timestamp']
            assert action['id']
            assert len(action['id']) == 26
            assert action['dag_execution_date'] == '2017-09-06 14:10:08.528402'
            assert action['dag_status'] == 'SCHEDULED'
            assert action['committed_rev_id'] == 1
        except ApiError:
            assert False, 'Should not raise an ApiError'
    validator.assert_called_once_with(action)