diff --git a/RobotFrameworkService/routers/robotframework.py b/RobotFrameworkService/routers/robotframework.py index 08bac1b..d1e78a1 100644 --- a/RobotFrameworkService/routers/robotframework.py +++ b/RobotFrameworkService/routers/robotframework.py @@ -9,19 +9,21 @@ import robot +import multiprocessing as mp + router = APIRouter( prefix="/robotframework", responses={404: {"description": "Not found: Webservice is either busy or requested endpoint is not supported."}}, ) +async def run_robot_in_brackground(func, args=[], kwargs={}): + p = mp.Process(target=func, args=args, kwargs=kwargs) + p.start() + return p -@router.get('/run/all', tags=["execution"]) -async def run(request: Request): - """ - Run all task available. - """ - id = request.headers["request-id"] - result: int = _start_all_robot_tasks(id) +async def run_robot_and_wait(func, args=[], kwargs={}): + # this is still blocking + result: int = func(*args, **kwargs) if result == 0: result_page = 'PASS' result_page += f'
' @@ -37,26 +39,47 @@ async def run(request: Request): return Response(content=result_page, media_type="text/html", status_code=status_code) +@router.get('/run/all', tags=["execution"]) +async def run_all(request: Request): + """ + Run all task available. + """ + id = request.headers["request-id"] + response = await run_robot_and_wait(func=_start_all_robot_tasks, args=[id]) + + return response + + +@router.get('/run/all/async', tags=["execution"]) +async def run_all_async(request: Request): + """ + Starts all Robot tasks. Returns execution id and continures to run Robot tasks in background. + """ + id = request.headers["request-id"] + await run_robot_in_brackground(func=_start_all_robot_tasks, args=[id]) + return id + + @router.get('/run/{task}', tags=["execution"]) async def run_task(task, request: Request): """ Run a given task. """ id = request.headers["request-id"] - variables = RequestHelper.parse_variables_from_query(request) - result: int = _start_specific_robot_task(id, task, variables=variables) - if result == 0: - result_page = 'PASS' - result_page += f'' - - elif 250 >= result >= 1: - result_page = f'FAIL: {result} tasks failed' - result_page += f'' + variables = RequestHelper.parse_variables_from_query(request) + response = await run_robot_and_wait(func=_start_specific_robot_task, kwargs={'id': id, 'task':task, 'variables':variables}) + return response - else: - result_page = f'FAIL: Errorcode {result}' - - return Response(content=result_page, media_type="text/html") + +@router.get('/run/{task}/async', tags=["execution"]) +async def run_task_async(task, request: Request): + """ + Start a given task. Returns execution id and continues to run Robot task in background. + """ + id = request.headers["request-id"] + variables = RequestHelper.parse_variables_from_query(request) + await run_robot_in_brackground(func=_start_specific_robot_task, kwargs={'task':task, 'variables':variables}) + return id @router.get('/run/suite/{suite}', tags=["execution"]) async def run_suite(suite, request: Request): @@ -65,35 +88,40 @@ async def run_suite(suite, request: Request): """ id = request.headers["request-id"] variables = RequestHelper.parse_variables_from_query(request) - result: int = _start_specific_robot_suite(id, suite, variables=variables) - if result == 0: - result_page = 'PASS' - elif 250 >= result >= 1: - result_page = f'FAIL: {result} tasks for suite {suite} failed' - else: - result_page = f'FAIL: Errorcode {result}' - result_page += f'' - return Response(content=result_page, media_type="text/html") + response = await run_robot_and_wait(func=_start_specific_robot_suite, kwargs={'id': id, 'suite':suite, 'variables':variables}) + return response + +@router.get('/run/suite/{suite}/async', tags=["execution"]) +async def run_suite_async(suite, request: Request): + """ + Start a given suite. Returns execution id and continues to run Robot suite in background. + """ + id = request.headers["request-id"] + variables = RequestHelper.parse_variables_from_query(request) + await run_robot_in_brackground(func=_start_specific_robot_suite, kwargs={'suite':suite, 'variables':variables}) + return id @router.get('/run_and_show/{task}', tags=["execution"], response_class=HTMLResponse) -async def start_robot_task_and_show_log(task: str, arguments: Request): +async def start_robot_task_and_show_log(task: str, request: Request): """ Run a given task with variables and return log.html """ - variables = RequestHelper.parse_variables_from_query(arguments) - _start_specific_robot_task(task, variables) - return RedirectResponse(f"/logs/{task}/log.html") + id = request.headers["request-id"] + variables = RequestHelper.parse_variables_from_query(request) + await run_robot_and_wait(func=_start_specific_robot_task, kwargs={'id':id, 'task':task, 'variables':variables}) + return RedirectResponse(f"/logs/{id}/log.html") @router.get('/run_and_show_report/{task}', tags=["execution"], response_class=HTMLResponse) -async def start_robot_task_and_show_report(task: str, arguments: Request): +async def start_robot_task_and_show_report(task: str, request: Request): """ Run a given task with variables and return report.html """ - variables = RequestHelper.parse_variables_from_query(arguments) - _start_specific_robot_task(task, variables) - return RedirectResponse(f"/logs/{task}/report.html") + id = request.headers["request-id"] + variables = RequestHelper.parse_variables_from_query(request) + await run_robot_and_wait(func=_start_specific_robot_task, kwargs={'id':id, 'task':task, 'variables':variables}) + return RedirectResponse(f"/logs/{id}/report.html") @router.get('/show_log/{executionid}', tags=["reporting"], response_class=HTMLResponse) diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 0000000..7a074a5 --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1 @@ +robotframework-tidy \ No newline at end of file diff --git a/requirements-test.txt b/requirements-test.txt new file mode 100644 index 0000000..7922838 --- /dev/null +++ b/requirements-test.txt @@ -0,0 +1 @@ +httpx \ No newline at end of file diff --git a/tests/test_app.py b/tests/test_app.py index 616a462..f3e638c 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -49,7 +49,7 @@ def test_is_robotreport_available(self): def __get_robot_webservice(self,endpoint, expected_response_code=200): with TestClient(app) as client: response = client.get(endpoint) - self.assertEqual(expected_response_code, response.status_code) + self.assertEqual(expected_response_code, response.status_code, response.text) return response def __is_robot_passed(self, response, msg=None):