Skip to content

Commit

Permalink
Merge pull request #36 from remi-picard/#6-delete-logs-report
Browse files Browse the repository at this point in the history
#6 Add an endpoint for deleting logs of an execution
  • Loading branch information
Noordsestern authored Feb 19, 2024
2 parents c5c5b9d + 4122b30 commit b0d571a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
46 changes: 40 additions & 6 deletions RobotFrameworkService/routers/robotframework.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -13,6 +13,7 @@
import multiprocessing as mp

import asyncio
import shutil

router = APIRouter(
prefix="/robotframework",
Expand All @@ -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()
Expand Down Expand Up @@ -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 = []
Expand Down
11 changes: 11 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b0d571a

Please sign in to comment.