def test_get_dag_run_by_id_empty(mock_get_dag_run_db): '''test that an empty dag_run_list will return None''' action_resource = ActionsIdResource() context.policy_engine = ShipyardPolicy() dag_id = 'test_dag_id' execution_date = 'test_execution_date' result = action_resource.get_dag_run_by_id(dag_id, execution_date) mock_get_dag_run_db.assert_called_once_with(dag_id, execution_date) assert result is None
def test_get_action_errors(mock_get_action): '''verify when get_action_db returns None, ApiError is raised''' action_resource = ActionsIdResource() action_id = '12345678901234567890123456' with pytest.raises(ApiError) as expected_exc: action_resource.get_action(action_id) assert action_id in str(expected_exc) assert 'Action not found' in str(expected_exc)
def start_api(): middlewares = [ AuthMiddleware(), ContextMiddleware(), LoggingMiddleware(), CommonParametersMiddleware() ] control_api = falcon.API( request_type=ShipyardRequest, middleware=middlewares) control_api.add_route('/versions', VersionsResource()) # v1.0 of Shipyard API v1_0_routes = [ # API for managing region data ('/health', HealthResource()), ('/actions', ActionsResource()), ('/actions/{action_id}', ActionsIdResource()), ('/actions/{action_id}/control/{control_verb}', ActionsControlResource()), ('/actions/{action_id}/steps/{step_id}', ActionsStepsResource()), ('/actions/{action_id}/steps/{step_id}/logs', ActionsStepsLogsResource()), ('/actions/{action_id}/validations/{validation_id}', ActionsValidationsResource()), ('/configdocs', ConfigDocsStatusResource()), ('/configdocs/{collection_id}', ConfigDocsResource()), ('/commitconfigdocs', CommitConfigDocsResource()), ('/notedetails/{note_id}', NoteDetailsResource()), ('/renderedconfigdocs', RenderedConfigDocsResource()), ('/workflows', WorkflowResource()), ('/workflows/{workflow_id}', WorkflowIdResource()), ('/site_statuses', StatusResource()), ] # Set up the 1.0 routes route_v1_0_prefix = '/api/v1.0' for path, res in v1_0_routes: route = '{}{}'.format(route_v1_0_prefix, path) LOG.info( 'Adding route: %s Handled by %s', route, res.__class__.__name__ ) control_api.add_route(route, res) # Error handlers (FILO handling) control_api.add_error_handler(Exception, default_exception_handler) control_api.add_error_handler(AppError, AppError.handle) # built-in error serializer control_api.set_error_serializer(default_error_serializer) return control_api
def test_on_get(mock_authorize, mock_get_action): action_resource = ActionsIdResource() context.policy_engine = ShipyardPolicy() kwargs = {'action_id': None} req = create_req(context, None) resp = create_resp() action_resource.on_get(req, resp, **kwargs) mock_authorize.assert_called_once_with('workflow_orchestrator:get_action', context) mock_get_action.assert_called_once_with(kwargs['action_id']) assert resp.body == '"action_returned"' assert resp.status == '200 OK'
def test_get_validations_db(mock_get_validation_by_action_id): expected = { 'id': '43', 'action_id': '12345678901234567890123456', 'validation_name': 'It has shiny goodness', 'details': 'This was not very shiny.' } mock_get_validation_by_action_id.return_value = expected action_resource = ActionsIdResource() action_id = 'test_action_id' result = action_resource.get_validations_db(action_id) mock_get_validation_by_action_id.assert_called_once_with( action_id=action_id) assert result == expected
def test_get_tasks_db(mock_get_tasks_by_id): expected = { 'id': '43', 'action_id': '12345678901234567890123456', 'validation_name': 'It has shiny goodness', 'details': 'This was not very shiny.' } mock_get_tasks_by_id.return_value = expected action_resource = ActionsIdResource() dag_id = 'test_dag_id' execution_date = 'test_execution_date' result = action_resource.get_tasks_db(dag_id, execution_date) mock_get_tasks_by_id.assert_called_once_with(dag_id=dag_id, execution_date=execution_date) assert result == expected
def test_get_dag_run_by_id_notempty(): '''test that a nonempty dag_run_list will return the 1st dag in the list''' action_resource = ActionsIdResource() action_resource.get_dag_run_db = dag_runs_db dag_id = 'test_dag_id' execution_date = 'test_execution_date' result = action_resource.get_dag_run_by_id(dag_id, execution_date) assert result == { 'dag_id': 'did2', 'execution_date': DATE_ONE, 'state': 'FAILED', 'run_id': '99', 'external_trigger': 'something', 'start_date': DATE_ONE, 'end_date': DATE_ONE }
def test_get_action_success(): """ Tests the main response from get all actions """ action_resource = ActionsIdResource() # stubs for db action_resource.get_action_db = actions_db action_resource.get_dag_run_db = dag_runs_db action_resource.get_tasks_db = tasks_db action_resource.get_validations_db = get_validations action_resource.get_action_command_audit_db = get_ac_audit action = action_resource.get_action('12345678901234567890123456') if action['name'] == 'dag_it': assert len(action['steps']) == 3 assert action['dag_status'] == 'FAILED' assert len(action['command_audit']) == 2
def test_get_action_command_audit_db(mock_get_command_audit_by_action_id): expected = { 'id': '12345678901234567890123456', 'name': 'dag_it', 'parameters': None, 'dag_id': 'did2', 'dag_execution_date': DATE_ONE_STR, 'user': '******', 'timestamp': DATE_ONE, 'context_marker': '8-4-4-4-12a' } mock_get_command_audit_by_action_id.return_value = expected action_resource = ActionsIdResource() action_id = 'test_action_id' result = action_resource.get_action_command_audit_db(action_id) mock_get_command_audit_by_action_id.assert_called_once_with(action_id) assert result == expected
def test_get_dag_run_db(mock_get_dag_runs_by_id): expected = { 'dag_id': 'did2', 'execution_date': DATE_ONE, 'state': 'FAILED', 'run_id': '99', 'external_trigger': 'something', 'start_date': DATE_ONE, 'end_date': DATE_ONE } mock_get_dag_runs_by_id.return_value = expected action_resource = ActionsIdResource() dag_id = 'test_dag_id' execution_date = 'test_execution_date' result = action_resource.get_dag_run_db(dag_id, execution_date) mock_get_dag_runs_by_id.assert_called_once_with( dag_id=dag_id, execution_date=execution_date) assert result == expected