From 4122b306c4a2a6182370c353ed62e373ff2eceb6 Mon Sep 17 00:00:00 2001 From: picard-remi Date: Sun, 21 Jan 2024 16:49:55 +0100 Subject: [PATCH] #6 Add an endpoint for deleting logs of an execution --- .../routers/robotframework.py | 46 ++++++++++++++++--- tests/test_app.py | 11 +++++ 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/RobotFrameworkService/routers/robotframework.py b/RobotFrameworkService/routers/robotframework.py index 54bdace..84828dd 100644 --- a/RobotFrameworkService/routers/robotframework.py +++ b/RobotFrameworkService/routers/robotframework.py @@ -1,7 +1,7 @@ from concurrent.futures import Executor import pathlib -from fastapi import APIRouter, Request, Path +from fastapi import APIRouter, Request, Path, status from fastapi.responses import HTMLResponse, Response, RedirectResponse from starlette.responses import JSONResponse @@ -13,6 +13,7 @@ import multiprocessing as mp import asyncio +import shutil router = APIRouter( prefix="/robotframework", @@ -24,6 +25,13 @@ ) +is_execution_finished = ( + lambda x: x.is_dir() + and (x / "report.html").exists() + and (x / "log.html").exists() +) + + async def run_robot_in_background(func, args): p = mp.Process(target=func, args=args) p.start() @@ -222,14 +230,40 @@ async def show_execution_ids(): get all execution ids for the finished tasks """ logs = pathlib.Path(f"./{LOGS}") - is_execution_finished = ( - lambda x: x.is_dir() - and (x / "report.html").exists() - and (x / "log.html").exists() - ) return [log.stem for log in logs.iterdir() if is_execution_finished(log)] +@router.delete( + "/logs/{executionid}", + tags=["reporting"], + status_code=status.HTTP_204_NO_CONTENT, + responses={ + status.HTTP_204_NO_CONTENT: { + "description": "The logs have been deleted", + }, + status.HTTP_404_NOT_FOUND: { + "content": {"text/html": {}}, + "description": "The logs not existing or being generating", + } + }, +) +async def delete_logs( + executionid: str = Path( + description="Insert here the value of a previous response header field 'x-request-id' which you want delete logs", + ) +): + log = pathlib.Path(f"./{LOGS}/{executionid}") + if is_execution_finished(log): + shutil.rmtree(log, ignore_errors=True) + return Response(status_code=status.HTTP_204_NO_CONTENT) + else: + return Response( + content=f"The logs {executionid} not existing or being generating", + media_type="text/html", + status_code=status.HTTP_404_NOT_FOUND + ) + + def _start_all_robot_tasks(id: str, config) -> int: if config.variablefiles is None: variablefiles = [] diff --git a/tests/test_app.py b/tests/test_app.py index 051aa67..fb38f51 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -79,6 +79,17 @@ def test_is_robot_run(self): response = client.post("/robotframework/run", json={"paths": ["examples"], "test": "Demonstration Test", "sync": True}) self.assertEqual(200, response.status_code) + def test_delete_robotlogs(self): + with TestClient(app) as client: + response = client.delete("/robotframework/logs/not_existing") + self.assertEqual(404, response.status_code) + self.assertEqual("The logs not_existing not existing or being generating", response.text) + + run_response = client.get("/robotframework/run/anotherTask") + execution_id = run_response.headers["x-request-id"] + response = client.delete(f"/robotframework/logs/{execution_id}") + self.assertEqual(204, response.status_code) + def __get_robot_webservice(self, endpoint, expected_response_code=200): with TestClient(app) as client: response = client.get(endpoint)