Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#6 Add an endpoint for deleting logs of an execution #36

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading